|
| 1 | +package haproxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "k8s.io/apimachinery/pkg/util/sets" |
| 10 | + "k8s.io/apimachinery/pkg/watch" |
| 11 | + kapi "k8s.io/kubernetes/pkg/apis/core" |
| 12 | + |
| 13 | + routeapi "github.com/openshift/origin/pkg/route/apis/route" |
| 14 | + templaterouter "github.com/openshift/origin/pkg/router/template" |
| 15 | +) |
| 16 | + |
| 17 | +type fakeConfigManager struct { |
| 18 | + blueprints map[string]*routeapi.Route |
| 19 | +} |
| 20 | + |
| 21 | +func newFakeConfigManager() *fakeConfigManager { |
| 22 | + return &fakeConfigManager{ |
| 23 | + blueprints: make(map[string]*routeapi.Route), |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func (cm *fakeConfigManager) Initialize(router templaterouter.RouterInterface, certPath string) { |
| 28 | +} |
| 29 | + |
| 30 | +func (cm *fakeConfigManager) AddBlueprint(route *routeapi.Route) { |
| 31 | + cm.blueprints[routeKey(route)] = route |
| 32 | +} |
| 33 | + |
| 34 | +func (cm *fakeConfigManager) RemoveBlueprint(route *routeapi.Route) { |
| 35 | + delete(cm.blueprints, routeKey(route)) |
| 36 | +} |
| 37 | + |
| 38 | +func (cm *fakeConfigManager) FindBlueprint(id string) (*routeapi.Route, bool) { |
| 39 | + route, ok := cm.blueprints[id] |
| 40 | + return route, ok |
| 41 | +} |
| 42 | + |
| 43 | +func (cm *fakeConfigManager) Register(id string, route *routeapi.Route) { |
| 44 | +} |
| 45 | + |
| 46 | +func (cm *fakeConfigManager) AddRoute(id string, route *routeapi.Route) error { |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +func (cm *fakeConfigManager) RemoveRoute(id string, route *routeapi.Route) error { |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +func (cm *fakeConfigManager) ReplaceRouteEndpoints(id string, oldEndpoints, newEndpoints []templaterouter.Endpoint, weight int32) error { |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +func (cm *fakeConfigManager) RemoveRouteEndpoints(id string, endpoints []templaterouter.Endpoint) error { |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +func (cm *fakeConfigManager) Notify(event templaterouter.RouterEventType) { |
| 63 | +} |
| 64 | + |
| 65 | +func (cm *fakeConfigManager) ServerTemplateName(id string) string { |
| 66 | + return "fakeConfigManager" |
| 67 | +} |
| 68 | + |
| 69 | +func (cm *fakeConfigManager) ServerTemplateSize(id string) string { |
| 70 | + return "1" |
| 71 | +} |
| 72 | + |
| 73 | +func (cm *fakeConfigManager) GenerateDynamicServerNames(id string) []string { |
| 74 | + return []string{} |
| 75 | +} |
| 76 | + |
| 77 | +func routeKey(route *routeapi.Route) string { |
| 78 | + return fmt.Sprintf("%s:%s", route.Name, route.Namespace) |
| 79 | +} |
| 80 | + |
| 81 | +// TestHandleRoute test route watch events |
| 82 | +func TestHandleRoute(t *testing.T) { |
| 83 | + original := metav1.Time{Time: time.Now()} |
| 84 | + |
| 85 | + route := &routeapi.Route{ |
| 86 | + ObjectMeta: metav1.ObjectMeta{ |
| 87 | + CreationTimestamp: original, |
| 88 | + Namespace: "bp", |
| 89 | + Name: "chevron", |
| 90 | + }, |
| 91 | + Spec: routeapi.RouteSpec{ |
| 92 | + Host: "www.blueprints.org", |
| 93 | + To: routeapi.RouteTargetReference{ |
| 94 | + Name: "TestService", |
| 95 | + Weight: new(int32), |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + cm := newFakeConfigManager() |
| 101 | + plugin := NewBlueprintPlugin(cm) |
| 102 | + plugin.HandleRoute(watch.Added, route) |
| 103 | + |
| 104 | + id := routeKey(route) |
| 105 | + if _, ok := cm.FindBlueprint(id); !ok { |
| 106 | + t.Errorf("TestHandleRoute was unable to find a blueprint %s after HandleRoute was called", id) |
| 107 | + } |
| 108 | + |
| 109 | + // update a blueprint with a newer time and host |
| 110 | + v2route := route.DeepCopy() |
| 111 | + v2route.CreationTimestamp = metav1.Time{Time: original.Add(time.Hour)} |
| 112 | + v2route.Spec.Host = "updated.blueprint.org" |
| 113 | + if err := plugin.HandleRoute(watch.Added, v2route); err != nil { |
| 114 | + t.Errorf("TestHandleRoute unexpected error after blueprint update: %v", err) |
| 115 | + } |
| 116 | + |
| 117 | + blueprints := []*routeapi.Route{v2route, route} |
| 118 | + for _, r := range blueprints { |
| 119 | + // delete the blueprint and check that it doesn't exist. |
| 120 | + if err := plugin.HandleRoute(watch.Deleted, v2route); err != nil { |
| 121 | + t.Errorf("TestHandleRoute unexpected error after blueprint delete: %v", err) |
| 122 | + } |
| 123 | + |
| 124 | + routeId := routeKey(r) |
| 125 | + if _, ok := cm.FindBlueprint(routeId); ok { |
| 126 | + t.Errorf("TestHandleRoute found a blueprint %s after it was deleted", routeId) |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestHandleNode(t *testing.T) { |
| 132 | + node := &kapi.Node{ |
| 133 | + ObjectMeta: metav1.ObjectMeta{ |
| 134 | + Labels: map[string]string{"design": "blueprint"}, |
| 135 | + }, |
| 136 | + } |
| 137 | + |
| 138 | + cm := newFakeConfigManager() |
| 139 | + plugin := NewBlueprintPlugin(cm) |
| 140 | + |
| 141 | + if err := plugin.HandleNode(watch.Added, node); err != nil { |
| 142 | + t.Errorf("TestHandleNode unexpected error after node add: %v", err) |
| 143 | + } |
| 144 | + |
| 145 | + if err := plugin.HandleNode(watch.Modified, node); err != nil { |
| 146 | + t.Errorf("TestHandleNode unexpected error after node modify: %v", err) |
| 147 | + } |
| 148 | + |
| 149 | + if err := plugin.HandleNode(watch.Deleted, node); err != nil { |
| 150 | + t.Errorf("TestHandleNode unexpected error after node delete: %v", err) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func TestHandleEndpoints(t *testing.T) { |
| 155 | + endpoints := &kapi.Endpoints{ |
| 156 | + ObjectMeta: metav1.ObjectMeta{ |
| 157 | + Namespace: "bpe", |
| 158 | + Name: "shell", |
| 159 | + }, |
| 160 | + Subsets: []kapi.EndpointSubset{{ |
| 161 | + Addresses: []kapi.EndpointAddress{{IP: "1.1.1.1"}}, |
| 162 | + Ports: []kapi.EndpointPort{{Port: 9876}}, |
| 163 | + }}, |
| 164 | + } |
| 165 | + |
| 166 | + v2Endpoints := &kapi.Endpoints{ |
| 167 | + ObjectMeta: metav1.ObjectMeta{ |
| 168 | + Namespace: "bpe", |
| 169 | + Name: "shell", |
| 170 | + }, |
| 171 | + Subsets: []kapi.EndpointSubset{{ |
| 172 | + Addresses: []kapi.EndpointAddress{{IP: "1.1.1.1"}, {IP: "2.2.2.2"}}, |
| 173 | + Ports: []kapi.EndpointPort{{Port: 9876}, {Port: 8888}}, |
| 174 | + }}, |
| 175 | + } |
| 176 | + |
| 177 | + cm := newFakeConfigManager() |
| 178 | + plugin := NewBlueprintPlugin(cm) |
| 179 | + |
| 180 | + if err := plugin.HandleEndpoints(watch.Added, endpoints); err != nil { |
| 181 | + t.Errorf("TestHandleEndpoints unexpected error after endpoints add: %v", err) |
| 182 | + } |
| 183 | + |
| 184 | + if err := plugin.HandleEndpoints(watch.Modified, v2Endpoints); err != nil { |
| 185 | + t.Errorf("TestHandleEndpoints unexpected error after endpoints modify: %v", err) |
| 186 | + } |
| 187 | + |
| 188 | + if err := plugin.HandleEndpoints(watch.Deleted, v2Endpoints); err != nil { |
| 189 | + t.Errorf("TestHandleEndpoints unexpected error after endpoints delete: %v", err) |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +func TestHandleNamespaces(t *testing.T) { |
| 194 | + cm := newFakeConfigManager() |
| 195 | + plugin := NewBlueprintPlugin(cm) |
| 196 | + |
| 197 | + if err := plugin.HandleNamespaces(sets.String{}); err != nil { |
| 198 | + t.Errorf("TestHandleNamespaces unexpected error after empty set: %v", err) |
| 199 | + } |
| 200 | + |
| 201 | + if err := plugin.HandleNamespaces(sets.NewString("76")); err != nil { |
| 202 | + t.Errorf("TestHandleNamespaces unexpected error after set: %v", err) |
| 203 | + } |
| 204 | + |
| 205 | + if err := plugin.HandleNamespaces(sets.NewString("76", "711")); err != nil { |
| 206 | + t.Errorf("TestHandleNamespaces unexpected error after set multiple: %v", err) |
| 207 | + } |
| 208 | + |
| 209 | + if err := plugin.HandleNamespaces(sets.NewString("arco")); err != nil { |
| 210 | + t.Errorf("TestHandleNamespaces unexpected error after reset: %v", err) |
| 211 | + } |
| 212 | +} |
0 commit comments