Skip to content

Commit a1697ac

Browse files
Update the router unique host test
The router unique host test was embedding the lost update that we observed as "correct" behavior. Clean up the test to not reuse objects and to set UIDs, and give each sequent a correct name. An error condition on the extended validation test was being masked by a unique_host wrapper - remove that wrapper and make the test correct.
1 parent bccae62 commit a1697ac

File tree

3 files changed

+104
-65
lines changed

3 files changed

+104
-65
lines changed

pkg/cmd/server/kubernetes/node/options/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func ComputeKubeletFlags(startingArgs map[string][]string, options configapi.Nod
7070
setIfUnset(args, "file-check-frequency", fmt.Sprintf("%ds", fileCheckInterval))
7171
setIfUnset(args, "pod-infra-container-image", imageTemplate.ExpandOrDie("pod"))
7272
setIfUnset(args, "max-pods", "250")
73-
setIfUnset(args, "pods-per-core", "10")
73+
setIfUnset(args, "pods-per-core", "40")
7474
setIfUnset(args, "cgroup-driver", "systemd")
7575
setIfUnset(args, "container-runtime-endpoint", options.DockerConfig.DockerShimSocket)
7676
setIfUnset(args, "image-service-endpoint", options.DockerConfig.DockerShimSocket)

pkg/router/controller/host_admitter_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,12 @@ func makeRoute(ns, name, host, path string, wildcard bool, creationTimestamp met
575575
policy = routeapi.WildcardPolicySubdomain
576576
}
577577
return &routeapi.Route{
578-
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: ns, CreationTimestamp: creationTimestamp},
578+
ObjectMeta: metav1.ObjectMeta{
579+
Name: name,
580+
Namespace: ns,
581+
CreationTimestamp: creationTimestamp,
582+
UID: types.UID(fmt.Sprintf("%d_%s_%s", creationTimestamp.Time.Unix(), ns, name)),
583+
},
579584
Spec: routeapi.RouteSpec{
580585
Host: host,
581586
Path: path,

pkg/router/template/plugin_test.go

Lines changed: 97 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package templaterouter
22

33
import (
4+
"fmt"
45
"reflect"
56
"testing"
67
"time"
78

89
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/apimachinery/pkg/types"
911
"k8s.io/apimachinery/pkg/util/sets"
1012
"k8s.io/apimachinery/pkg/watch"
1113
kapi "k8s.io/kubernetes/pkg/apis/core"
@@ -484,14 +486,20 @@ func TestHandleRoute(t *testing.T) {
484486
// here
485487
plugin := controller.NewUniqueHost(templatePlugin, controller.HostForRoute, false, rejections)
486488

489+
uidCount := 0
490+
nextUID := func() types.UID {
491+
uidCount++
492+
return types.UID(fmt.Sprintf("%03d", uidCount))
493+
}
487494
original := metav1.Time{Time: time.Now()}
488495

489496
//add
490-
route := &routeapi.Route{
497+
fooTest1 := &routeapi.Route{
491498
ObjectMeta: metav1.ObjectMeta{
492499
CreationTimestamp: original,
493500
Namespace: "foo",
494501
Name: "test",
502+
UID: nextUID(),
495503
},
496504
Spec: routeapi.RouteSpec{
497505
Host: "www.example.com",
@@ -501,22 +509,22 @@ func TestHandleRoute(t *testing.T) {
501509
},
502510
},
503511
}
504-
serviceUnitKey := endpointsKeyFromParts(route.Namespace, route.Spec.To.Name)
512+
serviceUnitKey := endpointsKeyFromParts(fooTest1.Namespace, fooTest1.Spec.To.Name)
505513

506-
plugin.HandleRoute(watch.Added, route)
514+
plugin.HandleRoute(watch.Added, fooTest1)
507515

508516
_, ok := router.FindServiceUnit(serviceUnitKey)
509517

510518
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)
512520
} else {
513-
serviceAliasCfg, ok := router.State[getKey(route)]
521+
serviceAliasCfg, ok := router.State[getKey(fooTest1)]
514522

515523
if !ok {
516-
t.Errorf("TestHandleRoute expected route key %s", getKey(route))
524+
t.Errorf("TestHandleRoute expected route key %s", getKey(fooTest1))
517525
} 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)
520528
}
521529
}
522530
}
@@ -526,11 +534,12 @@ func TestHandleRoute(t *testing.T) {
526534
}
527535

528536
// attempt to add a second route with a newer time, verify it is ignored
529-
duplicateRoute := &routeapi.Route{
537+
fooDupe2 := &routeapi.Route{
530538
ObjectMeta: metav1.ObjectMeta{
531539
CreationTimestamp: metav1.Time{Time: original.Add(time.Hour)},
532540
Namespace: "foo",
533541
Name: "dupe",
542+
UID: nextUID(),
534543
},
535544
Spec: routeapi.RouteSpec{
536545
Host: "www.example.com",
@@ -540,9 +549,10 @@ func TestHandleRoute(t *testing.T) {
540549
},
541550
},
542551
}
543-
if err := plugin.HandleRoute(watch.Added, duplicateRoute); err == nil {
552+
if err := plugin.HandleRoute(watch.Added, fooDupe2); err == nil {
544553
t.Fatal("unexpected non-error")
545554
}
555+
546556
if _, ok := router.FindServiceUnit(endpointsKeyFromParts("foo", "TestService2")); ok {
547557
t.Fatalf("unexpected second unit: %#v", router)
548558
}
@@ -558,9 +568,10 @@ func TestHandleRoute(t *testing.T) {
558568
rejections.rejections = nil
559569

560570
// 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")
563573
}
574+
564575
if _, ok := router.FindServiceUnit(endpointsKeyFromParts("foo", "TestService2")); ok {
565576
t.Fatalf("unexpected second unit: %#v", router)
566577
}
@@ -570,17 +581,17 @@ func TestHandleRoute(t *testing.T) {
570581
if r, ok := plugin.RoutesForHost("www.example.com"); !ok || r[0].Name != "test" {
571582
t.Fatalf("unexpected claimed routes: %#v", r)
572583
}
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 {
577585
t.Fatalf("did not record rejection: %#v", rejections)
578586
}
579587
rejections.rejections = nil
580588

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 {
584595
t.Fatal("unexpected error")
585596
}
586597
_, ok = router.FindServiceUnit(endpointsKeyFromParts("foo", "TestService2"))
@@ -596,24 +607,36 @@ func TestHandleRoute(t *testing.T) {
596607
rejections.rejections = nil
597608

598609
//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 {
601614
t.Fatal("unexpected error")
602615
}
616+
617+
key := getKey(fooTest1NewHost)
603618
_, ok = router.FindServiceUnit(serviceUnitKey)
604619
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)
606621
} else {
607-
serviceAliasCfg, ok := router.State[getKey(route)]
608-
622+
serviceAliasCfg, ok := router.State[key]
609623
if !ok {
610-
t.Errorf("TestHandleRoute expected route key %s", getKey(route))
624+
t.Errorf("TestHandleRoute expected route key %s", key)
611625
} 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)
614628
}
615629
}
616630
}
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+
617640
if plugin.HostLen() != 1 {
618641
t.Fatalf("did not clear claimed route: %#v", plugin)
619642
}
@@ -622,17 +645,18 @@ func TestHandleRoute(t *testing.T) {
622645
}
623646

624647
//delete
625-
if err := plugin.HandleRoute(watch.Deleted, route); err != nil {
648+
if err := plugin.HandleRoute(watch.Deleted, fooTest1NewHost); err != nil {
626649
t.Fatal("unexpected error")
627650
}
628651
_, ok = router.FindServiceUnit(serviceUnitKey)
629652
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)
631654
} else {
632-
_, ok := router.State[getKey(route)]
655+
656+
_, ok := router.State[key]
633657

634658
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)
636660
}
637661
}
638662
if plugin.HostLen() != 0 {
@@ -643,15 +667,37 @@ func TestHandleRoute(t *testing.T) {
643667
}
644668
}
645669

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+
646696
// TestHandleRouteExtendedValidation test route watch events with extended route configuration validation.
647697
func TestHandleRouteExtendedValidation(t *testing.T) {
648698
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)
655701

656702
original := metav1.Time{Time: time.Now()}
657703

@@ -670,24 +716,10 @@ func TestHandleRouteExtendedValidation(t *testing.T) {
670716
},
671717
},
672718
}
673-
serviceUnitKey := endpointsKeyFromParts(route.Namespace, route.Spec.To.Name)
674719

675720
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)
691723
}
692724

693725
if len(rejections.rejections) > 0 {
@@ -960,7 +992,7 @@ func TestHandleRouteExtendedValidation(t *testing.T) {
960992
},
961993
},
962994
},
963-
errorExpected: false,
995+
errorExpected: true,
964996
},
965997
{
966998
name: "Double escaped newlines",
@@ -981,16 +1013,18 @@ func TestHandleRouteExtendedValidation(t *testing.T) {
9811013
}
9821014

9831015
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+
}
9921026
}
993-
}
1027+
})
9941028
}
9951029
}
9961030

0 commit comments

Comments
 (0)