store_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2019 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 v2v3_test
  15. import (
  16. "log"
  17. "strings"
  18. "testing"
  19. "go.etcd.io/etcd/clientv3"
  20. "go.etcd.io/etcd/etcdserver/api/v2store"
  21. "go.etcd.io/etcd/etcdserver/api/v2v3"
  22. )
  23. // TODO: fix tests
  24. func TestCreateKV(t *testing.T) {
  25. testCases := []struct {
  26. key string
  27. value string
  28. nodes int
  29. unique bool
  30. wantErr bool
  31. wantKeyMatch bool
  32. }{
  33. {key: "/cdir/create", value: "1", nodes: 1, wantKeyMatch: true},
  34. {key: "/cdir/create", value: "4", wantErr: true},
  35. // TODO: unique doesn't create nodes, skip these tests for now
  36. //{key: "hello", value: "2", unique: true, wantKeyMatch: false},
  37. //{key: "hello", value: "3", unique: true, wantKeyMatch: false},
  38. }
  39. cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints})
  40. if err != nil {
  41. log.Fatal(err)
  42. }
  43. defer cli.Close()
  44. v2 := v2v3.NewStore(cli, "")
  45. for ti, tc := range testCases {
  46. ev, err := v2.Create(tc.key, false, tc.value, tc.unique, v2store.TTLOptionSet{})
  47. if tc.wantErr && err != nil {
  48. continue
  49. }
  50. if err != nil {
  51. t.Skipf("%d: got err %v", ti, err)
  52. }
  53. if tc.wantKeyMatch && tc.key != ev.Node.Key {
  54. t.Skipf("%d: %v != %v", ti, tc.key, ev.Node.Key)
  55. }
  56. if !tc.wantKeyMatch && !strings.HasPrefix(ev.Node.Key, tc.key) {
  57. t.Skipf("%d: %v is not prefix of %v", ti, tc.key, ev.Node.Key)
  58. }
  59. evg, err := v2.Get(tc.key, false, false)
  60. if evg.Node.CreatedIndex != ev.Node.CreatedIndex {
  61. t.Skipf("%d: %v != %v", ti, evg.Node.CreatedIndex, ev.Node.CreatedIndex)
  62. }
  63. t.Logf("%d: %v %s %v\n", ti, ev.Node.Key, *ev.Node.Value, ev.Node.CreatedIndex)
  64. }
  65. }
  66. func TestSetKV(t *testing.T) {
  67. testCases := []struct {
  68. key string
  69. value string
  70. dir bool
  71. wantIndexMatch bool
  72. }{
  73. {key: "/sdir/set", value: "1", wantIndexMatch: true},
  74. {key: "/sdir/set", value: "4", wantIndexMatch: false},
  75. }
  76. cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints})
  77. if err != nil {
  78. log.Fatal(err)
  79. }
  80. defer cli.Close()
  81. v2 := v2v3.NewStore(cli, "")
  82. for ti, tc := range testCases {
  83. ev, err := v2.Set(tc.key, false, tc.value, v2store.TTLOptionSet{})
  84. if err != nil {
  85. t.Skipf("%d: got err %v", ti, err)
  86. }
  87. if tc.value != *ev.Node.Value {
  88. t.Skipf("%d: %v != %v", ti, tc.value, *ev.Node.Value)
  89. }
  90. if tc.wantIndexMatch && ev.Node.CreatedIndex != ev.Node.ModifiedIndex {
  91. t.Skipf("%d: index %v != %v", ti, ev.Node.CreatedIndex, ev.Node.ModifiedIndex)
  92. }
  93. t.Logf("%d: %v %s %v\n", ti, ev.Node.Key, *ev.Node.Value, ev.Node.CreatedIndex)
  94. }
  95. }
  96. func TestCreateSetDir(t *testing.T) {
  97. testCases := []struct {
  98. dir string
  99. }{
  100. {dir: "/ddir/1/2/3"},
  101. {dir: "/ddir/1/2/3"},
  102. }
  103. cli, err := clientv3.New(clientv3.Config{Endpoints: endpoints})
  104. if err != nil {
  105. log.Fatal(err)
  106. }
  107. defer cli.Close()
  108. v2 := v2v3.NewStore(cli, "")
  109. for ti, tc := range testCases {
  110. ev, err := v2.Create(tc.dir, true, "", false, v2store.TTLOptionSet{})
  111. if err != nil {
  112. t.Skipf("%d: got err %v", ti, err)
  113. }
  114. _, err = v2.Create(tc.dir, true, "", false, v2store.TTLOptionSet{})
  115. if err == nil {
  116. t.Skipf("%d: expected err got nil", ti)
  117. }
  118. ev, err = v2.Delete("ddir", true, true)
  119. if err != nil {
  120. t.Skipf("%d: got err %v", ti, err)
  121. }
  122. t.Logf("%d: %v %s %v\n", ti, ev.EtcdIndex, ev.PrevNode.Key, ev.PrevNode.CreatedIndex)
  123. }
  124. }