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