client_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2016 CoreOS, Inc.
  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. "bytes"
  17. "testing"
  18. "github.com/coreos/etcd/clientv3"
  19. "github.com/coreos/etcd/integration"
  20. "github.com/coreos/etcd/lease"
  21. "github.com/coreos/etcd/pkg/testutil"
  22. )
  23. func TestKVPut(t *testing.T) {
  24. defer testutil.AfterTest(t)
  25. tests := []struct {
  26. key, val string
  27. leaseID lease.LeaseID
  28. }{
  29. {"foo", "bar", lease.NoLease},
  30. // TODO: test with leaseID
  31. }
  32. for i, tt := range tests {
  33. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  34. defer clus.Terminate(t)
  35. kv := clientv3.NewKV(clus.RandClient())
  36. if _, err := kv.Put(tt.key, tt.val, tt.leaseID); err != nil {
  37. t.Fatalf("#%d: couldn't put %q (%v)", i, tt.key, err)
  38. }
  39. resp, err := kv.Get(tt.key, 0)
  40. if err != nil {
  41. t.Fatalf("#%d: couldn't get key (%v)", i, err)
  42. }
  43. if len(resp.Kvs) != 1 {
  44. t.Fatalf("#%d: expected 1 key, got %d", i, len(resp.Kvs))
  45. }
  46. if !bytes.Equal([]byte(tt.val), resp.Kvs[0].Value) {
  47. t.Errorf("#%d: val = %s, want %s", i, tt.val, resp.Kvs[0].Value)
  48. }
  49. if tt.leaseID != lease.LeaseID(resp.Kvs[0].Lease) {
  50. t.Errorf("#%d: val = %d, want %d", i, tt.leaseID, resp.Kvs[0].Lease)
  51. }
  52. }
  53. }