github.com/deemoprobe/k8s-first-commit@v0.0.0-20230430165612-a541f1982be3/pkg/registry/replication_controller_test.go (about) 1 /* 2 Copyright 2014 Google Inc. All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 package registry 17 18 import ( 19 "encoding/json" 20 "fmt" 21 "net/http/httptest" 22 "reflect" 23 "testing" 24 25 . "github.com/GoogleCloudPlatform/kubernetes/pkg/api" 26 . "github.com/GoogleCloudPlatform/kubernetes/pkg/client" 27 "github.com/GoogleCloudPlatform/kubernetes/pkg/util" 28 "github.com/coreos/go-etcd/etcd" 29 ) 30 31 // TODO: Move this to a common place, it's needed in multiple tests. 32 var apiPath = "/api/v1beta1" 33 34 func makeUrl(suffix string) string { 35 return apiPath + suffix 36 } 37 38 type FakeTaskControl struct { 39 controllerSpec []ReplicationController 40 deleteTaskID []string 41 } 42 43 func (f *FakeTaskControl) createReplica(spec ReplicationController) { 44 f.controllerSpec = append(f.controllerSpec, spec) 45 } 46 47 func (f *FakeTaskControl) deleteTask(taskID string) error { 48 f.deleteTaskID = append(f.deleteTaskID, taskID) 49 return nil 50 } 51 52 func makeReplicationController(replicas int) ReplicationController { 53 return ReplicationController{ 54 DesiredState: ReplicationControllerState{ 55 Replicas: replicas, 56 TaskTemplate: TaskTemplate{ 57 DesiredState: TaskState{ 58 Manifest: ContainerManifest{ 59 Containers: []Container{ 60 Container{ 61 Image: "foo/bar", 62 }, 63 }, 64 }, 65 }, 66 Labels: map[string]string{ 67 "name": "foo", 68 "type": "production", 69 }, 70 }, 71 }, 72 } 73 } 74 75 func makeTaskList(count int) TaskList { 76 tasks := []Task{} 77 for i := 0; i < count; i++ { 78 tasks = append(tasks, Task{ 79 JSONBase: JSONBase{ 80 ID: fmt.Sprintf("task%d", i), 81 }, 82 }) 83 } 84 return TaskList{ 85 Items: tasks, 86 } 87 } 88 89 func validateSyncReplication(t *testing.T, fakeTaskControl *FakeTaskControl, expectedCreates, expectedDeletes int) { 90 if len(fakeTaskControl.controllerSpec) != expectedCreates { 91 t.Errorf("Unexpected number of creates. Expected %d, saw %d\n", expectedCreates, len(fakeTaskControl.controllerSpec)) 92 } 93 if len(fakeTaskControl.deleteTaskID) != expectedDeletes { 94 t.Errorf("Unexpected number of deletes. Expected %d, saw %d\n", expectedDeletes, len(fakeTaskControl.deleteTaskID)) 95 } 96 } 97 98 func TestSyncReplicationControllerDoesNothing(t *testing.T) { 99 body, _ := json.Marshal(makeTaskList(2)) 100 fakeHandler := util.FakeHandler{ 101 StatusCode: 200, 102 ResponseBody: string(body), 103 } 104 testServer := httptest.NewTLSServer(&fakeHandler) 105 client := Client{ 106 Host: testServer.URL, 107 } 108 109 fakeTaskControl := FakeTaskControl{} 110 111 manager := MakeReplicationManager(nil, &client) 112 manager.taskControl = &fakeTaskControl 113 114 controllerSpec := makeReplicationController(2) 115 116 manager.syncReplicationController(controllerSpec) 117 validateSyncReplication(t, &fakeTaskControl, 0, 0) 118 } 119 120 func TestSyncReplicationControllerDeletes(t *testing.T) { 121 body, _ := json.Marshal(makeTaskList(2)) 122 fakeHandler := util.FakeHandler{ 123 StatusCode: 200, 124 ResponseBody: string(body), 125 } 126 testServer := httptest.NewTLSServer(&fakeHandler) 127 client := Client{ 128 Host: testServer.URL, 129 } 130 131 fakeTaskControl := FakeTaskControl{} 132 133 manager := MakeReplicationManager(nil, &client) 134 manager.taskControl = &fakeTaskControl 135 136 controllerSpec := makeReplicationController(1) 137 138 manager.syncReplicationController(controllerSpec) 139 validateSyncReplication(t, &fakeTaskControl, 0, 1) 140 } 141 142 func TestSyncReplicationControllerCreates(t *testing.T) { 143 body := "{ \"items\": [] }" 144 fakeHandler := util.FakeHandler{ 145 StatusCode: 200, 146 ResponseBody: string(body), 147 } 148 testServer := httptest.NewTLSServer(&fakeHandler) 149 client := Client{ 150 Host: testServer.URL, 151 } 152 153 fakeTaskControl := FakeTaskControl{} 154 155 manager := MakeReplicationManager(nil, &client) 156 manager.taskControl = &fakeTaskControl 157 158 controllerSpec := makeReplicationController(2) 159 160 manager.syncReplicationController(controllerSpec) 161 validateSyncReplication(t, &fakeTaskControl, 2, 0) 162 } 163 164 func TestCreateReplica(t *testing.T) { 165 body := "{}" 166 fakeHandler := util.FakeHandler{ 167 StatusCode: 200, 168 ResponseBody: string(body), 169 } 170 testServer := httptest.NewTLSServer(&fakeHandler) 171 client := Client{ 172 Host: testServer.URL, 173 } 174 175 taskControl := RealTaskControl{ 176 kubeClient: client, 177 } 178 179 controllerSpec := ReplicationController{ 180 DesiredState: ReplicationControllerState{ 181 TaskTemplate: TaskTemplate{ 182 DesiredState: TaskState{ 183 Manifest: ContainerManifest{ 184 Containers: []Container{ 185 Container{ 186 Image: "foo/bar", 187 }, 188 }, 189 }, 190 }, 191 Labels: map[string]string{ 192 "name": "foo", 193 "type": "production", 194 }, 195 }, 196 }, 197 } 198 199 taskControl.createReplica(controllerSpec) 200 201 //expectedTask := Task{ 202 // Labels: controllerSpec.DesiredState.TaskTemplate.Labels, 203 // DesiredState: controllerSpec.DesiredState.TaskTemplate.DesiredState, 204 //} 205 // TODO: fix this so that it validates the body. 206 fakeHandler.ValidateRequest(t, makeUrl("/tasks"), "POST", nil) 207 } 208 209 func TestHandleWatchResponseNotSet(t *testing.T) { 210 body, _ := json.Marshal(makeTaskList(2)) 211 fakeHandler := util.FakeHandler{ 212 StatusCode: 200, 213 ResponseBody: string(body), 214 } 215 testServer := httptest.NewTLSServer(&fakeHandler) 216 client := Client{ 217 Host: testServer.URL, 218 } 219 220 fakeTaskControl := FakeTaskControl{} 221 222 manager := MakeReplicationManager(nil, &client) 223 manager.taskControl = &fakeTaskControl 224 _, err := manager.handleWatchResponse(&etcd.Response{ 225 Action: "delete", 226 }) 227 expectNoError(t, err) 228 } 229 230 func TestHandleWatchResponseNoNode(t *testing.T) { 231 body, _ := json.Marshal(makeTaskList(2)) 232 fakeHandler := util.FakeHandler{ 233 StatusCode: 200, 234 ResponseBody: string(body), 235 } 236 testServer := httptest.NewTLSServer(&fakeHandler) 237 client := Client{ 238 Host: testServer.URL, 239 } 240 241 fakeTaskControl := FakeTaskControl{} 242 243 manager := MakeReplicationManager(nil, &client) 244 manager.taskControl = &fakeTaskControl 245 _, err := manager.handleWatchResponse(&etcd.Response{ 246 Action: "set", 247 }) 248 if err == nil { 249 t.Error("Unexpected non-error") 250 } 251 } 252 253 func TestHandleWatchResponseBadData(t *testing.T) { 254 body, _ := json.Marshal(makeTaskList(2)) 255 fakeHandler := util.FakeHandler{ 256 StatusCode: 200, 257 ResponseBody: string(body), 258 } 259 testServer := httptest.NewTLSServer(&fakeHandler) 260 client := Client{ 261 Host: testServer.URL, 262 } 263 264 fakeTaskControl := FakeTaskControl{} 265 266 manager := MakeReplicationManager(nil, &client) 267 manager.taskControl = &fakeTaskControl 268 _, err := manager.handleWatchResponse(&etcd.Response{ 269 Action: "set", 270 Node: &etcd.Node{ 271 Value: "foobar", 272 }, 273 }) 274 if err == nil { 275 t.Error("Unexpected non-error") 276 } 277 } 278 279 func TestHandleWatchResponse(t *testing.T) { 280 body, _ := json.Marshal(makeTaskList(2)) 281 fakeHandler := util.FakeHandler{ 282 StatusCode: 200, 283 ResponseBody: string(body), 284 } 285 testServer := httptest.NewTLSServer(&fakeHandler) 286 client := Client{ 287 Host: testServer.URL, 288 } 289 290 fakeTaskControl := FakeTaskControl{} 291 292 manager := MakeReplicationManager(nil, &client) 293 manager.taskControl = &fakeTaskControl 294 295 controller := makeReplicationController(2) 296 297 data, err := json.Marshal(controller) 298 expectNoError(t, err) 299 controllerOut, err := manager.handleWatchResponse(&etcd.Response{ 300 Action: "set", 301 Node: &etcd.Node{ 302 Value: string(data), 303 }, 304 }) 305 if err != nil { 306 t.Errorf("Unexpected error: %#v", err) 307 } 308 if !reflect.DeepEqual(controller, *controllerOut) { 309 t.Errorf("Unexpected mismatch. Expected %#v, Saw: %#v", controller, controllerOut) 310 } 311 }