func TestEtcdCreateWithConflict(t *testing.T) { storage, bindingStorage, _, fakeClient := newStorage(t) ctx := api.NewDefaultContext() fakeClient.TestIndex = true key, _ := storage.KeyFunc(ctx, "foo") fakeClient.ExpectNotFoundGet(key) _, err := storage.Create(ctx, validNewPod()) if err != nil { t.Fatalf("unexpected error: %v", err) } // Suddenly, a wild scheduler appears: binding := api.Binding{ ObjectMeta: api.ObjectMeta{ Namespace: api.NamespaceDefault, Name: "foo", Annotations: map[string]string{"label1": "value1"}, }, Target: api.ObjectReference{Name: "machine"}, } _, err = bindingStorage.Create(ctx, &binding) if err != nil { t.Fatalf("unexpected error: %v", err) } _, err = bindingStorage.Create(ctx, &binding) if err == nil || !errors.IsConflict(err) { t.Fatalf("expected resource conflict error, not: %v", err) } }
func TestEtcdCreateWithContainersNotFound(t *testing.T) { storage, bindingStorage, _, server := newStorage(t) defer server.Terminate(t) defer storage.Store.DestroyFunc() ctx := api.NewDefaultContext() key, _ := storage.KeyFunc(ctx, "foo") key = etcdtest.AddPrefix(key) _, err := storage.Create(ctx, validNewPod()) if err != nil { t.Fatalf("unexpected error: %v", err) } // Suddenly, a wild scheduler appears: _, err = bindingStorage.Create(ctx, &api.Binding{ ObjectMeta: api.ObjectMeta{ Namespace: api.NamespaceDefault, Name: "foo", Annotations: map[string]string{"label1": "value1"}, }, Target: api.ObjectReference{Name: "machine"}, }) if err != nil { t.Fatalf("unexpected error: %v", err) } obj, err := storage.Get(ctx, "foo") if err != nil { t.Fatalf("Unexpected error %v", err) } pod := obj.(*api.Pod) if !(pod.Annotations != nil && pod.Annotations["label1"] == "value1") { t.Fatalf("Pod annotations don't match the expected: %v", pod.Annotations) } }
func TestEtcdUpdateNotScheduled(t *testing.T) { storage, _, _, server := newStorage(t) defer server.Terminate(t) defer storage.Store.DestroyFunc() ctx := api.NewDefaultContext() key, _ := storage.KeyFunc(ctx, "foo") key = etcdtest.AddPrefix(key) if _, err := storage.Create(ctx, validNewPod()); err != nil { t.Fatalf("unexpected error: %v", err) } podIn := validChangedPod() _, _, err := storage.Update(ctx, podIn.Name, rest.DefaultUpdatedObjectInfo(podIn, api.Scheme)) if err != nil { t.Errorf("Unexpected error: %v", err) } obj, err := storage.Get(ctx, validNewPod().ObjectMeta.Name) if err != nil { t.Errorf("unexpected error: %v", err) } podOut := obj.(*api.Pod) // validChangedPod only changes the Labels, so were checking the update was valid if !api.Semantic.DeepEqual(podIn.Labels, podOut.Labels) { t.Errorf("objects differ: %v", diff.ObjectDiff(podOut, podIn)) } }
func TestUpdate(t *testing.T) { fakeClient, helper := newHelper(t) storage := NewREST(helper, nil).Route test := resttest.New(t, storage, fakeClient.SetError) key, err := storage.KeyFunc(test.TestContext(), "foo") if err != nil { t.Fatal(err) } key = etcdtest.AddPrefix(key) fakeClient.ExpectNotFoundGet(key) fakeClient.ChangeIndex = 2 route := validNewRoute("foo") route.Namespace = test.TestNamespace() existing := validNewRoute("exists") existing.Namespace = test.TestNamespace() obj, err := storage.Create(test.TestContext(), existing) if err != nil { t.Fatalf("unable to create object: %v", err) } older := obj.(*api.Route) older.ResourceVersion = "1" test.TestUpdate( route, existing, older, ) }
func TestEtcdCreateWithExistingContainers(t *testing.T) { storage, bindingStorage, _, fakeClient := newStorage(t) ctx := api.NewDefaultContext() fakeClient.TestIndex = true key, _ := storage.KeyFunc(ctx, "foo") key = etcdtest.AddPrefix(key) fakeClient.ExpectNotFoundGet(key) _, err := storage.Create(ctx, validNewPod()) if err != nil { t.Fatalf("unexpected error: %v", err) } // Suddenly, a wild scheduler appears: _, err = bindingStorage.Create(ctx, &api.Binding{ ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"}, Target: api.ObjectReference{Name: "machine"}, }) if err != nil { t.Fatalf("unexpected error: %v", err) } resp, err := fakeClient.Get(key, false, false) if err != nil { t.Fatalf("Unexpected error %v", err) } var pod api.Pod err = testapi.Default.Codec().DecodeInto([]byte(resp.Node.Value), &pod) if err != nil { t.Errorf("unexpected error: %v", err) } if pod.Name != "foo" { t.Errorf("Unexpected pod: %#v %s", pod, resp.Node.Value) } }
func TestCreateSetsFields(t *testing.T) { fakeEtcdClient, etcdStorage := newEtcdStorage(t) storage, _, _ := NewStorage(etcdStorage) namespace := validNewNamespace() _, err := storage.Create(api.NewContext(), namespace) if err != fakeEtcdClient.Err { t.Fatalf("unexpected error: %v", err) } actual := &api.Namespace{} ctx := api.NewContext() key, err := storage.Etcd.KeyFunc(ctx, "foo") if err != nil { t.Fatalf("unexpected key error: %v", err) } if err := etcdStorage.Get(key, actual, false); err != nil { t.Fatalf("unexpected extraction error: %v", err) } if actual.Name != namespace.Name { t.Errorf("unexpected namespace: %#v", actual) } if len(actual.UID) == 0 { t.Errorf("expected namespace UID to be set: %#v", actual) } if actual.Status.Phase != api.NamespaceActive { t.Errorf("expected namespace phase to be set to active, but %v", actual.Status.Phase) } }
func TestEtcdCreateWithConflict(t *testing.T) { storage, bindingStorage, _, server := newStorage(t) defer server.Terminate(t) defer storage.Store.DestroyFunc() ctx := api.NewDefaultContext() _, err := storage.Create(ctx, validNewPod()) if err != nil { t.Fatalf("unexpected error: %v", err) } // Suddenly, a wild scheduler appears: binding := api.Binding{ ObjectMeta: api.ObjectMeta{ Namespace: api.NamespaceDefault, Name: "foo", Annotations: map[string]string{"label1": "value1"}, }, Target: api.ObjectReference{Name: "machine"}, } _, err = bindingStorage.Create(ctx, &binding) if err != nil { t.Fatalf("unexpected error: %v", err) } _, err = bindingStorage.Create(ctx, &binding) if err == nil || !errors.IsConflict(err) { t.Fatalf("expected resource conflict error, not: %v", err) } }
func TestUpdate(t *testing.T) { fakeEtcdClient, etcdStorage := newEtcdStorage(t) storage := NewStorage(etcdStorage) test := resttest.New(t, storage, fakeEtcdClient.SetError) key, err := storage.KeyFunc(test.TestContext(), "foo") if err != nil { t.Fatal(err) } key = etcdtest.AddPrefix(key) fakeEtcdClient.ExpectNotFoundGet(key) fakeEtcdClient.ChangeIndex = 2 serviceAccount := validNewServiceAccount("foo") existing := validNewServiceAccount("exists") existing.Namespace = test.TestNamespace() obj, err := storage.Create(test.TestContext(), existing) if err != nil { t.Fatalf("unable to create object: %v", err) } older := obj.(*api.ServiceAccount) older.ResourceVersion = "1" test.TestUpdate( serviceAccount, existing, older, ) }
func TestEtcdCreateWithExistingContainers(t *testing.T) { storage, bindingStorage, _, server := newStorage(t) defer server.Terminate(t) defer storage.Store.DestroyFunc() ctx := api.NewDefaultContext() key, _ := storage.KeyFunc(ctx, "foo") key = etcdtest.AddPrefix(key) _, err := storage.Create(ctx, validNewPod()) if err != nil { t.Fatalf("unexpected error: %v", err) } // Suddenly, a wild scheduler appears: _, err = bindingStorage.Create(ctx, &api.Binding{ ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"}, Target: api.ObjectReference{Name: "machine"}, }) if err != nil { t.Fatalf("unexpected error: %v", err) } _, err = storage.Get(ctx, "foo") if err != nil { t.Fatalf("Unexpected error %v", err) } }
func TestStrategyPrepareMethods(t *testing.T) { _, helper := newHelper(t) storage, _ := NewREST(helper, testDefaultRegistry, &fakeSubjectAccessReviewRegistry{}) stream := validNewStream() strategy := fakeStrategy{imagestream.NewStrategy(testDefaultRegistry, &fakeSubjectAccessReviewRegistry{})} storage.store.CreateStrategy = strategy storage.store.UpdateStrategy = strategy ctx := kapi.WithUser(kapi.NewDefaultContext(), &fakeUser{}) obj, err := storage.Create(ctx, stream) if err != nil { t.Fatalf("Unexpected error: %v", err) } updatedStream := obj.(*api.ImageStream) if updatedStream.Annotations["test"] != "PrepareForCreate" { t.Errorf("Expected PrepareForCreate annotation") } obj, _, err = storage.Update(ctx, updatedStream) if err != nil { t.Errorf("Unexpected error: %v", err) } updatedStream = obj.(*api.ImageStream) if updatedStream.Annotations["test"] != "PrepareForUpdate" { t.Errorf("Expected PrepareForUpdate annotation") } }
func TestCreateImageStreamOK(t *testing.T) { _, helper := newHelper(t) storage, _ := NewREST(helper, noDefaultRegistry, &fakeSubjectAccessReviewRegistry{}) stream := &api.ImageStream{ObjectMeta: kapi.ObjectMeta{Name: "foo"}} ctx := kapi.WithUser(kapi.NewDefaultContext(), &fakeUser{}) _, err := storage.Create(ctx, stream) if err != nil { t.Fatalf("Unexpected non-nil error: %#v", err) } actual := &api.ImageStream{} if err := helper.Get("/imagestreams/default/foo", actual, false); err != nil { t.Fatalf("unexpected extraction error: %v", err) } if actual.Name != stream.Name { t.Errorf("unexpected stream: %#v", actual) } if len(actual.UID) == 0 { t.Errorf("expected stream UID to be set: %#v", actual) } if stream.CreationTimestamp.IsZero() { t.Error("Unexpected zero CreationTimestamp") } if stream.Spec.DockerImageRepository != "" { t.Errorf("unexpected stream: %#v", stream) } }
func TestUpdate(t *testing.T) { storage, fakeEtcdClient, _ := newStorage(t) test := resttest.New(t, storage, fakeEtcdClient.SetError) key, err := storage.KeyFunc(test.TestContext(), "foo") if err != nil { t.Fatal(err) } key = etcdtest.AddPrefix(key) fakeEtcdClient.ExpectNotFoundGet(key) fakeEtcdClient.ChangeIndex = 2 rsrc := validNewThirdPartyResource("foo") existing := validNewThirdPartyResource("exists") existing.Namespace = test.TestNamespace() obj, err := storage.Create(test.TestContext(), existing) if err != nil { t.Fatalf("unable to create object: %v", err) } older := obj.(*expapi.ThirdPartyResource) older.ResourceVersion = "1" test.TestUpdate( rsrc, existing, older, ) }
func TestEtcdCreateBinding(t *testing.T) { ctx := api.NewDefaultContext() testCases := map[string]struct { binding api.Binding errOK func(error) bool }{ "noName": { binding: api.Binding{ ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"}, Target: api.ObjectReference{}, }, errOK: func(err error) bool { return err != nil }, }, "badKind": { binding: api.Binding{ ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"}, Target: api.ObjectReference{Name: "machine1", Kind: "unknown"}, }, errOK: func(err error) bool { return err != nil }, }, "emptyKind": { binding: api.Binding{ ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"}, Target: api.ObjectReference{Name: "machine2"}, }, errOK: func(err error) bool { return err == nil }, }, "kindNode": { binding: api.Binding{ ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"}, Target: api.ObjectReference{Name: "machine3", Kind: "Node"}, }, errOK: func(err error) bool { return err == nil }, }, } for k, test := range testCases { storage, bindingStorage, _, server := newStorage(t) key, _ := storage.KeyFunc(ctx, "foo") key = etcdtest.AddPrefix(key) if _, err := storage.Create(ctx, validNewPod()); err != nil { t.Fatalf("%s: unexpected error: %v", k, err) } if _, err := bindingStorage.Create(ctx, &test.binding); !test.errOK(err) { t.Errorf("%s: unexpected error: %v", k, err) } else if err == nil { // If bind succeeded, verify Host field in pod's Spec. pod, err := storage.Get(ctx, validNewPod().ObjectMeta.Name) if err != nil { t.Errorf("%s: unexpected error: %v", k, err) } else if pod.(*api.Pod).Spec.NodeName != test.binding.Target.Name { t.Errorf("%s: expected: %v, got: %v", k, pod.(*api.Pod).Spec.NodeName, test.binding.Target.Name) } } storage.Store.DestroyFunc() server.Terminate(t) } }
func TestCreate(t *testing.T) { _, helper := newHelper(t) storage, _ := NewREST(helper, noDefaultRegistry, &fakeSubjectAccessReviewRegistry{}) stream := validNewStream() ctx := kapi.WithUser(kapi.NewDefaultContext(), &fakeUser{}) _, err := storage.Create(ctx, stream) if err != nil { t.Fatalf("unexpected error: %v", err) } }
func TestCreateRegistryError(t *testing.T) { storage, _, _, fakeClient := newStorage(t) fakeClient.Err = fmt.Errorf("test error") pod := validNewPod() _, err := storage.Create(api.NewDefaultContext(), pod) if err != fakeClient.Err { t.Fatalf("unexpected error: %v", err) } }
func TestCreateSetsMetadata(t *testing.T) { testCases := []struct { image *api.Image expect func(*api.Image) bool }{ { image: &api.Image{ ObjectMeta: kapi.ObjectMeta{Name: "foo"}, DockerImageReference: "openshift/ruby-19-centos", }, }, { expect: func(image *api.Image) bool { if image.DockerImageMetadata.Size != 28643712 { t.Errorf("image had size %d", image.DockerImageMetadata.Size) return false } if len(image.DockerImageLayers) != 4 || image.DockerImageLayers[0].Name != "sha256:744b46d0ac8636c45870a03830d8d82c20b75fbfb9bc937d5e61005d23ad4cfe" || image.DockerImageLayers[0].Size != 15141568 { t.Errorf("unexpected layers: %#v", image.DockerImageLayers) return false } return true }, image: &api.Image{ ObjectMeta: kapi.ObjectMeta{Name: "foo"}, DockerImageReference: "openshift/ruby-19-centos", DockerImageManifest: etcdManifest, }, }, } for i, test := range testCases { _, helper := newHelper(t) storage := NewREST(helper) obj, err := storage.Create(kapi.NewDefaultContext(), test.image) if obj == nil { t.Errorf("%d: Expected nil obj, got %v", i, obj) continue } if err != nil { t.Errorf("%d: Unexpected non-nil error: %#v", i, err) continue } image, ok := obj.(*api.Image) if !ok { t.Errorf("%d: Expected image type, got: %#v", i, obj) continue } if test.expect != nil && !test.expect(image) { t.Errorf("%d: Unexpected image: %#v", i, obj) } } }
func TestCreateRegistryErrorSaving(t *testing.T) { fakeEtcdClient, helper := newHelper(t) fakeEtcdClient.Err = fmt.Errorf("foo") storage, _ := NewREST(helper, noDefaultRegistry, &fakeSubjectAccessReviewRegistry{}) ctx := kapi.WithUser(kapi.NewDefaultContext(), &fakeUser{}) _, err := storage.Create(ctx, &api.ImageStream{ObjectMeta: kapi.ObjectMeta{Name: "foo"}}) if err != fakeEtcdClient.Err { t.Fatalf("Unexpected non-nil error: %#v", err) } }
func TestCreateRegistryError(t *testing.T) { fakeEtcdClient, etcdStorage := newEtcdStorage(t) fakeEtcdClient.Err = fmt.Errorf("test error") storage := NewStorage(etcdStorage, nil).Pod pod := validNewPod() _, err := storage.Create(api.NewDefaultContext(), pod) if err != fakeEtcdClient.Err { t.Fatalf("unexpected error: %v", err) } }
func TestEtcdCreateFailsWithoutNamespace(t *testing.T) { storage, _, _, fakeClient := newStorage(t) fakeClient.TestIndex = true pod := validNewPod() pod.Namespace = "" _, err := storage.Create(api.NewContext(), pod) // Accept "namespace" or "Namespace". if err == nil || !strings.Contains(err.Error(), "amespace") { t.Fatalf("expected error that namespace was missing from context, got: %v", err) } }
func TestCreateRegistryError(t *testing.T) { fakeEtcdClient, etcdStorage := newEtcdStorage(t) fakeEtcdClient.Err = fmt.Errorf("test error") storage, _ := NewStorage(etcdStorage) resourcequota := validNewResourceQuota() _, err := storage.Create(api.NewDefaultContext(), resourcequota) if err != fakeEtcdClient.Err { t.Fatalf("unexpected error: %v", err) } }
func TestCreateRegistryError(t *testing.T) { fakeEtcdClient, helper := newHelper(t) fakeEtcdClient.Err = fmt.Errorf("test error") storage := NewREST(helper) image := validNewImage() _, err := storage.Create(kapi.NewDefaultContext(), image) if err != fakeEtcdClient.Err { t.Fatalf("unexpected error: %v", err) } }
func TestCreateMissingID(t *testing.T) { _, helper := newHelper(t) storage := NewREST(helper) obj, err := storage.Create(kapi.NewDefaultContext(), &api.Image{}) if obj != nil { t.Errorf("Expected nil obj, got %v", obj) } if !errors.IsInvalid(err) { t.Errorf("Expected 'invalid' error, got %v", err) } }
func TestEtcdCreateControllerAlreadyExisting(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) _, err := storage.Create(ctx, &validController) if !errors.IsAlreadyExists(err) { t.Errorf("expected already exists err, got %#v", err) } }
func TestEtcdCreateFailsWithoutNamespace(t *testing.T) { storage, _, _, server := newStorage(t) defer server.Terminate(t) defer storage.Store.DestroyFunc() pod := validNewPod() pod.Namespace = "" _, err := storage.Create(api.NewContext(), pod) // Accept "namespace" or "Namespace". if err == nil || !strings.Contains(err.Error(), "amespace") { t.Fatalf("expected error that namespace was missing from context, got: %v", err) } }
func TestEtcdCreateControllerValidates(t *testing.T) { ctx := api.NewDefaultContext() storage, _ := newStorage(t) emptyName := validController emptyName.Name = "" failureCases := []api.Daemon{emptyName} for _, failureCase := range failureCases { c, err := storage.Create(ctx, &failureCase) if c != nil { t.Errorf("Expected nil channel") } if !errors.IsInvalid(err) { t.Errorf("Expected to get an invalid resource error, got %v", err) } } }
// TODO: remove, this is covered by RESTTest.TestCreate func TestPodStorageValidatesCreate(t *testing.T) { fakeEtcdClient, etcdStorage := newEtcdStorage(t) fakeEtcdClient.Err = fmt.Errorf("test error") storage := NewStorage(etcdStorage, nil).Pod pod := validNewPod() pod.Labels = map[string]string{ "invalid/label/to/cause/validation/failure": "bar", } c, err := storage.Create(api.NewDefaultContext(), pod) if c != nil { t.Errorf("Expected nil object") } if !errors.IsInvalid(err) { t.Errorf("Expected to get an invalid resource error, got %v", err) } }
// TODO: remove, this is covered by RESTTest.TestCreate func TestCreateWithConflictingNamespace(t *testing.T) { _, etcdStorage := newEtcdStorage(t) storage := NewStorage(etcdStorage, nil).Pod pod := validNewPod() pod.Namespace = "not-default" obj, err := storage.Create(api.NewDefaultContext(), pod) if obj != nil { t.Error("Expected a nil obj, but we got a value") } if err == nil { t.Errorf("Expected an error, but we didn't get one") } else if strings.Contains(err.Error(), "Controller.Namespace does not match the provided context") { t.Errorf("Expected 'Pod.Namespace does not match the provided context' error, got '%v'", err.Error()) } }
func TestStrategyPrepareMethods(t *testing.T) { _, helper := newHelper(t) storage := NewREST(helper) img := validNewImage() strategy := fakeStrategy{image.Strategy} storage.store.CreateStrategy = strategy obj, err := storage.Create(kapi.NewDefaultContext(), img) if err != nil { t.Errorf("Unexpected error: %v", err) } newImage := obj.(*api.Image) if newImage.Annotations["test"] != "PrepareForCreate" { t.Errorf("Expected PrepareForCreate annotation") } }
func TestCreateControllerWithConflictingNamespace(t *testing.T) { storage, _ := newStorage(t) controller := &api.Daemon{ ObjectMeta: api.ObjectMeta{Name: "test", Namespace: "not-default"}, } ctx := api.NewDefaultContext() channel, err := storage.Create(ctx, controller) if channel != nil { t.Error("Expected a nil channel, but we got a value") } errSubString := "namespace" if err == nil { t.Errorf("Expected an error, but we didn't get one") } else if !errors.IsBadRequest(err) || strings.Index(err.Error(), errSubString) == -1 { t.Errorf("Expected a Bad Request error with the sub string '%s', got %v", errSubString, err) } }
func TestCreateSetsFields(t *testing.T) { storage, _, _, fakeClient := newStorage(t) pod := validNewPod() _, err := storage.Create(api.NewDefaultContext(), pod) if err != fakeClient.Err { t.Fatalf("unexpected error: %v", err) } ctx := api.NewDefaultContext() object, err := storage.Get(ctx, "foo") if err != nil { t.Errorf("unexpected error: %v", err) } actual := object.(*api.Pod) if actual.Name != pod.Name { t.Errorf("unexpected pod: %#v", actual) } if len(actual.UID) == 0 { t.Errorf("expected pod UID to be set: %#v", actual) } }