From 452b4d38754c702c483aad7dadd7d844098c91a4 Mon Sep 17 00:00:00 2001 From: Tomer Zait Date: Sat, 16 Dec 2023 16:28:43 +0200 Subject: [PATCH] add tests, die if inputfiles not specified --- pkg/app/app.go | 7 +- script/test/cmd/tests_new.sh | 8 ++ script/test/fixtures/configmap-pod/bar.env | 3 + .../fixtures/configmap-pod/docker-compose.yml | 16 ++++ script/test/fixtures/configmap-pod/foo.env | 2 + .../fixtures/configmap-pod/output-k8s.yaml | 76 +++++++++++++++++++ .../fixtures/configmap-pod/output-os.yaml | 53 +++++++++++++ 7 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 script/test/fixtures/configmap-pod/bar.env create mode 100644 script/test/fixtures/configmap-pod/docker-compose.yml create mode 100644 script/test/fixtures/configmap-pod/foo.env create mode 100644 script/test/fixtures/configmap-pod/output-k8s.yaml create mode 100644 script/test/fixtures/configmap-pod/output-os.yaml diff --git a/pkg/app/app.go b/pkg/app/app.go index 4ad3c022c6..39f3caf8bd 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -225,6 +225,11 @@ func Convert(opt kobject.ConvertOptions) ([]runtime.Object, error) { komposeObject.Namespace = opt.Namespace + // Check if input files are specified + if len(opt.InputFiles) <= 0 { + log.Fatal("No input files specified") + } + // convert env_file from absolute to relative path for _, service := range komposeObject.ServiceConfigs { if len(service.EnvFile) <= 0 { @@ -245,7 +250,7 @@ func Convert(opt kobject.ConvertOptions) ([]runtime.Object, error) { log.Fatalf(err.Error()) } - service.EnvFile[i] = strings.Replace(relPath, "\\", "/", -1) + service.EnvFile[i] = filepath.ToSlash(relPath) } } diff --git a/script/test/cmd/tests_new.sh b/script/test/cmd/tests_new.sh index 933a1920ae..c1bdac34b8 100755 --- a/script/test/cmd/tests_new.sh +++ b/script/test/cmd/tests_new.sh @@ -142,6 +142,14 @@ os_output="$KOMPOSE_ROOT/script/test/fixtures/configmap-volume/output-os-withlab convert::expect_success_and_warning "$k8s_cmd" "$k8s_output" || exit 1 convert::expect_success "$os_cmd" "$os_output" || exit 1 +# test configmap pod generation +k8s_cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/configmap-pod/docker-compose.yml convert --stdout --with-kompose-annotation=false" +k8s_output="$KOMPOSE_ROOT/script/test/fixtures/configmap-pod/output-k8s.yaml" +os_output="$KOMPOSE_ROOT/script/test/fixtures/configmap-pod/output-os.yaml" +os_cmd="kompose --provider=openshift -f $KOMPOSE_ROOT/script/test/fixtures/configmap-pod/docker-compose.yml convert --stdout --with-kompose-annotation=false" +convert::expect_success "$k8s_cmd" "$k8s_output" || exit 1 +convert::expect_success "$os_cmd" "$os_output" || exit 1 + # Test that emptyDir works k8s_cmd="kompose -f $KOMPOSE_ROOT/script/test/fixtures/change-in-volume/docker-compose.yml convert --with-kompose-annotation=false --stdout --volumes emptyDir" k8s_output="$KOMPOSE_ROOT/script/test/fixtures/change-in-volume/output-k8s-empty-vols-template.yaml" diff --git a/script/test/fixtures/configmap-pod/bar.env b/script/test/fixtures/configmap-pod/bar.env new file mode 100644 index 0000000000..8c113bf9d7 --- /dev/null +++ b/script/test/fixtures/configmap-pod/bar.env @@ -0,0 +1,3 @@ +# Multi-line test +FOO=BAR +BAR=FOO diff --git a/script/test/fixtures/configmap-pod/docker-compose.yml b/script/test/fixtures/configmap-pod/docker-compose.yml new file mode 100644 index 0000000000..6b77ab3618 --- /dev/null +++ b/script/test/fixtures/configmap-pod/docker-compose.yml @@ -0,0 +1,16 @@ +version: '3' + +services: + redis: + image: 'bitnami/redis:latest' + environment: + - ALLOW_EMPTY_PASSWORD=no + # Env file will override environment / warn! + env_file: + - "foo.env" + - bar.env + labels: + kompose.service.type: nodeport + ports: + - '6379:6379' + restart: "no" diff --git a/script/test/fixtures/configmap-pod/foo.env b/script/test/fixtures/configmap-pod/foo.env new file mode 100644 index 0000000000..63b8c81a0a --- /dev/null +++ b/script/test/fixtures/configmap-pod/foo.env @@ -0,0 +1,2 @@ +# Test comment! +ALLOW_EMPTY_PASSWORD=yes diff --git a/script/test/fixtures/configmap-pod/output-k8s.yaml b/script/test/fixtures/configmap-pod/output-k8s.yaml new file mode 100644 index 0000000000..edf1039e5a --- /dev/null +++ b/script/test/fixtures/configmap-pod/output-k8s.yaml @@ -0,0 +1,76 @@ +--- +apiVersion: v1 +kind: Service +metadata: + creationTimestamp: null + labels: + io.kompose.service: redis + name: redis +spec: + ports: + - name: "6379" + port: 6379 + targetPort: 6379 + selector: + io.kompose.service: redis + type: NodePort + +--- +apiVersion: v1 +kind: Pod +metadata: + creationTimestamp: null + labels: + io.kompose.network/configmap-pod-default: "true" + io.kompose.service: redis + name: redis +spec: + containers: + - env: + - name: ALLOW_EMPTY_PASSWORD + valueFrom: + configMapKeyRef: + key: ALLOW_EMPTY_PASSWORD + name: foo-env + - name: BAR + valueFrom: + configMapKeyRef: + key: BAR + name: bar-env + - name: FOO + valueFrom: + configMapKeyRef: + key: FOO + name: bar-env + image: bitnami/redis:latest + name: redis + ports: + - containerPort: 6379 + hostPort: 6379 + protocol: TCP + resources: {} + restartPolicy: Never + +--- +apiVersion: v1 +data: + ALLOW_EMPTY_PASSWORD: "yes" +kind: ConfigMap +metadata: + creationTimestamp: null + labels: + io.kompose.service: redis-foo-env + name: foo-env + +--- +apiVersion: v1 +data: + BAR: FOO + FOO: BAR +kind: ConfigMap +metadata: + creationTimestamp: null + labels: + io.kompose.service: redis-bar-env + name: bar-env + diff --git a/script/test/fixtures/configmap-pod/output-os.yaml b/script/test/fixtures/configmap-pod/output-os.yaml new file mode 100644 index 0000000000..eefa4acc93 --- /dev/null +++ b/script/test/fixtures/configmap-pod/output-os.yaml @@ -0,0 +1,53 @@ +--- +apiVersion: v1 +kind: Service +metadata: + creationTimestamp: null + labels: + io.kompose.service: redis + name: redis +spec: + ports: + - name: "6379" + port: 6379 + targetPort: 6379 + selector: + io.kompose.service: redis + type: NodePort + +--- +apiVersion: v1 +kind: Pod +metadata: + creationTimestamp: null + labels: + io.kompose.network/configmap-pod-default: "true" + io.kompose.service: redis + name: redis +spec: + containers: + - env: + - name: ALLOW_EMPTY_PASSWORD + valueFrom: + configMapKeyRef: + key: ALLOW_EMPTY_PASSWORD + name: foo-env + - name: BAR + valueFrom: + configMapKeyRef: + key: BAR + name: bar-env + - name: FOO + valueFrom: + configMapKeyRef: + key: FOO + name: bar-env + image: bitnami/redis:latest + name: redis + ports: + - containerPort: 6379 + hostPort: 6379 + protocol: TCP + resources: {} + restartPolicy: Never +