From 7e5574d3dfa3443c93ea4fd4b422dfef4b8cf6b6 Mon Sep 17 00:00:00 2001 From: Soule BA Date: Wed, 15 Dec 2021 16:23:54 +0100 Subject: [PATCH] Add e2e tests for build kustomization Signed-off-by: Soule BA --- cmd/flux/build_kustomization_test.go | 77 ++++++++++++ .../delete-service-result.yaml | 95 +++++++++++++++ .../delete-service/deployment.yaml | 74 ++++++++++++ .../delete-service/hpa.yaml | 20 +++ .../patch-deployment-result.yaml | 114 ++++++++++++++++++ .../patch-deployment/deployment.yaml | 74 ++++++++++++ .../patch-deployment/hpa.yaml | 20 +++ .../patch-deployment/service.yaml | 17 +++ go.mod | 2 +- 9 files changed, 492 insertions(+), 1 deletion(-) create mode 100644 cmd/flux/build_kustomization_test.go create mode 100644 cmd/flux/testdata/build-kustomization/delete-service-result.yaml create mode 100644 cmd/flux/testdata/build-kustomization/delete-service/deployment.yaml create mode 100644 cmd/flux/testdata/build-kustomization/delete-service/hpa.yaml create mode 100644 cmd/flux/testdata/build-kustomization/patch-deployment-result.yaml create mode 100644 cmd/flux/testdata/build-kustomization/patch-deployment/deployment.yaml create mode 100644 cmd/flux/testdata/build-kustomization/patch-deployment/hpa.yaml create mode 100644 cmd/flux/testdata/build-kustomization/patch-deployment/service.yaml diff --git a/cmd/flux/build_kustomization_test.go b/cmd/flux/build_kustomization_test.go new file mode 100644 index 0000000000..db75fc0888 --- /dev/null +++ b/cmd/flux/build_kustomization_test.go @@ -0,0 +1,77 @@ +/* +Copyright 2021 The Flux authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import "testing" + +func setup(t *testing.T) { + t.Helper() + _, err := executeCommand("flux create source git podinfo --url https://github.com/stefanprodan/podinfo --tag-semver=\">=3.2.3\"") + if err != nil { + t.Fatalf("failed to create source: %v", err) + } + _, err = executeCommand("flux create kustomization podinfo --target-namespace=default --source=podinfo --path=\"./kustomize\" --prune=true --interval=5m") + if err != nil { + t.Fatalf("failed to create kustomization: %v", err) + } + + t.Cleanup(func() { + _, err = executeCommand("flux delete kustomization podinfo") + if err != nil { + t.Fatalf("failed to delete kustomization: %v", err) + } + _, err := executeCommand("flux delete source git podinfo") + if err != nil { + t.Fatalf("failed to delete source: %v", err) + } + }) +} + +func TestBuildKustomization(t *testing.T) { + tests := []struct { + name string + args string + assert assertFunc + }{ + { + name: "no args", + args: "build kustomization podinfo", + assert: assertError("invalid resource path"), + }, + { + name: "build deployment patch", + args: "build kustomization podinfo --path ./testdata/build-kustomization/patch-deployment", + assert: assertGoldenFile("./testdata/build-kustomization/patch-deployment-result.yaml"), + }, + { + name: "build service delete", + args: "build kustomization podinfo --path ./testdata/build-kustomization/delete-service", + assert: assertGoldenFile("./testdata/build-kustomization/delete-service-result.yaml"), + }, + } + + for _, tt := range tests { + setup(t) + t.Run(tt.name, func(t *testing.T) { + cmd := cmdTestCase{ + args: tt.args, + assert: tt.assert, + } + cmd.runTestCmd(t) + }) + } +} diff --git a/cmd/flux/testdata/build-kustomization/delete-service-result.yaml b/cmd/flux/testdata/build-kustomization/delete-service-result.yaml new file mode 100644 index 0000000000..40bde413bc --- /dev/null +++ b/cmd/flux/testdata/build-kustomization/delete-service-result.yaml @@ -0,0 +1,95 @@ +apiVersion: autoscaling/v2beta2 +kind: HorizontalPodAutoscaler +metadata: + name: podinfo + namespace: flux-system +spec: + maxReplicas: 4 + metrics: + - resource: + name: cpu + target: + averageUtilization: 99 + type: Utilization + type: Resource + minReplicas: 2 + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: podinfo +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: podinfo + namespace: flux-system +spec: + minReadySeconds: 3 + progressDeadlineSeconds: 60 + revisionHistoryLimit: 5 + selector: + matchLabels: + app: podinfo + strategy: + rollingUpdate: + maxUnavailable: 0 + type: RollingUpdate + template: + metadata: + annotations: + prometheus.io/port: "9797" + prometheus.io/scrape: "true" + labels: + app: podinfo + spec: + containers: + - command: + - ./podinfo + - --port=9898 + - --port-metrics=9797 + - --grpc-port=9999 + - --grpc-service-name=podinfo + - --level=info + - --random-delay=false + - --random-error=false + env: + - name: PODINFO_UI_COLOR + value: '#34577c' + image: ghcr.io/stefanprodan/podinfo:6.0.3 + imagePullPolicy: IfNotPresent + livenessProbe: + exec: + command: + - podcli + - check + - http + - localhost:9898/healthz + initialDelaySeconds: 5 + timeoutSeconds: 5 + name: podinfod + ports: + - containerPort: 9898 + name: http + protocol: TCP + - containerPort: 9797 + name: http-metrics + protocol: TCP + - containerPort: 9999 + name: grpc + protocol: TCP + readinessProbe: + exec: + command: + - podcli + - check + - http + - localhost:9898/readyz + initialDelaySeconds: 5 + timeoutSeconds: 5 + resources: + limits: + cpu: 2000m + memory: 512Mi + requests: + cpu: 100m + memory: 64Mi \ No newline at end of file diff --git a/cmd/flux/testdata/build-kustomization/delete-service/deployment.yaml b/cmd/flux/testdata/build-kustomization/delete-service/deployment.yaml new file mode 100644 index 0000000000..33a65a3ac3 --- /dev/null +++ b/cmd/flux/testdata/build-kustomization/delete-service/deployment.yaml @@ -0,0 +1,74 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: podinfo +spec: + minReadySeconds: 3 + revisionHistoryLimit: 5 + progressDeadlineSeconds: 60 + strategy: + rollingUpdate: + maxUnavailable: 0 + type: RollingUpdate + selector: + matchLabels: + app: podinfo + template: + metadata: + annotations: + prometheus.io/scrape: "true" + prometheus.io/port: "9797" + labels: + app: podinfo + spec: + containers: + - name: podinfod + image: ghcr.io/stefanprodan/podinfo:6.0.3 + imagePullPolicy: IfNotPresent + ports: + - name: http + containerPort: 9898 + protocol: TCP + - name: http-metrics + containerPort: 9797 + protocol: TCP + - name: grpc + containerPort: 9999 + protocol: TCP + command: + - ./podinfo + - --port=9898 + - --port-metrics=9797 + - --grpc-port=9999 + - --grpc-service-name=podinfo + - --level=info + - --random-delay=false + - --random-error=false + env: + - name: PODINFO_UI_COLOR + value: "#34577c" + livenessProbe: + exec: + command: + - podcli + - check + - http + - localhost:9898/healthz + initialDelaySeconds: 5 + timeoutSeconds: 5 + readinessProbe: + exec: + command: + - podcli + - check + - http + - localhost:9898/readyz + initialDelaySeconds: 5 + timeoutSeconds: 5 + resources: + limits: + cpu: 2000m + memory: 512Mi + requests: + cpu: 100m + memory: 64Mi \ No newline at end of file diff --git a/cmd/flux/testdata/build-kustomization/delete-service/hpa.yaml b/cmd/flux/testdata/build-kustomization/delete-service/hpa.yaml new file mode 100644 index 0000000000..f8111598c0 --- /dev/null +++ b/cmd/flux/testdata/build-kustomization/delete-service/hpa.yaml @@ -0,0 +1,20 @@ +apiVersion: autoscaling/v2beta2 +kind: HorizontalPodAutoscaler +metadata: + name: podinfo +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: podinfo + minReplicas: 2 + maxReplicas: 4 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + # scale up if usage is above + # 99% of the requested CPU (100m) + averageUtilization: 99 \ No newline at end of file diff --git a/cmd/flux/testdata/build-kustomization/patch-deployment-result.yaml b/cmd/flux/testdata/build-kustomization/patch-deployment-result.yaml new file mode 100644 index 0000000000..123acab095 --- /dev/null +++ b/cmd/flux/testdata/build-kustomization/patch-deployment-result.yaml @@ -0,0 +1,114 @@ +apiVersion: autoscaling/v2beta2 +kind: HorizontalPodAutoscaler +metadata: + name: podinfo + namespace: flux-system +spec: + maxReplicas: 4 + metrics: + - resource: + name: cpu + target: + averageUtilization: 99 + type: Utilization + type: Resource + minReplicas: 2 + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: podinfo +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: podinfo + namespace: flux-system +spec: + minReadySeconds: 3 + progressDeadlineSeconds: 60 + revisionHistoryLimit: 5 + selector: + matchLabels: + app: podinfo + strategy: + rollingUpdate: + maxUnavailable: 0 + type: RollingUpdate + template: + metadata: + annotations: + prometheus.io/port: "9797" + prometheus.io/scrape: "true" + labels: + app: podinfo + spec: + containers: + - command: + - ./podinfo + - --port=9898 + - --port-metrics=9797 + - --grpc-port=9999 + - --grpc-service-name=podinfo + - --level=info + - --random-delay=false + - --random-error=false + env: + - name: PODINFO_UI_COLOR + value: '#34577c' + image: ghcr.io/stefanprodan/podinfo:6.0.10 + imagePullPolicy: IfNotPresent + livenessProbe: + exec: + command: + - podcli + - check + - http + - localhost:9898/healthz + initialDelaySeconds: 5 + timeoutSeconds: 5 + name: podinfod + ports: + - containerPort: 9898 + name: http + protocol: TCP + - containerPort: 9797 + name: http-metrics + protocol: TCP + - containerPort: 9999 + name: grpc + protocol: TCP + readinessProbe: + exec: + command: + - podcli + - check + - http + - localhost:9898/readyz + initialDelaySeconds: 5 + timeoutSeconds: 5 + resources: + limits: + cpu: 2000m + memory: 512Mi + requests: + cpu: 100m + memory: 64Mi +--- +apiVersion: v1 +kind: Service +metadata: + name: podinfo + namespace: flux-system +spec: + ports: + - name: http + port: 9898 + protocol: TCP + targetPort: http + - name: grpc + port: 9999 + protocol: TCP + targetPort: grpc + selector: + app: podinfo + type: ClusterIP \ No newline at end of file diff --git a/cmd/flux/testdata/build-kustomization/patch-deployment/deployment.yaml b/cmd/flux/testdata/build-kustomization/patch-deployment/deployment.yaml new file mode 100644 index 0000000000..1a3287bd04 --- /dev/null +++ b/cmd/flux/testdata/build-kustomization/patch-deployment/deployment.yaml @@ -0,0 +1,74 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: podinfo +spec: + minReadySeconds: 3 + revisionHistoryLimit: 5 + progressDeadlineSeconds: 60 + strategy: + rollingUpdate: + maxUnavailable: 0 + type: RollingUpdate + selector: + matchLabels: + app: podinfo + template: + metadata: + annotations: + prometheus.io/scrape: "true" + prometheus.io/port: "9797" + labels: + app: podinfo + spec: + containers: + - name: podinfod + image: ghcr.io/stefanprodan/podinfo:6.0.10 + imagePullPolicy: IfNotPresent + ports: + - name: http + containerPort: 9898 + protocol: TCP + - name: http-metrics + containerPort: 9797 + protocol: TCP + - name: grpc + containerPort: 9999 + protocol: TCP + command: + - ./podinfo + - --port=9898 + - --port-metrics=9797 + - --grpc-port=9999 + - --grpc-service-name=podinfo + - --level=info + - --random-delay=false + - --random-error=false + env: + - name: PODINFO_UI_COLOR + value: "#34577c" + livenessProbe: + exec: + command: + - podcli + - check + - http + - localhost:9898/healthz + initialDelaySeconds: 5 + timeoutSeconds: 5 + readinessProbe: + exec: + command: + - podcli + - check + - http + - localhost:9898/readyz + initialDelaySeconds: 5 + timeoutSeconds: 5 + resources: + limits: + cpu: 2000m + memory: 512Mi + requests: + cpu: 100m + memory: 64Mi \ No newline at end of file diff --git a/cmd/flux/testdata/build-kustomization/patch-deployment/hpa.yaml b/cmd/flux/testdata/build-kustomization/patch-deployment/hpa.yaml new file mode 100644 index 0000000000..f8111598c0 --- /dev/null +++ b/cmd/flux/testdata/build-kustomization/patch-deployment/hpa.yaml @@ -0,0 +1,20 @@ +apiVersion: autoscaling/v2beta2 +kind: HorizontalPodAutoscaler +metadata: + name: podinfo +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: podinfo + minReplicas: 2 + maxReplicas: 4 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + # scale up if usage is above + # 99% of the requested CPU (100m) + averageUtilization: 99 \ No newline at end of file diff --git a/cmd/flux/testdata/build-kustomization/patch-deployment/service.yaml b/cmd/flux/testdata/build-kustomization/patch-deployment/service.yaml new file mode 100644 index 0000000000..0d26eca389 --- /dev/null +++ b/cmd/flux/testdata/build-kustomization/patch-deployment/service.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Service +metadata: + name: podinfo +spec: + type: ClusterIP + selector: + app: podinfo + ports: + - name: http + port: 9898 + protocol: TCP + targetPort: http + - port: 9999 + targetPort: grpc + protocol: TCP + name: grpc \ No newline at end of file diff --git a/go.mod b/go.mod index f60598f026..6da4aa792c 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/fluxcd/image-reflector-controller/api v0.14.0 github.com/fluxcd/kustomize-controller/api v0.18.2 github.com/fluxcd/notification-controller/api v0.19.0 - github.com/fluxcd/pkg/apis/kustomize v0.2.0 + github.com/fluxcd/pkg/apis/kustomize v0.3.0 github.com/fluxcd/pkg/apis/meta v0.10.1 github.com/fluxcd/pkg/runtime v0.12.2 github.com/fluxcd/pkg/ssa v0.5.0