Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added integration tests to cover evicted pods in post conditions #1399

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions tests/integration/features/fixtures/cluster_test_fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@
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{
Expand All @@ -101,6 +110,27 @@
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
}

Check warning on line 129 in tests/integration/features/fixtures/cluster_test_fixtures.go

View check run for this annotation

Codecov / codecov/patch

tests/integration/features/fixtures/cluster_test_fixtures.go#L128-L129

Added lines #L128 - L129 were not covered by tests

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{
Expand Down
106 changes: 106 additions & 0 deletions tests/integration/features/postconditions_int_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package features_test

import (
"context"

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"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess here is
It("should succeed when expected pod in the namespace is ready", func(ctx context.Context) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise, i am fine with the change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

condition feature.WaitForPodsToBeReady will be satisfied only if all pods in the namespace are "Ready", in the test case there's only one pod but "should succeed when all pods in the namespace are ready" should still be valid, I can add more pods in the test if you think is necessary

// 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())
})
})
})
Loading