本文整理匯總了Golang中k8s/io/kubernetes/pkg/storage.Get函數的典型用法代碼示例。如果您正苦於以下問題:Golang Get函數的具體用法?Golang Get怎麽用?Golang Get使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Get函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestGenerationNumber
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)
}
}
示例2: TestGetOK
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)
}
}
示例3: TestEtcdGetEndpoints
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)
}
}
示例4: TestGet
func TestGet(t *testing.T) {
expect := &validScale
fakeEtcdClient, etcdStorage := newEtcdStorage(t)
key := etcdtest.AddPrefix("/controllers/test/foo")
fakeEtcdClient.Data[key] = tools.EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Value: runtime.EncodeOrDie(latest.Codec, &validController),
ModifiedIndex: 1,
},
},
}
storage := NewStorage(etcdStorage).Scale
obj, err := storage.Get(api.WithNamespace(api.NewContext(), "test"), "foo")
scaler := obj.(*expapi.Scale)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if e, a := expect, scaler; !api.Semantic.DeepEqual(e, a) {
t.Errorf("Unexpected scaler: %s", util.ObjectDiff(e, a))
}
}
示例5: TestEtcdGetPersistentVolumes
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)
}
}
示例6: TestEtcdCreateBindingNoPod
// Ensure that when scheduler creates a binding for a pod that has already been deleted
// by the API server, API server returns not-found error.
func TestEtcdCreateBindingNoPod(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)
// Assume that a pod has undergone the following:
// - Create (apiserver)
// - Schedule (scheduler)
// - Delete (apiserver)
_, err := bindingStorage.Create(ctx, &api.Binding{
ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"},
Target: api.ObjectReference{Name: "machine"},
})
if err == nil {
t.Fatalf("Expected not-found-error but got nothing")
}
if !errors.IsNotFound(storeerr.InterpretGetError(err, api.Resource("pods"), "foo")) {
t.Fatalf("Unexpected error returned: %#v", err)
}
_, err = storage.Get(ctx, "foo")
if err == nil {
t.Fatalf("Expected not-found-error but got nothing")
}
if !errors.IsNotFound(storeerr.InterpretGetError(err, api.Resource("pods"), "foo")) {
t.Fatalf("Unexpected error: %v", err)
}
}
示例7: 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)
}
}
示例8: TestGet
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))
}
}
示例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: TestGet
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))
}
}
示例11: 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))
}
}
示例12: TestUpdate
func TestUpdate(t *testing.T) {
storage, _, si, destroyFunc := newStorage(t)
defer destroyFunc()
ctx := api.WithNamespace(api.NewContext(), "test")
key := "/controllers/test/foo"
if err := si.Create(ctx, key, &validController, nil, 0); err != nil {
t.Fatalf("unexpected error: %v", err)
}
replicas := int32(12)
update := extensions.Scale{
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test"},
Spec: extensions.ScaleSpec{
Replicas: replicas,
},
}
if _, _, err := storage.Update(ctx, update.Name, rest.DefaultUpdatedObjectInfo(&update, api.Scheme)); err != nil {
t.Fatalf("unexpected error: %v", err)
}
obj, err := storage.Get(ctx, "foo", &metav1.GetOptions{})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
updated := obj.(*extensions.Scale)
if updated.Spec.Replicas != replicas {
t.Errorf("wrong replicas count expected: %d got: %d", replicas, updated.Spec.Replicas)
}
}
示例13: TestUpdate
func TestUpdate(t *testing.T) {
storage, server, si := newStorage(t)
defer server.Terminate(t)
ctx := api.WithNamespace(api.NewContext(), "test")
key := etcdtest.AddPrefix("/controllers/test/foo")
if err := si.Set(ctx, key, &validController, nil, 0); err != nil {
t.Fatalf("unexpected error: %v", err)
}
replicas := 12
update := extensions.Scale{
ObjectMeta: api.ObjectMeta{Name: "foo", Namespace: "test"},
Spec: extensions.ScaleSpec{
Replicas: replicas,
},
}
if _, _, err := storage.Update(ctx, &update); err != nil {
t.Fatalf("unexpected error: %v", err)
}
obj, err := storage.Get(ctx, "foo")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
updated := obj.(*extensions.Scale)
if updated.Spec.Replicas != replicas {
t.Errorf("wrong replicas count expected: %d got: %d", replicas, updated.Spec.Replicas)
}
}
示例14: 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)
}
}
示例15: TestEtcdGetControllerDifferentNamespace
// 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.Daemon)
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.Daemon)
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)
}
}