|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + v1 "k8s.io/api/core/v1" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "sigs.k8s.io/e2e-framework/klient" |
| 11 | + "sigs.k8s.io/e2e-framework/klient/k8s" |
| 12 | + "sigs.k8s.io/e2e-framework/klient/wait" |
| 13 | + "sigs.k8s.io/e2e-framework/klient/wait/conditions" |
| 14 | + "sigs.k8s.io/e2e-framework/pkg/envconf" |
| 15 | + "sigs.k8s.io/e2e-framework/pkg/features" |
| 16 | + |
| 17 | + spinapps_v1alpha1 "github.com/spinkube/spin-operator/api/v1alpha1" |
| 18 | + "github.com/spinkube/spin-operator/internal/generics" |
| 19 | +) |
| 20 | + |
| 21 | +// TestSpintainer is a test that checks that the minimal setup works |
| 22 | +// with the spintainer executor |
| 23 | +func TestSpintainer(t *testing.T) { |
| 24 | + var client klient.Client |
| 25 | + |
| 26 | + helloWorldImage := "ghcr.io/spinkube/spin-operator/hello-world:20240708-130250-gfefd2b1" |
| 27 | + testSpinAppName := "test-spintainer-app" |
| 28 | + |
| 29 | + defaultTest := features.New("default and most minimal setup"). |
| 30 | + Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 31 | + |
| 32 | + client = cfg.Client() |
| 33 | + |
| 34 | + if err := spinapps_v1alpha1.AddToScheme(client.Resources(testNamespace).GetScheme()); err != nil { |
| 35 | + t.Fatalf("failed to register the spinapps_v1alpha1 types with Kubernetes scheme: %s", err) |
| 36 | + } |
| 37 | + |
| 38 | + return ctx |
| 39 | + }). |
| 40 | + Assess("spin app custom resource is created", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 41 | + testSpinApp := newSpinAppCR(testSpinAppName, helloWorldImage, "spintainer") |
| 42 | + |
| 43 | + if err := client.Resources().Create(ctx, newSpintainerExecutor(testNamespace)); err != nil { |
| 44 | + t.Fatalf("Failed to create spinappexecutor: %s", err) |
| 45 | + } |
| 46 | + |
| 47 | + if err := client.Resources().Create(ctx, testSpinApp); err != nil { |
| 48 | + t.Fatalf("Failed to create spinapp: %s", err) |
| 49 | + } |
| 50 | + // wait for spinapp to be created |
| 51 | + if err := wait.For( |
| 52 | + conditions.New(client.Resources()).ResourceMatch(testSpinApp, func(object k8s.Object) bool { |
| 53 | + return true |
| 54 | + }), |
| 55 | + wait.WithTimeout(3*time.Minute), |
| 56 | + wait.WithInterval(30*time.Second), |
| 57 | + ); err != nil { |
| 58 | + t.Fatal(err) |
| 59 | + } |
| 60 | + |
| 61 | + return ctx |
| 62 | + }). |
| 63 | + Assess("spin app deployment and service are available", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 64 | + // wait for deployment to be ready |
| 65 | + if err := wait.For( |
| 66 | + conditions.New(client.Resources()).DeploymentAvailable(testSpinAppName, testNamespace), |
| 67 | + wait.WithTimeout(3*time.Minute), |
| 68 | + wait.WithInterval(30*time.Second), |
| 69 | + ); err != nil { |
| 70 | + t.Fatal(err) |
| 71 | + } |
| 72 | + |
| 73 | + svc := &v1.ServiceList{ |
| 74 | + Items: []v1.Service{ |
| 75 | + {ObjectMeta: metav1.ObjectMeta{Name: testSpinAppName, Namespace: testNamespace}}, |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + if err := wait.For( |
| 80 | + conditions.New(client.Resources()).ResourcesFound(svc), |
| 81 | + wait.WithTimeout(3*time.Minute), |
| 82 | + wait.WithInterval(30*time.Second), |
| 83 | + ); err != nil { |
| 84 | + t.Fatal(err) |
| 85 | + } |
| 86 | + return ctx |
| 87 | + }). |
| 88 | + Feature() |
| 89 | + testEnv.Test(t, defaultTest) |
| 90 | +} |
| 91 | + |
| 92 | +func newSpintainerExecutor(namespace string) *spinapps_v1alpha1.SpinAppExecutor { |
| 93 | + var testSpinAppExecutor = &spinapps_v1alpha1.SpinAppExecutor{ |
| 94 | + ObjectMeta: metav1.ObjectMeta{ |
| 95 | + Name: "spintainer", |
| 96 | + Namespace: namespace, |
| 97 | + }, |
| 98 | + Spec: spinapps_v1alpha1.SpinAppExecutorSpec{ |
| 99 | + CreateDeployment: true, |
| 100 | + DeploymentConfig: &spinapps_v1alpha1.ExecutorDeploymentConfig{ |
| 101 | + SpinImage: generics.Ptr("ghcr.io/fermyon/spin:v2.7.0"), |
| 102 | + }, |
| 103 | + }, |
| 104 | + } |
| 105 | + |
| 106 | + return testSpinAppExecutor |
| 107 | +} |
0 commit comments