Skip to content

Commit

Permalink
feat: add mixed deployment types e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiola committed Jul 30, 2024
1 parent 78f1ebc commit c5b5743
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 114 deletions.
91 changes: 70 additions & 21 deletions tests/e2e/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@

//go:build conformance

//nolint:thelper
package e2e

import (
"bytes"
"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"
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
25 changes: 0 additions & 25 deletions tests/e2e/testdata/delete-resources/stage1/test-deployment-1.yaml

This file was deleted.

27 changes: 0 additions & 27 deletions tests/e2e/testdata/delete-resources/stage1/test-deployment-2.yaml

This file was deleted.

25 changes: 0 additions & 25 deletions tests/e2e/testdata/delete-resources/stage2/test-deployment-1.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions tests/e2e/testdata/smart-deploy/stage1/test.secret.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Secret
metadata:
name: test
type: Opaque
data:
key: dmFsdWU=
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,5 +21,5 @@ spec:
protocol: TCP
targetPort: 3000
selector:
app: test-nginx
app: test
type: ClusterIP
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,5 +21,5 @@ spec:
protocol: TCP
targetPort: 3000
selector:
app: test-nginx
app: test
type: ClusterIP

0 comments on commit c5b5743

Please sign in to comment.