本文整理匯總了Golang中k8s/io/kubernetes/pkg/storage/etcd/etcdtest.AddPrefix函數的典型用法代碼示例。如果您正苦於以下問題:Golang AddPrefix函數的具體用法?Golang AddPrefix怎麽用?Golang AddPrefix使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了AddPrefix函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestGetNotFoundErr
func TestGetNotFoundErr(t *testing.T) {
server := etcdtesting.NewEtcdTestClientServer(t)
defer server.Terminate(t)
key := etcdtest.AddPrefix("/some/key")
boguskey := etcdtest.AddPrefix("/some/boguskey")
helper := newEtcdHelper(server.Client, testapi.Default.Codec(), key)
var got api.Pod
err := helper.Get(context.TODO(), boguskey, &got, false)
if !storage.IsNotFound(err) {
t.Errorf("Unexpected reponse on key=%v, err=%v", key, err)
}
}
示例2: TestUpdate
func TestUpdate(t *testing.T) {
storage, _, si, destroyFunc := newStorage(t)
defer destroyFunc()
ctx := api.WithNamespace(api.NewContext(), "test")
key := etcdtest.AddPrefix("/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")
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)
}
}
示例3: 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)
}
}
示例4: TestList
func TestList(t *testing.T) {
server := etcdtesting.NewEtcdTestClientServer(t)
defer server.Terminate(t)
key := etcdtest.AddPrefix("/some/key")
helper := newEtcdHelper(server.Client, testapi.Default.Codec(), key)
list := api.PodList{
Items: []api.Pod{
{
ObjectMeta: api.ObjectMeta{Name: "bar"},
Spec: apitesting.DeepEqualSafePodSpec(),
},
{
ObjectMeta: api.ObjectMeta{Name: "baz"},
Spec: apitesting.DeepEqualSafePodSpec(),
},
{
ObjectMeta: api.ObjectMeta{Name: "foo"},
Spec: apitesting.DeepEqualSafePodSpec(),
},
},
}
createPodList(t, helper, &list)
var got api.PodList
// TODO: a sorted filter function could be applied such implied
// ordering on the returned list doesn't matter.
err := helper.List(context.TODO(), key, "", storage.Everything, &got)
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if e, a := list.Items, got.Items; !reflect.DeepEqual(e, a) {
t.Errorf("Expected %#v, got %#v", e, a)
}
}
示例5: TestStatusUpdate
func TestStatusUpdate(t *testing.T) {
storage, server := newStorage(t)
defer server.Terminate(t)
ctx := api.WithNamespace(api.NewContext(), api.NamespaceDefault)
key := etcdtest.AddPrefix("/replicasets/" + api.NamespaceDefault + "/foo")
if err := storage.ReplicaSet.Storage.Create(ctx, key, &validReplicaSet, nil, 0); err != nil {
t.Fatalf("unexpected error: %v", err)
}
update := extensions.ReplicaSet{
ObjectMeta: validReplicaSet.ObjectMeta,
Spec: extensions.ReplicaSetSpec{
Replicas: defaultReplicas,
},
Status: extensions.ReplicaSetStatus{
Replicas: defaultReplicas,
},
}
if _, _, err := storage.Status.Update(ctx, &update); err != nil {
t.Fatalf("unexpected error: %v", err)
}
obj, err := storage.ReplicaSet.Get(ctx, "foo")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
rs := obj.(*extensions.ReplicaSet)
if rs.Spec.Replicas != 7 {
t.Errorf("we expected .spec.replicas to not be updated but it was updated to %v", rs.Spec.Replicas)
}
if rs.Status.Replicas != defaultReplicas {
t.Errorf("we expected .status.replicas to be updated to %d but it was %v", defaultReplicas, rs.Status.Replicas)
}
}
示例6: TestDeleteNetwork
func TestDeleteNetwork(t *testing.T) {
storage, server := newStorage(t)
defer server.Terminate(t)
key := etcdtest.AddPrefix("networks/foo")
ctx := api.NewContext()
now := unversioned.Now()
net := &api.Network{
ObjectMeta: api.ObjectMeta{
Name: "foo",
DeletionTimestamp: &now,
},
Spec: api.NetworkSpec{
TenantID: "123",
Subnets: map[string]api.Subnet{
"subnet1": {
CIDR: "192.168.0.0/24",
Gateway: "192.168.0.1",
},
},
},
Status: api.NetworkStatus{Phase: api.NetworkInitializing},
}
if err := storage.Storage.Set(ctx, key, net, nil, 0); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if _, err := storage.Delete(ctx, "foo", nil); err != nil {
t.Errorf("unexpected error: %v", err)
}
}
示例7: TestUpdateStatus
func TestUpdateStatus(t *testing.T) {
storage, statusStorage, server := newStorage(t)
defer server.Terminate(t)
ctx := api.NewContext()
key, _ := storage.KeyFunc(ctx, "foo")
key = etcdtest.AddPrefix(key)
pvStart := validNewPersistentVolume("foo")
err := storage.Storage.Create(ctx, key, pvStart, nil, 0)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
pvIn := &api.PersistentVolume{
ObjectMeta: api.ObjectMeta{
Name: "foo",
},
Status: api.PersistentVolumeStatus{
Phase: api.VolumeBound,
},
}
_, _, err = statusStorage.Update(ctx, pvIn)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
obj, err := storage.Get(ctx, "foo")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
pvOut := obj.(*api.PersistentVolume)
// only compare the relevant change b/c metadata will differ
if !api.Semantic.DeepEqual(pvIn.Status, pvOut.Status) {
t.Errorf("unexpected object: %s", diff.ObjectDiff(pvIn.Status, pvOut.Status))
}
}
示例8: setObjectsForList
func (t *Tester) setObjectsForList(objects []runtime.Object) []runtime.Object {
result := make([]runtime.Object, len(objects))
key := etcdtest.AddPrefix(t.storage.KeyRootFunc(t.tester.TestContext()))
if len(objects) > 0 {
nodes := make([]*etcd.Node, len(objects))
for i, obj := range objects {
codec, _ := getCodec(obj)
encoded := runtime.EncodeOrDie(codec, obj)
decoded, _ := codec.Decode([]byte(encoded))
nodes[i] = &etcd.Node{Value: encoded}
result[i] = decoded
}
t.fakeClient.Data[key] = tools.EtcdResponseWithError{
R: &etcd.Response{
Node: &etcd.Node{
Nodes: nodes,
},
},
E: nil,
}
} else {
t.fakeClient.Data[key] = tools.EtcdResponseWithError{
R: &etcd.Response{},
E: t.fakeClient.NewError(tools.EtcdErrorCodeNotFound),
}
}
return result
}
示例9: TestScaleUpdate
func TestScaleUpdate(t *testing.T) {
storage, server := newStorage(t)
defer server.Terminate(t)
ctx := api.WithNamespace(api.NewContext(), namespace)
key := etcdtest.AddPrefix("/deployments/" + namespace + "/" + name)
if err := storage.Deployment.Storage.Set(ctx, key, &validDeployment, nil, 0); err != nil {
t.Fatalf("unexpected error: %v", err)
}
replicas := 12
update := extensions.Scale{
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespace},
Spec: extensions.ScaleSpec{
Replicas: replicas,
},
}
if _, _, err := storage.Scale.Update(ctx, &update); err != nil {
t.Fatalf("unexpected error: %v", err)
}
obj, err := storage.Deployment.Get(ctx, name)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
deployment := obj.(*extensions.Deployment)
if deployment.Spec.Replicas != replicas {
t.Errorf("wrong replicas count expected: %d got: %d", replicas, deployment.Spec.Replicas)
}
}
示例10: TestFiltering
func TestFiltering(t *testing.T) {
server, etcdStorage := newEtcdTestStorage(t, testapi.Default.Codec(), etcdtest.PathPrefix())
defer server.Terminate(t)
cacher := newTestCacher(etcdStorage)
defer cacher.Stop()
// Ensure that the cacher is initialized, before creating any pods,
// so that we are sure that all events will be present in cacher.
syncWatcher, err := cacher.Watch(context.TODO(), "pods/ns/foo", "0", storage.Everything)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
syncWatcher.Stop()
podFoo := makeTestPod("foo")
podFoo.Labels = map[string]string{"filter": "foo"}
podFooFiltered := makeTestPod("foo")
podFooPrime := makeTestPod("foo")
podFooPrime.Labels = map[string]string{"filter": "foo"}
podFooPrime.Spec.NodeName = "fakeNode"
podFooNS2 := makeTestPod("foo")
podFooNS2.Namespace += "2"
podFooNS2.Labels = map[string]string{"filter": "foo"}
// Create in another namespace first to make sure events from other namespaces don't get delivered
updatePod(t, etcdStorage, podFooNS2, nil)
fooCreated := updatePod(t, etcdStorage, podFoo, nil)
fooFiltered := updatePod(t, etcdStorage, podFooFiltered, fooCreated)
fooUnfiltered := updatePod(t, etcdStorage, podFoo, fooFiltered)
_ = updatePod(t, etcdStorage, podFooPrime, fooUnfiltered)
deleted := api.Pod{}
if err := etcdStorage.Delete(context.TODO(), etcdtest.AddPrefix("pods/ns/foo"), &deleted, nil); err != nil {
t.Errorf("Unexpected error: %v", err)
}
// Set up Watch for object "podFoo" with label filter set.
selector := labels.SelectorFromSet(labels.Set{"filter": "foo"})
filterFunc := func(obj runtime.Object) bool {
metadata, err := meta.Accessor(obj)
if err != nil {
t.Errorf("Unexpected error: %v", err)
return false
}
return selector.Matches(labels.Set(metadata.GetLabels()))
}
filter := storage.NewSimpleFilter(filterFunc, storage.NoTriggerFunc)
watcher, err := cacher.Watch(context.TODO(), "pods/ns/foo", fooCreated.ResourceVersion, filter)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
defer watcher.Stop()
verifyWatchEvent(t, watcher, watch.Deleted, podFooFiltered)
verifyWatchEvent(t, watcher, watch.Added, podFoo)
verifyWatchEvent(t, watcher, watch.Modified, podFooPrime)
verifyWatchEvent(t, watcher, watch.Deleted, podFooPrime)
}
示例11: getObject
func (t *Tester) getObject(ctx api.Context, obj runtime.Object) (runtime.Object, error) {
meta, err := api.ObjectMetaFor(obj)
if err != nil {
return nil, err
}
key, err := t.storage.KeyFunc(ctx, meta.Name)
if err != nil {
return nil, err
}
key = etcdtest.AddPrefix(key)
resp, err := t.fakeClient.Get(key, false, false)
if err != nil {
return nil, err
}
result := t.storage.NewFunc()
codec, err := getCodec(obj)
if err != nil {
return nil, err
}
if err := codec.DecodeInto([]byte(resp.Node.Value), result); err != nil {
return nil, err
}
return result, nil
}
示例12: testInstallThirdPartyAPIDeleteVersion
func testInstallThirdPartyAPIDeleteVersion(t *testing.T, version string) {
_, etcdserver, server, assert := initThirdParty(t, version)
defer server.Close()
defer etcdserver.Terminate(t)
client := etcdserver.Client
expectedObj := Foo{
ObjectMeta: api.ObjectMeta{
Name: "test",
Namespace: "default",
},
TypeMeta: unversioned.TypeMeta{
Kind: "Foo",
},
SomeField: "test field",
OtherField: 10,
}
if !assert.NoError(storeToEtcd(client, "/ThirdPartyResourceData/company.com/foos/default/test", "test", expectedObj)) {
t.FailNow()
return
}
resp, err := http.Get(server.URL + "/apis/company.com/" + version + "/namespaces/default/foos/test")
if !assert.NoError(err) {
return
}
assert.Equal(http.StatusOK, resp.StatusCode)
item := Foo{}
assert.NoError(decodeResponse(resp, &item))
// Fill in fields set by the apiserver
expectedObj.SelfLink = item.SelfLink
expectedObj.ResourceVersion = item.ResourceVersion
expectedObj.Namespace = item.Namespace
if !assert.True(reflect.DeepEqual(item, expectedObj)) {
t.Errorf("expected:\n%v\nsaw:\n%v\n", expectedObj, item)
}
resp, err = httpDelete(server.URL + "/apis/company.com/" + version + "/namespaces/default/foos/test")
if !assert.NoError(err) {
return
}
assert.Equal(http.StatusOK, resp.StatusCode)
resp, err = http.Get(server.URL + "/apis/company.com/" + version + "/namespaces/default/foos/test")
if !assert.NoError(err) {
return
}
assert.Equal(http.StatusNotFound, resp.StatusCode)
expectedDeletedKey := etcdtest.AddPrefix("ThirdPartyResourceData/company.com/foos/default/test")
_, err = client.Get(expectedDeletedKey, false, false)
if !etcdstorage.IsEtcdNotFound(err) {
t.Errorf("expected deletion didn't happen: %v", err)
}
}
示例13: TestStatusUpdate
func TestStatusUpdate(t *testing.T) {
storage, fakeClient := newStorage(t)
ctx := api.WithNamespace(api.NewContext(), namespace)
key := etcdtest.AddPrefix("/deployments/" + namespace + "/" + name)
if _, err := fakeClient.Set(key, runtime.EncodeOrDie(testapi.Extensions.Codec(), &validDeployment), 0); err != nil {
t.Fatalf("unexpected error: %v", err)
}
update := extensions.Deployment{
ObjectMeta: validDeployment.ObjectMeta,
Spec: extensions.DeploymentSpec{
Replicas: 100,
},
Status: extensions.DeploymentStatus{
Replicas: 100,
},
}
if _, _, err := storage.Status.Update(ctx, &update); err != nil {
t.Fatalf("unexpected error: %v", err)
}
response, err := fakeClient.Get(key, false, false)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var deployment extensions.Deployment
testapi.Extensions.Codec().DecodeInto([]byte(response.Node.Value), &deployment)
if deployment.Spec.Replicas != 7 {
t.Errorf("we expected .spec.replicas to not be updated but it was updated to %v", deployment.Spec.Replicas)
}
if deployment.Status.Replicas != 100 {
t.Errorf("we expected .status.replicas to be updated to 100 but it was %v", deployment.Status.Replicas)
}
}
示例14: TestScaleUpdate
func TestScaleUpdate(t *testing.T) {
storage, fakeClient := newStorage(t)
ctx := api.WithNamespace(api.NewContext(), namespace)
key := etcdtest.AddPrefix("/deployments/" + namespace + "/" + name)
if _, err := fakeClient.Set(key, runtime.EncodeOrDie(testapi.Extensions.Codec(), &validDeployment), 0); err != nil {
t.Fatalf("unexpected error: %v", err)
}
replicas := 12
update := extensions.Scale{
ObjectMeta: api.ObjectMeta{Name: name, Namespace: namespace},
Spec: extensions.ScaleSpec{
Replicas: replicas,
},
}
if _, _, err := storage.Scale.Update(ctx, &update); err != nil {
t.Fatalf("unexpected error: %v", err)
}
response, err := fakeClient.Get(key, false, false)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var deployment extensions.Deployment
testapi.Extensions.Codec().DecodeInto([]byte(response.Node.Value), &deployment)
if deployment.Spec.Replicas != replicas {
t.Errorf("wrong replicas count expected: %d got: %d", replicas, deployment.Spec.Replicas)
}
}
示例15: TestWatchListFromZeroIndex
func TestWatchListFromZeroIndex(t *testing.T) {
codec := testapi.Default.Codec()
key := etcdtest.AddPrefix("/some/key")
server := etcdtesting.NewEtcdTestClientServer(t)
defer server.Terminate(t)
h := newEtcdHelper(server.Client, codec, key)
watching, err := h.WatchList(context.TODO(), key, "0", storage.Everything)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
defer watching.Stop()
// creates key/foo which should trigger the WatchList for "key"
pod := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
err = h.Create(context.TODO(), pod.Name, pod, pod, 0)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
event, _ := <-watching.ResultChan()
if event.Type != watch.Added {
t.Errorf("Unexpected event %#v", event)
}
if e, a := pod, event.Object; !api.Semantic.DeepDerivative(e, a) {
t.Errorf("Unexpected error: expected %v, got %v", e, a)
}
}