|
| 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/wait" |
| 12 | + "sigs.k8s.io/e2e-framework/klient/wait/conditions" |
| 13 | + "sigs.k8s.io/e2e-framework/pkg/envconf" |
| 14 | + "sigs.k8s.io/e2e-framework/pkg/features" |
| 15 | + |
| 16 | + spinapps_v1alpha1 "github.com/spinkube/spin-operator/api/v1alpha1" |
| 17 | + "github.com/spinkube/spin-operator/e2e/helper" |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | +) |
| 20 | + |
| 21 | +// TestComponentFiltering checks that the operator and shim have support for running a subset of a Spin app's components |
| 22 | +func TestComponentFiltering(t *testing.T) { |
| 23 | + var client klient.Client |
| 24 | + |
| 25 | + // TODO: Use an image from a sample app in this repository |
| 26 | + appImage := "ghcr.io/kate-goldenring/spin-operator/examples/spin-salutations:20241022-144454" |
| 27 | + testSpinAppName := "test-component-filtering" |
| 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 := newSpinAppWithComponentFiltering(testSpinAppName, appImage, "containerd-shim-spin") |
| 42 | + |
| 43 | + if err := client.Resources().Create(ctx, testSpinApp); err != nil { |
| 44 | + t.Fatalf("Failed to create spinapp: %s", err) |
| 45 | + } |
| 46 | + return ctx |
| 47 | + }). |
| 48 | + Assess("spin app deployment and service are available", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 49 | + // wait for deployment to be ready |
| 50 | + if err := wait.For( |
| 51 | + conditions.New(client.Resources()).DeploymentAvailable(testSpinAppName, testNamespace), |
| 52 | + wait.WithTimeout(3*time.Minute), |
| 53 | + wait.WithInterval(time.Second), |
| 54 | + ); err != nil { |
| 55 | + t.Fatal(err) |
| 56 | + } |
| 57 | + |
| 58 | + svc := &v1.ServiceList{ |
| 59 | + Items: []v1.Service{ |
| 60 | + {ObjectMeta: metav1.ObjectMeta{Name: testSpinAppName, Namespace: testNamespace}}, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + if err := wait.For( |
| 65 | + conditions.New(client.Resources()).ResourcesFound(svc), |
| 66 | + wait.WithTimeout(3*time.Minute), |
| 67 | + wait.WithInterval(500*time.Millisecond), |
| 68 | + ); err != nil { |
| 69 | + t.Fatal(err) |
| 70 | + } |
| 71 | + return ctx |
| 72 | + }). |
| 73 | + Assess("spin app is only serving hello component", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 74 | + helper.EnsureDebugContainer(t, ctx, cfg, testNamespace) |
| 75 | + |
| 76 | + _, status, err := helper.CurlSpinApp(t, ctx, cfg, testNamespace, testSpinAppName, "/hi", "") |
| 77 | + |
| 78 | + require.NoError(t, err) |
| 79 | + require.Equal(t, 200, status) |
| 80 | + |
| 81 | + _, status, err = helper.CurlSpinApp(t, ctx, cfg, testNamespace, testSpinAppName, "/bye", "") |
| 82 | + |
| 83 | + require.NoError(t, err) |
| 84 | + require.Equal(t, 404, status) |
| 85 | + |
| 86 | + return ctx |
| 87 | + }). |
| 88 | + Feature() |
| 89 | + testEnv.Test(t, defaultTest) |
| 90 | +} |
| 91 | + |
| 92 | +// TODO - Create Spintainer once `--component-id` flag is available in Spin 3.0 and support added in Spintainer to pull flags to set from env var |
| 93 | +// func TestSpintainerTestComponentFiltering(t *testing.T) { |
| 94 | +// } |
| 95 | + |
| 96 | +func newSpinAppWithComponentFiltering(name, image, executor string) *spinapps_v1alpha1.SpinApp { |
| 97 | + return &spinapps_v1alpha1.SpinApp{ |
| 98 | + ObjectMeta: metav1.ObjectMeta{ |
| 99 | + Name: name, |
| 100 | + Namespace: testNamespace, |
| 101 | + }, |
| 102 | + Spec: spinapps_v1alpha1.SpinAppSpec{ |
| 103 | + Replicas: 1, |
| 104 | + Image: image, |
| 105 | + Executor: executor, |
| 106 | + // Only execute the hello component |
| 107 | + Components: []string{"hello"}, |
| 108 | + }, |
| 109 | + } |
| 110 | +} |
0 commit comments