41
41
events * mockEventWriter
42
42
)
43
43
44
- func daemon (t * testing.T ) (* Daemon , func ()) {
45
- repo , repoCleanup := gittest .Repo (t )
44
+ func daemon (t * testing.T , files map [ string ] string ) (* Daemon , func ()) {
45
+ repo , repoCleanup := gittest .Repo (t , files )
46
46
47
47
k8s = & mock.Mock {}
48
48
k8s .ExportFunc = func (ctx context.Context ) ([]byte , error ) { return nil , nil }
@@ -132,7 +132,7 @@ func checkSyncManifestsMetrics(t *testing.T, manifestSuccess, manifestFailures i
132
132
}
133
133
134
134
func TestPullAndSync_InitialSync (t * testing.T ) {
135
- d , cleanup := daemon (t )
135
+ d , cleanup := daemon (t , testfiles . Files )
136
136
defer cleanup ()
137
137
138
138
syncCalled := 0
@@ -199,7 +199,7 @@ func TestPullAndSync_InitialSync(t *testing.T) {
199
199
}
200
200
201
201
func TestDoSync_NoNewCommits (t * testing.T ) {
202
- d , cleanup := daemon (t )
202
+ d , cleanup := daemon (t , testfiles . Files )
203
203
defer cleanup ()
204
204
205
205
var syncTag = "syncity"
@@ -279,7 +279,7 @@ func TestDoSync_NoNewCommits(t *testing.T) {
279
279
}
280
280
281
281
func TestDoSync_WithNewCommit (t * testing.T ) {
282
- d , cleanup := daemon (t )
282
+ d , cleanup := daemon (t , testfiles . Files )
283
283
defer cleanup ()
284
284
285
285
ctx := context .Background ()
@@ -376,7 +376,7 @@ func TestDoSync_WithNewCommit(t *testing.T) {
376
376
t .Errorf ("Sync was called with a nil syncDef" )
377
377
}
378
378
379
- // The emitted event has no workload ids
379
+ // An event is emitted and it only has a changed workload id
380
380
es , err := events .AllEvents (time.Time {}, - 1 , time.Time {})
381
381
if err != nil {
382
382
t .Error (err )
@@ -387,7 +387,6 @@ func TestDoSync_WithNewCommit(t *testing.T) {
387
387
} else {
388
388
gotResourceIDs := es [0 ].ServiceIDs
389
389
resource .IDs (gotResourceIDs ).Sort ()
390
- // Event should only have changed workload ids
391
390
if ! reflect .DeepEqual (gotResourceIDs , []resource.ID {resource .MustParseID ("default:deployment/helloworld" )}) {
392
391
t .Errorf ("Unexpected event workload ids: %#v, expected: %#v" , gotResourceIDs , []resource.ID {resource .MustParseID ("default:deployment/helloworld" )})
393
392
}
@@ -406,8 +405,125 @@ func TestDoSync_WithNewCommit(t *testing.T) {
406
405
}
407
406
}
408
407
408
+ func TestDoSync_WithKustomize (t * testing.T ) {
409
+ d , cleanup := daemon (t , testfiles .FilesForKustomize )
410
+ defer cleanup ()
411
+
412
+ d .GitConfig .Paths = []string {"staging" }
413
+ d .ManifestGenerationEnabled = true
414
+
415
+ ctx := context .Background ()
416
+
417
+ var syncTag = "syncy-mcsyncface"
418
+ // Set the sync tag to head
419
+ var oldRevision , newRevision string
420
+ err := d .WithWorkingClone (ctx , func (checkout * git.Checkout ) error {
421
+ ctx , cancel := context .WithTimeout (ctx , 5 * time .Second )
422
+ defer cancel ()
423
+
424
+ var err error
425
+ tagAction := git.TagAction {
426
+ Tag : syncTag ,
427
+ Revision : "master" ,
428
+ Message : "Sync pointer" ,
429
+ }
430
+ err = checkout .MoveTagAndPush (ctx , tagAction )
431
+ if err != nil {
432
+ return err
433
+ }
434
+ oldRevision , err = checkout .HeadRevision (ctx )
435
+ if err != nil {
436
+ return err
437
+ }
438
+
439
+ // Push some new changes
440
+ absolutePath := path .Join (checkout .Dir (), "base" , "foo.yaml" )
441
+ def , err := ioutil .ReadFile (absolutePath )
442
+ if err != nil {
443
+ return err
444
+ }
445
+
446
+ newDef := bytes .Replace (def , []byte ("key: value" ), []byte ("key: value2" ), - 1 )
447
+ if err := ioutil .WriteFile (absolutePath , newDef , 0600 ); err != nil {
448
+ return err
449
+ }
450
+
451
+ commitAction := git.CommitAction {Author : "" , Message : "test commit" }
452
+ err = checkout .CommitAndPush (ctx , commitAction , nil , true )
453
+ if err != nil {
454
+ return err
455
+ }
456
+ newRevision , err = checkout .HeadRevision (ctx )
457
+ return err
458
+ })
459
+ if err != nil {
460
+ t .Fatal (err )
461
+ }
462
+
463
+ err = d .Repo .Refresh (ctx )
464
+ if err != nil {
465
+ t .Error (err )
466
+ }
467
+
468
+ syncCalled := 0
469
+ var syncDef * cluster.SyncSet
470
+ k8s .SyncFunc = func (def cluster.SyncSet ) error {
471
+ syncCalled ++
472
+ syncDef = & def
473
+ return nil
474
+ }
475
+
476
+ head , err := d .Repo .BranchHead (ctx )
477
+ if err != nil {
478
+ t .Fatal (err )
479
+ }
480
+
481
+ gitSync , _ := fluxsync .NewGitTagSyncProvider (d .Repo , syncTag , "" , fluxsync .VerifySignaturesModeNone , d .GitConfig )
482
+ syncState := & lastKnownSyncState {logger : d .Logger , state : gitSync }
483
+
484
+ if err := d .Sync (ctx , time .Now ().UTC (), head , syncState ); err != nil {
485
+ t .Error (err )
486
+ }
487
+
488
+ // It applies everything
489
+ if syncCalled != 1 {
490
+ t .Errorf ("Sync was not called once, was called %d times" , syncCalled )
491
+ } else if syncDef == nil {
492
+ t .Errorf ("Sync was called with a nil syncDef" )
493
+ }
494
+
495
+ // An event is emitted and it only has a changed workload id
496
+ es , err := events .AllEvents (time.Time {}, - 1 , time.Time {})
497
+ if err != nil {
498
+ t .Error (err )
499
+ } else if len (es ) != 1 {
500
+ t .Errorf ("Unexpected events: %#v" , es )
501
+ } else if es [0 ].Type != event .EventSync {
502
+ t .Errorf ("Unexpected event type: %#v" , es [0 ])
503
+ } else {
504
+ gotResourceIDs := es [0 ].ServiceIDs
505
+ resource .IDs (gotResourceIDs ).Sort ()
506
+ if ! reflect .DeepEqual (gotResourceIDs , []resource.ID {resource .MustParseID ("default:namespace/foo" )}) {
507
+ t .Errorf ("Unexpected event workload ids: %#v, expected: %#v" , gotResourceIDs , []resource.ID {resource .MustParseID ("default:namespace/foo" )})
508
+ }
509
+ }
510
+
511
+ // It moves the tag
512
+ ctx , cancel := context .WithTimeout (ctx , 5 * time .Second )
513
+ defer cancel ()
514
+ if err := d .Repo .Refresh (ctx ); err != nil {
515
+ t .Errorf ("pulling sync tag: %v" , err )
516
+ } else if revs , err := d .Repo .CommitsBetween (ctx , oldRevision , syncTag , false ); err != nil {
517
+ t .Errorf ("finding revisions before sync tag: %v" , err )
518
+ } else if len (revs ) <= 0 {
519
+ t .Errorf ("Should have moved sync tag forward" )
520
+ } else if revs [len (revs )- 1 ].Revision != newRevision {
521
+ t .Errorf ("Should have moved sync tag to HEAD (%s), but was moved to: %s" , newRevision , revs [len (revs )- 1 ].Revision )
522
+ }
523
+ }
524
+
409
525
func TestDoSync_WithErrors (t * testing.T ) {
410
- d , cleanup := daemon (t )
526
+ d , cleanup := daemon (t , testfiles . Files )
411
527
defer cleanup ()
412
528
413
529
expectedResourceIDs := resource.IDs {}
0 commit comments