@@ -6,11 +6,15 @@ import (
6
6
"path"
7
7
"testing"
8
8
9
+ "github.com/ghodss/yaml"
10
+ "github.com/go-kit/kit/log"
9
11
. "github.com/onsi/ginkgo"
10
12
. "github.com/onsi/gomega"
11
13
"github.com/replicatedhq/ship/pkg/api"
12
14
"github.com/replicatedhq/ship/pkg/testing/logger"
13
15
"github.com/spf13/afero"
16
+ "github.com/stretchr/testify/require"
17
+ k8stypes "sigs.k8s.io/kustomize/pkg/types"
14
18
)
15
19
16
20
func TestShipPatcher (t * testing.T ) {
@@ -140,3 +144,164 @@ var _ = Describe("ShipPatcher", func() {
140
144
})
141
145
})
142
146
})
147
+
148
+ func TestShipPatcher_writeTempKustomization (t * testing.T ) {
149
+ type testFile struct {
150
+ path string
151
+ contents string
152
+ }
153
+ tests := []struct {
154
+ name string
155
+ step api.Kustomize
156
+ resource string
157
+ testFiles []testFile
158
+ expectKustomization k8stypes.Kustomization
159
+ expectErr bool
160
+ }{
161
+ {
162
+ name : "no matching resource" ,
163
+ step : api.Kustomize {Base : "base/" },
164
+ resource : "./base/file.yaml" ,
165
+ testFiles : []testFile {
166
+ {
167
+ path : "./base/strawberry.yaml" ,
168
+ contents : `apiVersion: apps/v1beta2
169
+ kind: Deployment
170
+ metadata:
171
+ labels:
172
+ app: strawberry
173
+ heritage: Tiller
174
+ chart: strawberry-1.0.0
175
+ name: strawberry` ,
176
+ },
177
+ },
178
+ expectErr : true ,
179
+ },
180
+ {
181
+ name : "matching resource" ,
182
+ step : api.Kustomize {Base : "base/" },
183
+ resource : "./base/strawberry.yaml" ,
184
+ testFiles : []testFile {
185
+ {
186
+ path : "base/strawberry.yaml" ,
187
+ contents : `apiVersion: apps/v1beta2
188
+ kind: Deployment
189
+ metadata:
190
+ labels:
191
+ app: strawberry
192
+ heritage: Tiller
193
+ chart: strawberry-1.0.0
194
+ name: strawberry` ,
195
+ },
196
+ },
197
+ expectErr : false ,
198
+ expectKustomization : k8stypes.Kustomization {
199
+ Resources : []string {"strawberry.yaml" },
200
+ },
201
+ },
202
+ {
203
+ name : "matching resource, unclean path" ,
204
+ step : api.Kustomize {Base : "base/" },
205
+ resource : "./base/strawberry.yaml" ,
206
+ testFiles : []testFile {
207
+ {
208
+ path : "./base/strawberry.yaml" ,
209
+ contents : `apiVersion: apps/v1beta2
210
+ kind: Deployment
211
+ metadata:
212
+ labels:
213
+ app: strawberry
214
+ heritage: Tiller
215
+ chart: strawberry-1.0.0
216
+ name: strawberry` ,
217
+ },
218
+ },
219
+ expectErr : false ,
220
+ expectKustomization : k8stypes.Kustomization {
221
+ Resources : []string {"strawberry.yaml" },
222
+ },
223
+ },
224
+ {
225
+ name : "matching resource, unclean path in subdir" ,
226
+ step : api.Kustomize {Base : "base/" },
227
+ resource : "./base/flowers/rose.yml" ,
228
+ testFiles : []testFile {
229
+ {
230
+ path : "./base/strawberry.yaml" ,
231
+ contents : `apiVersion: apps/v1beta2
232
+ kind: Deployment
233
+ metadata:
234
+ labels:
235
+ app: strawberry
236
+ heritage: Tiller
237
+ chart: strawberry-1.0.0
238
+ name: strawberry` ,
239
+ },
240
+ {
241
+ path : "./base/flowers/rose.yml" ,
242
+ contents : `apiVersion: v1
243
+ kind: Service
244
+ metadata:
245
+ labels:
246
+ app: rose
247
+ name: rose` ,
248
+ },
249
+ },
250
+ expectErr : false ,
251
+ expectKustomization : k8stypes.Kustomization {
252
+ Resources : []string {"flowers/rose.yml" },
253
+ },
254
+ },
255
+ {
256
+ name : "alternate base" ,
257
+ step : api.Kustomize {Base : "another/base/path/" },
258
+ resource : "another/base/path/raspberry.yaml" ,
259
+ testFiles : []testFile {
260
+ {
261
+ path : "another/base/path/raspberry.yaml" ,
262
+ contents : `apiVersion: apps/v1
263
+ kind: Deployment
264
+ metadata:
265
+ labels:
266
+ app: raspberry
267
+ name: raspberry` ,
268
+ },
269
+ },
270
+ expectErr : false ,
271
+ expectKustomization : k8stypes.Kustomization {
272
+ Resources : []string {"raspberry.yaml" },
273
+ },
274
+ },
275
+ }
276
+ for _ , tt := range tests {
277
+ t .Run (tt .name , func (t * testing.T ) {
278
+ req := require .New (t )
279
+
280
+ mockFs := afero.Afero {Fs : afero .NewMemMapFs ()}
281
+ for _ , testFile := range tt .testFiles {
282
+ err := mockFs .WriteFile (testFile .path , []byte (testFile .contents ), 0755 )
283
+ req .NoError (err )
284
+ }
285
+ p := & ShipPatcher {
286
+ Logger : log .NewNopLogger (),
287
+ FS : mockFs ,
288
+ }
289
+
290
+ err := p .writeTempKustomization (tt .step , tt .resource )
291
+
292
+ if ! tt .expectErr {
293
+ req .NoError (err )
294
+
295
+ kustomizationB , err := mockFs .ReadFile (path .Join (tt .step .Base , "kustomization.yaml" ))
296
+ req .NoError (err )
297
+
298
+ kustomizationYaml := k8stypes.Kustomization {}
299
+ err = yaml .Unmarshal (kustomizationB , & kustomizationYaml )
300
+ req .NoError (err )
301
+ req .Equal (tt .expectKustomization , kustomizationYaml )
302
+ } else {
303
+ req .Error (err )
304
+ }
305
+ })
306
+ }
307
+ }
0 commit comments