From c5b5743d0714239088c6e335c83b1a13fcd02384 Mon Sep 17 00:00:00 2001 From: Jacopo Andrea Giola Date: Tue, 30 Jul 2024 10:34:54 +0200 Subject: [PATCH] feat: add mixed deployment types e2e test --- tests/e2e/integration_test.go | 91 ++++++++++++++----- .../stage1/test-deployment-1.yaml | 25 ----- .../stage1/test-deployment-2.yaml | 27 ------ .../stage2/test-deployment-1.yaml | 25 ----- .../{test-cm-1.yml => test.configmap.yml} | 0 ...deployment-1.yaml => test.deployment.yaml} | 8 +- .../smart-deploy/stage1/test.secret.yml | 7 ++ .../test.service.yml} | 8 +- .../{test-cm-1.yml => test.configmap.yml} | 0 ...deployment-1.yaml => test.deployment.yaml} | 8 +- .../test.service.yml} | 8 +- 11 files changed, 93 insertions(+), 114 deletions(-) delete mode 100644 tests/e2e/testdata/delete-resources/stage1/test-deployment-1.yaml delete mode 100644 tests/e2e/testdata/delete-resources/stage1/test-deployment-2.yaml delete mode 100644 tests/e2e/testdata/delete-resources/stage2/test-deployment-1.yaml rename tests/e2e/testdata/smart-deploy/stage1/{test-cm-1.yml => test.configmap.yml} (100%) rename tests/e2e/testdata/smart-deploy/stage1/{test-deployment-1.yaml => test.deployment.yaml} (85%) create mode 100644 tests/e2e/testdata/smart-deploy/stage1/test.secret.yml rename tests/e2e/testdata/smart-deploy/{stage2/test-service-1.yml => stage1/test.service.yml} (83%) rename tests/e2e/testdata/smart-deploy/stage2/{test-cm-1.yml => test.configmap.yml} (100%) rename tests/e2e/testdata/smart-deploy/stage2/{test-deployment-1.yaml => test.deployment.yaml} (84%) rename tests/e2e/testdata/smart-deploy/{stage1/test-service-1.yml => stage2/test.service.yml} (83%) diff --git a/tests/e2e/integration_test.go b/tests/e2e/integration_test.go index 63e32a3..9a7fd16 100644 --- a/tests/e2e/integration_test.go +++ b/tests/e2e/integration_test.go @@ -15,7 +15,6 @@ //go:build conformance -//nolint:thelper package e2e import ( @@ -23,13 +22,16 @@ import ( "context" "path/filepath" "testing" + "time" "github.com/mia-platform/mlp/v2/pkg/cmd/deploy" + "github.com/mia-platform/mlp/v2/pkg/extensions" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" appsv1 "k8s.io/api/apps/v1" batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/cli-runtime/pkg/genericclioptions" "sigs.k8s.io/e2e-framework/pkg/envconf" "sigs.k8s.io/e2e-framework/pkg/features" @@ -38,6 +40,7 @@ import ( func TestDeployOnEmptyCluster(t *testing.T) { deploymentFeature := features.New("deployment on empty cluster"). Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { + t.Helper() t.Logf("starting test with kubeconfig %q and namespace %q", cfg.KubeconfigFile(), cfg.Namespace()) buffer := new(bytes.Buffer) @@ -60,6 +63,8 @@ func TestDeployOnEmptyCluster(t *testing.T) { return ctx }). Assess("resoures are being created", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { + t.Helper() + deployment := new(appsv1.Deployment) require.NoError(t, cfg.Client().Resources().Get(ctx, "test", cfg.Namespace(), deployment)) t.Logf("deployment found: %s", deployment.Name) @@ -115,35 +120,79 @@ func TestDeployOnEmptyCluster(t *testing.T) { } func TestSmartDeploy(t *testing.T) { - deploymentFeature := features.New("smart deploy"). + deploying := func(ctx context.Context, cfg *envconf.Config, deployType, stage string) { + buffer := new(bytes.Buffer) + deployCmd := deploy.NewCommand(genericclioptions.NewConfigFlags(false)) + deployCmd.SetErr(buffer) + deployCmd.SetOut(buffer) + + deployCmd.SetArgs([]string{ + "--deploy-type", + deployType, + "--kubeconfig", + cfg.KubeconfigFile(), + "--namespace", + cfg.Namespace(), + "--filename", + filepath.Join("testdata", "smart-deploy", stage), + }) + + assert.NoError(t, deployCmd.ExecuteContext(ctx)) + t.Log(buffer.String()) + buffer.Reset() + } + + var deployChecksum, dependenciesChecksum string + + smartDeployPhase1 := features.New("deploy phase 1"). Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { + t.Helper() t.Logf("starting test with kubeconfig %q and namespace %q", cfg.KubeconfigFile(), cfg.Namespace()) + deploying(ctx, cfg, extensions.DeployAll, "stage1") + return ctx + }). + Assess("deploy phase 1", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { + t.Helper() - buffer := new(bytes.Buffer) - deployCmd := deploy.NewCommand(genericclioptions.NewConfigFlags(false)) - deployCmd.SetErr(buffer) - deployCmd.SetOut(buffer) + deployment := new(appsv1.Deployment) + require.NoError(t, cfg.Client().Resources().Get(ctx, "test", cfg.Namespace(), deployment)) + t.Logf("deployment found: %s", deployment.Name) + deployChecksum = deployment.Spec.Template.Annotations["mia-platform.eu/deploy-checksum"] + assert.NotEmpty(t, deployChecksum) + dependenciesChecksum = deployment.Spec.Template.Annotations["mia-platform.eu/dependencies-checksum"] + assert.NotEmpty(t, dependenciesChecksum) - deployCmd.SetArgs([]string{ - "--deploy-type", - "smart_deploy", - "--kubeconfig", - cfg.KubeconfigFile(), - "--namespace", - cfg.Namespace(), - "--filename", - filepath.Join("testdata", "smart-deploy", "stage1"), - }) + secret := new(corev1.Secret) + require.NoError(t, cfg.Client().Resources().Get(ctx, "test", cfg.Namespace(), secret)) + t.Logf("secret found: %s", secret.Name) - assert.NoError(t, deployCmd.ExecuteContext(ctx)) - t.Log(buffer.String()) - buffer.Reset() return ctx }). - Assess("smart deploy", func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context { + Feature() + + smartDeployPhase2 := features.New("smart deploy phase 2"). + Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { + t.Helper() + t.Logf("starting test with kubeconfig %q and namespace %q", cfg.KubeconfigFile(), cfg.Namespace()) + deploying(ctx, cfg, extensions.DeploySmart, "stage2") + time.Sleep(2 * time.Second) // sleep to wait background deletions + return ctx + }). + Assess("smart deploy phase 2", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { + t.Helper() + deployment := new(appsv1.Deployment) + require.NoError(t, cfg.Client().Resources().Get(ctx, "test", cfg.Namespace(), deployment)) + t.Logf("deployment found: %s", deployment.Name) + assert.Equal(t, deployChecksum, deployment.Spec.Template.Annotations["mia-platform.eu/deploy-checksum"]) + assert.NotEqual(t, dependenciesChecksum, deployment.Spec.Template.Annotations["mia-platform.eu/dependencies-checksum"]) + assert.EqualValues(t, 2, deployment.Generation) + + secret := new(corev1.Secret) + assert.True(t, apierrors.IsNotFound(cfg.Client().Resources().Get(ctx, "test", cfg.Namespace(), secret))) + return ctx }). Feature() - testenv.Test(t, deploymentFeature) + testenv.Test(t, smartDeployPhase1, smartDeployPhase2) } diff --git a/tests/e2e/testdata/delete-resources/stage1/test-deployment-1.yaml b/tests/e2e/testdata/delete-resources/stage1/test-deployment-1.yaml deleted file mode 100644 index a640b89..0000000 --- a/tests/e2e/testdata/delete-resources/stage1/test-deployment-1.yaml +++ /dev/null @@ -1,25 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - creationTimestamp: null - labels: - app: test-deployment - name: test-deployment - namespace: default -spec: - replicas: 1 - selector: - matchLabels: - app: test-deployment - strategy: {} - template: - metadata: - creationTimestamp: null - labels: - app: test-deployment - spec: - containers: - - image: nginx - name: nginx - resources: {} -status: {} diff --git a/tests/e2e/testdata/delete-resources/stage1/test-deployment-2.yaml b/tests/e2e/testdata/delete-resources/stage1/test-deployment-2.yaml deleted file mode 100644 index d64bfed..0000000 --- a/tests/e2e/testdata/delete-resources/stage1/test-deployment-2.yaml +++ /dev/null @@ -1,27 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - creationTimestamp: null - labels: - app: test-deployment-2 - annotation: - announo: valueuno - name: test-deployment-2 - namespace: default -spec: - replicas: 2 - selector: - matchLabels: - app: test-deployment-2 - strategy: {} - template: - metadata: - creationTimestamp: null - labels: - app: test-deployment-2 - spec: - containers: - - image: nginx - name: nginx - resources: {} -status: {} diff --git a/tests/e2e/testdata/delete-resources/stage2/test-deployment-1.yaml b/tests/e2e/testdata/delete-resources/stage2/test-deployment-1.yaml deleted file mode 100644 index a640b89..0000000 --- a/tests/e2e/testdata/delete-resources/stage2/test-deployment-1.yaml +++ /dev/null @@ -1,25 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - creationTimestamp: null - labels: - app: test-deployment - name: test-deployment - namespace: default -spec: - replicas: 1 - selector: - matchLabels: - app: test-deployment - strategy: {} - template: - metadata: - creationTimestamp: null - labels: - app: test-deployment - spec: - containers: - - image: nginx - name: nginx - resources: {} -status: {} diff --git a/tests/e2e/testdata/smart-deploy/stage1/test-cm-1.yml b/tests/e2e/testdata/smart-deploy/stage1/test.configmap.yml similarity index 100% rename from tests/e2e/testdata/smart-deploy/stage1/test-cm-1.yml rename to tests/e2e/testdata/smart-deploy/stage1/test.configmap.yml diff --git a/tests/e2e/testdata/smart-deploy/stage1/test-deployment-1.yaml b/tests/e2e/testdata/smart-deploy/stage1/test.deployment.yaml similarity index 85% rename from tests/e2e/testdata/smart-deploy/stage1/test-deployment-1.yaml rename to tests/e2e/testdata/smart-deploy/stage1/test.deployment.yaml index 893859e..0b28eca 100644 --- a/tests/e2e/testdata/smart-deploy/stage1/test-deployment-1.yaml +++ b/tests/e2e/testdata/smart-deploy/stage1/test.deployment.yaml @@ -3,19 +3,19 @@ kind: Deployment metadata: creationTimestamp: null labels: - app: test-deployment - name: test-deployment + app: test + name: test spec: replicas: 1 selector: matchLabels: - app: test-deployment + app: test strategy: {} template: metadata: creationTimestamp: null labels: - app: test-deployment + app: test annotations: mia-platform.eu/deploy-checksum: "" spec: diff --git a/tests/e2e/testdata/smart-deploy/stage1/test.secret.yml b/tests/e2e/testdata/smart-deploy/stage1/test.secret.yml new file mode 100644 index 0000000..74380bd --- /dev/null +++ b/tests/e2e/testdata/smart-deploy/stage1/test.secret.yml @@ -0,0 +1,7 @@ +apiVersion: v1 +kind: Secret +metadata: + name: test +type: Opaque +data: + key: dmFsdWU= diff --git a/tests/e2e/testdata/smart-deploy/stage2/test-service-1.yml b/tests/e2e/testdata/smart-deploy/stage1/test.service.yml similarity index 83% rename from tests/e2e/testdata/smart-deploy/stage2/test-service-1.yml rename to tests/e2e/testdata/smart-deploy/stage1/test.service.yml index cb2e2b8..27ab3c2 100644 --- a/tests/e2e/testdata/smart-deploy/stage2/test-service-1.yml +++ b/tests/e2e/testdata/smart-deploy/stage1/test.service.yml @@ -4,15 +4,15 @@ metadata: annotations: mia-platform.eu/version: 8.4.0 labels: - app: test-nginx + app: test app.kubernetes.io/component: custom app.kubernetes.io/managed-by: mia-platform - app.kubernetes.io/name: test-nginx + app.kubernetes.io/name: test app.kubernetes.io/part-of: test-mlp-kustomize-2 app.kubernetes.io/version: latest mia-platform.eu/stage: 'DEV' mia-platform.eu/tenant: kustomize-tenant - name: test-nginx + name: test spec: ports: - name: http @@ -21,5 +21,5 @@ spec: protocol: TCP targetPort: 3000 selector: - app: test-nginx + app: test type: ClusterIP diff --git a/tests/e2e/testdata/smart-deploy/stage2/test-cm-1.yml b/tests/e2e/testdata/smart-deploy/stage2/test.configmap.yml similarity index 100% rename from tests/e2e/testdata/smart-deploy/stage2/test-cm-1.yml rename to tests/e2e/testdata/smart-deploy/stage2/test.configmap.yml diff --git a/tests/e2e/testdata/smart-deploy/stage2/test-deployment-1.yaml b/tests/e2e/testdata/smart-deploy/stage2/test.deployment.yaml similarity index 84% rename from tests/e2e/testdata/smart-deploy/stage2/test-deployment-1.yaml rename to tests/e2e/testdata/smart-deploy/stage2/test.deployment.yaml index ab8f4bb..7725e41 100644 --- a/tests/e2e/testdata/smart-deploy/stage2/test-deployment-1.yaml +++ b/tests/e2e/testdata/smart-deploy/stage2/test.deployment.yaml @@ -3,19 +3,19 @@ kind: Deployment metadata: creationTimestamp: null labels: - app: test-deployment - name: test-deployment + app: test + name: test spec: replicas: 1 selector: matchLabels: - app: test-deployment + app: test strategy: {} template: metadata: creationTimestamp: null labels: - app: test-deployment + app: test spec: containers: - image: nginx diff --git a/tests/e2e/testdata/smart-deploy/stage1/test-service-1.yml b/tests/e2e/testdata/smart-deploy/stage2/test.service.yml similarity index 83% rename from tests/e2e/testdata/smart-deploy/stage1/test-service-1.yml rename to tests/e2e/testdata/smart-deploy/stage2/test.service.yml index cb2e2b8..27ab3c2 100644 --- a/tests/e2e/testdata/smart-deploy/stage1/test-service-1.yml +++ b/tests/e2e/testdata/smart-deploy/stage2/test.service.yml @@ -4,15 +4,15 @@ metadata: annotations: mia-platform.eu/version: 8.4.0 labels: - app: test-nginx + app: test app.kubernetes.io/component: custom app.kubernetes.io/managed-by: mia-platform - app.kubernetes.io/name: test-nginx + app.kubernetes.io/name: test app.kubernetes.io/part-of: test-mlp-kustomize-2 app.kubernetes.io/version: latest mia-platform.eu/stage: 'DEV' mia-platform.eu/tenant: kustomize-tenant - name: test-nginx + name: test spec: ports: - name: http @@ -21,5 +21,5 @@ spec: protocol: TCP targetPort: 3000 selector: - app: test-nginx + app: test type: ClusterIP