func TestPodDecode(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) storage := REST{ registry: podRegistry, podCache: &fakeCache{statusToReturn: &api.PodStatus{}}, } expected := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "foo", }, } body, err := latest.Codec.Encode(expected) if err != nil { t.Errorf("unexpected error: %v", err) } actual := storage.New() if err := latest.Codec.DecodeInto(body, actual); err != nil { t.Errorf("unexpected error: %v", err) } if !reflect.DeepEqual(expected, actual) { t.Errorf("Expected %#v, Got %#v", expected, actual) } }
func TestPodUpdateAllContainers(t *testing.T) { pod := api.Pod{ ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: api.NamespaceDefault}, Status: api.PodStatus{ Host: "machine", }, } pods := []api.Pod{pod} mockRegistry := registrytest.NewPodRegistry(&api.PodList{Items: pods}) expected := api.PodInfo{ "foo": api.ContainerStatus{}, } fake := FakePodInfoGetter{ data: expected, } cache := NewPodCache(&fake, mockRegistry) cache.UpdateAllContainers() if fake.host != "machine" || fake.id != "foo" || fake.namespace != api.NamespaceDefault { t.Errorf("Unexpected access: %#v", fake) } info, err := cache.GetPodInfo("machine", api.NamespaceDefault, "foo") if err != nil { t.Errorf("Unexpected error: %#v", err) } if !reflect.DeepEqual(info, expected) { t.Errorf("Unexpected mismatch. Expected: %#v, Got: #%v", &expected, info) } }
func TestCreatePod(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Pod = &api.Pod{ ObjectMeta: api.ObjectMeta{Name: "foo"}, Status: api.PodStatus{ Host: "machine", }, } storage := REST{ registry: podRegistry, podCache: &fakeCache{statusToReturn: &api.PodStatus{}}, } pod := &api.Pod{} pod.Name = "foo" ctx := api.NewDefaultContext() channel, err := storage.Create(ctx, pod) if err != nil { t.Errorf("unexpected error: %v", err) } select { case <-channel: // Do nothing, this is expected. case <-time.After(time.Millisecond * 100): t.Error("Unexpected timeout on async channel") } if !api.HasObjectMetaSystemFieldValues(&podRegistry.Pod.ObjectMeta) { t.Errorf("Expected ObjectMeta field values were populated") } }
func TestListPodList(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Pods = &api.PodList{ Items: []api.Pod{ { JSONBase: api.JSONBase{ ID: "foo", }, }, { JSONBase: api.JSONBase{ ID: "bar", }, }, }, } storage := RegistryStorage{ registry: podRegistry, } podsObj, err := storage.List(labels.Everything()) pods := podsObj.(*api.PodList) if err != nil { t.Errorf("unexpected error: %v", err) } if len(pods.Items) != 2 { t.Errorf("Unexpected pod list: %#v", pods) } if pods.Items[0].ID != "foo" { t.Errorf("Unexpected pod: %#v", pods.Items[0]) } if pods.Items[1].ID != "bar" { t.Errorf("Unexpected pod: %#v", pods.Items[1]) } }
func TestPodUpdateAllContainers(t *testing.T) { pod := api.Pod{ JSONBase: api.JSONBase{ID: "foo"}, CurrentState: api.PodState{ Host: "machine", }, } pods := []api.Pod{pod} mockRegistry := registrytest.NewPodRegistry(&api.PodList{Items: pods}) expected := api.PodInfo{"foo": docker.Container{ID: "foo"}} fake := FakePodInfoGetter{ data: expected, } cache := NewPodCache(&fake, mockRegistry) cache.UpdateAllContainers() if fake.host != "machine" || fake.id != "foo" { t.Errorf("Unexpected access: %#v", fake) } info, err := cache.GetPodInfo("machine", "foo") if err != nil { t.Errorf("Unexpected error: %#v", err) } if !reflect.DeepEqual(info, expected) { t.Errorf("Unexpected mismatch. Expected: %#v, Got: #%v", &expected, info) } }
func TestCreatePod(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Pod = &api.Pod{ JSONBase: api.JSONBase{ID: "foo"}, CurrentState: api.PodState{ Host: "machine", }, } storage := RegistryStorage{ registry: podRegistry, podPollPeriod: time.Millisecond * 100, } desiredState := api.PodState{ Manifest: api.ContainerManifest{ Version: "v1beta1", }, } pod := &api.Pod{ JSONBase: api.JSONBase{ID: "foo"}, DesiredState: desiredState, } channel, err := storage.Create(pod) if err != nil { t.Errorf("unexpected error: %v", err) } select { case <-channel: // Do nothing, this is expected. case <-time.After(time.Millisecond * 100): t.Error("Unexpected timeout on async channel") } }
func TestCreatePodSetsIds(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Err = fmt.Errorf("test error") storage := RegistryStorage{ registry: podRegistry, } desiredState := api.PodState{ Manifest: api.ContainerManifest{ Version: "v1beta1", }, } pod := &api.Pod{DesiredState: desiredState} ch, err := storage.Create(pod) if err != nil { t.Errorf("Expected %#v, Got %#v", nil, err) } expectApiStatusError(t, ch, podRegistry.Err.Error()) if len(podRegistry.Pod.ID) == 0 { t.Errorf("Expected pod ID to be set, Got %#v", pod) } if podRegistry.Pod.DesiredState.Manifest.ID != podRegistry.Pod.ID { t.Errorf("Expected manifest ID to be equal to pod ID, Got %#v", pod) } }
func TestListPodsCacheError(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Pods = &api.PodList{ Items: []api.Pod{ { ObjectMeta: api.ObjectMeta{ Name: "foo", }, }, }, } storage := REST{ registry: podRegistry, podCache: &fakeCache{errorToReturn: client.ErrPodInfoNotAvailable}, } ctx := api.NewContext() pods, err := storage.List(ctx, labels.Everything(), labels.Everything()) if err != nil { t.Fatalf("Expected no error, got %#v", err) } pl := pods.(*api.PodList) if len(pl.Items) != 1 { t.Fatalf("Unexpected 0-len pod list: %+v", pl) } if e, a := api.PodUnknown, pl.Items[0].Status.Phase; e != a { t.Errorf("Expected %v, got %v", e, a) } }
func TestGetPod(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Pod = &api.Pod{ ObjectMeta: api.ObjectMeta{Name: "foo"}, Status: api.PodStatus{Host: "machine"}, } storage := REST{ registry: podRegistry, podCache: &fakeCache{statusToReturn: &api.PodStatus{Phase: api.PodRunning}}, } ctx := api.NewContext() obj, err := storage.Get(ctx, "foo") pod := obj.(*api.Pod) if err != nil { t.Errorf("unexpected error: %v", err) } expect := *podRegistry.Pod expect.Status.Phase = api.PodRunning // TODO: when host is moved to spec, remove this line. expect.Status.Host = "machine" if e, a := &expect, pod; !reflect.DeepEqual(e, a) { t.Errorf("Unexpected pod. Expected %#v, Got %#v", e, a) } }
func TestListEmptyPodList(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) storage := RegistryStorage{ registry: podRegistry, } pods, err := storage.List(labels.Everything()) if err != nil { t.Errorf("unexpected error: %v", err) } if len(pods.(api.PodList).Items) != 0 { t.Errorf("Unexpected non-zero pod list: %#v", pods) } }
func TestCreatePodRegistryError(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Err = fmt.Errorf("test error") storage := REST{ registry: podRegistry, } pod := &api.Pod{} ctx := api.NewDefaultContext() ch, err := storage.Create(ctx, pod) if err != nil { t.Errorf("Expected %#v, Got %#v", nil, err) } expectApiStatusError(t, ch, podRegistry.Err.Error()) }
func TestListPodsError(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Err = fmt.Errorf("test error") storage := RegistryStorage{ registry: podRegistry, } pods, err := storage.List(labels.Everything()) if err != podRegistry.Err { t.Errorf("Expected %#v, Got %#v", podRegistry.Err, err) } if pods.(*api.PodList) != nil { t.Errorf("Unexpected non-nil pod list: %#v", pods) } }
func TestPodStorageValidatesUpdate(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Err = fmt.Errorf("test error") storage := RegistryStorage{ registry: podRegistry, } pod := &api.Pod{} c, err := storage.Update(pod) if c != nil { t.Errorf("Expected nil channel") } if err == nil { t.Errorf("Expected to get an error") } }
func TestPodStorageValidatesUpdate(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Err = fmt.Errorf("test error") storage := RegistryStorage{ registry: podRegistry, } pod := &api.Pod{} c, err := storage.Update(pod) if c != nil { t.Errorf("Expected nil channel") } if !apiserver.IsInvalid(err) { t.Errorf("Expected to get an invalid resource error, got %v", err) } }
func TestPodStorageValidatesUpdate(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Err = fmt.Errorf("test error") storage := REST{ registry: podRegistry, } ctx := api.NewDefaultContext() pod := &api.Pod{} c, err := storage.Update(ctx, pod) if c != nil { t.Errorf("Expected nil channel") } if !errors.IsInvalid(err) { t.Errorf("Expected to get an invalid resource error, got %v", err) } }
func TestListPodsError(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Err = fmt.Errorf("test error") storage := REST{ registry: podRegistry, podCache: &fakeCache{statusToReturn: &api.PodStatus{}}, } ctx := api.NewContext() pods, err := storage.List(ctx, labels.Everything(), labels.Everything()) if err != podRegistry.Err { t.Errorf("Expected %#v, Got %#v", podRegistry.Err, err) } if pods.(*api.PodList) != nil { t.Errorf("Unexpected non-nil pod list: %#v", pods) } }
func TestGetPod(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Pod = &api.Pod{JSONBase: api.JSONBase{ID: "foo"}} storage := RegistryStorage{ registry: podRegistry, } obj, err := storage.Get("foo") pod := obj.(*api.Pod) if err != nil { t.Errorf("unexpected error: %v", err) } if e, a := podRegistry.Pod, pod; !reflect.DeepEqual(e, a) { t.Errorf("Unexpected pod. Expected %#v, Got %#v", e, a) } }
func TestPodStorageValidatesCreate(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Err = fmt.Errorf("test error") storage := RegistryStorage{ scheduler: ®istrytest.Scheduler{Machine: "test"}, registry: podRegistry, } pod := &api.Pod{} c, err := storage.Create(pod) if c != nil { t.Errorf("Expected nil channel") } if err == nil { t.Errorf("Expected to get an error") } }
func TestListEmptyPodList(t *testing.T) { podRegistry := registrytest.NewPodRegistry(&api.PodList{JSONBase: api.JSONBase{ResourceVersion: 1}}) storage := RegistryStorage{ registry: podRegistry, } pods, err := storage.List(labels.Everything()) if err != nil { t.Errorf("unexpected error: %v", err) } if len(pods.(*api.PodList).Items) != 0 { t.Errorf("Unexpected non-zero pod list: %#v", pods) } if pods.(*api.PodList).ResourceVersion != 1 { t.Errorf("Unexpected resource version: %#v", pods) } }
func TestCreatePodRegistryError(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Err = fmt.Errorf("test error") storage := RegistryStorage{ registry: podRegistry, } desiredState := api.PodState{ Manifest: api.ContainerManifest{ Version: "v1beta1", }, } pod := &api.Pod{DesiredState: desiredState} ch, err := storage.Create(pod) if err != nil { t.Errorf("Expected %#v, Got %#v", nil, err) } expectApiStatusError(t, ch, podRegistry.Err.Error()) }
func TestListEmptyPodList(t *testing.T) { podRegistry := registrytest.NewPodRegistry(&api.PodList{ListMeta: api.ListMeta{ResourceVersion: "1"}}) storage := REST{ registry: podRegistry, } ctx := api.NewContext() pods, err := storage.List(ctx, labels.Everything(), labels.Everything()) if err != nil { t.Errorf("unexpected error: %v", err) } if len(pods.(*api.PodList).Items) != 0 { t.Errorf("Unexpected non-zero pod list: %#v", pods) } if pods.(*api.PodList).ResourceVersion != "1" { t.Errorf("Unexpected resource version: %#v", pods) } }
func TestGetPodCloud(t *testing.T) { fakeCloud := &fake_cloud.FakeCloud{} podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Pod = &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}, CurrentState: api.PodState{Host: "machine"}} clock := &fakeClock{t: time.Now()} storage := REST{ registry: podRegistry, cloudProvider: fakeCloud, ipCache: ipCache{}, clock: clock, } ctx := api.NewContext() obj, err := storage.Get(ctx, "foo") pod := obj.(*api.Pod) if err != nil { t.Errorf("unexpected error: %v", err) } if e, a := podRegistry.Pod, pod; !reflect.DeepEqual(e, a) { t.Errorf("Unexpected pod. Expected %#v, Got %#v", e, a) } // This call should hit the cache, so we expect no additional calls to the cloud obj, err = storage.Get(ctx, "foo") if err != nil { t.Errorf("unexpected error: %v", err) } if len(fakeCloud.Calls) != 1 || fakeCloud.Calls[0] != "ip-address" { t.Errorf("Unexpected calls: %#v", fakeCloud.Calls) } // Advance the clock, this call should miss the cache, so expect one more call. clock.t = clock.t.Add(60 * time.Second) obj, err = storage.Get(ctx, "foo") if err != nil { t.Errorf("unexpected error: %v", err) } if len(fakeCloud.Calls) != 2 || fakeCloud.Calls[1] != "ip-address" { t.Errorf("Unexpected calls: %#v", fakeCloud.Calls) } }
func TestCreatePodSetsUID(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Err = fmt.Errorf("test error") storage := REST{ registry: podRegistry, podCache: &fakeCache{statusToReturn: &api.PodStatus{}}, } pod := &api.Pod{} ctx := api.NewDefaultContext() ch, err := storage.Create(ctx, pod) if err != nil { t.Errorf("Expected %#v, Got %#v", nil, err) } expectApiStatusError(t, ch, podRegistry.Err.Error()) if len(podRegistry.Pod.UID) == 0 { t.Errorf("Expected pod UID to be set, Got %#v", pod) } }
func TestGetPod(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Pod = &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}} storage := REST{ registry: podRegistry, ipCache: ipCache{}, clock: &fakeClock{}, } ctx := api.NewContext() obj, err := storage.Get(ctx, "foo") pod := obj.(*api.Pod) if err != nil { t.Errorf("unexpected error: %v", err) } if e, a := podRegistry.Pod, pod; !reflect.DeepEqual(e, a) { t.Errorf("Unexpected pod. Expected %#v, Got %#v", e, a) } }
func (c *podCacheTestConfig) Construct() *PodCache { c.fakePodInfo = &FakePodInfoGetter{ data: api.PodStatusResult{ Status: c.kubeletContainerInfo, }, } c.fakeNodes = &client.Fake{ MinionsList: api.NodeList{ Items: c.nodes, }, } c.fakePods = registrytest.NewPodRegistry(&api.PodList{Items: c.pods}) c.fakePods.Pod = c.pod c.fakePods.Err = c.err return NewPodCache( c.fakePodInfo, c.fakeNodes.Nodes(), c.fakePods, ) }
func TestGetPodCacheError(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Pod = &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}} storage := REST{ registry: podRegistry, podCache: &fakeCache{errorToReturn: client.ErrPodInfoNotAvailable}, } ctx := api.NewContext() obj, err := storage.Get(ctx, "foo") pod := obj.(*api.Pod) if err != nil { t.Errorf("unexpected error: %v", err) } expect := *podRegistry.Pod expect.Status.Phase = api.PodUnknown if e, a := &expect, pod; !reflect.DeepEqual(e, a) { t.Errorf("Unexpected pod. Expected %#v, Got %#v", e, a) } }
func TestGetPodCloud(t *testing.T) { fakeCloud := &fake_cloud.FakeCloud{} podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Pod = &api.Pod{JSONBase: api.JSONBase{ID: "foo"}} storage := RegistryStorage{ registry: podRegistry, cloudProvider: fakeCloud, } obj, err := storage.Get("foo") pod := obj.(*api.Pod) if err != nil { t.Errorf("unexpected error: %v", err) } if e, a := podRegistry.Pod, pod; !reflect.DeepEqual(e, a) { t.Errorf("Unexpected pod. Expected %#v, Got %#v", e, a) } if len(fakeCloud.Calls) != 1 || fakeCloud.Calls[0] != "ip-address" { t.Errorf("Unexpected calls: %#v", fakeCloud.Calls) } }
func TestCreatePodSetsIds(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Err = fmt.Errorf("test error") storage := REST{ registry: podRegistry, } pod := &api.Pod{} ctx := api.NewDefaultContext() ch, err := storage.Create(ctx, pod) if err != nil { t.Errorf("Expected %#v, Got %#v", nil, err) } expectApiStatusError(t, ch, podRegistry.Err.Error()) if len(podRegistry.Pod.Name) == 0 { t.Errorf("Expected pod ID to be set, Got %#v", pod) } if pod.Name != podRegistry.Pod.Name { t.Errorf("Expected manifest ID to be equal to pod ID, Got %#v", pod) } }
func TestPodStorageValidatesCreate(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Err = fmt.Errorf("test error") storage := REST{ registry: podRegistry, } ctx := api.NewDefaultContext() pod := &api.Pod{ ObjectMeta: api.ObjectMeta{ Labels: map[string]string{ "invalid-label-to-cause-validation-failure": "bar", }, }, } c, err := storage.Create(ctx, pod) if c != nil { t.Errorf("Expected nil channel") } if !errors.IsInvalid(err) { t.Errorf("Expected to get an invalid resource error, got %v", err) } }
func TestListPodList(t *testing.T) { podRegistry := registrytest.NewPodRegistry(nil) podRegistry.Pods = &api.PodList{ Items: []api.Pod{ { ObjectMeta: api.ObjectMeta{ Name: "foo", }, }, { ObjectMeta: api.ObjectMeta{ Name: "bar", }, }, }, } storage := REST{ registry: podRegistry, ipCache: ipCache{}, clock: &fakeClock{}, } ctx := api.NewContext() podsObj, err := storage.List(ctx, labels.Everything(), labels.Everything()) pods := podsObj.(*api.PodList) if err != nil { t.Errorf("unexpected error: %v", err) } if len(pods.Items) != 2 { t.Errorf("Unexpected pod list: %#v", pods) } if pods.Items[0].Name != "foo" { t.Errorf("Unexpected pod: %#v", pods.Items[0]) } if pods.Items[1].Name != "bar" { t.Errorf("Unexpected pod: %#v", pods.Items[1]) } }