From 8095b16c9a301eb864d64027579b96485364a15b Mon Sep 17 00:00:00 2001 From: Jonathan Wong Date: Fri, 13 Dec 2019 04:15:36 -0800 Subject: [PATCH] Add test for merge3 command --- cmd/config/cmd/merge3_test.go | 236 ++++++++++++++++++++++++++++++++++ 1 file changed, 236 insertions(+) create mode 100644 cmd/config/cmd/merge3_test.go diff --git a/cmd/config/cmd/merge3_test.go b/cmd/config/cmd/merge3_test.go new file mode 100644 index 000000000..761739144 --- /dev/null +++ b/cmd/config/cmd/merge3_test.go @@ -0,0 +1,236 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package cmd_test + +import ( + "io/ioutil" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "sigs.k8s.io/kustomize/cmd/config/cmd" + "sigs.k8s.io/kustomize/kyaml/copyutil" +) + +// TestMerge3Command verifies the merge3 correctly applies the diff between 2 sets of resources into another +func TestMerge3Command(t *testing.T) { + datadir, err := ioutil.TempDir("", "test-data") + defer os.RemoveAll(datadir) + if !assert.NoError(t, err) { + return + } + + err = ioutil.WriteFile(filepath.Join(datadir, "java-deployment.resource.yaml"), []byte(`apiVersion: apps/v1 +kind: Deployment +metadata: + name: app + labels: + app: java +spec: + replicas: 1 + selector: + matchLabels: + app: java + template: + metadata: + labels: + app: java + spec: + restartPolicy: Always + containers: + - name: app + image: gcr.io/project/app:version + command: + - java + - -jar + - /app.jar + ports: + - containerPort: 8080 + envFrom: + - configMapRef: + name: app-config + env: + - name: JAVA_OPTS + value: -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap + -Djava.security.egd=file:/dev/./urandom + imagePullPolicy: Always + minReadySeconds: 5 +`), 0600) + if !assert.NoError(t, err) { + return + } + + expected_dir, err := ioutil.TempDir("", "test-data-expected") + defer os.RemoveAll(expected_dir) + if !assert.NoError(t, err) { + return + } + + err = ioutil.WriteFile(filepath.Join(expected_dir, "java-deployment.resource.yaml"), []byte(`apiVersion: apps/v1 +kind: Deployment +metadata: + name: app + labels: + app: java + new-local: label + new-remote: label +spec: + replicas: 3 + selector: + matchLabels: + app: java + template: + metadata: + labels: + app: java + spec: + restartPolicy: Always + containers: + - name: app + image: gcr.io/project/app:version + command: + - java + - -jar + - /app.jar + - otherstuff + args: + - foo + ports: + - containerPort: 8080 + envFrom: + - configMapRef: + name: app-config + env: + - name: JAVA_OPTS + value: -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap + -Djava.security.egd=file:/dev/./urandom + imagePullPolicy: Always + minReadySeconds: 20 +`), 0600) + if !assert.NoError(t, err) { + return + } + + updated_dir, err := ioutil.TempDir("", "test-data-updated") + defer os.RemoveAll(updated_dir) + if !assert.NoError(t, err) { + return + } + + err = ioutil.WriteFile(filepath.Join(updated_dir, "java-deployment.resource.yaml"), []byte(`apiVersion: apps/v1 +kind: Deployment +metadata: + name: app + labels: + app: java + new-remote: label +spec: + replicas: 3 + selector: + matchLabels: + app: java + template: + metadata: + labels: + app: java + spec: + restartPolicy: Always + containers: + - name: app + image: gcr.io/project/app:version + command: + - java + - -jar + - /app.jar + - otherstuff + ports: + - containerPort: 8080 + envFrom: + - configMapRef: + name: app-config + env: + - name: JAVA_OPTS + value: -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap + -Djava.security.egd=file:/dev/./urandom + imagePullPolicy: Always + minReadySeconds: 5 +`), 0600) + if !assert.NoError(t, err) { + return + } + + dest_dir, err := ioutil.TempDir("", "test-data-dest") + defer os.RemoveAll(dest_dir) + if !assert.NoError(t, err) { + return + } + + err = ioutil.WriteFile(filepath.Join(dest_dir, "java-deployment.resource.yaml"), []byte(`apiVersion: apps/v1 +kind: Deployment +metadata: + name: app + labels: + app: java + new-local: label +spec: + replicas: 2 + selector: + matchLabels: + app: java + template: + metadata: + labels: + app: java + spec: + restartPolicy: Always + containers: + - name: app + image: gcr.io/project/app:version + command: + - java + - -jar + - /app.jar + args: + - foo + ports: + - containerPort: 8080 + envFrom: + - configMapRef: + name: app-config + env: + - name: JAVA_OPTS + value: -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap + -Djava.security.egd=file:/dev/./urandom + imagePullPolicy: Always + minReadySeconds: 20 +`), 0600) + if !assert.NoError(t, err) { + return + } + + // Perform merge3 with newly created sets + r := cmd.GetMerge3Runner("") + r.Command.SetArgs([]string{ + "--ancestor", + datadir, + "--from", + updated_dir, + "--to", + dest_dir, + }) + if !assert.NoError(t, r.Command.Execute()) { + return + } + + diffs, err := copyutil.Diff(dest_dir, expected_dir) + if !assert.NoError(t, err) { + t.FailNow() + } + + // Verify there are no diffs + if !assert.Empty(t, diffs.List()) { + t.FailNow() + } +} \ No newline at end of file