Repository: kubernetes/kubernetes
File: pkg/controller/replicaset/replica_set_test.go (Go)

Code before:
	stopCh := make(chan struct{})
	defer close(stopCh)
	manager, fakePodControl, informers := setupManagerWithGCEnabled(t, stopCh, rs)
	// Lister (cache) says it's NOT deleted.
	rs2 := *rs
	rs2.DeletionTimestamp = nil
	informers.Apps().V1().ReplicaSets().Informer().GetIndexer().Add(&rs2)

	// Recheck occurs if a matching orphan is present.
	pod1 := newPod("pod1", rs, v1.PodRunning, nil, false)
	informers.Core().V1().Pods().Informer().GetIndexer().Add(pod1)

	// sync should abort.
	err := manager.syncReplicaSet(ctx, GetKey(rs, t))
	if err == nil {
		t.Error("syncReplicaSet() err = nil, expected non-nil")
	}
	// no patch, no create.
	err = validateSyncReplicaSet(fakePodControl, 0, 0, 0)
	if err != nil {
		t.Fatal(err)
	}
}

func TestReplicaSetAvailabilityCheck(t *testing.T) {
	_, tCtx := ktesting.NewTestContext(t)

	labelMap := map[string]string{"foo": "bar"}
	rs := newReplicaSet(4, labelMap)
	rs.Spec.MinReadySeconds = 5
	client := fake.NewClientset(rs)
	stopCh := make(chan struct{})
	defer close(stopCh)

	manager, informers := testNewReplicaSetControllerFromClient(t, client, stopCh, BurstReplicas)
	if err := informers.Apps().V1().ReplicaSets().Informer().GetIndexer().Add(rs); err != nil {
		t.Fatal(err)
	}

	now := time.Now()
	pod1 := newPod("foobar-1", rs, v1.PodPending, nil, true)
	pod2 := newPod("foobar-2", rs, v1.PodRunning, &metav1.Time{Time: now}, true)
	pod3 := newPod("foobar-3", rs, v1.PodRunning, &metav1.Time{Time: now.Add(-2 * time.Second)}, true)
	pod4 := newPod("foobar-4", rs, v1.PodRunning, &metav1.Time{Time: now.Add(-4300 * time.Millisecond)}, true)
	if err := informers.Core().V1().Pods().Informer().GetIndexer().Add(pod1); err != nil {
		t.Fatal(err)
	}
	if err := informers.Core().V1().Pods().Informer().GetIndexer().Add(pod2); err != nil {
		t.Fatal(err)
	}
	if err := informers.Core().V1().Pods().Informer().GetIndexer().Add(pod3); err != nil {

Review comment (suggestion):
I'll probably change `NewTestContext` to return `context.Context`, to make it a drop-in replacement for `k8s.io/klog/v2/ktesting`.

Instead, use `tCtx := ktesting.Init(t)`.


Code after:
	stopCh := make(chan struct{})
	defer close(stopCh)
	manager, fakePodControl, informers := setupManagerWithGCEnabled(t, stopCh, rs)
	// Lister (cache) says it's NOT deleted.
	rs2 := *rs
	rs2.DeletionTimestamp = nil
	informers.Apps().V1().ReplicaSets().Informer().GetIndexer().Add(&rs2)

	// Recheck occurs if a matching orphan is present.
	pod1 := newPod("pod1", rs, v1.PodRunning, nil, false)
	informers.Core().V1().Pods().Informer().GetIndexer().Add(pod1)

	// sync should abort.
	err := manager.syncReplicaSet(ctx, GetKey(rs, t))
	if err == nil {
		t.Error("syncReplicaSet() err = nil, expected non-nil")
	}
	// no patch, no create.
	err = validateSyncReplicaSet(fakePodControl, 0, 0, 0)
	if err != nil {
		t.Fatal(err)
	}
}

func TestReplicaSetAvailabilityCheck(t *testing.T) {
	tCtx := ktesting.Init(t)

	labelMap := map[string]string{"foo": "bar"}
	rs := newReplicaSet(4, labelMap)
	rs.Spec.MinReadySeconds = 5
	client := fake.NewClientset(rs)
	stopCh := make(chan struct{})
	defer close(stopCh)

	manager, informers := testNewReplicaSetControllerFromClient(t, client, stopCh, BurstReplicas)
	if err := informers.Apps().V1().ReplicaSets().Informer().GetIndexer().Add(rs); err != nil {
		t.Fatal(err)
	}

	now := time.Now()
	pod1 := newPod("foobar-1", rs, v1.PodPending, nil, true)
	pod2 := newPod("foobar-2", rs, v1.PodRunning, &metav1.Time{Time: now}, true)
	pod3 := newPod("foobar-3", rs, v1.PodRunning, &metav1.Time{Time: now.Add(-2 * time.Second)}, true)
	pod4 := newPod("foobar-4", rs, v1.PodRunning, &metav1.Time{Time: now.Add(-4300 * time.Millisecond)}, true)
	if err := informers.Core().V1().Pods().Informer().GetIndexer().Add(pod1); err != nil {
		t.Fatal(err)
	}
	if err := informers.Core().V1().Pods().Informer().GetIndexer().Add(pod2); err != nil {
		t.Fatal(err)
	}
	if err := informers.Core().V1().Pods().Informer().GetIndexer().Add(pod3); err != nil {