From 6565acfffcb11c57bcc6bd152cfa7be1428a6cc1 Mon Sep 17 00:00:00 2001 From: Alessio Pragliola Date: Wed, 27 Nov 2024 18:29:47 +0100 Subject: [PATCH 1/2] feat: added integration tests to cover evicted pods in post conditions Signed-off-by: Alessio Pragliola --- .../fixtures/cluster_test_fixtures.go | 30 +++++ .../features/postconditions_int_test.go | 104 ++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 tests/integration/features/postconditions_int_test.go diff --git a/tests/integration/features/fixtures/cluster_test_fixtures.go b/tests/integration/features/fixtures/cluster_test_fixtures.go index 47b17414274..8cca195ba98 100644 --- a/tests/integration/features/fixtures/cluster_test_fixtures.go +++ b/tests/integration/features/fixtures/cluster_test_fixtures.go @@ -79,6 +79,15 @@ func GetService(ctx context.Context, client client.Client, namespace, name strin return svc, err } +func GetPod(ctx context.Context, client client.Client, namespace, name string) (*corev1.Pod, error) { + pod := &corev1.Pod{} + err := client.Get(ctx, types.NamespacedName{ + Name: name, Namespace: namespace, + }, pod) + + return pod, err +} + func CreateService(ctx context.Context, client client.Client, namespace, svcName string) (*corev1.Service, error) { if err := client.Create(ctx, &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ @@ -101,6 +110,27 @@ func CreateService(ctx context.Context, client client.Client, namespace, svcName return GetService(ctx, client, namespace, svcName) } +func CreatePod(ctx context.Context, client client.Client, namespace, name string) (*corev1.Pod, error) { + if err := client.Create(ctx, &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + Namespace: namespace, + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "test", + Image: "test", + }, + }, + }, + }); err != nil { + return nil, err + } + + return GetPod(ctx, client, namespace, name) +} + func CreateSecret(name, namespace string) feature.Action { return func(ctx context.Context, cli client.Client, f *feature.Feature) error { secret := &corev1.Secret{ diff --git a/tests/integration/features/postconditions_int_test.go b/tests/integration/features/postconditions_int_test.go new file mode 100644 index 00000000000..d1568d4b65c --- /dev/null +++ b/tests/integration/features/postconditions_int_test.go @@ -0,0 +1,104 @@ +package features_test + +import ( + "context" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1" + "github.com/opendatahub-io/opendatahub-operator/v2/pkg/feature" + "github.com/opendatahub-io/opendatahub-operator/v2/tests/envtestutil" + "github.com/opendatahub-io/opendatahub-operator/v2/tests/integration/features/fixtures" + corev1 "k8s.io/api/core/v1" +) + +var _ = Describe("feature postconditions", func() { + Context("wait for pods to be ready", func() { + var ( + objectCleaner *envtestutil.Cleaner + namespace string + dsci *dsciv1.DSCInitialization + ) + + BeforeEach(func(ctx context.Context) { + objectCleaner = envtestutil.CreateCleaner(envTestClient, envTest.Config, fixtures.Timeout, fixtures.Interval) + + testFeatureName := "test-pods-ready" + namespace = envtestutil.AppendRandomNameTo(testFeatureName) + dsciName := envtestutil.AppendRandomNameTo(testFeatureName) + dsci = fixtures.NewDSCInitialization(ctx, envTestClient, dsciName, namespace) + }) + + AfterEach(func(ctx context.Context) { + objectCleaner.DeleteAll(ctx, dsci) + }) + + It("should succeed when all pods in the namespace are ready", func(ctx context.Context) { + // given + ns := fixtures.NewNamespace(namespace) + Expect(envTestClient.Create(ctx, ns)).To(Succeed()) + + podReady, err := fixtures.CreatePod(ctx, envTestClient, namespace, "test-pod") + Expect(err).ToNot(HaveOccurred()) + + podReady.Status.Phase = corev1.PodSucceeded + + Expect(envTestClient.Status().Update(ctx, podReady)).To(Succeed()) + + // when + featuresHandler := feature.ClusterFeaturesHandler(dsci, func(registry feature.FeaturesRegistry) error { + errFeatureAdd := registry.Add(feature.Define("check-pods-ready"). + PostConditions(feature.WaitForPodsToBeReady(namespace)), + ) + + Expect(errFeatureAdd).ToNot(HaveOccurred()) + + return nil + }) + + // then + Expect(featuresHandler.Apply(ctx, envTestClient)).To(Succeed()) + }) + + It("should succeed when there are evicted pods in the namespace", func(ctx context.Context) { + // given + ns := fixtures.NewNamespace(namespace) + Expect(envTestClient.Create(ctx, ns)).To(Succeed()) + + podReady, err := fixtures.CreatePod(ctx, envTestClient, namespace, "test-pod") + Expect(err).ToNot(HaveOccurred()) + + podReady.Status.Phase = corev1.PodSucceeded + + Expect(envTestClient.Status().Update(ctx, podReady)).To(Succeed()) + + podEvicted, err := fixtures.CreatePod(ctx, envTestClient, namespace, "test-pod-evicted") + Expect(err).ToNot(HaveOccurred()) + + podEvicted.Status.Phase = corev1.PodFailed + podEvicted.Status.Reason = "Evicted" + podEvicted.Status.Conditions = []corev1.PodCondition{ + { + Type: corev1.PodReady, + Status: corev1.ConditionFalse, + }, + } + + Expect(envTestClient.Status().Update(ctx, podEvicted)).To(Succeed()) + + // when + featuresHandler := feature.ClusterFeaturesHandler(dsci, func(registry feature.FeaturesRegistry) error { + errFeatureAdd := registry.Add(feature.Define("check-pods-ready"). + PostConditions(feature.WaitForPodsToBeReady(namespace)), + ) + + Expect(errFeatureAdd).ToNot(HaveOccurred()) + + return nil + }) + + // then + Expect(featuresHandler.Apply(ctx, envTestClient)).To(Succeed()) + }) + }) +}) From 7546b8d6f4eb7463b6f8d8765fd3f2d1010dc01d Mon Sep 17 00:00:00 2001 From: Alessio Pragliola Date: Wed, 27 Nov 2024 18:36:51 +0100 Subject: [PATCH 2/2] fix: linting issues Signed-off-by: Alessio Pragliola --- tests/integration/features/postconditions_int_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/integration/features/postconditions_int_test.go b/tests/integration/features/postconditions_int_test.go index d1568d4b65c..f1dc73eda65 100644 --- a/tests/integration/features/postconditions_int_test.go +++ b/tests/integration/features/postconditions_int_test.go @@ -3,13 +3,15 @@ package features_test import ( "context" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" + dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1" "github.com/opendatahub-io/opendatahub-operator/v2/pkg/feature" "github.com/opendatahub-io/opendatahub-operator/v2/tests/envtestutil" "github.com/opendatahub-io/opendatahub-operator/v2/tests/integration/features/fixtures" - corev1 "k8s.io/api/core/v1" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" ) var _ = Describe("feature postconditions", func() {