func TestEtcdListServices(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) key := "/registry/services/specs" fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ { Value: util.MakeJSONString(api.Service{JSONBase: api.JSONBase{ID: "foo"}}), }, { Value: util.MakeJSONString(api.Service{JSONBase: api.JSONBase{ID: "bar"}}), }, }, }, }, E: nil, } registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) services, err := registry.ListServices() if err != nil { t.Errorf("unexpected error: %v", err) } if len(services.Items) != 2 || services.Items[0].ID != "foo" || services.Items[1].ID != "bar" { t.Errorf("Unexpected pod list: %#v", services) } }
func TestEtcdDeletePodMultipleContainers(t *testing.T) { fakeClient := MakeFakeEtcdClient(t) key := "/registry/hosts/machine/pods/foo" fakeClient.Set(key, util.MakeJSONString(api.Pod{JSONBase: api.JSONBase{ID: "foo"}}), 0) fakeClient.Set("/registry/hosts/machine/kubelet", util.MakeJSONString([]api.ContainerManifest{ {Id: "foo"}, {Id: "bar"}, }), 0) registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) err := registry.DeletePod("foo") expectNoError(t, err) if len(fakeClient.deletedKeys) != 1 { t.Errorf("Expected 1 delete, found %#v", fakeClient.deletedKeys) } if fakeClient.deletedKeys[0] != key { t.Errorf("Unexpected key: %s, expected %s", fakeClient.deletedKeys[0], key) } response, _ := fakeClient.Get("/registry/hosts/machine/kubelet", false, false) var manifests []api.ContainerManifest json.Unmarshal([]byte(response.Node.Value), &manifests) if len(manifests) != 1 { t.Errorf("Unexpected manifest set: %#v, expected empty", manifests) } if manifests[0].Id != "bar" { t.Errorf("Deleted wrong manifest: %#v", manifests) } }
func TestEtcdListControllers(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) key := "/registry/controllers" fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ { Value: util.MakeJSONString(api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}), }, { Value: util.MakeJSONString(api.ReplicationController{JSONBase: api.JSONBase{ID: "bar"}}), }, }, }, }, E: nil, } registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) controllers, err := registry.ListControllers() if err != nil { t.Errorf("unexpected error: %v", err) } if len(controllers) != 2 || controllers[0].ID != "foo" || controllers[1].ID != "bar" { t.Errorf("Unexpected controller list: %#v", controllers) } }
func TestEtcdListPods(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) key := "/registry/hosts/machine/pods" fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ { Value: util.MakeJSONString(api.Pod{JSONBase: api.JSONBase{ID: "foo"}}), }, { Value: util.MakeJSONString(api.Pod{JSONBase: api.JSONBase{ID: "bar"}}), }, }, }, }, E: nil, } registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) pods, err := registry.ListPods(labels.Everything()) if err != nil { t.Errorf("unexpected error: %v", err) } if len(pods) != 2 || pods[0].ID != "foo" || pods[1].ID != "bar" { t.Errorf("Unexpected pod list: %#v", pods) } if pods[0].CurrentState.Host != "machine" || pods[1].CurrentState.Host != "machine" { t.Errorf("Failed to populate host name.") } }
func TestEtcdListPods(t *testing.T) { fakeClient := MakeFakeEtcdClient(t) key := "/registry/hosts/machine/pods" fakeClient.Data[key] = EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ { Value: util.MakeJSONString(Pod{JSONBase: JSONBase{ID: "foo"}}), }, { Value: util.MakeJSONString(Pod{JSONBase: JSONBase{ID: "bar"}}), }, }, }, }, E: nil, } registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) pods, err := registry.ListPods(nil) expectNoError(t, err) if len(pods) != 2 || pods[0].ID != "foo" || pods[1].ID != "bar" { t.Errorf("Unexpected pod list: %#v", pods) } }
func TestWatchEtcd(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) ch := make(chan interface{}, 1) fakeClient.Data["/registry/hosts/machine/kubelet"] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Value: util.MakeJSONString([]api.Container{}), ModifiedIndex: 2, }, }, E: nil, } c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond} lastIndex, err := c.fetchNextState(1) if err != nil { t.Errorf("Unexpected error: %v", err) } if lastIndex != 3 { t.Errorf("Expected %d, Got %d", 3, lastIndex) } update := (<-ch).(kubelet.PodUpdate) expected := CreatePodUpdate(kubelet.SET) if !reflect.DeepEqual(expected, update) { t.Errorf("Expected %#v, Got %#v", expected, update) } }
func TestEtcdUpdateService(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) fakeClient.TestIndex = true resp, _ := fakeClient.Set("/registry/services/specs/foo", util.MakeJSONString(api.Service{JSONBase: api.JSONBase{ID: "foo"}}), 0) registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) testService := api.Service{ JSONBase: api.JSONBase{ID: "foo", ResourceVersion: resp.Node.ModifiedIndex}, Labels: map[string]string{ "baz": "bar", }, Selector: map[string]string{ "baz": "bar", }, } err := registry.UpdateService(testService) if err != nil { t.Errorf("unexpected error: %v", err) } svc, err := registry.GetService("foo") if err != nil { t.Errorf("unexpected error: %v", err) } // Clear modified indices before the equality test. svc.ResourceVersion = 0 testService.ResourceVersion = 0 if !reflect.DeepEqual(*svc, testService) { t.Errorf("Unexpected service: got\n %#v\n, wanted\n %#v", svc, testService) } }
func TestEtcdUpdateEndpoints(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) fakeClient.TestIndex = true registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) endpoints := api.Endpoints{ JSONBase: api.JSONBase{ID: "foo"}, Endpoints: []string{"baz", "bar"}, } fakeClient.Set("/registry/services/endpoints/foo", util.MakeJSONString(api.Endpoints{}), 0) err := registry.UpdateEndpoints(endpoints) if err != nil { t.Errorf("unexpected error: %v", err) } response, err := fakeClient.Get("/registry/services/endpoints/foo", false, false) if err != nil { t.Fatalf("Unexpected error %v", err) } var endpointsOut api.Endpoints err = api.DecodeInto([]byte(response.Node.Value), &endpointsOut) if !reflect.DeepEqual(endpoints, endpointsOut) { t.Errorf("Unexpected endpoints: %#v, expected %#v", endpointsOut, endpoints) } }
func TestEtcdDeletePod(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) fakeClient.TestIndex = true key := "/registry/hosts/machine/pods/foo" fakeClient.Set(key, util.MakeJSONString(api.Pod{JSONBase: api.JSONBase{ID: "foo"}}), 0) fakeClient.Set("/registry/hosts/machine/kubelet", api.EncodeOrDie(&api.ContainerManifestList{ Items: []api.ContainerManifest{ {ID: "foo"}, }, }), 0) registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) err := registry.DeletePod("foo") if err != nil { t.Errorf("unexpected error: %v", err) } if len(fakeClient.DeletedKeys) != 1 { t.Errorf("Expected 1 delete, found %#v", fakeClient.DeletedKeys) } else if fakeClient.DeletedKeys[0] != key { t.Errorf("Unexpected key: %s, expected %s", fakeClient.DeletedKeys[0], key) } response, err := fakeClient.Get("/registry/hosts/machine/kubelet", false, false) if err != nil { t.Fatalf("Unexpected error %v", err) } var manifests api.ContainerManifestList api.DecodeInto([]byte(response.Node.Value), &manifests) if len(manifests.Items) != 0 { t.Errorf("Unexpected container set: %s, expected empty", response.Node.Value) } }
func TestEtcdGetService(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) fakeClient.Set("/registry/services/specs/foo", util.MakeJSONString(api.Service{JSONBase: api.JSONBase{ID: "foo"}}), 0) registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) service, err := registry.GetService("foo") expectNoError(t, err) if service.ID != "foo" { t.Errorf("Unexpected pod: %#v", service) } }
func TestEtcdGetController(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) fakeClient.Set("/registry/controllers/foo", util.MakeJSONString(api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}), 0) registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) ctrl, err := registry.GetController("foo") expectNoError(t, err) if ctrl.ID != "foo" { t.Errorf("Unexpected controller: %#v", ctrl) } }
func TestEtcdGetPod(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) fakeClient.Set("/registry/hosts/machine/pods/foo", util.MakeJSONString(api.Pod{JSONBase: api.JSONBase{ID: "foo"}}), 0) registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) pod, err := registry.GetPod("foo") expectNoError(t, err) if pod.ID != "foo" { t.Errorf("Unexpected pod: %#v", pod) } }
func TestEtcdCreateServiceAlreadyExisting(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) fakeClient.Set("/registry/services/specs/foo", util.MakeJSONString(api.Service{JSONBase: api.JSONBase{ID: "foo"}}), 0) registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) err := registry.CreateService(api.Service{ JSONBase: api.JSONBase{ID: "foo"}, }) if !apiserver.IsAlreadyExists(err) { t.Errorf("expected already exists err, got %#v", err) } }
func TestEtcdDeletePod(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) key := "/registry/hosts/machine/pods/foo" fakeClient.Set(key, util.MakeJSONString(api.Pod{JSONBase: api.JSONBase{ID: "foo"}}), 0) fakeClient.Set("/registry/hosts/machine/kubelet", util.MakeJSONString([]api.ContainerManifest{ { ID: "foo", }, }), 0) registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) err := registry.DeletePod("foo") expectNoError(t, err) if len(fakeClient.DeletedKeys) != 1 { t.Errorf("Expected 1 delete, found %#v", fakeClient.DeletedKeys) } else if fakeClient.DeletedKeys[0] != key { t.Errorf("Unexpected key: %s, expected %s", fakeClient.DeletedKeys[0], key) } response, _ := fakeClient.Get("/registry/hosts/machine/kubelet", false, false) if response.Node.Value != "[]" { t.Errorf("Unexpected container set: %s, expected empty", response.Node.Value) } }
func TestWatchControllers(t *testing.T) { defer beginTimeout(20 * time.Second).done() fakeEtcd := tools.MakeFakeEtcdClient(t) manager := MakeReplicationManager(fakeEtcd, nil) var testControllerSpec api.ReplicationController received := make(chan bool) manager.syncHandler = func(controllerSpec api.ReplicationController) error { if !reflect.DeepEqual(controllerSpec, testControllerSpec) { t.Errorf("Expected %#v, but got %#v", testControllerSpec, controllerSpec) } close(received) return nil } go manager.watchControllers() fakeEtcd.WaitForWatchCompletion() // Test normal case testControllerSpec.ID = "foo" fakeEtcd.WatchResponse <- &etcd.Response{ Action: "set", Node: &etcd.Node{ Value: util.MakeJSONString(testControllerSpec), }, } select { case <-received: case <-time.After(10 * time.Millisecond): t.Errorf("Expected 1 call but got 0") } // Test error case fakeEtcd.WatchInjectError <- fmt.Errorf("Injected error") // Did everything shut down? if _, open := <-fakeEtcd.WatchResponse; open { t.Errorf("An injected error did not cause a graceful shutdown") } // Test purposeful shutdown go manager.watchControllers() fakeEtcd.WaitForWatchCompletion() fakeEtcd.WatchStop <- true // Did everything shut down? if _, open := <-fakeEtcd.WatchResponse; open { t.Errorf("A stop did not cause a graceful shutdown") } }
func TestExtractObj(t *testing.T) { fakeClient := MakeFakeEtcdClient(t) expect := testMarshalType{ID: "foo"} fakeClient.Set("/some/key", util.MakeJSONString(expect), 0) helper := EtcdHelper{fakeClient} var got testMarshalType err := helper.ExtractObj("/some/key", &got, false) if err != nil { t.Errorf("Unexpected error %#v", err) } if !reflect.DeepEqual(got, expect) { t.Errorf("Wanted %#v, got %#v", expect, got) } }
func TestSetObj(t *testing.T) { obj := testMarshalType{ID: "foo"} fakeClient := MakeFakeEtcdClient(t) helper := EtcdHelper{fakeClient} err := helper.SetObj("/some/key", obj) if err != nil { t.Errorf("Unexpected error %#v", err) } expect := util.MakeJSONString(obj) got := fakeClient.Data["/some/key"].R.Node.Value if expect != got { t.Errorf("Wanted %v, got %v", expect, got) } }
func TestEtcdCreateControllerAlreadyExisting(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) fakeClient.Set("/registry/controllers/foo", util.MakeJSONString(api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}), 0) registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) err := registry.CreateController(api.ReplicationController{ JSONBase: api.JSONBase{ ID: "foo", }, }) if !apiserver.IsAlreadyExists(err) { t.Errorf("expected already exists err, got %#v", err) } }
func TestExtractObj(t *testing.T) { fakeClient := MakeFakeEtcdClient(t) expect := api.Pod{JSONBase: api.JSONBase{ID: "foo"}} fakeClient.Set("/some/key", util.MakeJSONString(expect), 0) helper := EtcdHelper{fakeClient, codec, versioner} var got api.Pod err := helper.ExtractObj("/some/key", &got, false) if err != nil { t.Errorf("Unexpected error %#v", err) } if !reflect.DeepEqual(got, expect) { t.Errorf("Wanted %#v, got %#v", expect, got) } }
func TestResponseToManifests(t *testing.T) { kubelet, _, _ := makeTestKubelet(t) list, err := kubelet.ResponseToManifests(&etcd.Response{ Node: &etcd.Node{ Value: util.MakeJSONString([]api.ContainerManifest{ {ID: "foo"}, {ID: "bar"}, }), }, }) if len(list) != 2 || list[0].ID != "foo" || list[1].ID != "bar" { t.Errorf("Unexpected list: %#v", list) } expectNoError(t, err) }
func TestEtcdUpdateService(t *testing.T) { fakeClient := MakeFakeEtcdClient(t) fakeClient.Set("/registry/services/specs/foo", util.MakeJSONString(api.Service{JSONBase: api.JSONBase{ID: "foo"}}), 0) registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) err := registry.UpdateService(api.Service{ JSONBase: api.JSONBase{ID: "foo"}, Labels: map[string]string{ "baz": "bar", }, }) expectNoError(t, err) svc, err := registry.GetService("foo") if svc.Labels["baz"] != "bar" { t.Errorf("Unexpected service: %#v", svc) } }
func TestEtcdUpdateController(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) fakeClient.Set("/registry/controllers/foo", util.MakeJSONString(api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}), 0) registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) err := registry.UpdateController(api.ReplicationController{ JSONBase: api.JSONBase{ID: "foo"}, DesiredState: api.ReplicationControllerState{ Replicas: 2, }, }) expectNoError(t, err) ctrl, err := registry.GetController("foo") if ctrl.DesiredState.Replicas != 2 { t.Errorf("Unexpected controller: %#v", ctrl) } }
func TestEtcdCreatePodWithExistingContainers(t *testing.T) { fakeClient := MakeFakeEtcdClient(t) fakeClient.Data["/registry/hosts/machine/pods/foo"] = EtcdResponseWithError{ R: &etcd.Response{ Node: nil, }, E: &etcd.EtcdError{ErrorCode: 100}, } fakeClient.Set("/registry/hosts/machine/kubelet", util.MakeJSONString([]api.ContainerManifest{ { Id: "bar", }, }), 0) registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) err := registry.CreatePod("machine", api.Pod{ JSONBase: api.JSONBase{ ID: "foo", }, DesiredState: api.PodState{ Manifest: api.ContainerManifest{ Id: "foo", Containers: []api.Container{ { Name: "foo", }, }, }, }, }) expectNoError(t, err) resp, err := fakeClient.Get("/registry/hosts/machine/pods/foo", false, false) expectNoError(t, err) var pod api.Pod err = json.Unmarshal([]byte(resp.Node.Value), &pod) expectNoError(t, err) if pod.ID != "foo" { t.Errorf("Unexpected pod: %#v %s", pod, resp.Node.Value) } var manifests []api.ContainerManifest resp, err = fakeClient.Get("/registry/hosts/machine/kubelet", false, false) expectNoError(t, err) err = json.Unmarshal([]byte(resp.Node.Value), &manifests) if len(manifests) != 2 || manifests[1].Id != "foo" { t.Errorf("Unexpected manifest list: %#v", manifests) } }
func TestContainer(t *testing.T) { fw := makeServerTest() expected := api.ContainerManifest{Id: "test_manifest"} body := bytes.NewBuffer([]byte(util.MakeJSONString(expected))) resp, err := http.Post(fw.testHttpServer.URL+"/container", "application/json", body) if err != nil { t.Errorf("Post returned: %v", err) } resp.Body.Close() close(fw.updateChan) received := fw.updateReader.GetList() if len(received) != 1 { t.Errorf("Expected 1 manifest, but got %v", len(received)) } if !reflect.DeepEqual(expected, received[0]) { t.Errorf("Expected %#v, but got %#v", expected, received[0]) } }
func TestGetKubeletStateFromEtcd(t *testing.T) { kubelet, fakeClient, _ := makeTestKubelet(t) channel := make(chan manifestUpdate) reader := startReading(channel) fakeClient.Data["/registry/hosts/machine/kubelet"] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Value: util.MakeJSONString([]api.Container{}), }, }, E: nil, } err := kubelet.getKubeletStateFromEtcd("/registry/hosts/machine/kubelet", channel) expectNoError(t, err) close(channel) list := reader.GetList() if len(list) != 1 { t.Errorf("Unexpected list: %#v", list) } }
func TestEtcdCreatePodAlreadyExisting(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) fakeClient.Data["/registry/hosts/machine/pods/foo"] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Value: util.MakeJSONString(api.Pod{JSONBase: api.JSONBase{ID: "foo"}}), }, }, E: nil, } registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) err := registry.CreatePod("machine", api.Pod{ JSONBase: api.JSONBase{ ID: "foo", }, }) if err == nil { t.Error("Unexpected non-error") } }
func TestEtcdUpdateService(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) fakeClient.Set("/registry/services/specs/foo", util.MakeJSONString(api.Service{JSONBase: api.JSONBase{ID: "foo"}}), 0) registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) testService := api.Service{ JSONBase: api.JSONBase{ID: "foo"}, Labels: map[string]string{ "baz": "bar", }, Selector: map[string]string{ "baz": "bar", }, } err := registry.UpdateService(testService) expectNoError(t, err) svc, err := registry.GetService("foo") expectNoError(t, err) if !reflect.DeepEqual(*svc, testService) { t.Errorf("Unexpected service: got %#v, wanted %#v", svc, testService) } }
func TestEtcdUpdateController(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) fakeClient.TestIndex = true resp, _ := fakeClient.Set("/registry/controllers/foo", util.MakeJSONString(api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}), 0) registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) err := registry.UpdateController(api.ReplicationController{ JSONBase: api.JSONBase{ID: "foo", ResourceVersion: resp.Node.ModifiedIndex}, DesiredState: api.ReplicationControllerState{ Replicas: 2, }, }) if err != nil { t.Errorf("unexpected error: %v", err) } ctrl, err := registry.GetController("foo") if ctrl.DesiredState.Replicas != 2 { t.Errorf("Unexpected controller: %#v", ctrl) } }
func TestContainers(t *testing.T) { fw := makeServerTest() expected := []api.ContainerManifest{ {ID: "test_manifest_1"}, {ID: "test_manifest_2"}, } body := bytes.NewBuffer([]byte(util.MakeJSONString(expected))) resp, err := http.Post(fw.testHTTPServer.URL+"/containers", "application/json", body) if err != nil { t.Errorf("Post returned: %v", err) } resp.Body.Close() close(fw.updateChan) received := fw.updateReader.GetList() if len(received) != 1 { t.Errorf("Expected 1 update, but got %v", len(received)) } expectedPods := []Pod{Pod{Name: "1", Manifest: expected[0]}, Pod{Name: "2", Manifest: expected[1]}} if !reflect.DeepEqual(expectedPods, received[0]) { t.Errorf("Expected %#v, but got %#v", expectedPods, received[0]) } }
func TestGetEtcd(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) ch := make(chan interface{}, 1) fakeClient.Data["/registry/hosts/machine/kubelet"] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Value: util.MakeJSONString([]api.ContainerManifest{api.ContainerManifest{ID: "foo"}}), ModifiedIndex: 1, }, }, E: nil, } c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond} lastIndex, err := c.fetchNextState(0) expectNoError(t, err) if lastIndex != 2 { t.Errorf("Expected %#v, Got %#v", 2, lastIndex) } update := (<-ch).(kubelet.PodUpdate) expected := CreatePodUpdate(kubelet.SET, kubelet.Pod{Name: "foo", Manifest: api.ContainerManifest{ID: "foo"}}) if !reflect.DeepEqual(expected, update) { t.Errorf("Expected %#v, Got %#v", expected, update) } }