github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/render/generate/generate_test.go (about) 1 /* 2 Copyright 2021 The Skaffold Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package generate 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/manifest" 24 latestV2 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v2" 25 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" 26 "github.com/GoogleContainerTools/skaffold/testutil" 27 ) 28 29 const ( 30 // Test file under <tmp>/pod.yaml 31 podYaml = `apiVersion: v1 32 kind: Pod 33 metadata: 34 name: leeroy-web 35 spec: 36 containers: 37 - name: leeroy-web 38 image: leeroy-web 39 ` 40 41 // Test file under <tmp>/pods.yaml. This file contains multiple config object. 42 podsYaml = `apiVersion: v1 43 kind: Pod 44 metadata: 45 name: leeroy-web2 46 spec: 47 containers: 48 - name: leeroy-web2 49 image: leeroy-web2 50 --- 51 apiVersion: v1 52 kind: Pod 53 metadata: 54 name: leeroy-web3 55 spec: 56 containers: 57 - name: leeroy-web3 58 image: leeroy-web3 59 ` 60 61 // Test file under <tmp>/base/patch.yaml 62 patchYaml = `apiVersion: apps/v1 63 kind: Deployment 64 metadata: 65 name: kustomize-test 66 spec: 67 template: 68 spec: 69 containers: 70 - name: kustomize-test 71 image: index.docker.io/library/busybox 72 command: 73 - sleep 74 - "3600" 75 ` 76 // Test file under <tmp>/base/deployment.yaml 77 kustomizeDeploymentYaml = `apiVersion: apps/v1 78 kind: Deployment 79 metadata: 80 name: kustomize-test 81 labels: 82 app: kustomize-test 83 spec: 84 replicas: 1 85 selector: 86 matchLabels: 87 app: kustomize-test 88 template: 89 metadata: 90 labels: 91 app: kustomize-test 92 spec: 93 containers: 94 - name: kustomize-test 95 image: not/a/valid/image 96 ` 97 // The kustomize build result from kustomizeYaml file. 98 kustomizePatchedOutput = `apiVersion: apps/v1 99 kind: Deployment 100 metadata: 101 labels: 102 app: kustomize-test 103 name: kustomize-test 104 spec: 105 replicas: 1 106 selector: 107 matchLabels: 108 app: kustomize-test 109 template: 110 metadata: 111 labels: 112 app: kustomize-test 113 spec: 114 containers: 115 - command: 116 - sleep 117 - "3600" 118 image: index.docker.io/library/busybox 119 name: kustomize-test 120 ` 121 // Test file under <tmp>/base/kustomization.yaml 122 kustomizeYaml = `resources: 123 - deployment.yaml 124 patches: 125 - patch.yaml 126 ` 127 ) 128 129 func TestGenerate(t *testing.T) { 130 tests := []struct { 131 description string 132 generateConfig latestV2.Generate 133 expected manifest.ManifestList 134 }{ 135 { 136 description: "render raw manifests", 137 generateConfig: latestV2.Generate{ 138 RawK8s: []string{"pod.yaml"}, 139 }, 140 expected: manifest.ManifestList{[]byte(podYaml)}, 141 }, 142 { 143 description: "render glob raw manifests", 144 generateConfig: latestV2.Generate{ 145 RawK8s: []string{"*.yaml"}, 146 }, 147 expected: manifest.ManifestList{[]byte(podYaml), []byte(podsYaml)}, 148 }, 149 /* disabled 150 { 151 description: "render kustomize manifests", 152 generateConfig: latestV2.Generate{ 153 Kustomize: []string{"base"}, 154 }, 155 expected: manifest.ManifestList{[]byte(kustomizePatchedOutput)}, 156 }, 157 */ 158 } 159 for _, test := range tests { 160 testutil.Run(t, test.description, func(t *testutil.T) { 161 fakeCmd := testutil.CmdRunOut("kustomize build base", kustomizePatchedOutput) 162 t.Override(&util.DefaultExecCommand, fakeCmd) 163 164 t.NewTempDir(). 165 Write("pod.yaml", podYaml). 166 Write("pods.yaml", podsYaml). 167 Write("base/kustomization.yaml", kustomizeYaml). 168 Write("base/patch.yaml", patchYaml). 169 Write("base/deployment.yaml", kustomizeDeploymentYaml). 170 Touch("empty.ignored"). 171 Chdir() 172 173 g := NewGenerator(".", test.generateConfig) 174 actual, err := g.Generate(context.Background()) 175 t.CheckNoError(err) 176 t.CheckDeepEqual(actual.String(), test.expected.String()) 177 }) 178 } 179 }