Repository: kubernetes/kubernetes
File: pkg/controller/statefulset/stateful_set_test.go (Go)

Code before:
	}
	err := ssc.syncStatefulSet(tCtx, set, pods)
	if err != nil {
		t.Fatal(err)
	}

	if set, err = om.setsLister.StatefulSets(set.Namespace).Get(set.Name); err != nil {
		t.Fatalf("Could not get StatefulSet: %v", err)
	}

	// one pod is not ready
	if set.Status.ReadyReplicas != 3 {
		t.Errorf("Expected updated StatefulSet to contain ready replicas %v, got %v instead",
			3, set.Status.ReadyReplicas)
	}
	if set.Status.AvailableReplicas != 0 {
		t.Errorf("Expected updated StatefulSet to contain available replicas %v, got %v instead",
			0, set.Status.AvailableReplicas)
	}

	if got, want := ssc.queue.Len(), 0; got != want {
		t.Errorf("queue.Len() = %v, want %v", got, want)
	}

	// RS should be re-queued after 700ms to recompute .status.availableReplicas (200ms extra for the test).
	ktesting.Eventually(tCtx, func(tCtx ktesting.TContext) int {
		return ssc.queue.Len()
	}).WithTimeout(900*time.Millisecond).
		WithPolling(10*time.Millisecond).
		Should(gomega.Equal(1), " StatefulSet should be re-queued to recompute .status.availableReplicas")
}

func newFakeStatefulSetController(ctx context.Context, initialObjects ...runtime.Object) (*StatefulSetController, *StatefulPodControl, *fakeObjectManager, history.Interface) {
	client := fake.NewSimpleClientset(initialObjects...)
	informerFactory := informers.NewSharedInformerFactory(client, controller.NoResyncPeriodFunc())
	om := newFakeObjectManager(informerFactory)
	spc := NewStatefulPodControlFromManager(om, &noopRecorder{})
	ssu := newFakeStatefulSetStatusUpdater(informerFactory.Apps().V1().StatefulSets())
	ssc := NewStatefulSetController(
		ctx,
		informerFactory.Core().V1().Pods(),
		informerFactory.Apps().V1().StatefulSets(),
		informerFactory.Core().V1().PersistentVolumeClaims(),
		informerFactory.Apps().V1().ControllerRevisions(),
		client,
	)
	ssh := history.NewFakeHistory(informerFactory.Apps().V1().ControllerRevisions())
	ssc.podListerSynced = alwaysReady
	ssc.setListerSynced = alwaysReady
	ssc.control = NewDefaultStatefulSetControl(spc, ssu, ssh)


Review comment (suggestion):
```suggestion
	tCtx.Eventually(func(tCtx ktesting.TContext) int {
```

Code after:
	}
	err := ssc.syncStatefulSet(tCtx, set, pods)
	if err != nil {
		t.Fatal(err)
	}

	if set, err = om.setsLister.StatefulSets(set.Namespace).Get(set.Name); err != nil {
		t.Fatalf("Could not get StatefulSet: %v", err)
	}

	// one pod is not ready
	if set.Status.ReadyReplicas != 3 {
		t.Errorf("Expected updated StatefulSet to contain ready replicas %v, got %v instead",
			3, set.Status.ReadyReplicas)
	}
	if set.Status.AvailableReplicas != 0 {
		t.Errorf("Expected updated StatefulSet to contain available replicas %v, got %v instead",
			0, set.Status.AvailableReplicas)
	}

	if got, want := ssc.queue.Len(), 0; got != want {
		t.Errorf("queue.Len() = %v, want %v", got, want)
	}

	// RS should be re-queued after 700ms to recompute .status.availableReplicas (200ms extra for the test).
	tCtx.Eventually(ssc.queue.Len).WithTimeout(900*time.Millisecond).
		WithPolling(10*time.Millisecond).
		Should(gomega.Equal(1), " StatefulSet should be re-queued to recompute .status.availableReplicas")
}

func newFakeStatefulSetController(ctx context.Context, initialObjects ...runtime.Object) (*StatefulSetController, *StatefulPodControl, *fakeObjectManager, history.Interface) {
	client := fake.NewSimpleClientset(initialObjects...)
	informerFactory := informers.NewSharedInformerFactory(client, controller.NoResyncPeriodFunc())
	om := newFakeObjectManager(informerFactory)
	spc := NewStatefulPodControlFromManager(om, &noopRecorder{})
	ssu := newFakeStatefulSetStatusUpdater(informerFactory.Apps().V1().StatefulSets())
	ssc := NewStatefulSetController(
		ctx,
		informerFactory.Core().V1().Pods(),
		informerFactory.Apps().V1().StatefulSets(),
		informerFactory.Core().V1().PersistentVolumeClaims(),
		informerFactory.Apps().V1().ControllerRevisions(),
		client,
	)
	ssh := history.NewFakeHistory(informerFactory.Apps().V1().ControllerRevisions())
	ssc.podListerSynced = alwaysReady
	ssc.setListerSynced = alwaysReady
	ssc.control = NewDefaultStatefulSetControl(spc, ssu, ssh)

	return ssc, spc, om, ssh
}