func TestGenerationNumber(t *testing.T) { storage, _ := newStorage(t) modifiedSno := validController modifiedSno.Generation = 100 modifiedSno.Status.ObservedGeneration = 10 ctx := api.NewDefaultContext() rc, err := createController(storage, modifiedSno, t) ctrl, err := storage.Get(ctx, rc.Name) if err != nil { t.Errorf("unexpected error: %v", err) } controller, _ := ctrl.(*api.ReplicationController) // Generation initialization if controller.Generation != 1 && controller.Status.ObservedGeneration != 0 { t.Fatalf("Unexpected generation number %v, status generation %v", controller.Generation, controller.Status.ObservedGeneration) } // Updates to spec should increment the generation number controller.Spec.Replicas += 1 storage.Update(ctx, controller) if err != nil { t.Errorf("unexpected error: %v", err) } ctrl, err = storage.Get(ctx, rc.Name) if err != nil { t.Errorf("unexpected error: %v", err) } controller, _ = ctrl.(*api.ReplicationController) if controller.Generation != 2 || controller.Status.ObservedGeneration != 0 { t.Fatalf("Unexpected generation, spec: %v, status: %v", controller.Generation, controller.Status.ObservedGeneration) } // Updates to status should not increment either spec or status generation numbers controller.Status.Replicas += 1 storage.Update(ctx, controller) if err != nil { t.Errorf("unexpected error: %v", err) } ctrl, err = storage.Get(ctx, rc.Name) if err != nil { t.Errorf("unexpected error: %v", err) } controller, _ = ctrl.(*api.ReplicationController) if controller.Generation != 2 || controller.Status.ObservedGeneration != 0 { t.Fatalf("Unexpected generation number, spec: %v, status: %v", controller.Generation, controller.Status.ObservedGeneration) } }
func TestGet(t *testing.T) { expect := validNewNamespace() expect.Status.Phase = api.NamespaceActive storage, fakeEtcdClient, _ := newStorage(t) ctx := api.NewContext() key, err := storage.Etcd.KeyFunc(ctx, "foo") key = etcdtest.AddPrefix(key) if err != nil { t.Fatalf("unexpected key error: %v", err) } fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Value: runtime.EncodeOrDie(latest.Codec, expect), }, }, } obj, err := storage.Get(api.NewContext(), "foo") namespace := obj.(*api.Namespace) if err != nil { t.Fatalf("unexpected error: %v", err) } expect.Status.Phase = api.NamespaceActive if e, a := expect, namespace; !api.Semantic.DeepEqual(e, a) { t.Errorf("Unexpected namespace: %s", util.ObjectDiff(e, a)) } }
func TestGet(t *testing.T) { expect := validNewPod() expect.Status.Phase = api.PodRunning expect.Spec.NodeName = "machine" fakeEtcdClient, etcdStorage := newEtcdStorage(t) key := etcdtest.AddPrefix("/pods/test/foo") fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Value: runtime.EncodeOrDie(latest.Codec, expect), }, }, } storage := NewStorage(etcdStorage, nil).Pod obj, err := storage.Get(api.WithNamespace(api.NewContext(), "test"), "foo") pod := obj.(*api.Pod) if err != nil { t.Fatalf("unexpected error: %v", err) } if e, a := expect, pod; !api.Semantic.DeepEqual(e, a) { t.Errorf("Unexpected pod: %s", util.ObjectDiff(e, a)) } }
func TestGetOK(t *testing.T) { fakeEtcdClient, helper := newHelper(t) expectedImage := &api.Image{ ObjectMeta: kapi.ObjectMeta{Name: "foo"}, DockerImageReference: "openshift/ruby-19-centos", } fakeEtcdClient.Data["/images/foo"] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Value: runtime.EncodeOrDie(latest.Codec, expectedImage), }, }, } storage := NewREST(helper) image, err := storage.Get(kapi.NewDefaultContext(), "foo") if image == nil { t.Fatal("Unexpected nil image") } if err != nil { t.Fatal("Unexpected non-nil error", err) } if image.(*api.Image).Name != "foo" { t.Errorf("Unexpected image: %#v", image) } }
func TestEtcdGetPersistentVolumes(t *testing.T) { ctx := api.NewContext() storage, _, fakeClient, _ := newStorage(t) persistentVolume := validNewPersistentVolume("foo") name := persistentVolume.Name key, _ := storage.KeyFunc(ctx, name) key = etcdtest.AddPrefix(key) fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, persistentVolume), 0) response, err := fakeClient.Get(key, false, false) if err != nil { t.Fatalf("Unexpected error %v", err) } var persistentVolumeOut api.PersistentVolume err = latest.Codec.DecodeInto([]byte(response.Node.Value), &persistentVolumeOut) if err != nil { t.Errorf("unexpected error: %v", err) } obj, err := storage.Get(ctx, name) if err != nil { t.Errorf("unexpected error: %v", err) } got := obj.(*api.PersistentVolume) persistentVolume.ObjectMeta.ResourceVersion = got.ObjectMeta.ResourceVersion if e, a := persistentVolume, got; !api.Semantic.DeepEqual(*e, *a) { t.Errorf("Unexpected persistentVolume: %#v, expected %#v", e, a) } }
func TestEtcdGetEndpoints(t *testing.T) { ctx := api.NewDefaultContext() storage, fakeClient := newStorage(t) endpoints := validNewEndpoints() name := endpoints.Name key, _ := storage.KeyFunc(ctx, name) key = etcdtest.AddPrefix(key) fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, endpoints), 0) response, err := fakeClient.Get(key, false, false) if err != nil { t.Fatalf("Unexpected error %v", err) } var endpointsOut api.Endpoints err = latest.Codec.DecodeInto([]byte(response.Node.Value), &endpointsOut) if err != nil { t.Errorf("unexpected error: %v", err) } obj, err := storage.Get(ctx, name) if err != nil { t.Errorf("unexpected error: %v", err) } got := obj.(*api.Endpoints) endpoints.ObjectMeta.ResourceVersion = got.ObjectMeta.ResourceVersion if e, a := endpoints, got; !api.Semantic.DeepEqual(*e, *a) { t.Errorf("Unexpected endpoints: %#v, expected %#v", e, a) } }
// TestEtcdGetControllerDifferentNamespace ensures same-name controllers in different namespaces do not clash func TestEtcdGetControllerDifferentNamespace(t *testing.T) { storage, fakeClient := newStorage(t) otherNs := "other" ctx1 := api.NewDefaultContext() ctx2 := api.WithNamespace(api.NewContext(), otherNs) key1, _ := makeControllerKey(ctx1, validController.Name) key2, _ := makeControllerKey(ctx2, validController.Name) key1 = etcdtest.AddPrefix(key1) key2 = etcdtest.AddPrefix(key2) fakeClient.Set(key1, runtime.EncodeOrDie(latest.Codec, &validController), 0) otherNsController := validController otherNsController.Namespace = otherNs fakeClient.Set(key2, runtime.EncodeOrDie(latest.Codec, &otherNsController), 0) obj, err := storage.Get(ctx1, validController.Name) ctrl1, _ := obj.(*api.ReplicationController) if err != nil { t.Errorf("unexpected error: %v", err) } if ctrl1.Name != "foo" { t.Errorf("Unexpected controller: %#v", ctrl1) } if ctrl1.Namespace != "default" { t.Errorf("Unexpected controller: %#v", ctrl1) } obj, err = storage.Get(ctx2, validController.Name) ctrl2, _ := obj.(*api.ReplicationController) if err != nil { t.Errorf("unexpected error: %v", err) } if ctrl2.Name != "foo" { t.Errorf("Unexpected controller: %#v", ctrl2) } if ctrl2.Namespace != "other" { t.Errorf("Unexpected controller: %#v", ctrl2) } }
func TestGetImageStreamError(t *testing.T) { fakeEtcdClient, helper := newHelper(t) fakeEtcdClient.Err = fmt.Errorf("foo") storage, _ := NewREST(helper, noDefaultRegistry, &fakeSubjectAccessReviewRegistry{}) image, err := storage.Get(kapi.NewDefaultContext(), "image1") if image != nil { t.Errorf("Unexpected non-nil image stream: %#v", image) } if err != fakeEtcdClient.Err { t.Errorf("Expected %#v, got %#v", fakeEtcdClient.Err, err) } }
func TestGetError(t *testing.T) { fakeEtcdClient, helper := newHelper(t) fakeEtcdClient.Err = fmt.Errorf("bad") storage := NewREST(helper) image, err := storage.Get(kapi.NewDefaultContext(), "foo") if image != nil { t.Errorf("Unexpected non-nil image: %#v", image) } if err != fakeEtcdClient.Err { t.Errorf("Expected %v, got %v", fakeEtcdClient.Err, err) } }
func TestEtcdGetNodeNotFound(t *testing.T) { ctx := api.NewContext() storage, fakeClient := newStorage(t) key := etcdtest.AddPrefix("minions/foo") fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: nil, }, E: tools.EtcdErrorNotFound, } _, err := storage.Get(ctx, "foo") if !errors.IsNotFound(err) { t.Errorf("Unexpected error returned: %#v", err) } }
func TestEtcdGetController(t *testing.T) { ctx := api.NewDefaultContext() storage, fakeClient := newStorage(t) key, _ := makeControllerKey(ctx, validController.Name) key = etcdtest.AddPrefix(key) fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &validController), 0) ctrl, err := storage.Get(ctx, validController.Name) if err != nil { t.Errorf("unexpected error: %v", err) } controller, ok := ctrl.(*api.ReplicationController) if !ok { t.Errorf("Expected a controller, got %#v", ctrl) } if controller.Name != validController.Name { t.Errorf("Unexpected controller: %#v", controller) } }
func TestGetNotFound(t *testing.T) { fakeEtcdClient, helper := newHelper(t) storage := NewREST(helper) fakeEtcdClient.Data["/images/foo"] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: nil, }, E: tools.EtcdErrorNotFound, } image, err := storage.Get(kapi.NewDefaultContext(), "foo") if err == nil { t.Errorf("Unexpected non-error.") } if image != nil { t.Errorf("Unexpected image: %#v", image) } }
func TestEtcdGetNode(t *testing.T) { ctx := api.NewContext() storage, fakeClient := newStorage(t) node := validNewNode() key, _ := storage.KeyFunc(ctx, node.Name) key = etcdtest.AddPrefix(key) fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, node), 0) nodeObj, err := storage.Get(ctx, node.Name) if err != nil { t.Errorf("unexpected error: %v", err) } got := nodeObj.(*api.Node) node.ObjectMeta.ResourceVersion = got.ObjectMeta.ResourceVersion if e, a := node, got; !api.Semantic.DeepEqual(*e, *a) { t.Errorf("Unexpected node: %#v, expected %#v", e, a) } }
func TestEtcdGetControllerNotFound(t *testing.T) { ctx := api.NewDefaultContext() storage, fakeClient := newStorage(t) key, _ := makeControllerKey(ctx, validController.Name) key = etcdtest.AddPrefix(key) fakeClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: nil, }, E: tools.EtcdErrorNotFound, } ctrl, err := storage.Get(ctx, validController.Name) if ctrl != nil { t.Errorf("Unexpected non-nil controller: %#v", ctrl) } if !errors.IsNotFound(err) { t.Errorf("Unexpected error returned: %#v", err) } }
func TestGetImageStreamOK(t *testing.T) { fakeEtcdClient, helper := newHelper(t) storage, _ := NewREST(helper, noDefaultRegistry, &fakeSubjectAccessReviewRegistry{}) ctx := kapi.NewDefaultContext() repoName := "foo" key, _ := storage.store.KeyFunc(ctx, repoName) fakeEtcdClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.ImageStream{ObjectMeta: kapi.ObjectMeta{Name: repoName}}), 0) obj, err := storage.Get(kapi.NewDefaultContext(), repoName) if obj == nil { t.Fatalf("Unexpected nil stream") } if err != nil { t.Fatalf("Unexpected non-nil error: %#v", err) } stream := obj.(*api.ImageStream) if e, a := repoName, stream.Name; e != a { t.Errorf("Expected %#v, got %#v", e, a) } }
func TestGet(t *testing.T) { expect := validNewResourceQuota() fakeEtcdClient, etcdStorage := newEtcdStorage(t) storage, _ := NewStorage(etcdStorage) key := "/resourcequotas/test/foo" key = etcdtest.AddPrefix(key) fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ Value: runtime.EncodeOrDie(latest.Codec, expect), }, }, } obj, err := storage.Get(api.WithNamespace(api.NewContext(), "test"), "foo") resourcequota := obj.(*api.ResourceQuota) if err != nil { t.Fatalf("unexpected error: %v", err) } if e, a := expect, resourcequota; !api.Semantic.DeepEqual(e, a) { t.Errorf("Unexpected resourcequota: %s", util.ObjectDiff(e, a)) } }
func TestEtcdUpdateController(t *testing.T) { ctx := api.NewDefaultContext() storage, fakeClient := newStorage(t) key, _ := makeControllerKey(ctx, validController.Name) key = etcdtest.AddPrefix(key) // set a key, then retrieve the current resource version and try updating it resp, _ := fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &validController), 0) update := validController update.ResourceVersion = strconv.FormatUint(resp.Node.ModifiedIndex, 10) update.Spec.Replicas = validController.Spec.Replicas + 1 _, created, err := storage.Update(ctx, &update) if err != nil { t.Errorf("unexpected error: %v", err) } if created { t.Errorf("expected an update but created flag was returned") } ctrl, err := storage.Get(ctx, validController.Name) updatedController, _ := ctrl.(*api.ReplicationController) if updatedController.Spec.Replicas != validController.Spec.Replicas+1 { t.Errorf("Unexpected controller: %#v", ctrl) } }