func TestWatch(t *testing.T) { client := framework.NewEtcdClient() etcdStorage := tools.NewEtcdStorage(client, testapi.Codec(), etcdtest.PathPrefix()) framework.WithEtcdKey(func(key string) { key = etcdtest.AddPrefix(key) resp, err := client.Set(key, runtime.EncodeOrDie(testapi.Codec(), &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}), 0) if err != nil { t.Fatalf("unexpected error: %v", err) } expectedVersion := resp.Node.ModifiedIndex // watch should load the object at the current index w, err := etcdStorage.Watch(key, 0, tools.Everything) if err != nil { t.Fatalf("Unexpected error: %v", err) } event := <-w.ResultChan() if event.Type != watch.Added || event.Object == nil { t.Fatalf("expected first value to be set to ADDED, got %#v", event) } // version should match what we set pod := event.Object.(*api.Pod) if pod.ResourceVersion != strconv.FormatUint(expectedVersion, 10) { t.Errorf("expected version %d, got %#v", expectedVersion, pod) } // should be no events in the stream select { case event, ok := <-w.ResultChan(): if !ok { t.Fatalf("channel closed unexpectedly") } t.Fatalf("unexpected object in channel: %#v", event) default: } // should return the previously deleted item in the watch, but with the latest index resp, err = client.Delete(key, false) if err != nil { t.Fatalf("unexpected error: %v", err) } expectedVersion = resp.Node.ModifiedIndex event = <-w.ResultChan() if event.Type != watch.Deleted { t.Errorf("expected deleted event %#v", event) } pod = event.Object.(*api.Pod) if pod.ResourceVersion != strconv.FormatUint(expectedVersion, 10) { t.Errorf("expected version %d, got %#v", expectedVersion, pod) } }) }
func TestExtractObj(t *testing.T) { client := framework.NewEtcdClient() helper := tools.EtcdHelper{Client: client, Codec: stringCodec{}} framework.WithEtcdKey(func(key string) { _, err := client.Set(key, "object", 0) if err != nil { t.Fatalf("unexpected error: %v", err) } s := fakeAPIObject("") if err := helper.ExtractObj(key, &s, false); err != nil { t.Fatalf("unexpected error: %v", err) } if s != "object" { t.Errorf("unexpected response: %#v", s) } }) }
func TestSetObj(t *testing.T) { client := framework.NewEtcdClient() helper := tools.EtcdHelper{Client: client, Codec: stringCodec{}} framework.WithEtcdKey(func(key string) { fakeObject := fakeAPIObject("object") if err := helper.SetObj(key, &fakeObject, nil, 0); err != nil { t.Fatalf("unexpected error: %v", err) } resp, err := client.Get(key, false, false) if err != nil || resp.Node == nil { t.Fatalf("unexpected error: %v %v", err, resp) } if resp.Node.Value != "object" { t.Errorf("unexpected response: %#v", resp.Node) } }) }
func TestSetObj(t *testing.T) { client := framework.NewEtcdClient() etcdStorage := tools.NewEtcdStorage(client, testapi.Codec(), "") framework.WithEtcdKey(func(key string) { testObject := api.ServiceAccount{ObjectMeta: api.ObjectMeta{Name: "foo"}} if err := etcdStorage.SetObj(key, &testObject, nil, 0); err != nil { t.Fatalf("unexpected error: %v", err) } resp, err := client.Get(key, false, false) if err != nil || resp.Node == nil { t.Fatalf("unexpected error: %v %v", err, resp) } decoded, err := testapi.Codec().Decode([]byte(resp.Node.Value)) if err != nil { t.Fatalf("unexpected response: %#v", resp.Node) } result := *decoded.(*api.ServiceAccount) if !api.Semantic.DeepEqual(testObject, result) { t.Errorf("expected: %#v got: %#v", testObject, result) } }) }
func TestExtractObj(t *testing.T) { client := framework.NewEtcdClient() etcdStorage := tools.NewEtcdStorage(client, testapi.Codec(), "") framework.WithEtcdKey(func(key string) { testObject := api.ServiceAccount{ObjectMeta: api.ObjectMeta{Name: "foo"}} coded, err := testapi.Codec().Encode(&testObject) if err != nil { t.Fatalf("unexpected error: %v", err) } _, err = client.Set(key, string(coded), 0) if err != nil { t.Fatalf("unexpected error: %v", err) } result := api.ServiceAccount{} if err := etcdStorage.ExtractObj(key, &result, false); err != nil { t.Fatalf("unexpected error: %v", err) } // Propagate ResourceVersion (it is set automatically). testObject.ObjectMeta.ResourceVersion = result.ObjectMeta.ResourceVersion if !api.Semantic.DeepEqual(testObject, result) { t.Errorf("expected: %#v got: %#v", testObject, result) } }) }