go.etcd.io/etcd@v3.3.27+incompatible/clientv3/integration/namespace_test.go (about) 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 15 package integration 16 17 import ( 18 "context" 19 "reflect" 20 "testing" 21 22 "github.com/coreos/etcd/clientv3" 23 "github.com/coreos/etcd/clientv3/namespace" 24 "github.com/coreos/etcd/integration" 25 "github.com/coreos/etcd/mvcc/mvccpb" 26 "github.com/coreos/etcd/pkg/testutil" 27 ) 28 29 func TestNamespacePutGet(t *testing.T) { 30 defer testutil.AfterTest(t) 31 32 clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1}) 33 defer clus.Terminate(t) 34 35 c := clus.Client(0) 36 nsKV := namespace.NewKV(c.KV, "foo/") 37 38 if _, err := nsKV.Put(context.TODO(), "abc", "bar"); err != nil { 39 t.Fatal(err) 40 } 41 resp, err := nsKV.Get(context.TODO(), "abc") 42 if err != nil { 43 t.Fatal(err) 44 } 45 if string(resp.Kvs[0].Key) != "abc" { 46 t.Errorf("expected key=%q, got key=%q", "abc", resp.Kvs[0].Key) 47 } 48 49 resp, err = c.Get(context.TODO(), "foo/abc") 50 if err != nil { 51 t.Fatal(err) 52 } 53 if string(resp.Kvs[0].Value) != "bar" { 54 t.Errorf("expected value=%q, got value=%q", "bar", resp.Kvs[0].Value) 55 } 56 } 57 58 func TestNamespaceWatch(t *testing.T) { 59 defer testutil.AfterTest(t) 60 61 clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1}) 62 defer clus.Terminate(t) 63 64 c := clus.Client(0) 65 nsKV := namespace.NewKV(c.KV, "foo/") 66 nsWatcher := namespace.NewWatcher(c.Watcher, "foo/") 67 68 if _, err := nsKV.Put(context.TODO(), "abc", "bar"); err != nil { 69 t.Fatal(err) 70 } 71 72 nsWch := nsWatcher.Watch(context.TODO(), "abc", clientv3.WithRev(1)) 73 wkv := &mvccpb.KeyValue{Key: []byte("abc"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1} 74 if wr := <-nsWch; len(wr.Events) != 1 || !reflect.DeepEqual(wr.Events[0].Kv, wkv) { 75 t.Errorf("expected namespaced event %+v, got %+v", wkv, wr.Events[0].Kv) 76 } 77 78 wch := c.Watch(context.TODO(), "foo/abc", clientv3.WithRev(1)) 79 wkv = &mvccpb.KeyValue{Key: []byte("foo/abc"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1} 80 if wr := <-wch; len(wr.Events) != 1 || !reflect.DeepEqual(wr.Events[0].Kv, wkv) { 81 t.Errorf("expected unnamespaced event %+v, got %+v", wkv, wr) 82 } 83 84 // let client close teardown namespace watch 85 c.Watcher = nsWatcher 86 }