1
1
package templaterouter
2
2
3
3
import (
4
+ "fmt"
4
5
"reflect"
5
6
"testing"
6
7
"time"
7
8
8
9
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10
+ "k8s.io/apimachinery/pkg/types"
9
11
"k8s.io/apimachinery/pkg/util/sets"
10
12
"k8s.io/apimachinery/pkg/watch"
11
13
kapi "k8s.io/kubernetes/pkg/apis/core"
@@ -484,14 +486,20 @@ func TestHandleRoute(t *testing.T) {
484
486
// here
485
487
plugin := controller .NewUniqueHost (templatePlugin , controller .HostForRoute , false , rejections )
486
488
489
+ uidCount := 0
490
+ nextUID := func () types.UID {
491
+ uidCount ++
492
+ return types .UID (fmt .Sprintf ("%03d" , uidCount ))
493
+ }
487
494
original := metav1.Time {Time : time .Now ()}
488
495
489
496
//add
490
- route := & routeapi.Route {
497
+ fooTest1 := & routeapi.Route {
491
498
ObjectMeta : metav1.ObjectMeta {
492
499
CreationTimestamp : original ,
493
500
Namespace : "foo" ,
494
501
Name : "test" ,
502
+ UID : nextUID (),
495
503
},
496
504
Spec : routeapi.RouteSpec {
497
505
Host : "www.example.com" ,
@@ -501,22 +509,22 @@ func TestHandleRoute(t *testing.T) {
501
509
},
502
510
},
503
511
}
504
- serviceUnitKey := endpointsKeyFromParts (route .Namespace , route .Spec .To .Name )
512
+ serviceUnitKey := endpointsKeyFromParts (fooTest1 .Namespace , fooTest1 .Spec .To .Name )
505
513
506
- plugin .HandleRoute (watch .Added , route )
514
+ plugin .HandleRoute (watch .Added , fooTest1 )
507
515
508
516
_ , ok := router .FindServiceUnit (serviceUnitKey )
509
517
510
518
if ! ok {
511
- t .Errorf ("TestHandleRoute was unable to find the service unit %s after HandleRoute was called" , route .Spec .To .Name )
519
+ t .Errorf ("TestHandleRoute was unable to find the service unit %s after HandleRoute was called" , fooTest1 .Spec .To .Name )
512
520
} else {
513
- serviceAliasCfg , ok := router .State [getKey (route )]
521
+ serviceAliasCfg , ok := router .State [getKey (fooTest1 )]
514
522
515
523
if ! ok {
516
- t .Errorf ("TestHandleRoute expected route key %s" , getKey (route ))
524
+ t .Errorf ("TestHandleRoute expected route key %s" , getKey (fooTest1 ))
517
525
} else {
518
- if serviceAliasCfg .Host != route .Spec .Host || serviceAliasCfg .Path != route .Spec .Path {
519
- t .Errorf ("Expected route did not match service alias config %v : %v" , route , serviceAliasCfg )
526
+ if serviceAliasCfg .Host != fooTest1 .Spec .Host || serviceAliasCfg .Path != fooTest1 .Spec .Path {
527
+ t .Errorf ("Expected route did not match service alias config %v : %v" , fooTest1 , serviceAliasCfg )
520
528
}
521
529
}
522
530
}
@@ -526,11 +534,12 @@ func TestHandleRoute(t *testing.T) {
526
534
}
527
535
528
536
// attempt to add a second route with a newer time, verify it is ignored
529
- duplicateRoute := & routeapi.Route {
537
+ fooDupe2 := & routeapi.Route {
530
538
ObjectMeta : metav1.ObjectMeta {
531
539
CreationTimestamp : metav1.Time {Time : original .Add (time .Hour )},
532
540
Namespace : "foo" ,
533
541
Name : "dupe" ,
542
+ UID : nextUID (),
534
543
},
535
544
Spec : routeapi.RouteSpec {
536
545
Host : "www.example.com" ,
@@ -540,9 +549,10 @@ func TestHandleRoute(t *testing.T) {
540
549
},
541
550
},
542
551
}
543
- if err := plugin .HandleRoute (watch .Added , duplicateRoute ); err == nil {
552
+ if err := plugin .HandleRoute (watch .Added , fooDupe2 ); err == nil {
544
553
t .Fatal ("unexpected non-error" )
545
554
}
555
+
546
556
if _ , ok := router .FindServiceUnit (endpointsKeyFromParts ("foo" , "TestService2" )); ok {
547
557
t .Fatalf ("unexpected second unit: %#v" , router )
548
558
}
@@ -558,9 +568,10 @@ func TestHandleRoute(t *testing.T) {
558
568
rejections .rejections = nil
559
569
560
570
// attempt to remove the second route that is not being used, verify it is ignored
561
- if err := plugin .HandleRoute (watch .Deleted , duplicateRoute ); err = = nil {
562
- t .Fatal ("unexpected non- error" )
571
+ if err := plugin .HandleRoute (watch .Deleted , fooDupe2 ); err ! = nil {
572
+ t .Fatal ("unexpected error" )
563
573
}
574
+
564
575
if _ , ok := router .FindServiceUnit (endpointsKeyFromParts ("foo" , "TestService2" )); ok {
565
576
t .Fatalf ("unexpected second unit: %#v" , router )
566
577
}
@@ -570,17 +581,17 @@ func TestHandleRoute(t *testing.T) {
570
581
if r , ok := plugin .RoutesForHost ("www.example.com" ); ! ok || r [0 ].Name != "test" {
571
582
t .Fatalf ("unexpected claimed routes: %#v" , r )
572
583
}
573
- if len (rejections .rejections ) != 1 ||
574
- rejections .rejections [0 ].route .Name != "dupe" ||
575
- rejections .rejections [0 ].reason != "HostAlreadyClaimed" ||
576
- rejections .rejections [0 ].message != "route test already exposes www.example.com and is older" {
584
+ if len (rejections .rejections ) != 0 {
577
585
t .Fatalf ("did not record rejection: %#v" , rejections )
578
586
}
579
587
rejections .rejections = nil
580
588
581
- // add a second route with an older time, verify it takes effect
582
- duplicateRoute .CreationTimestamp = metav1.Time {Time : original .Add (- time .Hour )}
583
- if err := plugin .HandleRoute (watch .Added , duplicateRoute ); err != nil {
589
+ // add a third route with an older time, verify it takes effect
590
+ copied := * fooDupe2
591
+ fooDupe3 := & copied
592
+ fooDupe3 .UID = nextUID ()
593
+ fooDupe3 .CreationTimestamp = metav1.Time {Time : original .Add (- time .Hour )}
594
+ if err := plugin .HandleRoute (watch .Added , fooDupe3 ); err != nil {
584
595
t .Fatal ("unexpected error" )
585
596
}
586
597
_ , ok = router .FindServiceUnit (endpointsKeyFromParts ("foo" , "TestService2" ))
@@ -596,24 +607,36 @@ func TestHandleRoute(t *testing.T) {
596
607
rejections .rejections = nil
597
608
598
609
//mod
599
- route .Spec .Host = "www.example2.com"
600
- if err := plugin .HandleRoute (watch .Modified , route ); err != nil {
610
+ copied2 := * fooTest1
611
+ fooTest1NewHost := & copied2
612
+ fooTest1NewHost .Spec .Host = "www.example2.com"
613
+ if err := plugin .HandleRoute (watch .Modified , fooTest1NewHost ); err != nil {
601
614
t .Fatal ("unexpected error" )
602
615
}
616
+
617
+ key := getKey (fooTest1NewHost )
603
618
_ , ok = router .FindServiceUnit (serviceUnitKey )
604
619
if ! ok {
605
- t .Errorf ("TestHandleRoute was unable to find the service unit %s after HandleRoute was called" , route .Spec .To .Name )
620
+ t .Errorf ("TestHandleRoute was unable to find the service unit %s after HandleRoute was called" , fooTest1NewHost .Spec .To .Name )
606
621
} else {
607
- serviceAliasCfg , ok := router .State [getKey (route )]
608
-
622
+ serviceAliasCfg , ok := router .State [key ]
609
623
if ! ok {
610
- t .Errorf ("TestHandleRoute expected route key %s" , getKey ( route ) )
624
+ t .Errorf ("TestHandleRoute expected route key %s" , key )
611
625
} else {
612
- if serviceAliasCfg .Host != route .Spec .Host || serviceAliasCfg .Path != route .Spec .Path {
613
- t .Errorf ("Expected route did not match service alias config %v : %v" , route , serviceAliasCfg )
626
+ if serviceAliasCfg .Host != fooTest1NewHost .Spec .Host || serviceAliasCfg .Path != fooTest1NewHost .Spec .Path {
627
+ t .Errorf ("Expected route did not match service alias config %v : %v" , fooTest1NewHost , serviceAliasCfg )
614
628
}
615
629
}
616
630
}
631
+ if plugin .HostLen () != 2 {
632
+ t .Fatalf ("did not clear claimed route: %#v" , plugin )
633
+ }
634
+ if len (rejections .rejections ) != 0 {
635
+ t .Fatalf ("unexpected rejection: %#v" , rejections )
636
+ }
637
+
638
+ plugin .HandleRoute (watch .Deleted , fooDupe3 )
639
+
617
640
if plugin .HostLen () != 1 {
618
641
t .Fatalf ("did not clear claimed route: %#v" , plugin )
619
642
}
@@ -622,17 +645,18 @@ func TestHandleRoute(t *testing.T) {
622
645
}
623
646
624
647
//delete
625
- if err := plugin .HandleRoute (watch .Deleted , route ); err != nil {
648
+ if err := plugin .HandleRoute (watch .Deleted , fooTest1NewHost ); err != nil {
626
649
t .Fatal ("unexpected error" )
627
650
}
628
651
_ , ok = router .FindServiceUnit (serviceUnitKey )
629
652
if ! ok {
630
- t .Errorf ("TestHandleRoute was unable to find the service unit %s after HandleRoute was called" , route .Spec .To .Name )
653
+ t .Errorf ("TestHandleRoute was unable to find the service unit %s after HandleRoute was called" , fooTest1NewHost .Spec .To .Name )
631
654
} else {
632
- _ , ok := router .State [getKey (route )]
655
+
656
+ _ , ok := router .State [key ]
633
657
634
658
if ok {
635
- t .Errorf ("TestHandleRoute did not expect route key %s" , getKey ( route ) )
659
+ t .Errorf ("TestHandleRoute did not expect route key %s" , key )
636
660
}
637
661
}
638
662
if plugin .HostLen () != 0 {
@@ -643,15 +667,37 @@ func TestHandleRoute(t *testing.T) {
643
667
}
644
668
}
645
669
670
+ type fakePlugin struct {
671
+ Route * routeapi.Route
672
+ Err error
673
+ }
674
+
675
+ func (p * fakePlugin ) HandleRoute (event watch.EventType , route * routeapi.Route ) error {
676
+ p .Route = route
677
+ return p .Err
678
+ }
679
+
680
+ func (p * fakePlugin ) HandleEndpoints (watch.EventType , * kapi.Endpoints ) error {
681
+ return p .Err
682
+ }
683
+
684
+ func (p * fakePlugin ) HandleNamespaces (namespaces sets.String ) error {
685
+ return p .Err
686
+ }
687
+
688
+ func (p * fakePlugin ) HandleNode (watch.EventType , * kapi.Node ) error {
689
+ return p .Err
690
+ }
691
+
692
+ func (p * fakePlugin ) Commit () error {
693
+ return p .Err
694
+ }
695
+
646
696
// TestHandleRouteExtendedValidation test route watch events with extended route configuration validation.
647
697
func TestHandleRouteExtendedValidation (t * testing.T ) {
648
698
rejections := & fakeRejections {}
649
- router := newTestRouter (make (map [string ]ServiceAliasConfig ))
650
- templatePlugin := newDefaultTemplatePlugin (router , true , nil )
651
- // TODO: move tests that rely on unique hosts to pkg/router/controller and remove them from
652
- // here
653
- extendedValidatorPlugin := controller .NewExtendedValidator (templatePlugin , rejections )
654
- plugin := controller .NewUniqueHost (extendedValidatorPlugin , controller .HostForRoute , false , rejections )
699
+ fake := & fakePlugin {}
700
+ plugin := controller .NewExtendedValidator (fake , rejections )
655
701
656
702
original := metav1.Time {Time : time .Now ()}
657
703
@@ -670,24 +716,10 @@ func TestHandleRouteExtendedValidation(t *testing.T) {
670
716
},
671
717
},
672
718
}
673
- serviceUnitKey := endpointsKeyFromParts (route .Namespace , route .Spec .To .Name )
674
719
675
720
plugin .HandleRoute (watch .Added , route )
676
-
677
- _ , ok := router .FindServiceUnit (serviceUnitKey )
678
-
679
- if ! ok {
680
- t .Errorf ("TestHandleRoute was unable to find the service unit %s after HandleRoute was called" , route .Spec .To .Name )
681
- } else {
682
- serviceAliasCfg , ok := router .State [getKey (route )]
683
-
684
- if ! ok {
685
- t .Errorf ("TestHandleRoute expected route key %s" , getKey (route ))
686
- } else {
687
- if serviceAliasCfg .Host != route .Spec .Host || serviceAliasCfg .Path != route .Spec .Path {
688
- t .Errorf ("Expected route did not match service alias config %v : %v" , route , serviceAliasCfg )
689
- }
690
- }
721
+ if fake .Route != route {
722
+ t .Fatalf ("unexpected route: %#v" , fake .Route )
691
723
}
692
724
693
725
if len (rejections .rejections ) > 0 {
@@ -960,7 +992,7 @@ func TestHandleRouteExtendedValidation(t *testing.T) {
960
992
},
961
993
},
962
994
},
963
- errorExpected : false ,
995
+ errorExpected : true ,
964
996
},
965
997
{
966
998
name : "Double escaped newlines" ,
@@ -981,16 +1013,18 @@ func TestHandleRouteExtendedValidation(t *testing.T) {
981
1013
}
982
1014
983
1015
for _ , tc := range tests {
984
- err := plugin .HandleRoute (watch .Added , tc .route )
985
- if tc .errorExpected {
986
- if err == nil {
987
- t .Fatalf ("test case %s: expected an error, got none" , tc .name )
988
- }
989
- } else {
990
- if err != nil {
991
- t .Fatalf ("test case %s: expected no errors, got %v" , tc .name , err )
1016
+ t .Run (tc .name , func (t * testing.T ) {
1017
+ err := plugin .HandleRoute (watch .Added , tc .route )
1018
+ if tc .errorExpected {
1019
+ if err == nil {
1020
+ t .Fatalf ("test case %s: expected an error, got none" , tc .name )
1021
+ }
1022
+ } else {
1023
+ if err != nil {
1024
+ t .Fatalf ("test case %s: expected no errors, got %v" , tc .name , err )
1025
+ }
992
1026
}
993
- }
1027
+ })
994
1028
}
995
1029
}
996
1030
0 commit comments