Skip to content

Commit

Permalink
update By(...) statements with nested func
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Bortnikov <artem.bortnikov@telekom.com>
  • Loading branch information
aobort committed Apr 19, 2024
1 parent ff281ea commit bb1c039
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 87 deletions.
90 changes: 49 additions & 41 deletions internal/controller/etcdcluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,50 +111,58 @@ var _ = Describe("EtcdCluster Controller", func() {
})

It("should reconcile a new EtcdCluster", func(ctx SpecContext) {
By("reconciling the EtcdCluster")
_, err = reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&etcdcluster)})
Expect(err).ToNot(HaveOccurred())
Eventually(Get(&etcdcluster)).Should(Succeed())
Expect(etcdcluster.Status.Conditions).To(HaveLen(2))
Expect(etcdcluster.Status.Conditions[0].Type).To(Equal(etcdaenixiov1alpha1.EtcdConditionInitialized))
Expect(etcdcluster.Status.Conditions[0].Status).To(Equal(metav1.ConditionStatus("True")))
Expect(etcdcluster.Status.Conditions[1].Type).To(Equal(etcdaenixiov1alpha1.EtcdConditionReady))
Expect(etcdcluster.Status.Conditions[1].Status).To(Equal(metav1.ConditionStatus("False")))

By("reconciling owned ConfigMap")
Eventually(Get(&configMap)).Should(Succeed())
Expect(configMap.Data).Should(HaveKeyWithValue("ETCD_INITIAL_CLUSTER_STATE", "new"))

By("reconciling owned headless Service")
Eventually(Get(&headlessService)).Should(Succeed())
Expect(headlessService.Spec.ClusterIP).Should(Equal("None"))

By("reconciling owned Service")
Eventually(Get(&service)).Should(Succeed())
Expect(service.Spec.ClusterIP).ShouldNot(Equal("None"))

By("reconciling owned StatefulSet")
Eventually(Get(&statefulSet)).Should(Succeed())
By("reconciling the EtcdCluster", func() {
_, err = reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&etcdcluster)})
Expect(err).ToNot(HaveOccurred())
Eventually(Get(&etcdcluster)).Should(Succeed())
Expect(etcdcluster.Status.Conditions).To(HaveLen(2))
Expect(etcdcluster.Status.Conditions[0].Type).To(Equal(etcdaenixiov1alpha1.EtcdConditionInitialized))
Expect(etcdcluster.Status.Conditions[0].Status).To(Equal(metav1.ConditionStatus("True")))
Expect(etcdcluster.Status.Conditions[1].Type).To(Equal(etcdaenixiov1alpha1.EtcdConditionReady))
Expect(etcdcluster.Status.Conditions[1].Status).To(Equal(metav1.ConditionStatus("False")))
})

By("reconciling owned ConfigMap", func() {
Eventually(Get(&configMap)).Should(Succeed())
Expect(configMap.Data).Should(HaveKeyWithValue("ETCD_INITIAL_CLUSTER_STATE", "new"))
})

By("reconciling owned headless Service", func() {
Eventually(Get(&headlessService)).Should(Succeed())
Expect(headlessService.Spec.ClusterIP).Should(Equal("None"))
})

By("reconciling owned Service", func() {
Eventually(Get(&service)).Should(Succeed())
Expect(service.Spec.ClusterIP).ShouldNot(Equal("None"))
})

By("reconciling owned StatefulSet", func() {
Eventually(Get(&statefulSet)).Should(Succeed())
})
})

It("should successfully reconcile the resource twice and mark as ready", func(ctx SpecContext) {
By("reconciling the EtcdCluster")
_, err = reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&etcdcluster)})
Expect(err).ToNot(HaveOccurred())

By("setting owned StatefulSet to ready state")
Eventually(Get(&statefulSet)).Should(Succeed())
Eventually(UpdateStatus(&statefulSet, func() {
statefulSet.Status.ReadyReplicas = *etcdcluster.Spec.Replicas
statefulSet.Status.Replicas = *etcdcluster.Spec.Replicas
})).Should(Succeed())

By("reconciling the EtcdCluster after owned StatefulSet is ready")
_, err = reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&etcdcluster)})
Expect(err).ToNot(HaveOccurred())
Eventually(Get(&etcdcluster)).Should(Succeed())
Expect(etcdcluster.Status.Conditions[1].Type).To(Equal(etcdaenixiov1alpha1.EtcdConditionReady))
Expect(string(etcdcluster.Status.Conditions[1].Status)).To(Equal("True"))
By("reconciling the EtcdCluster", func() {
_, err = reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&etcdcluster)})
Expect(err).ToNot(HaveOccurred())
})

By("setting owned StatefulSet to ready state", func() {
Eventually(Get(&statefulSet)).Should(Succeed())
Eventually(UpdateStatus(&statefulSet, func() {
statefulSet.Status.ReadyReplicas = *etcdcluster.Spec.Replicas
statefulSet.Status.Replicas = *etcdcluster.Spec.Replicas
})).Should(Succeed())
})

By("reconciling the EtcdCluster after owned StatefulSet is ready", func() {
_, err = reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&etcdcluster)})
Expect(err).ToNot(HaveOccurred())
Eventually(Get(&etcdcluster)).Should(Succeed())
Expect(etcdcluster.Status.Conditions[1].Type).To(Equal(etcdaenixiov1alpha1.EtcdConditionReady))
Expect(string(etcdcluster.Status.Conditions[1].Status)).To(Equal("True"))
})
})
})
})
30 changes: 16 additions & 14 deletions internal/controller/factory/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,23 @@ var _ = Describe("Condition builder", func() {
})
timestamp := etcdCluster.Status.Conditions[idx].LastTransitionTime

By("setting condition without status change")
SetCondition(etcdCluster, NewCondition(etcdaenixiov1alpha1.EtcdConditionInitialized).
WithStatus(false).
WithReason(string(etcdaenixiov1alpha1.EtcdCondTypeInitStarted)).
WithMessage("test").
Complete())
Expect(etcdCluster.Status.Conditions[idx].LastTransitionTime).To(Equal(timestamp))
By("setting condition without status change", func() {
SetCondition(etcdCluster, NewCondition(etcdaenixiov1alpha1.EtcdConditionInitialized).
WithStatus(false).
WithReason(string(etcdaenixiov1alpha1.EtcdCondTypeInitStarted)).
WithMessage("test").
Complete())
Expect(etcdCluster.Status.Conditions[idx].LastTransitionTime).To(Equal(timestamp))
})

By("setting condition with status changed")
SetCondition(etcdCluster, NewCondition(etcdaenixiov1alpha1.EtcdConditionInitialized).
WithStatus(true).
WithReason(string(etcdaenixiov1alpha1.EtcdCondTypeInitStarted)).
WithMessage("test").
Complete())
Expect(etcdCluster.Status.Conditions[idx].LastTransitionTime).NotTo(Equal(timestamp))
By("setting condition with status changed", func() {
SetCondition(etcdCluster, NewCondition(etcdaenixiov1alpha1.EtcdConditionInitialized).
WithStatus(true).
WithReason(string(etcdaenixiov1alpha1.EtcdCondTypeInitStarted)).
WithMessage("test").
Complete())
Expect(etcdCluster.Status.Conditions[idx].LastTransitionTime).NotTo(Equal(timestamp))
})
})
})

Expand Down
34 changes: 18 additions & 16 deletions internal/controller/factory/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,20 @@ func TestFactories(t *testing.T) {
var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: true,

// The BinaryAssetsDirectory is only required if you want to run the tests directly
// without call the makefile target test. If not informed it will look for the
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
// Note that you must have the required binaries setup under the bin directory to perform
// the tests directly. When we run make test it will be setup and used automatically.
BinaryAssetsDirectory: filepath.Join("..", "..", "..", "bin", "k8s",
fmt.Sprintf("1.29.0-%s-%s", runtime.GOOS, runtime.GOARCH)),
}
By("bootstrapping test environment", func() {
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: true,

// The BinaryAssetsDirectory is only required if you want to run the tests directly
// without call the makefile target test. If not informed it will look for the
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
// Note that you must have the required binaries setup under the bin directory to perform
// the tests directly. When we run make test it will be setup and used automatically.
BinaryAssetsDirectory: filepath.Join("..", "..", "..", "bin", "k8s",
fmt.Sprintf("1.29.0-%s-%s", runtime.GOOS, runtime.GOARCH)),
}
})

var err error
// cfg is defined in this file globally.
Expand All @@ -86,7 +87,8 @@ var _ = BeforeSuite(func() {
})

var _ = AfterSuite(func() {
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
By("tearing down the test environment", func() {
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})
})
34 changes: 18 additions & 16 deletions internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,20 @@ func TestControllers(t *testing.T) {
var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: true,

// The BinaryAssetsDirectory is only required if you want to run the tests directly
// without call the makefile target test. If not informed it will look for the
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
// Note that you must have the required binaries setup under the bin directory to perform
// the tests directly. When we run make test it will be setup and used automatically.
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s",
fmt.Sprintf("1.29.0-%s-%s", runtime.GOOS, runtime.GOARCH)),
}
By("bootstrapping test environment", func() {
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: true,

// The BinaryAssetsDirectory is only required if you want to run the tests directly
// without call the makefile target test. If not informed it will look for the
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
// Note that you must have the required binaries setup under the bin directory to perform
// the tests directly. When we run make test it will be setup and used automatically.
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s",
fmt.Sprintf("1.29.0-%s-%s", runtime.GOOS, runtime.GOARCH)),
}
})

var err error
// cfg is defined in this file globally.
Expand All @@ -87,7 +88,8 @@ var _ = BeforeSuite(func() {
})

var _ = AfterSuite(func() {
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
By("tearing down the test environment", func() {
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})
})

0 comments on commit bb1c039

Please sign in to comment.