本文整理匯總了Golang中k8s/io/kubernetes/pkg/storage.Create函數的典型用法代碼示例。如果您正苦於以下問題:Golang Create函數的具體用法?Golang Create怎麽用?Golang Create使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Create函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestEtcdCreateWithConflict
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)
}
}
示例2: TestEtcdCreateWithContainersNotFound
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)
}
}
示例3: TestEtcdUpdateNotScheduled
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))
}
}
示例4: TestUpdate
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,
)
}
示例5: TestEtcdCreateWithExistingContainers
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)
}
}
示例6: TestCreateSetsFields
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)
}
}
示例7: TestEtcdCreateWithConflict
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)
}
}
示例8: TestUpdate
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,
)
}
示例9: TestEtcdCreateWithExistingContainers
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)
}
}
示例10: TestStrategyPrepareMethods
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")
}
}
示例11: TestCreateImageStreamOK
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)
}
}
示例12: TestUpdate
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,
)
}
示例13: TestEtcdCreateBinding
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)
}
}
示例14: TestCreate
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)
}
}
示例15: TestCreateRegistryError
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)
}
}