From a838b8542605e6298945ef79a4b9f5cb5d65d8e7 Mon Sep 17 00:00:00 2001 From: jregan Date: Sat, 29 Dec 2018 14:09:42 -0800 Subject: [PATCH] Convert CRD tests to in-memory. --- .../build/testdata/testcase-crds/crd/bee.yaml | 6 - .../testcase-crds/crd/kustomization.yaml | 11 -- .../testdata/testcase-crds/crd/mykind.yaml | 9 - .../testdata/testcase-crds/crd/secret.yaml | 6 - .../testdata/testcase-crds/expected.diff | 36 ---- .../testdata/testcase-crds/expected.yaml | 23 --- .../build/testdata/testcase-crds/test.yaml | 5 - .../crd/mycrd.json => target/crd_test.go} | 170 +++++++++++++++++- 8 files changed, 161 insertions(+), 105 deletions(-) delete mode 100644 pkg/commands/build/testdata/testcase-crds/crd/bee.yaml delete mode 100644 pkg/commands/build/testdata/testcase-crds/crd/kustomization.yaml delete mode 100644 pkg/commands/build/testdata/testcase-crds/crd/mykind.yaml delete mode 100644 pkg/commands/build/testdata/testcase-crds/crd/secret.yaml delete mode 100644 pkg/commands/build/testdata/testcase-crds/expected.diff delete mode 100644 pkg/commands/build/testdata/testcase-crds/expected.yaml delete mode 100644 pkg/commands/build/testdata/testcase-crds/test.yaml rename pkg/{commands/build/testdata/testcase-crds/crd/mycrd.json => target/crd_test.go} (61%) diff --git a/pkg/commands/build/testdata/testcase-crds/crd/bee.yaml b/pkg/commands/build/testdata/testcase-crds/crd/bee.yaml deleted file mode 100644 index 28cc9bd9e..000000000 --- a/pkg/commands/build/testdata/testcase-crds/crd/bee.yaml +++ /dev/null @@ -1,6 +0,0 @@ -apiVersion: v1beta1 -kind: Bee -metadata: - name: bee -spec: - action: fly diff --git a/pkg/commands/build/testdata/testcase-crds/crd/kustomization.yaml b/pkg/commands/build/testdata/testcase-crds/crd/kustomization.yaml deleted file mode 100644 index b5f7ff246..000000000 --- a/pkg/commands/build/testdata/testcase-crds/crd/kustomization.yaml +++ /dev/null @@ -1,11 +0,0 @@ -apiVersion: v1beta1 -kind: Kustomization -crds: -- mycrd.json - -resources: -- secret.yaml -- mykind.yaml -- bee.yaml - -namePrefix: test- \ No newline at end of file diff --git a/pkg/commands/build/testdata/testcase-crds/crd/mykind.yaml b/pkg/commands/build/testdata/testcase-crds/crd/mykind.yaml deleted file mode 100644 index 0a644daa6..000000000 --- a/pkg/commands/build/testdata/testcase-crds/crd/mykind.yaml +++ /dev/null @@ -1,9 +0,0 @@ -apiVersion: jingfang.example.com/v1beta1 -kind: MyKind -metadata: - name: mykind -spec: - secretRef: - name: crdsecret - beeRef: - name: bee diff --git a/pkg/commands/build/testdata/testcase-crds/crd/secret.yaml b/pkg/commands/build/testdata/testcase-crds/crd/secret.yaml deleted file mode 100644 index 9126dd868..000000000 --- a/pkg/commands/build/testdata/testcase-crds/crd/secret.yaml +++ /dev/null @@ -1,6 +0,0 @@ -apiVersion: v1 -kind: Secret -metadata: - name: crdsecret -data: - PATH: YmJiYmJiYmIK diff --git a/pkg/commands/build/testdata/testcase-crds/expected.diff b/pkg/commands/build/testdata/testcase-crds/expected.diff deleted file mode 100644 index ed2cf5559..000000000 --- a/pkg/commands/build/testdata/testcase-crds/expected.diff +++ /dev/null @@ -1,36 +0,0 @@ -diff -u -N /tmp/noop/jingfang.example.com_v1beta1_MyKind_mykind.yaml /tmp/transformed/jingfang.example.com_v1beta1_MyKind_mykind.yaml ---- /tmp/noop/jingfang.example.com_v1beta1_MyKind_mykind.yaml YYYY-MM-DD HH:MM:SS -+++ /tmp/transformed/jingfang.example.com_v1beta1_MyKind_mykind.yaml YYYY-MM-DD HH:MM:SS -@@ -1,9 +1,9 @@ - apiVersion: jingfang.example.com/v1beta1 - kind: MyKind - metadata: -- name: mykind -+ name: test-mykind - spec: - beeRef: -- name: bee -+ name: test-bee - secretRef: -- name: crdsecret -+ name: test-crdsecret -diff -u -N /tmp/noop/v1beta1_Bee_bee.yaml /tmp/transformed/v1beta1_Bee_bee.yaml ---- /tmp/noop/v1beta1_Bee_bee.yaml YYYY-MM-DD HH:MM:SS -+++ /tmp/transformed/v1beta1_Bee_bee.yaml YYYY-MM-DD HH:MM:SS -@@ -1,6 +1,6 @@ - apiVersion: v1beta1 - kind: Bee - metadata: -- name: bee -+ name: test-bee - spec: - action: fly -diff -u -N /tmp/noop/v1_Secret_crdsecret.yaml /tmp/transformed/v1_Secret_crdsecret.yaml ---- /tmp/noop/v1_Secret_crdsecret.yaml YYYY-MM-DD HH:MM:SS -+++ /tmp/transformed/v1_Secret_crdsecret.yaml YYYY-MM-DD HH:MM:SS -@@ -3,4 +3,4 @@ - PATH: YmJiYmJiYmIK - kind: Secret - metadata: -- name: crdsecret -+ name: test-crdsecret diff --git a/pkg/commands/build/testdata/testcase-crds/expected.yaml b/pkg/commands/build/testdata/testcase-crds/expected.yaml deleted file mode 100644 index 8116db1e6..000000000 --- a/pkg/commands/build/testdata/testcase-crds/expected.yaml +++ /dev/null @@ -1,23 +0,0 @@ -apiVersion: v1 -data: - PATH: YmJiYmJiYmIK -kind: Secret -metadata: - name: test-crdsecret ---- -apiVersion: jingfang.example.com/v1beta1 -kind: MyKind -metadata: - name: test-mykind -spec: - beeRef: - name: test-bee - secretRef: - name: test-crdsecret ---- -apiVersion: v1beta1 -kind: Bee -metadata: - name: test-bee -spec: - action: fly diff --git a/pkg/commands/build/testdata/testcase-crds/test.yaml b/pkg/commands/build/testdata/testcase-crds/test.yaml deleted file mode 100644 index c85121c60..000000000 --- a/pkg/commands/build/testdata/testcase-crds/test.yaml +++ /dev/null @@ -1,5 +0,0 @@ -description: name reference in CRDs -args: [] -filename: testdata/testcase-crds/crd -expectedStdout: testdata/testcase-crds/expected.yaml -expectedDiff: testdata/testcase-crds/expected.diff diff --git a/pkg/commands/build/testdata/testcase-crds/crd/mycrd.json b/pkg/target/crd_test.go similarity index 61% rename from pkg/commands/build/testdata/testcase-crds/crd/mycrd.json rename to pkg/target/crd_test.go index afe38882d..68e79b2a7 100644 --- a/pkg/commands/build/testdata/testcase-crds/crd/mycrd.json +++ b/pkg/target/crd_test.go @@ -1,14 +1,78 @@ +/* +Copyright 2018 The Kubernetes 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 target + +import ( + "testing" +) + +func writeBaseWithCrd(th *KustTestHarness) { + th.writeK("/app/base", ` +apiVersion: v1beta1 +kind: Kustomization +crds: +- mycrd.json + +resources: +- secret.yaml +- mykind.yaml +- bee.yaml + +namePrefix: x- +`) + th.writeF("/app/base/bee.yaml", ` +apiVersion: v1beta1 +kind: Bee +metadata: + name: bee +spec: + action: fly +`) + th.writeF("/app/base/mykind.yaml", ` +apiVersion: jingfang.example.com/v1beta1 +kind: MyKind +metadata: + name: mykind +spec: + secretRef: + name: crdsecret + beeRef: + name: bee +`) + th.writeF("/app/base/secret.yaml", ` +apiVersion: v1 +kind: Secret +metadata: + name: crdsecret +data: + PATH: yellowBrickRoad +`) + th.writeF("/app/base/mycrd.json", ` { "github.com/example/pkg/apis/jingfang/v1beta1.Bee": { "Schema": { "description": "Bee", "properties": { "apiVersion": { - "description": "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources", + "description": "APIVersion defines the versioned schema of this representation of an object.", "type": "string" }, "kind": { - "description": "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds", + "description": "Kind is a string value representing the REST resource this object represents.", "type": "string" }, "metadata": { @@ -35,7 +99,7 @@ ], "properties": { "apiVersion": { - "description": "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources", + "description": "APIVersion defines the versioned schema of this representation of an object.", "type": "string" }, "items": { @@ -45,7 +109,7 @@ } }, "kind": { - "description": "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds", + "description": "Kind is a string value representing the REST resource this object represents.", "type": "string" }, "metadata": { @@ -85,11 +149,11 @@ "description": "MyKind", "properties": { "apiVersion": { - "description": "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources", + "description": "APIVersion defines the versioned schema of this representation of an object.", "type": "string" }, "kind": { - "description": "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds", + "description": "Kind is a string value representing the REST resource this object represents.", "type": "string" }, "metadata": { @@ -116,7 +180,7 @@ ], "properties": { "apiVersion": { - "description": "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources", + "description": "APIVersion defines the versioned schema of this representation of an object.", "type": "string" }, "items": { @@ -126,7 +190,7 @@ } }, "kind": { - "description": "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds", + "description": "Kind is a string value representing the REST resource this object represents.", "type": "string" }, "metadata": { @@ -149,7 +213,7 @@ "$ref": "github.com/example/pkg/apis/jingfang/v1beta1.BeeObjectReference" }, "secretRef": { - "description": "If defined, we use this secret for configuring the MYSQL_ROOT_PASSWORD If it is not set we generate a secret dynamically", + "description": "If defined, use this secret for configuring the MYSQL_ROOT_PASSWORD", "x-kubernetes-object-ref-api-version": "v1", "x-kubernetes-object-ref-kind": "Secret", "$ref": "k8s.io/api/core/v1.LocalObjectReference" @@ -168,3 +232,91 @@ "Dependencies": [] } } +`) +} + +func TestCrdBase(t *testing.T) { + th := NewKustTestHarness(t, "/app/base") + writeBaseWithCrd(th) + m, err := th.makeKustTarget().MakeCustomizedResMap() + if err != nil { + t.Fatalf("Err: %v", err) + } + th.assertActualEqualsExpected(m, ` +apiVersion: v1 +data: + PATH: yellowBrickRoad +kind: Secret +metadata: + name: x-crdsecret +--- +apiVersion: jingfang.example.com/v1beta1 +kind: MyKind +metadata: + name: x-mykind +spec: + beeRef: + name: x-bee + secretRef: + name: x-crdsecret +--- +apiVersion: v1beta1 +kind: Bee +metadata: + name: x-bee +spec: + action: fly +`) +} + +func TestCrdWithOverlay(t *testing.T) { + th := NewKustTestHarness(t, "/app/overlay") + writeBaseWithCrd(th) + th.writeK("/app/overlay", ` +apiVersion: v1beta1 +kind: Kustomization +namePrefix: prod- +bases: +- ../base +patchesStrategicMerge: +- bee.yaml +`) + th.writeF("/app/overlay/bee.yaml", ` +apiVersion: v1beta1 +kind: Bee +metadata: + name: bee +spec: + action: makehoney +`) + m, err := th.makeKustTarget().MakeCustomizedResMap() + if err != nil { + t.Fatalf("Err: %v", err) + } + // TODO(#605): Some output here is wrong due to #605 + th.assertActualEqualsExpected(m, ` +apiVersion: v1 +data: + PATH: yellowBrickRoad +kind: Secret +metadata: + name: prod-x-crdsecret +--- +apiVersion: jingfang.example.com/v1beta1 +kind: MyKind +metadata: + name: prod-x-mykind +spec: + beeRef: + name: bee + secretRef: + name: crdsecret +--- +apiVersion: v1beta1 +kind: Bee +metadata: + name: prod-bee +spec: + action: makehoney +`) +}