forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_test.go
1147 lines (1063 loc) · 36 KB
/
plugin_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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package templaterouter
import (
"fmt"
"reflect"
"strings"
"testing"
"time"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/util/sets"
"k8s.io/kubernetes/pkg/watch"
routeapi "github.com/openshift/origin/pkg/route/api"
"github.com/openshift/origin/pkg/router/controller"
)
const (
testExpiredCAUnknownCertificate = `-----BEGIN CERTIFICATE-----
MIIDIjCCAgqgAwIBAgIBBjANBgkqhkiG9w0BAQUFADCBoTELMAkGA1UEBhMCVVMx
CzAJBgNVBAgMAlNDMRUwEwYDVQQHDAxEZWZhdWx0IENpdHkxHDAaBgNVBAoME0Rl
ZmF1bHQgQ29tcGFueSBMdGQxEDAOBgNVBAsMB1Rlc3QgQ0ExGjAYBgNVBAMMEXd3
dy5leGFtcGxlY2EuY29tMSIwIAYJKoZIhvcNAQkBFhNleGFtcGxlQGV4YW1wbGUu
Y29tMB4XDTE2MDExMzE5NDA1N1oXDTI2MDExMDE5NDA1N1owfDEYMBYGA1UEAxMP
d3d3LmV4YW1wbGUuY29tMQswCQYDVQQIEwJTQzELMAkGA1UEBhMCVVMxIjAgBgkq
hkiG9w0BCQEWE2V4YW1wbGVAZXhhbXBsZS5jb20xEDAOBgNVBAoTB0V4YW1wbGUx
EDAOBgNVBAsTB0V4YW1wbGUwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAM0B
u++oHV1wcphWRbMLUft8fD7nPG95xs7UeLPphFZuShIhhdAQMpvcsFeg+Bg9PWCu
v3jZljmk06MLvuWLfwjYfo9q/V+qOZVfTVHHbaIO5RTXJMC2Nn+ACF0kHBmNcbth
OOgF8L854a/P8tjm1iPR++vHnkex0NH7lyosVc/vAgMBAAGjDTALMAkGA1UdEwQC
MAAwDQYJKoZIhvcNAQEFBQADggEBADjFm5AlNH3DNT1Uzx3m66fFjqqrHEs25geT
yA3r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EB
AQUAA4GNADCBiQKBgQCsdVIJ6GSrkFdE9LzsMItYGE4q3qqSqIbs/uwMoVsMT+33
pLeyzeecPuoQsdO6SEuqhUM1ivUN4GyXIR1+aW2baMwMXpjX9VIJu5d4FqtGi6SD
RfV+tbERWwifPJlN+ryuvqbbDxrjQeXhemeo7yrJdgJ1oyDmoM5pTiSUUmltvQID
AQABo1AwTjAdBgNVHQ4EFgQUOVuieqGfp2wnKo7lX2fQt+Yk1C4wHwYDVR0jBBgw
FoAUOVuieqGfp2wnKo7lX2fQt+Yk1C4wDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0B
AQsFAAOBgQA8VhmNeicRnKgXInVyYZDjL0P4WRbKJY7DkJxRMRWxikbEVHdySki6
jegpqgJqYbzU6EiuTS2sl2bAjIK9nGUtTDt1PJIC1Evn5Q6v5ylNflpv6GxtUbCt
bGvtpjWA4r9WASIDPFsxk/cDEEEO6iPxgMOf5MdpQC2y2MU0rzF/Gg==
-----END CERTIFICATE-----`
testDestinationCACertificate = testCACertificate
)
// TestRouter provides an implementation of the plugin's router interface suitable for unit testing.
type TestRouter struct {
State map[string]ServiceAliasConfig
ServiceUnits map[string]ServiceUnit
Committed bool
}
// NewTestRouter creates a new TestRouter and registers the initial state.
func newTestRouter(state map[string]ServiceAliasConfig) *TestRouter {
return &TestRouter{
State: state,
ServiceUnits: make(map[string]ServiceUnit),
Committed: false,
}
}
// CreateServiceUnit creates an empty service unit identified by id
func (r *TestRouter) CreateServiceUnit(id string) {
su := ServiceUnit{
Name: id,
EndpointTable: []Endpoint{},
}
r.ServiceUnits[id] = su
}
// FindServiceUnit finds the service unit in the state
func (r *TestRouter) FindServiceUnit(id string) (v ServiceUnit, ok bool) {
v, ok = r.ServiceUnits[id]
return
}
// AddEndpoints adds the endpoints to the service unit identified by id
func (r *TestRouter) AddEndpoints(id string, endpoints []Endpoint) bool {
r.Committed = false //expect any call to this method to subsequently call commit
su, _ := r.FindServiceUnit(id)
// simulate the logic that compares endpoints
if reflect.DeepEqual(su.EndpointTable, endpoints) {
return false
}
su.EndpointTable = endpoints
r.ServiceUnits[id] = su
return true
}
// DeleteEndpoints removes all endpoints from the service unit
func (r *TestRouter) DeleteEndpoints(id string) {
r.Committed = false //expect any call to this method to subsequently call commit
if su, ok := r.FindServiceUnit(id); !ok {
return
} else {
su.EndpointTable = []Endpoint{}
r.ServiceUnits[id] = su
}
}
// AddRoute adds a ServiceAliasConfig for the route with the ServiceUnit identified by id
func (r *TestRouter) AddRoute(id string, weight int32, route *routeapi.Route, host string) bool {
r.Committed = false //expect any call to this method to subsequently call commit
su, _ := r.FindServiceUnit(id)
routeKey := r.routeKey(route)
config := ServiceAliasConfig{
Host: host,
Path: route.Spec.Path,
ServiceUnitNames: make(map[string]int32),
}
config.ServiceUnitNames[su.Name] = weight
r.State[routeKey] = config
return true
}
// RemoveRoute removes the service alias config for Route
func (r *TestRouter) RemoveRoute(route *routeapi.Route) {
r.Committed = false //expect any call to this method to subsequently call commit
routeKey := r.routeKey(route)
_, ok := r.State[routeKey]
if !ok {
return
} else {
delete(r.State, routeKey)
}
}
func (r *TestRouter) FilterNamespaces(namespaces sets.String) {
if len(namespaces) == 0 {
r.State = make(map[string]ServiceAliasConfig)
r.ServiceUnits = make(map[string]ServiceUnit)
}
for k := range r.ServiceUnits {
// TODO: the id of a service unit should be defined inside this class, not passed in from the outside
// remove the leak of the abstraction when we refactor this code
ns := strings.SplitN(k, "/", 2)[0]
if namespaces.Has(ns) {
continue
}
delete(r.ServiceUnits, k)
}
for k := range r.State {
ns := strings.SplitN(k, "-", 2)[0]
if namespaces.Has(ns) {
continue
}
delete(r.State, k)
}
}
// routeKey create an identifier for the route consisting of host-path
func (r *TestRouter) routeKey(route *routeapi.Route) string {
return route.Spec.Host + "-" + route.Spec.Path
}
// Commit saves router state
func (r *TestRouter) Commit() {
r.Committed = true
}
func (r *TestRouter) SetSkipCommit(skipCommit bool) {
}
func (r *TestRouter) SetSyncedAtLeastOnce() {
}
func (r *TestRouter) HasServiceUnit(key string) bool {
return false
}
// TestHandleEndpoints test endpoint watch events
func TestHandleEndpoints(t *testing.T) {
testCases := []struct {
name string //human readable name for test case
eventType watch.EventType //type to be passed to the HandleEndpoints method
endpoints *kapi.Endpoints //endpoints to be passed to the HandleEndpoints method
expectedServiceUnit *ServiceUnit //service unit that will be compared against.
excludeUDP bool
}{
{
name: "Endpoint add",
eventType: watch.Added,
endpoints: &kapi.Endpoints{
ObjectMeta: kapi.ObjectMeta{
Namespace: "foo",
Name: "test", //kapi.endpoints inherits the name of the service
},
Subsets: []kapi.EndpointSubset{{
Addresses: []kapi.EndpointAddress{{IP: "1.1.1.1"}},
Ports: []kapi.EndpointPort{{Port: 345}},
}}, //not specifying a port to force the port 80 assumption
},
expectedServiceUnit: &ServiceUnit{
Name: "foo/test", //service name from kapi.endpoints object
EndpointTable: []Endpoint{
{
ID: "1.1.1.1:345",
IP: "1.1.1.1",
Port: "345",
},
},
},
},
{
name: "Endpoint mod",
eventType: watch.Modified,
endpoints: &kapi.Endpoints{
ObjectMeta: kapi.ObjectMeta{
Namespace: "foo",
Name: "test",
},
Subsets: []kapi.EndpointSubset{{
Addresses: []kapi.EndpointAddress{{IP: "2.2.2.2"}},
Ports: []kapi.EndpointPort{{Port: 8080}},
}},
},
expectedServiceUnit: &ServiceUnit{
Name: "foo/test",
EndpointTable: []Endpoint{
{
ID: "2.2.2.2:8080",
IP: "2.2.2.2",
Port: "8080",
},
},
},
},
{
name: "Endpoint delete",
eventType: watch.Deleted,
endpoints: &kapi.Endpoints{
ObjectMeta: kapi.ObjectMeta{
Namespace: "foo",
Name: "test",
},
Subsets: []kapi.EndpointSubset{{
Addresses: []kapi.EndpointAddress{{IP: "3.3.3.3"}},
Ports: []kapi.EndpointPort{{Port: 0}},
}},
},
expectedServiceUnit: &ServiceUnit{
Name: "foo/test",
EndpointTable: []Endpoint{},
},
},
}
router := newTestRouter(make(map[string]ServiceAliasConfig))
templatePlugin := newDefaultTemplatePlugin(router, true, nil)
// TODO: move tests that rely on unique hosts to pkg/router/controller and remove them from
// here
plugin := controller.NewUniqueHost(templatePlugin, controller.HostForRoute, controller.LogRejections)
for _, tc := range testCases {
plugin.HandleEndpoints(tc.eventType, tc.endpoints)
if !router.Committed {
t.Errorf("Expected router to be committed after HandleEndpoints call")
}
su, ok := router.FindServiceUnit(tc.expectedServiceUnit.Name)
if !ok {
t.Errorf("TestHandleEndpoints test case %s failed. Couldn't find expected service unit with name %s", tc.name, tc.expectedServiceUnit.Name)
} else {
if len(su.EndpointTable) != len(tc.expectedServiceUnit.EndpointTable) {
t.Errorf("TestHandleEndpoints test case %s failed. endpoints: %d expected %d", tc.name, len(su.EndpointTable), len(tc.expectedServiceUnit.EndpointTable))
}
for expectedKey, expectedEp := range tc.expectedServiceUnit.EndpointTable {
actualEp := su.EndpointTable[expectedKey]
if expectedEp.ID != actualEp.ID || expectedEp.IP != actualEp.IP || expectedEp.Port != actualEp.Port {
t.Errorf("TestHandleEndpoints test case %s failed. Expected endpoint didn't match actual endpoint %v : %v", tc.name, expectedEp, actualEp)
}
}
}
}
}
// TestHandleCPEndpoints test endpoint watch events with UDP excluded
func TestHandleTCPEndpoints(t *testing.T) {
testCases := []struct {
name string //human readable name for test case
eventType watch.EventType //type to be passed to the HandleEndpoints method
endpoints *kapi.Endpoints //endpoints to be passed to the HandleEndpoints method
expectedServiceUnit *ServiceUnit //service unit that will be compared against.
}{
{
name: "Endpoint add",
eventType: watch.Added,
endpoints: &kapi.Endpoints{
ObjectMeta: kapi.ObjectMeta{
Namespace: "foo",
Name: "test", //kapi.endpoints inherits the name of the service
},
Subsets: []kapi.EndpointSubset{{
Addresses: []kapi.EndpointAddress{{IP: "1.1.1.1"}},
Ports: []kapi.EndpointPort{
{Port: 345},
{Port: 346, Protocol: kapi.ProtocolUDP},
},
}}, //not specifying a port to force the port 80 assumption
},
expectedServiceUnit: &ServiceUnit{
Name: "foo/test", //service name from kapi.endpoints object
EndpointTable: []Endpoint{
{
ID: "1.1.1.1:345",
IP: "1.1.1.1",
Port: "345",
},
},
},
},
{
name: "Endpoint mod",
eventType: watch.Modified,
endpoints: &kapi.Endpoints{
ObjectMeta: kapi.ObjectMeta{
Namespace: "foo",
Name: "test",
},
Subsets: []kapi.EndpointSubset{{
Addresses: []kapi.EndpointAddress{{IP: "2.2.2.2"}},
Ports: []kapi.EndpointPort{
{Port: 8080},
{Port: 8081, Protocol: kapi.ProtocolUDP},
},
}},
},
expectedServiceUnit: &ServiceUnit{
Name: "foo/test",
EndpointTable: []Endpoint{
{
ID: "2.2.2.2:8080",
IP: "2.2.2.2",
Port: "8080",
},
},
},
},
{
name: "Endpoint delete",
eventType: watch.Deleted,
endpoints: &kapi.Endpoints{
ObjectMeta: kapi.ObjectMeta{
Namespace: "foo",
Name: "test",
},
Subsets: []kapi.EndpointSubset{{
Addresses: []kapi.EndpointAddress{{IP: "3.3.3.3"}},
Ports: []kapi.EndpointPort{{Port: 0}},
}},
},
expectedServiceUnit: &ServiceUnit{
Name: "foo/test",
EndpointTable: []Endpoint{},
},
},
}
router := newTestRouter(make(map[string]ServiceAliasConfig))
templatePlugin := newDefaultTemplatePlugin(router, false, nil)
// TODO: move tests that rely on unique hosts to pkg/router/controller and remove them from
// here
plugin := controller.NewUniqueHost(templatePlugin, controller.HostForRoute, controller.LogRejections)
for _, tc := range testCases {
plugin.HandleEndpoints(tc.eventType, tc.endpoints)
if !router.Committed {
t.Errorf("Expected router to be committed after HandleEndpoints call")
}
su, ok := router.FindServiceUnit(tc.expectedServiceUnit.Name)
if !ok {
t.Errorf("TestHandleEndpoints test case %s failed. Couldn't find expected service unit with name %s", tc.name, tc.expectedServiceUnit.Name)
} else {
for expectedKey, expectedEp := range tc.expectedServiceUnit.EndpointTable {
actualEp := su.EndpointTable[expectedKey]
if expectedEp.ID != actualEp.ID || expectedEp.IP != actualEp.IP || expectedEp.Port != actualEp.Port {
t.Errorf("TestHandleEndpoints test case %s failed. Expected endpoint didn't match actual endpoint %v : %v", tc.name, expectedEp, actualEp)
}
}
}
}
}
type rejection struct {
route *routeapi.Route
reason string
message string
}
type fakeRejections struct {
rejections []rejection
}
func (r *fakeRejections) RecordRouteRejection(route *routeapi.Route, reason, message string) {
r.rejections = append(r.rejections, rejection{route: route, reason: reason, message: message})
}
// TestHandleRoute test route watch events
func TestHandleRoute(t *testing.T) {
rejections := &fakeRejections{}
router := newTestRouter(make(map[string]ServiceAliasConfig))
templatePlugin := newDefaultTemplatePlugin(router, true, nil)
// TODO: move tests that rely on unique hosts to pkg/router/controller and remove them from
// here
plugin := controller.NewUniqueHost(templatePlugin, controller.HostForRoute, rejections)
original := unversioned.Time{Time: time.Now()}
//add
route := &routeapi.Route{
ObjectMeta: kapi.ObjectMeta{
CreationTimestamp: original,
Namespace: "foo",
Name: "test",
},
Spec: routeapi.RouteSpec{
Host: "www.example.com",
To: routeapi.RouteTargetReference{
Name: "TestService",
Weight: new(int32),
},
},
}
serviceUnitKey := fmt.Sprintf("%s/%s", route.Namespace, route.Spec.To.Name)
plugin.HandleRoute(watch.Added, route)
if !router.Committed {
t.Errorf("Expected router to be committed after HandleRoute call")
}
_, ok := router.FindServiceUnit(serviceUnitKey)
if !ok {
t.Errorf("TestHandleRoute was unable to find the service unit %s after HandleRoute was called", route.Spec.To.Name)
} else {
serviceAliasCfg, ok := router.State[router.routeKey(route)]
if !ok {
t.Errorf("TestHandleRoute expected route key %s", router.routeKey(route))
} else {
if serviceAliasCfg.Host != route.Spec.Host || serviceAliasCfg.Path != route.Spec.Path {
t.Errorf("Expected route did not match service alias config %v : %v", route, serviceAliasCfg)
}
}
}
if len(rejections.rejections) > 0 {
t.Fatalf("did not expect a recorded rejection: %#v", rejections)
}
// attempt to add a second route with a newer time, verify it is ignored
duplicateRoute := &routeapi.Route{
ObjectMeta: kapi.ObjectMeta{
CreationTimestamp: unversioned.Time{Time: original.Add(time.Hour)},
Namespace: "foo",
Name: "dupe",
},
Spec: routeapi.RouteSpec{
Host: "www.example.com",
To: routeapi.RouteTargetReference{
Name: "TestService2",
Weight: new(int32),
},
},
}
if err := plugin.HandleRoute(watch.Added, duplicateRoute); err == nil {
t.Fatal("unexpected non-error")
}
if _, ok := router.FindServiceUnit("foo/TestService2"); ok {
t.Fatalf("unexpected second unit: %#v", router)
}
if r, ok := plugin.RoutesForHost("www.example.com"); !ok || r[0].Name != "test" {
t.Fatalf("unexpected claimed routes: %#v", r)
}
if len(rejections.rejections) != 1 ||
rejections.rejections[0].route.Name != "dupe" ||
rejections.rejections[0].reason != "HostAlreadyClaimed" ||
rejections.rejections[0].message != "route test already exposes www.example.com and is older" {
t.Fatalf("did not record rejection: %#v", rejections)
}
rejections.rejections = nil
// attempt to remove the second route that is not being used, verify it is ignored
if err := plugin.HandleRoute(watch.Deleted, duplicateRoute); err == nil {
t.Fatal("unexpected non-error")
}
if _, ok := router.FindServiceUnit("foo/TestService2"); ok {
t.Fatalf("unexpected second unit: %#v", router)
}
if _, ok := router.FindServiceUnit("foo/TestService"); !ok {
t.Fatalf("unexpected first unit: %#v", router)
}
if r, ok := plugin.RoutesForHost("www.example.com"); !ok || r[0].Name != "test" {
t.Fatalf("unexpected claimed routes: %#v", r)
}
if len(rejections.rejections) != 1 ||
rejections.rejections[0].route.Name != "dupe" ||
rejections.rejections[0].reason != "HostAlreadyClaimed" ||
rejections.rejections[0].message != "route test already exposes www.example.com and is older" {
t.Fatalf("did not record rejection: %#v", rejections)
}
rejections.rejections = nil
// add a second route with an older time, verify it takes effect
duplicateRoute.CreationTimestamp = unversioned.Time{Time: original.Add(-time.Hour)}
if err := plugin.HandleRoute(watch.Added, duplicateRoute); err != nil {
t.Fatal("unexpected error")
}
_, ok = router.FindServiceUnit("foo/TestService2")
if !ok {
t.Fatalf("missing second unit: %#v", router)
}
if len(rejections.rejections) != 1 ||
rejections.rejections[0].route.Name != "test" ||
rejections.rejections[0].reason != "HostAlreadyClaimed" ||
rejections.rejections[0].message != "replaced by older route dupe" {
t.Fatalf("did not record rejection: %#v", rejections)
}
rejections.rejections = nil
//mod
route.Spec.Host = "www.example2.com"
if err := plugin.HandleRoute(watch.Modified, route); err != nil {
t.Fatal("unexpected error")
}
if !router.Committed {
t.Errorf("Expected router to be committed after HandleRoute call")
}
_, ok = router.FindServiceUnit(serviceUnitKey)
if !ok {
t.Errorf("TestHandleRoute was unable to find the service unit %s after HandleRoute was called", route.Spec.To.Name)
} else {
serviceAliasCfg, ok := router.State[router.routeKey(route)]
if !ok {
t.Errorf("TestHandleRoute expected route key %s", router.routeKey(route))
} else {
if serviceAliasCfg.Host != route.Spec.Host || serviceAliasCfg.Path != route.Spec.Path {
t.Errorf("Expected route did not match service alias config %v : %v", route, serviceAliasCfg)
}
}
}
if plugin.HostLen() != 1 {
t.Fatalf("did not clear claimed route: %#v", plugin)
}
if len(rejections.rejections) != 0 {
t.Fatalf("unexpected rejection: %#v", rejections)
}
//delete
if err := plugin.HandleRoute(watch.Deleted, route); err != nil {
t.Fatal("unexpected error")
}
if !router.Committed {
t.Errorf("Expected router to be committed after HandleRoute call")
}
_, ok = router.FindServiceUnit(serviceUnitKey)
if !ok {
t.Errorf("TestHandleRoute was unable to find the service unit %s after HandleRoute was called", route.Spec.To.Name)
} else {
_, ok := router.State[router.routeKey(route)]
if ok {
t.Errorf("TestHandleRoute did not expect route key %s", router.routeKey(route))
}
}
if plugin.HostLen() != 0 {
t.Errorf("did not clear claimed route: %#v", plugin)
}
if len(rejections.rejections) != 0 {
t.Fatalf("unexpected rejection: %#v", rejections)
}
}
// TestHandleRouteExtendedValidation test route watch events with extended route configuration validation.
func TestHandleRouteExtendedValidation(t *testing.T) {
rejections := &fakeRejections{}
router := newTestRouter(make(map[string]ServiceAliasConfig))
templatePlugin := newDefaultTemplatePlugin(router, true, nil)
// TODO: move tests that rely on unique hosts to pkg/router/controller and remove them from
// here
extendedValidatorPlugin := controller.NewExtendedValidator(templatePlugin, rejections)
plugin := controller.NewUniqueHost(extendedValidatorPlugin, controller.HostForRoute, rejections)
original := unversioned.Time{Time: time.Now()}
//add
route := &routeapi.Route{
ObjectMeta: kapi.ObjectMeta{
CreationTimestamp: original,
Namespace: "foo",
Name: "test",
},
Spec: routeapi.RouteSpec{
Host: "www.example.com",
To: routeapi.RouteTargetReference{
Name: "TestService",
Weight: new(int32),
},
},
}
serviceUnitKey := fmt.Sprintf("%s/%s", route.Namespace, route.Spec.To.Name)
plugin.HandleRoute(watch.Added, route)
if !router.Committed {
t.Errorf("Expected router to be committed after HandleRoute call")
}
_, ok := router.FindServiceUnit(serviceUnitKey)
if !ok {
t.Errorf("TestHandleRoute was unable to find the service unit %s after HandleRoute was called", route.Spec.To.Name)
} else {
serviceAliasCfg, ok := router.State[router.routeKey(route)]
if !ok {
t.Errorf("TestHandleRoute expected route key %s", router.routeKey(route))
} else {
if serviceAliasCfg.Host != route.Spec.Host || serviceAliasCfg.Path != route.Spec.Path {
t.Errorf("Expected route did not match service alias config %v : %v", route, serviceAliasCfg)
}
}
}
if len(rejections.rejections) > 0 {
t.Fatalf("did not expect a recorded rejection: %#v", rejections)
}
tests := []struct {
name string
route *routeapi.Route
errorExpected bool
}{
{
name: "No TLS Termination",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
Host: "www.no.tls.test",
TLS: &routeapi.TLSConfig{
Termination: "",
},
},
},
errorExpected: true,
},
{
name: "Passthrough termination OK",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
Host: "www.passthrough.test",
TLS: &routeapi.TLSConfig{
Termination: routeapi.TLSTerminationPassthrough,
},
},
},
errorExpected: false,
},
{
name: "Reencrypt termination OK with certs",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
Host: "www.example.com",
TLS: &routeapi.TLSConfig{
Termination: routeapi.TLSTerminationReencrypt,
Certificate: testCertificate,
Key: testPrivateKey,
CACertificate: testCACertificate,
DestinationCACertificate: testDestinationCACertificate,
},
},
},
errorExpected: false,
},
{
name: "Reencrypt termination OK with bad config",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
Host: "www.reencypt.badconfig.test",
TLS: &routeapi.TLSConfig{
Termination: routeapi.TLSTerminationReencrypt,
Certificate: "def",
Key: "ghi",
CACertificate: "jkl",
DestinationCACertificate: "abc",
},
},
},
errorExpected: true,
},
{
name: "Reencrypt termination OK without certs",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
Host: "www.reencypt.nocerts.test",
TLS: &routeapi.TLSConfig{
Termination: routeapi.TLSTerminationReencrypt,
DestinationCACertificate: testDestinationCACertificate,
},
},
},
errorExpected: false,
},
{
name: "Reencrypt termination bad config without certs",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
Host: "www.reencypt.badconfignocerts.test",
TLS: &routeapi.TLSConfig{
Termination: routeapi.TLSTerminationReencrypt,
DestinationCACertificate: "abc",
},
},
},
errorExpected: true,
},
{
name: "Reencrypt termination no dest cert",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
Host: "www.reencypt.nodestcert.test",
TLS: &routeapi.TLSConfig{
Termination: routeapi.TLSTerminationReencrypt,
Certificate: testCertificate,
Key: testPrivateKey,
CACertificate: testCACertificate,
},
},
},
errorExpected: true,
},
{
name: "Edge termination OK with certs without host",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
TLS: &routeapi.TLSConfig{
Termination: routeapi.TLSTerminationEdge,
Certificate: testCertificate,
Key: testPrivateKey,
CACertificate: testCACertificate,
},
},
},
errorExpected: false,
},
{
name: "Edge termination OK with certs",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
Host: "www.example.com",
TLS: &routeapi.TLSConfig{
Termination: routeapi.TLSTerminationEdge,
Certificate: testCertificate,
Key: testPrivateKey,
CACertificate: testCACertificate,
},
},
},
errorExpected: false,
},
{
name: "Edge termination bad config with certs",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
Host: "www.edge.badconfig.test",
TLS: &routeapi.TLSConfig{
Termination: routeapi.TLSTerminationEdge,
Certificate: "abc",
Key: "abc",
CACertificate: "abc",
},
},
},
errorExpected: true,
},
{
name: "Edge termination mismatched key and cert",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
Host: "www.edge.mismatchdkeyandcert.test",
TLS: &routeapi.TLSConfig{
Termination: routeapi.TLSTerminationEdge,
Certificate: testCertificate,
Key: testExpiredCertPrivateKey,
CACertificate: testCACertificate,
},
},
},
errorExpected: true,
},
{
name: "Edge termination expired cert",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
Host: "www.edge.expiredcert.test",
TLS: &routeapi.TLSConfig{
Termination: routeapi.TLSTerminationEdge,
Certificate: testExpiredCAUnknownCertificate,
Key: testExpiredCertPrivateKey,
CACertificate: testCACertificate,
},
},
},
errorExpected: true,
},
{
name: "Edge termination expired cert key mismatch",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
Host: "www.edge.expiredcertkeymismatch.test",
TLS: &routeapi.TLSConfig{
Termination: routeapi.TLSTerminationEdge,
Certificate: testExpiredCAUnknownCertificate,
Key: testPrivateKey,
CACertificate: testCACertificate,
},
},
},
errorExpected: true,
},
{
name: "Edge termination OK without certs",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
Host: "www.edge.nocerts.test",
TLS: &routeapi.TLSConfig{
Termination: routeapi.TLSTerminationEdge,
},
},
},
errorExpected: false,
},
{
name: "Edge termination, bad dest cert",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
Host: "www.edge.baddestcert.test",
TLS: &routeapi.TLSConfig{
Termination: routeapi.TLSTerminationEdge,
DestinationCACertificate: "abc",
},
},
},
errorExpected: true,
},
{
name: "Passthrough termination, bad cert",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
Host: "www.passthrough.badcert.test",
TLS: &routeapi.TLSConfig{Termination: routeapi.TLSTerminationPassthrough, Certificate: "test"},
},
},
errorExpected: true,
},
{
name: "Passthrough termination, bad key",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
Host: "www.passthrough.badkey.test",
TLS: &routeapi.TLSConfig{Termination: routeapi.TLSTerminationPassthrough, Key: "test"},
},
},
errorExpected: true,
},
{
name: "Passthrough termination, bad ca cert",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
Host: "www.passthrough.badcacert.test",
TLS: &routeapi.TLSConfig{Termination: routeapi.TLSTerminationPassthrough, CACertificate: "test"},
},
},
errorExpected: true,
},
{
name: "Passthrough termination, bad dest ca cert",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
Host: "www.passthrough.baddestcacert.test",
TLS: &routeapi.TLSConfig{Termination: routeapi.TLSTerminationPassthrough, DestinationCACertificate: "test"},
},
},
errorExpected: true,
},
{
name: "Invalid termination type",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
TLS: &routeapi.TLSConfig{
Termination: "invalid",
},
},
},
errorExpected: false,
},
{
name: "Double escaped newlines",
route: &routeapi.Route{
Spec: routeapi.RouteSpec{
Host: "www.reencrypt.doubleescapednewlines.test",
TLS: &routeapi.TLSConfig{
Termination: routeapi.TLSTerminationReencrypt,
Certificate: "d\\nef",
Key: "g\\nhi",
CACertificate: "j\\nkl",
DestinationCACertificate: "j\\nkl",
},
},