namespace_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2017 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package integration
  15. import (
  16. "context"
  17. "reflect"
  18. "testing"
  19. "go.etcd.io/etcd/clientv3"
  20. "go.etcd.io/etcd/clientv3/namespace"
  21. "go.etcd.io/etcd/integration"
  22. "go.etcd.io/etcd/mvcc/mvccpb"
  23. "go.etcd.io/etcd/pkg/testutil"
  24. )
  25. func TestNamespacePutGet(t *testing.T) {
  26. defer testutil.AfterTest(t)
  27. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  28. defer clus.Terminate(t)
  29. c := clus.Client(0)
  30. nsKV := namespace.NewKV(c.KV, "foo/")
  31. if _, err := nsKV.Put(context.TODO(), "abc", "bar"); err != nil {
  32. t.Fatal(err)
  33. }
  34. resp, err := nsKV.Get(context.TODO(), "abc")
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. if string(resp.Kvs[0].Key) != "abc" {
  39. t.Errorf("expected key=%q, got key=%q", "abc", resp.Kvs[0].Key)
  40. }
  41. resp, err = c.Get(context.TODO(), "foo/abc")
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. if string(resp.Kvs[0].Value) != "bar" {
  46. t.Errorf("expected value=%q, got value=%q", "bar", resp.Kvs[0].Value)
  47. }
  48. }
  49. func TestNamespaceWatch(t *testing.T) {
  50. defer testutil.AfterTest(t)
  51. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  52. defer clus.Terminate(t)
  53. c := clus.Client(0)
  54. nsKV := namespace.NewKV(c.KV, "foo/")
  55. nsWatcher := namespace.NewWatcher(c.Watcher, "foo/")
  56. if _, err := nsKV.Put(context.TODO(), "abc", "bar"); err != nil {
  57. t.Fatal(err)
  58. }
  59. nsWch := nsWatcher.Watch(context.TODO(), "abc", clientv3.WithRev(1))
  60. wkv := &mvccpb.KeyValue{Key: []byte("abc"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1}
  61. if wr := <-nsWch; len(wr.Events) != 1 || !reflect.DeepEqual(wr.Events[0].Kv, wkv) {
  62. t.Errorf("expected namespaced event %+v, got %+v", wkv, wr.Events[0].Kv)
  63. }
  64. wch := c.Watch(context.TODO(), "foo/abc", clientv3.WithRev(1))
  65. wkv = &mvccpb.KeyValue{Key: []byte("foo/abc"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1}
  66. if wr := <-wch; len(wr.Events) != 1 || !reflect.DeepEqual(wr.Events[0].Kv, wkv) {
  67. t.Errorf("expected unnamespaced event %+v, got %+v", wkv, wr)
  68. }
  69. // let client close teardown namespace watch
  70. c.Watcher = nsWatcher
  71. }