This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp_repository_test.go
612 lines (531 loc) · 18 KB
/
app_repository_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
package repositories_test
import (
"context"
"fmt"
"testing"
"time"
. "code.cloudfoundry.org/cf-k8s-api/repositories"
workloadsv1alpha1 "code.cloudfoundry.org/cf-k8s-controllers/apis/workloads/v1alpha1"
"github.com/google/uuid"
. "github.com/onsi/gomega"
"github.com/sclevine/spec"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var _ = SuiteDescribe("App Repository FetchApp", testFetchApp)
var _ = SuiteDescribe("App Repository FetchAppList", testFetchAppList)
var _ = SuiteDescribe("App Repository CreateApp", testCreateApp)
var _ = SuiteDescribe("App Repository Secret Create/Update", testEnvSecretCreate)
var _ = SuiteDescribe("App Repository FetchNamespace", func(t *testing.T, when spec.G, it spec.S) {
g := NewWithT(t)
when("space does not exist", func() {
it("returns an unauthorized or not found err", func() {
appRepo := AppRepo{}
client, err := BuildClient(k8sConfig)
_, err = appRepo.FetchNamespace(context.Background(), client, "some-guid")
g.Expect(err).To(HaveOccurred())
g.Expect(err).To(MatchError("Resource not found or permission denied."))
})
})
})
func testFetchApp(t *testing.T, when spec.G, it spec.S) {
g := NewWithT(t)
const namespace = "default"
var testCtx context.Context
it.Before(func() {
testCtx = context.Background()
})
when("multiple Apps exist", func() {
var (
app1GUID string
app2GUID string
cfApp1 *workloadsv1alpha1.CFApp
cfApp2 *workloadsv1alpha1.CFApp
)
it.Before(func() {
beforeCtx := context.Background()
app1GUID = generateGUID()
app2GUID = generateGUID()
cfApp1 = &workloadsv1alpha1.CFApp{
ObjectMeta: metav1.ObjectMeta{
Name: app1GUID,
Namespace: namespace,
},
Spec: workloadsv1alpha1.CFAppSpec{
Name: "test-app1",
DesiredState: "STOPPED",
Lifecycle: workloadsv1alpha1.Lifecycle{
Type: "buildpack",
Data: workloadsv1alpha1.LifecycleData{
Buildpacks: []string{},
Stack: "",
},
},
},
}
g.Expect(k8sClient.Create(beforeCtx, cfApp1)).To(Succeed())
cfApp2 = &workloadsv1alpha1.CFApp{
ObjectMeta: metav1.ObjectMeta{
Name: app2GUID,
Namespace: namespace,
},
Spec: workloadsv1alpha1.CFAppSpec{
Name: "test-app2",
DesiredState: "STOPPED",
Lifecycle: workloadsv1alpha1.Lifecycle{
Type: "buildpack",
Data: workloadsv1alpha1.LifecycleData{
Buildpacks: []string{"java"},
Stack: "",
},
},
},
}
g.Expect(k8sClient.Create(beforeCtx, cfApp2)).To(Succeed())
})
it.After(func() {
afterCtx := context.Background()
g.Expect(k8sClient.Delete(afterCtx, cfApp1)).To(Succeed())
g.Expect(k8sClient.Delete(afterCtx, cfApp2)).To(Succeed())
})
it("can fetch the AppRecord CR we're looking for", func() {
appRepo := AppRepo{}
client, err := BuildClient(k8sConfig)
g.Expect(err).ToNot(HaveOccurred())
app, err := appRepo.FetchApp(testCtx, client, app2GUID)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(app.GUID).To(Equal(app2GUID))
g.Expect(app.Name).To(Equal("test-app2"))
g.Expect(app.SpaceGUID).To(Equal(namespace))
g.Expect(app.State).To(Equal(DesiredState("STOPPED")))
expectedLifecycle := Lifecycle{
Data: LifecycleData{
Buildpacks: []string{"java"},
Stack: "",
},
}
g.Expect(app.Lifecycle).To(Equal(expectedLifecycle))
})
})
when("duplicate Apps exist across namespaces with the same name", func() {
const otherNamespaceName = "other-namespace"
var (
testAppGUID string
cfApp1 *workloadsv1alpha1.CFApp
cfApp2 *workloadsv1alpha1.CFApp
)
it.Before(func() {
beforeCtx := context.Background()
testAppGUID = generateGUID()
g.Expect(k8sClient.Create(beforeCtx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: otherNamespaceName}})).To(Succeed())
cfApp1 = &workloadsv1alpha1.CFApp{
ObjectMeta: metav1.ObjectMeta{
Name: testAppGUID,
Namespace: namespace,
},
Spec: workloadsv1alpha1.CFAppSpec{
Name: "test-app1",
DesiredState: "STOPPED",
Lifecycle: workloadsv1alpha1.Lifecycle{
Type: "buildpack",
Data: workloadsv1alpha1.LifecycleData{
Buildpacks: []string{},
Stack: "",
},
},
},
}
g.Expect(k8sClient.Create(beforeCtx, cfApp1)).To(Succeed())
cfApp2 = &workloadsv1alpha1.CFApp{
ObjectMeta: metav1.ObjectMeta{
Name: testAppGUID,
Namespace: otherNamespaceName,
},
Spec: workloadsv1alpha1.CFAppSpec{
Name: "test-app2",
DesiredState: "STOPPED",
Lifecycle: workloadsv1alpha1.Lifecycle{
Type: "buildpack",
Data: workloadsv1alpha1.LifecycleData{
Buildpacks: []string{},
Stack: "",
},
},
},
}
g.Expect(k8sClient.Create(beforeCtx, cfApp2)).To(Succeed())
})
it.After(func() {
afterCtx := context.Background()
g.Expect(k8sClient.Delete(afterCtx, cfApp1)).To(Succeed())
g.Expect(k8sClient.Delete(afterCtx, cfApp2)).To(Succeed())
})
it("returns an error", func() {
appRepo := AppRepo{}
client, err := BuildClient(k8sConfig)
g.Expect(err).ToNot(HaveOccurred())
_, err = appRepo.FetchApp(testCtx, client, testAppGUID)
g.Expect(err).To(HaveOccurred())
g.Expect(err).To(MatchError("duplicate apps exist"))
})
})
when("no Apps exist", func() {
it("returns an error", func() {
appRepo := AppRepo{}
client, err := BuildClient(k8sConfig)
g.Expect(err).ToNot(HaveOccurred())
_, err = appRepo.FetchApp(testCtx, client, "i don't exist")
g.Expect(err).To(HaveOccurred())
g.Expect(err).To(MatchError("not found"))
})
})
when("there is some other error fetching apps", func() {
it.Before(func() {
})
it("returns status 500 InternalServerError", func() {
})
it("returns Content-Type as JSON in header", func() {
})
it("returns a CF API formatted Error response", func() {
})
})
}
func testFetchAppList(t *testing.T, when spec.G, it spec.S) {
g := NewWithT(t)
var testCtx context.Context
const namespace = "default"
it.Before(func() {
testCtx = context.Background()
})
when("multiple Apps exist", func() {
var (
app1GUID string
app2GUID string
cfApp1 *workloadsv1alpha1.CFApp
cfApp2 *workloadsv1alpha1.CFApp
)
it.Before(func() {
beforeCtx := context.Background()
app1GUID = generateGUID()
app2GUID = generateGUID()
cfApp1 = &workloadsv1alpha1.CFApp{
ObjectMeta: metav1.ObjectMeta{
Name: app1GUID,
Namespace: namespace,
},
Spec: workloadsv1alpha1.CFAppSpec{
Name: "test-app1",
DesiredState: "STOPPED",
Lifecycle: workloadsv1alpha1.Lifecycle{
Type: "buildpack",
Data: workloadsv1alpha1.LifecycleData{
Buildpacks: []string{},
Stack: "",
},
},
},
}
g.Expect(k8sClient.Create(beforeCtx, cfApp1)).To(Succeed())
cfApp2 = &workloadsv1alpha1.CFApp{
ObjectMeta: metav1.ObjectMeta{
Name: app2GUID,
Namespace: namespace,
},
Spec: workloadsv1alpha1.CFAppSpec{
Name: "test-app2",
DesiredState: "STOPPED",
Lifecycle: workloadsv1alpha1.Lifecycle{
Type: "buildpack",
Data: workloadsv1alpha1.LifecycleData{
Buildpacks: []string{"java"},
Stack: "",
},
},
},
}
g.Expect(k8sClient.Create(beforeCtx, cfApp2)).To(Succeed())
})
it.After(func() {
afterCtx := context.Background()
g.Expect(k8sClient.Delete(afterCtx, cfApp1)).To(Succeed())
g.Expect(k8sClient.Delete(afterCtx, cfApp2)).To(Succeed())
})
// TODO: Update this test annotation to reflect proper filtering by caller permissions when that is available
it("returns all the AppRecord CRs", func() {
appRepo := AppRepo{}
client, err := BuildClient(k8sConfig)
g.Expect(err).ToNot(HaveOccurred())
appList, err := appRepo.FetchAppList(testCtx, client)
g.Expect(err).NotTo(HaveOccurred())
// Expect response list to contain 2 particular apps, unordered
g.Expect(appList).To(HaveLen(2), "repository should return 2 app records")
// Assert on equality for each expected appRecord? Could just hardcode checks for each app?
})
})
when("no Apps exist", func() {
// This test setup may fail if we run tests in parallel that create app records
it("returns an error", func() {
appRepo := AppRepo{}
client, err := BuildClient(k8sConfig)
g.Expect(err).ToNot(HaveOccurred())
_, err = appRepo.FetchAppList(testCtx, client)
g.Expect(err).NotTo(HaveOccurred())
})
})
}
func initializeAppCR(appName string, appGUID string, spaceGUID string) workloadsv1alpha1.CFApp {
return workloadsv1alpha1.CFApp{
ObjectMeta: metav1.ObjectMeta{
Name: appGUID,
Namespace: spaceGUID,
},
Spec: workloadsv1alpha1.CFAppSpec{
Name: appName,
DesiredState: "STOPPED",
Lifecycle: workloadsv1alpha1.Lifecycle{
Type: "buildpack",
Data: workloadsv1alpha1.LifecycleData{
Buildpacks: []string{},
Stack: "",
},
},
},
}
}
func initializeAppRecord(appName string, appGUID string, spaceGUID string) AppRecord {
return AppRecord{
Name: appName,
GUID: appGUID,
SpaceGUID: spaceGUID,
State: "STOPPED",
Lifecycle: Lifecycle{
Type: "buildpack",
Data: LifecycleData{
Buildpacks: []string{},
Stack: "cflinuxfs3",
},
},
}
}
func generateGUID() string {
newUUID, err := uuid.NewUUID()
if err != nil {
errorMessage := fmt.Sprintf("could not generate a UUID %v", err)
panic(errorMessage)
}
return newUUID.String()
}
func cleanupApp(k8sClient client.Client, ctx context.Context, appGUID, appNamespace string) error {
app := workloadsv1alpha1.CFApp{
ObjectMeta: metav1.ObjectMeta{
Name: appGUID,
Namespace: appNamespace,
},
}
return k8sClient.Delete(ctx, &app)
}
func testCreateApp(t *testing.T, when spec.G, it spec.S) {
g := NewWithT(t)
const (
defaultNamespace = "default"
)
when("creating an App record and", func() {
const (
testAppName = "test-app-name"
)
var (
testAppGUID string
emptyAppRecord = AppRecord{}
testCtx context.Context
)
it.Before(func() {
testAppGUID = generateGUID()
testCtx = context.Background()
})
when("app does not already exist", func() {
var (
appRepo AppRepo
client client.Client
appRecord AppRecord
)
it.Before(func() {
appRepo = AppRepo{}
var err error
client, err = BuildClient(k8sConfig)
g.Expect(err).NotTo(HaveOccurred())
appRecord = initializeAppRecord(testAppName, testAppGUID, defaultNamespace)
})
it("should create a new app CR successfully", func() {
createdAppRecord, err := appRepo.CreateApp(testCtx, client, appRecord)
g.Expect(err).To(BeNil())
g.Expect(createdAppRecord).NotTo(BeNil())
cfAppLookupKey := types.NamespacedName{Name: testAppGUID, Namespace: defaultNamespace}
createdCFApp := new(workloadsv1alpha1.CFApp)
g.Eventually(func() string {
err := k8sClient.Get(context.Background(), cfAppLookupKey, createdCFApp)
if err != nil {
return ""
}
return createdCFApp.Name
}, 10*time.Second, 250*time.Millisecond).Should(Equal(testAppGUID))
g.Expect(cleanupApp(k8sClient, testCtx, testAppGUID, defaultNamespace)).To(Succeed())
})
when("an app is created with the repository", func() {
var (
beforeCreationTime time.Time
createdAppRecord AppRecord
)
it.Before(func() {
beforeCtx := context.Background()
beforeCreationTime = time.Now().UTC().AddDate(0, 0, -1)
var err error
createdAppRecord, err = appRepo.CreateApp(beforeCtx, client, appRecord)
g.Expect(err).To(BeNil())
})
it.After(func() {
afterCtx := context.Background()
g.Expect(cleanupApp(k8sClient, afterCtx, testAppGUID, defaultNamespace)).To(Succeed())
})
it("should return a non-empty AppRecord", func() {
g.Expect(createdAppRecord).NotTo(Equal(emptyAppRecord))
})
it("should return an AppRecord with matching GUID, spaceGUID, and name", func() {
g.Expect(createdAppRecord.GUID).To(Equal(testAppGUID), "App GUID in record did not match input")
g.Expect(createdAppRecord.SpaceGUID).To(Equal(defaultNamespace), "App SpaceGUID in record did not match input")
g.Expect(createdAppRecord.Name).To(Equal(testAppName), "App Name in record did not match input")
})
it("should return an AppRecord with CreatedAt and UpdatedAt fields that make sense", func() {
afterTestTime := time.Now().UTC().AddDate(0, 0, 1)
recordCreatedTime, err := time.Parse(TimestampFormat, createdAppRecord.CreatedAt)
g.Expect(err).To(BeNil(), "There was an error converting the createAppRecord CreatedTime to string")
recordUpdatedTime, err := time.Parse(TimestampFormat, createdAppRecord.UpdatedAt)
g.Expect(err).To(BeNil(), "There was an error converting the createAppRecord UpdatedTime to string")
g.Expect(recordCreatedTime.After(beforeCreationTime)).To(BeTrue(), "app record creation time was not after the expected creation time")
g.Expect(recordCreatedTime.Before(afterTestTime)).To(BeTrue(), "app record creation time was not before the expected testing time")
g.Expect(recordUpdatedTime.After(beforeCreationTime)).To(BeTrue(), "app record updated time was not after the expected creation time")
g.Expect(recordUpdatedTime.Before(afterTestTime)).To(BeTrue(), "app record updated time was not before the expected testing time")
})
})
})
when("the app already exists", func() {
var (
appCR workloadsv1alpha1.CFApp
appRepo AppRepo
client client.Client
)
it.Before(func() {
beforeCtx := context.Background()
appCR = initializeAppCR(testAppName, testAppGUID, defaultNamespace)
g.Expect(k8sClient.Create(beforeCtx, &appCR)).To(Succeed())
appRepo = AppRepo{}
client, _ = BuildClient(k8sConfig)
})
it.After(func() {
afterCtx := context.Background()
g.Expect(k8sClient.Delete(afterCtx, &appCR)).To(Succeed())
})
it("should error when trying to create the same app again", func() {
appRecord := initializeAppRecord(testAppName, testAppGUID, defaultNamespace)
createdAppRecord, err := appRepo.CreateApp(testCtx, client, appRecord)
g.Expect(err).NotTo(BeNil())
g.Expect(createdAppRecord).To(Equal(emptyAppRecord))
})
})
})
}
func generateAppEnvSecretName(appGUID string) string {
return appGUID + "-env"
}
func testEnvSecretCreate(t *testing.T, when spec.G, it spec.S) {
g := NewWithT(t)
const (
defaultNamespace = "default"
)
when("an envSecret is created for a CFApp with the Repo", func() {
const (
testAppName = "test-app-name"
)
var (
appRepo AppRepo
client client.Client
testAppGUID string
testAppEnvSecretName string
testAppEnvSecret AppEnvVarsRecord
returnedAppEnvVarsRecord AppEnvVarsRecord
returnedErr error
)
it.Before(func() {
beforeCtx := context.Background()
appRepo = AppRepo{}
client, _ = BuildClient(k8sConfig)
testAppGUID = generateGUID()
testAppEnvSecretName = generateAppEnvSecretName(testAppGUID)
testAppEnvSecret = AppEnvVarsRecord{
AppGUID: testAppGUID,
SpaceGUID: defaultNamespace,
EnvironmentVariables: map[string]string{"foo": "foo", "bar": "bar"},
}
returnedAppEnvVarsRecord, returnedErr = appRepo.CreateAppEnvironmentVariables(beforeCtx, client, testAppEnvSecret)
})
it.After(func() {
afterCtx := context.Background()
lookupSecretK8sResource := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: testAppEnvSecretName,
Namespace: defaultNamespace,
},
}
g.Expect(client.Delete(afterCtx, &lookupSecretK8sResource)).To(Succeed(), "Could not clean up the created App Env Secret")
})
// this test appears flakey
it("returns a record matching the input and no error", func() {
g.Expect(returnedAppEnvVarsRecord.AppGUID).To(Equal(testAppEnvSecret.AppGUID))
g.Expect(returnedAppEnvVarsRecord.SpaceGUID).To(Equal(testAppEnvSecret.SpaceGUID))
g.Expect(len(returnedAppEnvVarsRecord.EnvironmentVariables)).To(Equal(len(testAppEnvSecret.EnvironmentVariables)))
g.Expect(returnedErr).To(BeNil())
})
it("returns a record with the created Secret's name", func() {
g.Expect(returnedAppEnvVarsRecord.Name).ToNot(BeEmpty())
})
when("examining the created Secret in the k8s api", func() {
var (
createdCFAppSecret corev1.Secret
)
it.Before(func() {
beforeCtx := context.Background()
cfAppSecretLookupKey := types.NamespacedName{Name: testAppEnvSecretName, Namespace: defaultNamespace}
createdCFAppSecret = corev1.Secret{}
g.Eventually(func() bool {
err := client.Get(beforeCtx, cfAppSecretLookupKey, &createdCFAppSecret)
if err != nil {
return false
}
return true
}, 10*time.Second, 250*time.Millisecond).Should(BeTrue(), "could not find the secret created by the repo")
})
it("is not empty", func() {
g.Expect(createdCFAppSecret).ToNot(Equal(corev1.Secret{}))
})
it("has a Name that is derived from the CFApp", func() {
g.Expect(createdCFAppSecret.Name).To(Equal(testAppEnvSecretName))
})
it("has a label that matches the CFApp GUID", func() {
labelValue, exists := createdCFAppSecret.Labels[CFAppGUIDLabel]
g.Expect(exists).To(BeTrue(), "label for envSecret AppGUID not found")
g.Expect(labelValue).To(Equal(testAppGUID))
})
it("contains string data that matches the input record length", func() {
g.Expect(len(createdCFAppSecret.Data)).To(Equal(len(testAppEnvSecret.EnvironmentVariables)))
})
})
it("returns an error if the secret already exists", func() {
testCtx := context.Background()
testAppEnvSecret.EnvironmentVariables = map[string]string{"foo": "foo", "bar": "bar"}
returnedUpdatedAppEnvVarsRecord, returnedUpdatedErr := appRepo.CreateAppEnvironmentVariables(testCtx, client, testAppEnvSecret)
g.Expect(returnedUpdatedErr).ToNot(BeNil())
g.Expect(returnedUpdatedAppEnvVarsRecord).To(Equal(AppEnvVarsRecord{}))
})
})
}