func TestListPopulatedList(t *testing.T) { fakeEtcdClient, helper := newHelper(t) fakeEtcdClient.ChangeIndex = 1 fakeEtcdClient.Data["/images"] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ {Value: runtime.EncodeOrDie(latest.Codec, &api.Image{ObjectMeta: kapi.ObjectMeta{Name: "foo"}})}, {Value: runtime.EncodeOrDie(latest.Codec, &api.Image{ObjectMeta: kapi.ObjectMeta{Name: "bar"}})}, }, }, }, } storage := NewREST(helper) list, err := storage.List(kapi.NewDefaultContext(), labels.Everything(), fields.Everything()) if err != nil { t.Errorf("Unexpected non-nil error: %#v", err) } images := list.(*api.ImageList) if e, a := 2, len(images.Items); e != a { t.Errorf("Expected %v, got %v", e, a) } }
func TestEtcdListPersistentVolumes(t *testing.T) { ctx := api.NewContext() storage, _, fakeClient, _ := newStorage(t) key := storage.KeyRootFunc(ctx) key = etcdtest.AddPrefix(key) fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ { Value: runtime.EncodeOrDie(latest.Codec, validNewPersistentVolume("foo")), }, { Value: runtime.EncodeOrDie(latest.Codec, validNewPersistentVolume("bar")), }, }, }, }, E: nil, } pvObj, err := storage.List(ctx, labels.Everything(), fields.Everything()) if err != nil { t.Errorf("unexpected error: %v", err) } pvs := pvObj.(*api.PersistentVolumeList) if len(pvs.Items) != 2 || pvs.Items[0].Name != "foo" || pvs.Items[1].Name != "bar" { t.Errorf("Unexpected persistentVolume list: %#v", pvs) } }
func TestListEmptyResourceQuotaList(t *testing.T) { fakeEtcdClient, etcdStorage := newEtcdStorage(t) fakeEtcdClient.ChangeIndex = 1 storage, _ := NewStorage(etcdStorage) ctx := api.NewContext() key := storage.Etcd.KeyRootFunc(ctx) key = etcdtest.AddPrefix(key) fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{}, E: fakeEtcdClient.NewError(tools.EtcdErrorCodeNotFound), } resourcequotas, err := storage.List(api.NewContext(), labels.Everything(), fields.Everything()) if err != nil { t.Fatalf("unexpected error: %v", err) } if len(resourcequotas.(*api.ResourceQuotaList).Items) != 0 { t.Errorf("Unexpected non-zero resourcequota list: %#v", resourcequotas) } if resourcequotas.(*api.ResourceQuotaList).ResourceVersion != "1" { t.Errorf("Unexpected resource version: %#v", resourcequotas) } }
func TestEtcdListNodes(t *testing.T) { ctx := api.NewContext() storage, fakeClient := newStorage(t) key := storage.KeyRootFunc(ctx) key = etcdtest.AddPrefix(key) fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ { Value: runtime.EncodeOrDie(latest.Codec, &api.Node{ ObjectMeta: api.ObjectMeta{Name: "foo"}, }), }, { Value: runtime.EncodeOrDie(latest.Codec, &api.Node{ ObjectMeta: api.ObjectMeta{Name: "bar"}, }), }, }, }, }, E: nil, } nodesObj, err := storage.List(ctx, labels.Everything(), fields.Everything()) if err != nil { t.Errorf("unexpected error: %v", err) } nodes := nodesObj.(*api.NodeList) if len(nodes.Items) != 2 || nodes.Items[0].Name != "foo" || nodes.Items[1].Name != "bar" { t.Errorf("Unexpected nodes list: %#v", nodes) } }
func TestEtcdListControllers(t *testing.T) { storage, fakeClient := newStorage(t) ctx := api.NewDefaultContext() key := makeControllerListKey(ctx) key = etcdtest.AddPrefix(key) controller := validController controller.Name = "bar" fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ { Value: runtime.EncodeOrDie(latest.Codec, &validController), }, { Value: runtime.EncodeOrDie(latest.Codec, &controller), }, }, }, }, E: nil, } objList, err := storage.List(ctx, labels.Everything(), fields.Everything()) if err != nil { t.Errorf("unexpected error: %v", err) } controllers, _ := objList.(*api.DaemonList) if len(controllers.Items) != 2 || controllers.Items[0].Name != validController.Name || controllers.Items[1].Name != controller.Name { t.Errorf("Unexpected controller list: %#v", controllers) } }
func TestListImageStreamsPopulatedList(t *testing.T) { fakeEtcdClient, helper := newHelper(t) storage, _ := NewREST(helper, noDefaultRegistry, &fakeSubjectAccessReviewRegistry{}) fakeEtcdClient.Data["/imagestreams/default"] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ {Value: runtime.EncodeOrDie(latest.Codec, &api.ImageStream{ObjectMeta: kapi.ObjectMeta{Name: "foo"}})}, {Value: runtime.EncodeOrDie(latest.Codec, &api.ImageStream{ObjectMeta: kapi.ObjectMeta{Name: "bar"}})}, }, }, }, } list, err := storage.List(kapi.NewDefaultContext(), labels.Everything(), fields.Everything()) if err != nil { t.Fatalf("Unexpected non-nil error: %#v", err) } imageStreams := list.(*api.ImageStreamList) if e, a := 2, len(imageStreams.Items); e != a { t.Errorf("Expected %v, got %v", e, a) } }
func TestListError(t *testing.T) { fakeEtcdClient, helper := newHelper(t) fakeEtcdClient.Err = fmt.Errorf("test error") storage := NewREST(helper) images, err := storage.List(kapi.NewDefaultContext(), labels.Everything(), fields.Everything()) if err != fakeEtcdClient.Err { t.Fatalf("Expected %#v, Got %#v", fakeEtcdClient.Err, err) } if images != nil { t.Errorf("Unexpected non-nil image list: %#v", images) } }
func TestListError(t *testing.T) { fakeEtcdClient, etcdStorage := newEtcdStorage(t) fakeEtcdClient.Err = fmt.Errorf("test error") storage := NewStorage(etcdStorage, nil).Pod pods, err := storage.List(api.NewDefaultContext(), labels.Everything(), fields.Everything()) if err != fakeEtcdClient.Err { t.Fatalf("Expected %#v, Got %#v", fakeEtcdClient.Err, err) } if pods != nil { t.Errorf("Unexpected non-nil pod list: %#v", pods) } }
func TestListImageStreamsError(t *testing.T) { fakeEtcdClient, helper := newHelper(t) fakeEtcdClient.Err = fmt.Errorf("foo") storage, _ := NewREST(helper, noDefaultRegistry, &fakeSubjectAccessReviewRegistry{}) imageStreams, err := storage.List(kapi.NewDefaultContext(), nil, nil) if err != fakeEtcdClient.Err { t.Errorf("Expected %#v, Got %#v", fakeEtcdClient.Err, err) } if imageStreams != nil { t.Errorf("Unexpected non-nil imageStreams list: %#v", imageStreams) } }
func TestListPodList(t *testing.T) { fakeEtcdClient, etcdStorage := newEtcdStorage(t) ctx := api.NewDefaultContext() storage := NewStorage(etcdStorage, nil).Pod key := storage.Etcd.KeyRootFunc(ctx) key = etcdtest.AddPrefix(key) fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ { Value: runtime.EncodeOrDie(latest.Codec, &api.Pod{ ObjectMeta: api.ObjectMeta{Name: "foo"}, Spec: api.PodSpec{NodeName: "machine"}, Status: api.PodStatus{Phase: api.PodRunning}, }), }, { Value: runtime.EncodeOrDie(latest.Codec, &api.Pod{ ObjectMeta: api.ObjectMeta{Name: "bar"}, Spec: api.PodSpec{NodeName: "machine"}, }), }, }, }, }, } podsObj, err := storage.List(api.NewDefaultContext(), labels.Everything(), fields.Everything()) pods := podsObj.(*api.PodList) if err != nil { t.Fatalf("unexpected error: %v", err) } if len(pods.Items) != 2 { t.Errorf("Unexpected pod list: %#v", pods) } if pods.Items[0].Name != "foo" || pods.Items[0].Status.Phase != api.PodRunning || pods.Items[0].Spec.NodeName != "machine" { t.Errorf("Unexpected pod: %#v", pods.Items[0]) } if pods.Items[1].Name != "bar" || pods.Items[1].Spec.NodeName != "machine" { t.Errorf("Unexpected pod: %#v", pods.Items[1]) } }
func TestEtcdListControllersNotFound(t *testing.T) { storage, fakeClient := newStorage(t) ctx := api.NewDefaultContext() key := makeControllerListKey(ctx) key = etcdtest.AddPrefix(key) fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{}, E: tools.EtcdErrorNotFound, } objList, err := storage.List(ctx, labels.Everything(), fields.Everything()) if err != nil { t.Errorf("unexpected error: %v", err) } controllers, _ := objList.(*api.DaemonList) if len(controllers.Items) != 0 { t.Errorf("Unexpected controller list: %#v", controllers) } }
func TestListEmptyList(t *testing.T) { fakeEtcdClient, helper := newHelper(t) fakeEtcdClient.ChangeIndex = 1 fakeEtcdClient.Data["/images"] = tools.EtcdResponseWithError{ R: &etcd.Response{}, E: fakeEtcdClient.NewError(tools.EtcdErrorCodeNotFound), } storage := NewREST(helper) images, err := storage.List(kapi.NewDefaultContext(), labels.Everything(), fields.Everything()) if err != nil { t.Errorf("Unexpected non-nil error: %#v", err) } if len(images.(*api.ImageList).Items) != 0 { t.Errorf("Unexpected non-zero images list: %#v", images) } if images.(*api.ImageList).ResourceVersion != "1" { t.Errorf("Unexpected resource version: %#v", images) } }
func TestListImageStreamsEmptyList(t *testing.T) { fakeEtcdClient, helper := newHelper(t) fakeEtcdClient.ChangeIndex = 1 fakeEtcdClient.Data[etcdtest.AddPrefix("/imagestreams/default")] = tools.EtcdResponseWithError{ R: &etcd.Response{}, E: fakeEtcdClient.NewError(tools.EtcdErrorCodeNotFound), } storage, _ := NewREST(helper, noDefaultRegistry, &fakeSubjectAccessReviewRegistry{}) imageStreams, err := storage.List(kapi.NewDefaultContext(), labels.Everything(), fields.Everything()) if err != nil { t.Fatalf("Unexpected non-nil error: %#v", err) } if len(imageStreams.(*api.ImageStreamList).Items) != 0 { t.Errorf("Unexpected non-zero imageStreams list: %#v", imageStreams) } if imageStreams.(*api.ImageStreamList).ResourceVersion != "1" { t.Errorf("Unexpected resource version: %#v", imageStreams) } }
func TestListPersistentVolumesList(t *testing.T) { ctx := api.NewContext() storage, _, fakeClient, _ := newStorage(t) fakeClient.ChangeIndex = 1 key := storage.KeyRootFunc(ctx) key = etcdtest.AddPrefix(key) fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ { Value: runtime.EncodeOrDie(latest.Codec, &api.PersistentVolume{ ObjectMeta: api.ObjectMeta{Name: "foo"}, }), }, { Value: runtime.EncodeOrDie(latest.Codec, &api.PersistentVolume{ ObjectMeta: api.ObjectMeta{Name: "bar"}, }), }, }, }, }, } persistentVolumeObj, err := storage.List(ctx, labels.Everything(), fields.Everything()) persistentVolumeList := persistentVolumeObj.(*api.PersistentVolumeList) if err != nil { t.Fatalf("unexpected error: %v", err) } if len(persistentVolumeList.Items) != 2 { t.Errorf("Unexpected persistentVolume list: %#v", persistentVolumeList) } if persistentVolumeList.Items[0].Name != "foo" { t.Errorf("Unexpected persistentVolume: %#v", persistentVolumeList.Items[0]) } if persistentVolumeList.Items[1].Name != "bar" { t.Errorf("Unexpected persistentVolume: %#v", persistentVolumeList.Items[1]) } }
func TestListEndpointsList(t *testing.T) { ctx := api.NewDefaultContext() storage, fakeClient := newStorage(t) fakeClient.ChangeIndex = 1 key := storage.KeyRootFunc(ctx) key = etcdtest.AddPrefix(key) fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ { Value: runtime.EncodeOrDie(latest.Codec, &api.Endpoints{ ObjectMeta: api.ObjectMeta{Name: "foo"}, }), }, { Value: runtime.EncodeOrDie(latest.Codec, &api.Endpoints{ ObjectMeta: api.ObjectMeta{Name: "bar"}, }), }, }, }, }, } endpointsObj, err := storage.List(ctx, labels.Everything(), fields.Everything()) endpoints := endpointsObj.(*api.EndpointsList) if err != nil { t.Fatalf("unexpected error: %v", err) } if len(endpoints.Items) != 2 { t.Errorf("Unexpected endpoints list: %#v", endpoints) } if endpoints.Items[0].Name != "foo" { t.Errorf("Unexpected endpoints: %#v", endpoints.Items[0]) } if endpoints.Items[1].Name != "bar" { t.Errorf("Unexpected endpoints: %#v", endpoints.Items[1]) } }
func TestListEmptyNamespaceList(t *testing.T) { fakeEtcdClient, etcdStorage := newEtcdStorage(t) fakeEtcdClient.ChangeIndex = 1 key := etcdtest.AddPrefix("/namespaces") fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{}, E: fakeEtcdClient.NewError(tools.EtcdErrorCodeNotFound), } storage, _, _ := NewStorage(etcdStorage) namespaces, err := storage.List(api.NewContext(), labels.Everything(), fields.Everything()) if err != nil { t.Errorf("Unexpected error: %v", err) } if len(namespaces.(*api.NamespaceList).Items) != 0 { t.Errorf("Unexpected non-zero namespace list: %#v", namespaces) } if namespaces.(*api.NamespaceList).ResourceVersion != "1" { t.Errorf("Unexpected resource version: %#v", namespaces) } }
func TestListResourceQuotaList(t *testing.T) { fakeEtcdClient, etcdStorage := newEtcdStorage(t) storage, _ := NewStorage(etcdStorage) ctx := api.NewDefaultContext() key := storage.Etcd.KeyRootFunc(ctx) key = etcdtest.AddPrefix(key) fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ { Value: runtime.EncodeOrDie(latest.Codec, &api.ResourceQuota{ ObjectMeta: api.ObjectMeta{Name: "foo"}, }), }, { Value: runtime.EncodeOrDie(latest.Codec, &api.ResourceQuota{ ObjectMeta: api.ObjectMeta{Name: "bar"}, }), }, }, }, }, } resourcequotasObj, err := storage.List(api.NewDefaultContext(), labels.Everything(), fields.Everything()) resourcequotas := resourcequotasObj.(*api.ResourceQuotaList) if err != nil { t.Fatalf("unexpected error: %v", err) } if len(resourcequotas.Items) != 2 { t.Errorf("Unexpected resourcequota list: %#v", resourcequotas) } if resourcequotas.Items[0].Name != "foo" { t.Errorf("Unexpected resourcequota: %#v", resourcequotas.Items[0]) } if resourcequotas.Items[1].Name != "bar" { t.Errorf("Unexpected resourcequota: %#v", resourcequotas.Items[1]) } }
func TestEtcdListEndpoints(t *testing.T) { ctx := api.NewDefaultContext() storage, fakeClient := newStorage(t) key := storage.KeyRootFunc(ctx) key = etcdtest.AddPrefix(key) fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ { Value: runtime.EncodeOrDie(latest.Codec, &api.Endpoints{ ObjectMeta: api.ObjectMeta{Name: "foo"}, Subsets: []api.EndpointSubset{{ Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}}, Ports: []api.EndpointPort{{Port: 8345, Protocol: "TCP"}}, }}, }), }, { Value: runtime.EncodeOrDie(latest.Codec, &api.Endpoints{ ObjectMeta: api.ObjectMeta{Name: "bar"}, }), }, }, }, }, E: nil, } endpointsObj, err := storage.List(ctx, labels.Everything(), fields.Everything()) if err != nil { t.Errorf("unexpected error: %v", err) } endpoints := endpointsObj.(*api.EndpointsList) if len(endpoints.Items) != 2 || endpoints.Items[0].Name != "foo" || endpoints.Items[1].Name != "bar" { t.Errorf("Unexpected endpoints list: %#v", endpoints) } }
func TestListFiltered(t *testing.T) { fakeEtcdClient, helper := newHelper(t) fakeEtcdClient.ChangeIndex = 1 fakeEtcdClient.Data["/images"] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ { Value: runtime.EncodeOrDie(latest.Codec, &api.Image{ ObjectMeta: kapi.ObjectMeta{ Name: "foo", Labels: map[string]string{"env": "prod"}, }, }), }, { Value: runtime.EncodeOrDie(latest.Codec, &api.Image{ ObjectMeta: kapi.ObjectMeta{ Name: "bar", Labels: map[string]string{"env": "dev"}, }, }), }, }, }, }, E: nil, } storage := NewREST(helper) list, err := storage.List(kapi.NewDefaultContext(), labels.SelectorFromSet(labels.Set{"env": "dev"}), fields.Everything()) if err != nil { t.Errorf("unexpected error: %v", err) } images := list.(*api.ImageList) if len(images.Items) != 1 || images.Items[0].Name != "bar" { t.Errorf("Unexpected images list: %#v", images) } }
func TestListNamespaceList(t *testing.T) { fakeEtcdClient, etcdStorage := newEtcdStorage(t) key := etcdtest.AddPrefix("/namespaces") fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ { Value: runtime.EncodeOrDie(latest.Codec, &api.Namespace{ ObjectMeta: api.ObjectMeta{Name: "foo"}, }), }, { Value: runtime.EncodeOrDie(latest.Codec, &api.Namespace{ ObjectMeta: api.ObjectMeta{Name: "bar"}, }), }, }, }, }, } storage, _, _ := NewStorage(etcdStorage) namespacesObj, err := storage.List(api.NewContext(), labels.Everything(), fields.Everything()) namespaces := namespacesObj.(*api.NamespaceList) if err != nil { t.Fatalf("unexpected error: %v", err) } if len(namespaces.Items) != 2 { t.Errorf("Unexpected namespaces list: %#v", namespaces) } if namespaces.Items[0].Name != "foo" || namespaces.Items[0].Status.Phase != api.NamespaceActive { t.Errorf("Unexpected namespace: %#v", namespaces.Items[0]) } if namespaces.Items[1].Name != "bar" { t.Errorf("Unexpected namespace: %#v", namespaces.Items[1]) } }
func TestListEmptyPersistentVolumesList(t *testing.T) { ctx := api.NewContext() storage, _, fakeClient, _ := newStorage(t) fakeClient.ChangeIndex = 1 key := storage.KeyRootFunc(ctx) key = etcdtest.AddPrefix(key) fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{}, E: fakeClient.NewError(tools.EtcdErrorCodeNotFound), } persistentVolume, err := storage.List(ctx, labels.Everything(), fields.Everything()) if err != nil { t.Fatalf("unexpected error: %v", err) } if len(persistentVolume.(*api.PersistentVolumeList).Items) != 0 { t.Errorf("Unexpected non-zero pod list: %#v", persistentVolume) } if persistentVolume.(*api.PersistentVolumeList).ResourceVersion != "1" { t.Errorf("Unexpected resource version: %#v", persistentVolume) } }
func TestEtcdListControllersLabelsMatch(t *testing.T) { storage, fakeClient := newStorage(t) ctx := api.NewDefaultContext() key := makeControllerListKey(ctx) key = etcdtest.AddPrefix(key) controller := validController controller.Labels = map[string]string{"k": "v"} controller.Name = "bar" fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ { Value: runtime.EncodeOrDie(latest.Codec, &validController), }, { Value: runtime.EncodeOrDie(latest.Codec, &controller), }, }, }, }, E: nil, } testLabels := labels.SelectorFromSet(labels.Set(controller.Labels)) objList, err := storage.List(ctx, testLabels, fields.Everything()) if err != nil { t.Errorf("unexpected error: %v", err) } controllers, _ := objList.(*api.DaemonList) if len(controllers.Items) != 1 || controllers.Items[0].Name != controller.Name || !testLabels.Matches(labels.Set(controllers.Items[0].Labels)) { t.Errorf("Unexpected controller list: %#v for query with labels %#v", controllers, testLabels) } }
func TestListResourceQuotaListSelection(t *testing.T) { fakeEtcdClient, etcdStorage := newEtcdStorage(t) storage, _ := NewStorage(etcdStorage) ctx := api.NewDefaultContext() key := storage.Etcd.KeyRootFunc(ctx) key = etcdtest.AddPrefix(key) fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ {Value: runtime.EncodeOrDie(latest.Codec, &api.ResourceQuota{ ObjectMeta: api.ObjectMeta{Name: "foo"}, })}, {Value: runtime.EncodeOrDie(latest.Codec, &api.ResourceQuota{ ObjectMeta: api.ObjectMeta{ Name: "qux", Labels: map[string]string{"label": "qux"}, }, })}, {Value: runtime.EncodeOrDie(latest.Codec, &api.ResourceQuota{ ObjectMeta: api.ObjectMeta{Name: "zot"}, })}, }, }, }, } table := []struct { label, field string expectedIDs util.StringSet }{ { expectedIDs: util.NewStringSet("foo", "qux", "zot"), }, { field: "name=zot", expectedIDs: util.NewStringSet("zot"), }, { label: "label=qux", expectedIDs: util.NewStringSet("qux"), }, } for index, item := range table { label, err := labels.Parse(item.label) if err != nil { t.Errorf("unexpected error: %v", err) continue } field, err := fields.ParseSelector(item.field) if err != nil { t.Errorf("unexpected error: %v", err) continue } resourcequotasObj, err := storage.List(ctx, label, field) if err != nil { t.Errorf("unexpected error: %v", err) } resourcequotas := resourcequotasObj.(*api.ResourceQuotaList) set := util.NewStringSet() for i := range resourcequotas.Items { set.Insert(resourcequotas.Items[i].Name) } if e, a := len(item.expectedIDs), len(set); e != a { t.Errorf("%v: Expected %v, got %v", index, item.expectedIDs, set) } } }
func TestListPodListSelection(t *testing.T) { fakeEtcdClient, etcdStorage := newEtcdStorage(t) ctx := api.NewDefaultContext() storage := NewStorage(etcdStorage, nil).Pod rootKey := etcdtest.AddPrefix("pods/default") key := etcdtest.AddPrefix("pods/default/zot") fakeEtcdClient.Data[rootKey] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ {Value: runtime.EncodeOrDie(latest.Codec, &api.Pod{ ObjectMeta: api.ObjectMeta{Name: "foo"}, })}, {Value: runtime.EncodeOrDie(latest.Codec, &api.Pod{ ObjectMeta: api.ObjectMeta{Name: "bar"}, Spec: api.PodSpec{NodeName: "barhost"}, })}, {Value: runtime.EncodeOrDie(latest.Codec, &api.Pod{ ObjectMeta: api.ObjectMeta{Name: "baz"}, Status: api.PodStatus{Phase: api.PodFailed}, })}, {Value: runtime.EncodeOrDie(latest.Codec, &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "qux", Labels: map[string]string{"label": "qux"}, }, })}, {Value: runtime.EncodeOrDie(latest.Codec, &api.Pod{ ObjectMeta: api.ObjectMeta{Name: "zot"}, })}, }, }, }, } fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Value: runtime.EncodeOrDie(latest.Codec, &api.Pod{ ObjectMeta: api.ObjectMeta{Name: "zot"}, }), }, }, } table := []struct { label, field string expectedIDs util.StringSet }{ { expectedIDs: util.NewStringSet("foo", "bar", "baz", "qux", "zot"), }, { field: "metadata.name=zot", expectedIDs: util.NewStringSet("zot"), }, { label: "label=qux", expectedIDs: util.NewStringSet("qux"), }, { field: "status.phase=Failed", expectedIDs: util.NewStringSet("baz"), }, { field: "spec.nodeName=barhost", expectedIDs: util.NewStringSet("bar"), }, { field: "spec.nodeName=", expectedIDs: util.NewStringSet("foo", "baz", "qux", "zot"), }, { field: "spec.nodeName!=", expectedIDs: util.NewStringSet("bar"), }, } for index, item := range table { label, err := labels.Parse(item.label) if err != nil { t.Errorf("unexpected error: %v", err) continue } field, err := fields.ParseSelector(item.field) if err != nil { t.Errorf("unexpected error: %v", err) continue } podsObj, err := storage.List(ctx, label, field) if err != nil { t.Errorf("unexpected error: %v", err) } pods := podsObj.(*api.PodList) set := util.NewStringSet() for i := range pods.Items { set.Insert(pods.Items[i].Name) } if e, a := len(item.expectedIDs), len(set); e != a { t.Errorf("%v: Expected %v, got %v", index, item.expectedIDs, set) } /*for _, pod := range pods.Items { if !item.expectedIDs.Has(pod.Name) { t.Errorf("%v: Unexpected pod %v", index, pod.Name) } t.Logf("%v: Got pod Name: %v", index, pod.Name) }*/ } }
func TestListNamespaceListSelection(t *testing.T) { fakeEtcdClient, etcdStorage := newEtcdStorage(t) key := etcdtest.AddPrefix("/namespaces") fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Nodes: []*etcd.Node{ {Value: runtime.EncodeOrDie(latest.Codec, &api.Namespace{ ObjectMeta: api.ObjectMeta{Name: "foo"}, })}, {Value: runtime.EncodeOrDie(latest.Codec, &api.Namespace{ ObjectMeta: api.ObjectMeta{Name: "bar"}, })}, {Value: runtime.EncodeOrDie(latest.Codec, &api.Namespace{ ObjectMeta: api.ObjectMeta{Name: "baz"}, Status: api.NamespaceStatus{Phase: api.NamespaceTerminating}, })}, {Value: runtime.EncodeOrDie(latest.Codec, &api.Namespace{ ObjectMeta: api.ObjectMeta{ Name: "qux", Labels: map[string]string{"label": "qux"}, }, })}, {Value: runtime.EncodeOrDie(latest.Codec, &api.Namespace{ ObjectMeta: api.ObjectMeta{Name: "zot"}, })}, }, }, }, } storage, _, _ := NewStorage(etcdStorage) ctx := api.NewContext() table := []struct { label, field string expectedIDs util.StringSet }{ { expectedIDs: util.NewStringSet("foo", "bar", "baz", "qux", "zot"), }, { field: "name=zot", expectedIDs: util.NewStringSet("zot"), }, { label: "label=qux", expectedIDs: util.NewStringSet("qux"), }, { field: "status.phase=Terminating", expectedIDs: util.NewStringSet("baz"), }, } for index, item := range table { label, err := labels.Parse(item.label) if err != nil { t.Errorf("unexpected error: %v", err) continue } field, err := fields.ParseSelector(item.field) if err != nil { t.Errorf("unexpected error: %v", err) continue } namespacesObj, err := storage.List(ctx, label, field) if err != nil { t.Errorf("unexpected error: %v", err) } namespaces := namespacesObj.(*api.NamespaceList) set := util.NewStringSet() for i := range namespaces.Items { set.Insert(namespaces.Items[i].Name) } if e, a := len(item.expectedIDs), len(set); e != a { t.Errorf("%v: Expected %v, got %v", index, item.expectedIDs, set) } } }