store_test.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. // Copyright 2015 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 store_test
  15. import (
  16. "testing"
  17. "time"
  18. etcdErr "github.com/coreos/etcd/error"
  19. "github.com/coreos/etcd/pkg/testutil"
  20. "github.com/coreos/etcd/store"
  21. )
  22. type StoreCloser interface {
  23. store.Store
  24. Close()
  25. }
  26. func TestNewStoreWithNamespaces(t *testing.T) {
  27. s := newTestStore(t, "/0", "/1")
  28. defer s.Close()
  29. _, err := s.Get("/0", false, false)
  30. testutil.AssertNil(t, err)
  31. _, err = s.Get("/1", false, false)
  32. testutil.AssertNil(t, err)
  33. }
  34. // Ensure that the store can retrieve an existing value.
  35. func TestStoreGetValue(t *testing.T) {
  36. s := newTestStore(t)
  37. defer s.Close()
  38. s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  39. var eidx uint64 = 1
  40. e, err := s.Get("/foo", false, false)
  41. testutil.AssertNil(t, err)
  42. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  43. testutil.AssertEqual(t, e.Action, "get")
  44. testutil.AssertEqual(t, e.Node.Key, "/foo")
  45. testutil.AssertEqual(t, *e.Node.Value, "bar")
  46. }
  47. // Ensure that the store can retrieve a directory in sorted order.
  48. func TestStoreGetSorted(t *testing.T) {
  49. s := newTestStore(t)
  50. defer s.Close()
  51. s.Create("/foo", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  52. s.Create("/foo/x", false, "0", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  53. s.Create("/foo/z", false, "0", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  54. s.Create("/foo/y", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  55. s.Create("/foo/y/a", false, "0", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  56. s.Create("/foo/y/b", false, "0", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  57. var eidx uint64 = 6
  58. e, err := s.Get("/foo", true, true)
  59. testutil.AssertNil(t, err)
  60. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  61. var yNodes store.NodeExterns
  62. sortedStrings := []string{"/foo/x", "/foo/y", "/foo/z"}
  63. for i := range e.Node.Nodes {
  64. node := e.Node.Nodes[i]
  65. if node.Key != sortedStrings[i] {
  66. t.Errorf("expect key = %s, got key = %s", sortedStrings[i], node.Key)
  67. }
  68. if node.Key == "/foo/y" {
  69. yNodes = node.Nodes
  70. }
  71. }
  72. sortedStrings = []string{"/foo/y/a", "/foo/y/b"}
  73. for i := range yNodes {
  74. node := yNodes[i]
  75. if node.Key != sortedStrings[i] {
  76. t.Errorf("expect key = %s, got key = %s", sortedStrings[i], node.Key)
  77. }
  78. }
  79. }
  80. func TestSet(t *testing.T) {
  81. s := newTestStore(t)
  82. defer s.Close()
  83. // Set /foo=""
  84. var eidx uint64 = 1
  85. e, err := s.Set("/foo", false, "", store.TTLOptionSet{ExpireTime: store.Permanent})
  86. testutil.AssertNil(t, err)
  87. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  88. testutil.AssertEqual(t, e.Action, "set")
  89. testutil.AssertEqual(t, e.Node.Key, "/foo")
  90. testutil.AssertFalse(t, e.Node.Dir)
  91. testutil.AssertEqual(t, *e.Node.Value, "")
  92. testutil.AssertNil(t, e.Node.Nodes)
  93. testutil.AssertNil(t, e.Node.Expiration)
  94. testutil.AssertEqual(t, e.Node.TTL, int64(0))
  95. testutil.AssertEqual(t, e.Node.ModifiedIndex, uint64(1))
  96. // Set /foo="bar"
  97. eidx = 2
  98. e, err = s.Set("/foo", false, "bar", store.TTLOptionSet{ExpireTime: store.Permanent})
  99. testutil.AssertNil(t, err)
  100. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  101. testutil.AssertEqual(t, e.Action, "set")
  102. testutil.AssertEqual(t, e.Node.Key, "/foo")
  103. testutil.AssertFalse(t, e.Node.Dir)
  104. testutil.AssertEqual(t, *e.Node.Value, "bar")
  105. testutil.AssertNil(t, e.Node.Nodes)
  106. testutil.AssertNil(t, e.Node.Expiration)
  107. testutil.AssertEqual(t, e.Node.TTL, int64(0))
  108. testutil.AssertEqual(t, e.Node.ModifiedIndex, uint64(2))
  109. // check prevNode
  110. testutil.AssertNotNil(t, e.PrevNode)
  111. testutil.AssertEqual(t, e.PrevNode.Key, "/foo")
  112. testutil.AssertEqual(t, *e.PrevNode.Value, "")
  113. testutil.AssertEqual(t, e.PrevNode.ModifiedIndex, uint64(1))
  114. // Set /foo="baz" (for testing prevNode)
  115. eidx = 3
  116. e, err = s.Set("/foo", false, "baz", store.TTLOptionSet{ExpireTime: store.Permanent})
  117. testutil.AssertNil(t, err)
  118. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  119. testutil.AssertEqual(t, e.Action, "set")
  120. testutil.AssertEqual(t, e.Node.Key, "/foo")
  121. testutil.AssertFalse(t, e.Node.Dir)
  122. testutil.AssertEqual(t, *e.Node.Value, "baz")
  123. testutil.AssertNil(t, e.Node.Nodes)
  124. testutil.AssertNil(t, e.Node.Expiration)
  125. testutil.AssertEqual(t, e.Node.TTL, int64(0))
  126. testutil.AssertEqual(t, e.Node.ModifiedIndex, uint64(3))
  127. // check prevNode
  128. testutil.AssertNotNil(t, e.PrevNode)
  129. testutil.AssertEqual(t, e.PrevNode.Key, "/foo")
  130. testutil.AssertEqual(t, *e.PrevNode.Value, "bar")
  131. testutil.AssertEqual(t, e.PrevNode.ModifiedIndex, uint64(2))
  132. // Set /dir as a directory
  133. eidx = 4
  134. e, err = s.Set("/dir", true, "", store.TTLOptionSet{ExpireTime: store.Permanent})
  135. testutil.AssertNil(t, err)
  136. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  137. testutil.AssertEqual(t, e.Action, "set")
  138. testutil.AssertEqual(t, e.Node.Key, "/dir")
  139. testutil.AssertTrue(t, e.Node.Dir)
  140. testutil.AssertNil(t, e.Node.Value)
  141. testutil.AssertNil(t, e.Node.Nodes)
  142. testutil.AssertNil(t, e.Node.Expiration)
  143. testutil.AssertEqual(t, e.Node.TTL, int64(0))
  144. testutil.AssertEqual(t, e.Node.ModifiedIndex, uint64(4))
  145. }
  146. // Ensure that the store can create a new key if it doesn't already exist.
  147. func TestStoreCreateValue(t *testing.T) {
  148. s := newTestStore(t)
  149. defer s.Close()
  150. // Create /foo=bar
  151. var eidx uint64 = 1
  152. e, err := s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  153. testutil.AssertNil(t, err)
  154. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  155. testutil.AssertEqual(t, e.Action, "create")
  156. testutil.AssertEqual(t, e.Node.Key, "/foo")
  157. testutil.AssertFalse(t, e.Node.Dir)
  158. testutil.AssertEqual(t, *e.Node.Value, "bar")
  159. testutil.AssertNil(t, e.Node.Nodes)
  160. testutil.AssertNil(t, e.Node.Expiration)
  161. testutil.AssertEqual(t, e.Node.TTL, int64(0))
  162. testutil.AssertEqual(t, e.Node.ModifiedIndex, uint64(1))
  163. // Create /empty=""
  164. eidx = 2
  165. e, err = s.Create("/empty", false, "", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  166. testutil.AssertNil(t, err)
  167. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  168. testutil.AssertEqual(t, e.Action, "create")
  169. testutil.AssertEqual(t, e.Node.Key, "/empty")
  170. testutil.AssertFalse(t, e.Node.Dir)
  171. testutil.AssertEqual(t, *e.Node.Value, "")
  172. testutil.AssertNil(t, e.Node.Nodes)
  173. testutil.AssertNil(t, e.Node.Expiration)
  174. testutil.AssertEqual(t, e.Node.TTL, int64(0))
  175. testutil.AssertEqual(t, e.Node.ModifiedIndex, uint64(2))
  176. }
  177. // Ensure that the store can create a new directory if it doesn't already exist.
  178. func TestStoreCreateDirectory(t *testing.T) {
  179. s := newTestStore(t)
  180. defer s.Close()
  181. var eidx uint64 = 1
  182. e, err := s.Create("/foo", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  183. testutil.AssertNil(t, err)
  184. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  185. testutil.AssertEqual(t, e.Action, "create")
  186. testutil.AssertEqual(t, e.Node.Key, "/foo")
  187. testutil.AssertTrue(t, e.Node.Dir)
  188. }
  189. // Ensure that the store fails to create a key if it already exists.
  190. func TestStoreCreateFailsIfExists(t *testing.T) {
  191. s := newTestStore(t)
  192. defer s.Close()
  193. // create /foo as dir
  194. s.Create("/foo", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  195. // create /foo as dir again
  196. e, _err := s.Create("/foo", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  197. err := _err.(*etcdErr.Error)
  198. testutil.AssertEqual(t, err.ErrorCode, etcdErr.EcodeNodeExist)
  199. testutil.AssertEqual(t, err.Message, "Key already exists")
  200. testutil.AssertEqual(t, err.Cause, "/foo")
  201. testutil.AssertEqual(t, err.Index, uint64(1))
  202. testutil.AssertNil(t, e)
  203. }
  204. // Ensure that the store can update a key if it already exists.
  205. func TestStoreUpdateValue(t *testing.T) {
  206. s := newTestStore(t)
  207. defer s.Close()
  208. // create /foo=bar
  209. s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  210. // update /foo="bzr"
  211. var eidx uint64 = 2
  212. e, err := s.Update("/foo", "baz", store.TTLOptionSet{ExpireTime: store.Permanent})
  213. testutil.AssertNil(t, err)
  214. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  215. testutil.AssertEqual(t, e.Action, "update")
  216. testutil.AssertEqual(t, e.Node.Key, "/foo")
  217. testutil.AssertFalse(t, e.Node.Dir)
  218. testutil.AssertEqual(t, *e.Node.Value, "baz")
  219. testutil.AssertEqual(t, e.Node.TTL, int64(0))
  220. testutil.AssertEqual(t, e.Node.ModifiedIndex, uint64(2))
  221. // check prevNode
  222. testutil.AssertEqual(t, e.PrevNode.Key, "/foo")
  223. testutil.AssertEqual(t, *e.PrevNode.Value, "bar")
  224. testutil.AssertEqual(t, e.PrevNode.TTL, int64(0))
  225. testutil.AssertEqual(t, e.PrevNode.ModifiedIndex, uint64(1))
  226. e, _ = s.Get("/foo", false, false)
  227. testutil.AssertEqual(t, *e.Node.Value, "baz")
  228. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  229. // update /foo=""
  230. eidx = 3
  231. e, err = s.Update("/foo", "", store.TTLOptionSet{ExpireTime: store.Permanent})
  232. testutil.AssertNil(t, err)
  233. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  234. testutil.AssertEqual(t, e.Action, "update")
  235. testutil.AssertEqual(t, e.Node.Key, "/foo")
  236. testutil.AssertFalse(t, e.Node.Dir)
  237. testutil.AssertEqual(t, *e.Node.Value, "")
  238. testutil.AssertEqual(t, e.Node.TTL, int64(0))
  239. testutil.AssertEqual(t, e.Node.ModifiedIndex, uint64(3))
  240. // check prevNode
  241. testutil.AssertEqual(t, e.PrevNode.Key, "/foo")
  242. testutil.AssertEqual(t, *e.PrevNode.Value, "baz")
  243. testutil.AssertEqual(t, e.PrevNode.TTL, int64(0))
  244. testutil.AssertEqual(t, e.PrevNode.ModifiedIndex, uint64(2))
  245. e, _ = s.Get("/foo", false, false)
  246. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  247. testutil.AssertEqual(t, *e.Node.Value, "")
  248. }
  249. // Ensure that the store cannot update a directory.
  250. func TestStoreUpdateFailsIfDirectory(t *testing.T) {
  251. s := newTestStore(t)
  252. defer s.Close()
  253. s.Create("/foo", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  254. e, _err := s.Update("/foo", "baz", store.TTLOptionSet{ExpireTime: store.Permanent})
  255. err := _err.(*etcdErr.Error)
  256. testutil.AssertEqual(t, err.ErrorCode, etcdErr.EcodeNotFile)
  257. testutil.AssertEqual(t, err.Message, "Not a file")
  258. testutil.AssertEqual(t, err.Cause, "/foo")
  259. testutil.AssertNil(t, e)
  260. }
  261. // Ensure that the store can delete a value.
  262. func TestStoreDeleteValue(t *testing.T) {
  263. s := newTestStore(t)
  264. defer s.Close()
  265. var eidx uint64 = 2
  266. s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  267. e, err := s.Delete("/foo", false, false)
  268. testutil.AssertNil(t, err)
  269. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  270. testutil.AssertEqual(t, e.Action, "delete")
  271. // check prevNode
  272. testutil.AssertNotNil(t, e.PrevNode)
  273. testutil.AssertEqual(t, e.PrevNode.Key, "/foo")
  274. testutil.AssertEqual(t, *e.PrevNode.Value, "bar")
  275. }
  276. // Ensure that the store can delete a directory if recursive is specified.
  277. func TestStoreDeleteDirectory(t *testing.T) {
  278. s := newTestStore(t)
  279. defer s.Close()
  280. // create directory /foo
  281. var eidx uint64 = 2
  282. s.Create("/foo", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  283. // delete /foo with dir = true and recursive = false
  284. // this should succeed, since the directory is empty
  285. e, err := s.Delete("/foo", true, false)
  286. testutil.AssertNil(t, err)
  287. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  288. testutil.AssertEqual(t, e.Action, "delete")
  289. // check prevNode
  290. testutil.AssertNotNil(t, e.PrevNode)
  291. testutil.AssertEqual(t, e.PrevNode.Key, "/foo")
  292. testutil.AssertEqual(t, e.PrevNode.Dir, true)
  293. // create directory /foo and directory /foo/bar
  294. _, err = s.Create("/foo/bar", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  295. testutil.AssertNil(t, err)
  296. // delete /foo with dir = true and recursive = false
  297. // this should fail, since the directory is not empty
  298. _, err = s.Delete("/foo", true, false)
  299. testutil.AssertNotNil(t, err)
  300. // delete /foo with dir=false and recursive = true
  301. // this should succeed, since recursive implies dir=true
  302. // and recursively delete should be able to delete all
  303. // items under the given directory
  304. e, err = s.Delete("/foo", false, true)
  305. testutil.AssertNil(t, err)
  306. testutil.AssertEqual(t, e.Action, "delete")
  307. }
  308. // Ensure that the store cannot delete a directory if both of recursive
  309. // and dir are not specified.
  310. func TestStoreDeleteDirectoryFailsIfNonRecursiveAndDir(t *testing.T) {
  311. s := newTestStore(t)
  312. defer s.Close()
  313. s.Create("/foo", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  314. e, _err := s.Delete("/foo", false, false)
  315. err := _err.(*etcdErr.Error)
  316. testutil.AssertEqual(t, err.ErrorCode, etcdErr.EcodeNotFile)
  317. testutil.AssertEqual(t, err.Message, "Not a file")
  318. testutil.AssertNil(t, e)
  319. }
  320. func TestRootRdOnly(t *testing.T) {
  321. s := newTestStore(t, "/0")
  322. defer s.Close()
  323. for _, tt := range []string{"/", "/0"} {
  324. _, err := s.Set(tt, true, "", store.TTLOptionSet{ExpireTime: store.Permanent})
  325. testutil.AssertNotNil(t, err)
  326. _, err = s.Delete(tt, true, true)
  327. testutil.AssertNotNil(t, err)
  328. _, err = s.Create(tt, true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  329. testutil.AssertNotNil(t, err)
  330. _, err = s.Update(tt, "", store.TTLOptionSet{ExpireTime: store.Permanent})
  331. testutil.AssertNotNil(t, err)
  332. _, err = s.CompareAndSwap(tt, "", 0, "", store.TTLOptionSet{ExpireTime: store.Permanent})
  333. testutil.AssertNotNil(t, err)
  334. }
  335. }
  336. func TestStoreCompareAndDeletePrevValue(t *testing.T) {
  337. s := newTestStore(t)
  338. defer s.Close()
  339. var eidx uint64 = 2
  340. s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  341. e, err := s.CompareAndDelete("/foo", "bar", 0)
  342. testutil.AssertNil(t, err)
  343. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  344. testutil.AssertEqual(t, e.Action, "compareAndDelete")
  345. testutil.AssertEqual(t, e.Node.Key, "/foo")
  346. // check prevNode
  347. testutil.AssertNotNil(t, e.PrevNode)
  348. testutil.AssertEqual(t, e.PrevNode.Key, "/foo")
  349. testutil.AssertEqual(t, *e.PrevNode.Value, "bar")
  350. testutil.AssertEqual(t, e.PrevNode.ModifiedIndex, uint64(1))
  351. testutil.AssertEqual(t, e.PrevNode.CreatedIndex, uint64(1))
  352. }
  353. func TestStoreCompareAndDeletePrevValueFailsIfNotMatch(t *testing.T) {
  354. s := newTestStore(t)
  355. defer s.Close()
  356. var eidx uint64 = 1
  357. s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  358. e, _err := s.CompareAndDelete("/foo", "baz", 0)
  359. err := _err.(*etcdErr.Error)
  360. testutil.AssertEqual(t, err.ErrorCode, etcdErr.EcodeTestFailed)
  361. testutil.AssertEqual(t, err.Message, "Compare failed")
  362. testutil.AssertNil(t, e)
  363. e, _ = s.Get("/foo", false, false)
  364. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  365. testutil.AssertEqual(t, *e.Node.Value, "bar")
  366. }
  367. func TestStoreCompareAndDeletePrevIndex(t *testing.T) {
  368. s := newTestStore(t)
  369. defer s.Close()
  370. var eidx uint64 = 2
  371. s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  372. e, err := s.CompareAndDelete("/foo", "", 1)
  373. testutil.AssertNil(t, err)
  374. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  375. testutil.AssertEqual(t, e.Action, "compareAndDelete")
  376. // check prevNode
  377. testutil.AssertNotNil(t, e.PrevNode)
  378. testutil.AssertEqual(t, e.PrevNode.Key, "/foo")
  379. testutil.AssertEqual(t, *e.PrevNode.Value, "bar")
  380. testutil.AssertEqual(t, e.PrevNode.ModifiedIndex, uint64(1))
  381. testutil.AssertEqual(t, e.PrevNode.CreatedIndex, uint64(1))
  382. }
  383. func TestStoreCompareAndDeletePrevIndexFailsIfNotMatch(t *testing.T) {
  384. s := newTestStore(t)
  385. defer s.Close()
  386. var eidx uint64 = 1
  387. s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  388. e, _err := s.CompareAndDelete("/foo", "", 100)
  389. testutil.AssertNotNil(t, _err)
  390. err := _err.(*etcdErr.Error)
  391. testutil.AssertEqual(t, err.ErrorCode, etcdErr.EcodeTestFailed)
  392. testutil.AssertEqual(t, err.Message, "Compare failed")
  393. testutil.AssertNil(t, e)
  394. e, _ = s.Get("/foo", false, false)
  395. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  396. testutil.AssertEqual(t, *e.Node.Value, "bar")
  397. }
  398. // Ensure that the store cannot delete a directory.
  399. func TestStoreCompareAndDeleteDirectoryFail(t *testing.T) {
  400. s := newTestStore(t)
  401. defer s.Close()
  402. s.Create("/foo", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  403. _, _err := s.CompareAndDelete("/foo", "", 0)
  404. testutil.AssertNotNil(t, _err)
  405. err := _err.(*etcdErr.Error)
  406. testutil.AssertEqual(t, err.ErrorCode, etcdErr.EcodeNotFile)
  407. }
  408. // Ensure that the store can conditionally update a key if it has a previous value.
  409. func TestStoreCompareAndSwapPrevValue(t *testing.T) {
  410. s := newTestStore(t)
  411. defer s.Close()
  412. var eidx uint64 = 2
  413. s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  414. e, err := s.CompareAndSwap("/foo", "bar", 0, "baz", store.TTLOptionSet{ExpireTime: store.Permanent})
  415. testutil.AssertNil(t, err)
  416. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  417. testutil.AssertEqual(t, e.Action, "compareAndSwap")
  418. testutil.AssertEqual(t, *e.Node.Value, "baz")
  419. // check prevNode
  420. testutil.AssertNotNil(t, e.PrevNode)
  421. testutil.AssertEqual(t, e.PrevNode.Key, "/foo")
  422. testutil.AssertEqual(t, *e.PrevNode.Value, "bar")
  423. testutil.AssertEqual(t, e.PrevNode.ModifiedIndex, uint64(1))
  424. testutil.AssertEqual(t, e.PrevNode.CreatedIndex, uint64(1))
  425. e, _ = s.Get("/foo", false, false)
  426. testutil.AssertEqual(t, *e.Node.Value, "baz")
  427. }
  428. // Ensure that the store cannot conditionally update a key if it has the wrong previous value.
  429. func TestStoreCompareAndSwapPrevValueFailsIfNotMatch(t *testing.T) {
  430. s := newTestStore(t)
  431. defer s.Close()
  432. var eidx uint64 = 1
  433. s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  434. e, _err := s.CompareAndSwap("/foo", "wrong_value", 0, "baz", store.TTLOptionSet{ExpireTime: store.Permanent})
  435. err := _err.(*etcdErr.Error)
  436. testutil.AssertEqual(t, err.ErrorCode, etcdErr.EcodeTestFailed)
  437. testutil.AssertEqual(t, err.Message, "Compare failed")
  438. testutil.AssertNil(t, e)
  439. e, _ = s.Get("/foo", false, false)
  440. testutil.AssertEqual(t, *e.Node.Value, "bar")
  441. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  442. }
  443. // Ensure that the store can conditionally update a key if it has a previous index.
  444. func TestStoreCompareAndSwapPrevIndex(t *testing.T) {
  445. s := newTestStore(t)
  446. defer s.Close()
  447. var eidx uint64 = 2
  448. s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  449. e, err := s.CompareAndSwap("/foo", "", 1, "baz", store.TTLOptionSet{ExpireTime: store.Permanent})
  450. testutil.AssertNil(t, err)
  451. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  452. testutil.AssertEqual(t, e.Action, "compareAndSwap")
  453. testutil.AssertEqual(t, *e.Node.Value, "baz")
  454. // check prevNode
  455. testutil.AssertNotNil(t, e.PrevNode)
  456. testutil.AssertEqual(t, e.PrevNode.Key, "/foo")
  457. testutil.AssertEqual(t, *e.PrevNode.Value, "bar")
  458. testutil.AssertEqual(t, e.PrevNode.ModifiedIndex, uint64(1))
  459. testutil.AssertEqual(t, e.PrevNode.CreatedIndex, uint64(1))
  460. e, _ = s.Get("/foo", false, false)
  461. testutil.AssertEqual(t, *e.Node.Value, "baz")
  462. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  463. }
  464. // Ensure that the store cannot conditionally update a key if it has the wrong previous index.
  465. func TestStoreCompareAndSwapPrevIndexFailsIfNotMatch(t *testing.T) {
  466. s := newTestStore(t)
  467. defer s.Close()
  468. var eidx uint64 = 1
  469. s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  470. e, _err := s.CompareAndSwap("/foo", "", 100, "baz", store.TTLOptionSet{ExpireTime: store.Permanent})
  471. err := _err.(*etcdErr.Error)
  472. testutil.AssertEqual(t, err.ErrorCode, etcdErr.EcodeTestFailed)
  473. testutil.AssertEqual(t, err.Message, "Compare failed")
  474. testutil.AssertNil(t, e)
  475. e, _ = s.Get("/foo", false, false)
  476. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  477. testutil.AssertEqual(t, *e.Node.Value, "bar")
  478. }
  479. // Ensure that the store can watch for key creation.
  480. func TestStoreWatchCreate(t *testing.T) {
  481. s := newTestStore(t)
  482. defer s.Close()
  483. var eidx uint64 = 0
  484. w, _ := s.Watch("/foo", false, false, 0)
  485. c := w.EventChan()
  486. testutil.AssertEqual(t, w.StartIndex(), eidx)
  487. s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  488. eidx = 1
  489. e := timeoutSelect(t, c)
  490. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  491. testutil.AssertEqual(t, e.Action, "create")
  492. testutil.AssertEqual(t, e.Node.Key, "/foo")
  493. select {
  494. case e = <-w.EventChan():
  495. testutil.AssertNil(t, e)
  496. case <-time.After(100 * time.Millisecond):
  497. }
  498. }
  499. // Ensure that the store can watch for recursive key creation.
  500. func TestStoreWatchRecursiveCreate(t *testing.T) {
  501. s := newTestStore(t)
  502. defer s.Close()
  503. var eidx uint64 = 0
  504. w, err := s.Watch("/foo", true, false, 0)
  505. testutil.AssertNil(t, err)
  506. testutil.AssertEqual(t, w.StartIndex(), eidx)
  507. eidx = 1
  508. s.Create("/foo/bar", false, "baz", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  509. e := timeoutSelect(t, w.EventChan())
  510. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  511. testutil.AssertEqual(t, e.Action, "create")
  512. testutil.AssertEqual(t, e.Node.Key, "/foo/bar")
  513. }
  514. // Ensure that the store can watch for key updates.
  515. func TestStoreWatchUpdate(t *testing.T) {
  516. s := newTestStore(t)
  517. defer s.Close()
  518. var eidx uint64 = 1
  519. s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  520. w, _ := s.Watch("/foo", false, false, 0)
  521. testutil.AssertEqual(t, w.StartIndex(), eidx)
  522. eidx = 2
  523. s.Update("/foo", "baz", store.TTLOptionSet{ExpireTime: store.Permanent})
  524. e := timeoutSelect(t, w.EventChan())
  525. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  526. testutil.AssertEqual(t, e.Action, "update")
  527. testutil.AssertEqual(t, e.Node.Key, "/foo")
  528. }
  529. // Ensure that the store can watch for recursive key updates.
  530. func TestStoreWatchRecursiveUpdate(t *testing.T) {
  531. s := newTestStore(t)
  532. defer s.Close()
  533. var eidx uint64 = 1
  534. s.Create("/foo/bar", false, "baz", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  535. w, err := s.Watch("/foo", true, false, 0)
  536. testutil.AssertNil(t, err)
  537. testutil.AssertEqual(t, w.StartIndex(), eidx)
  538. eidx = 2
  539. s.Update("/foo/bar", "baz", store.TTLOptionSet{ExpireTime: store.Permanent})
  540. e := timeoutSelect(t, w.EventChan())
  541. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  542. testutil.AssertEqual(t, e.Action, "update")
  543. testutil.AssertEqual(t, e.Node.Key, "/foo/bar")
  544. }
  545. // Ensure that the store can watch for key deletions.
  546. func TestStoreWatchDelete(t *testing.T) {
  547. s := newTestStore(t)
  548. defer s.Close()
  549. var eidx uint64 = 1
  550. s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  551. w, _ := s.Watch("/foo", false, false, 0)
  552. testutil.AssertEqual(t, w.StartIndex(), eidx)
  553. eidx = 2
  554. s.Delete("/foo", false, false)
  555. e := timeoutSelect(t, w.EventChan())
  556. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  557. testutil.AssertEqual(t, e.Action, "delete")
  558. testutil.AssertEqual(t, e.Node.Key, "/foo")
  559. }
  560. // Ensure that the store can watch for recursive key deletions.
  561. func TestStoreWatchRecursiveDelete(t *testing.T) {
  562. s := newTestStore(t)
  563. defer s.Close()
  564. var eidx uint64 = 1
  565. s.Create("/foo/bar", false, "baz", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  566. w, err := s.Watch("/foo", true, false, 0)
  567. testutil.AssertNil(t, err)
  568. testutil.AssertEqual(t, w.StartIndex(), eidx)
  569. eidx = 2
  570. s.Delete("/foo/bar", false, false)
  571. e := timeoutSelect(t, w.EventChan())
  572. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  573. testutil.AssertEqual(t, e.Action, "delete")
  574. testutil.AssertEqual(t, e.Node.Key, "/foo/bar")
  575. }
  576. // Ensure that the store can watch for CAS updates.
  577. func TestStoreWatchCompareAndSwap(t *testing.T) {
  578. s := newTestStore(t)
  579. defer s.Close()
  580. var eidx uint64 = 1
  581. s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  582. w, _ := s.Watch("/foo", false, false, 0)
  583. testutil.AssertEqual(t, w.StartIndex(), eidx)
  584. eidx = 2
  585. s.CompareAndSwap("/foo", "bar", 0, "baz", store.TTLOptionSet{ExpireTime: store.Permanent})
  586. e := timeoutSelect(t, w.EventChan())
  587. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  588. testutil.AssertEqual(t, e.Action, "compareAndSwap")
  589. testutil.AssertEqual(t, e.Node.Key, "/foo")
  590. }
  591. // Ensure that the store can watch for recursive CAS updates.
  592. func TestStoreWatchRecursiveCompareAndSwap(t *testing.T) {
  593. s := newTestStore(t)
  594. defer s.Close()
  595. var eidx uint64 = 1
  596. s.Create("/foo/bar", false, "baz", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  597. w, _ := s.Watch("/foo", true, false, 0)
  598. testutil.AssertEqual(t, w.StartIndex(), eidx)
  599. eidx = 2
  600. s.CompareAndSwap("/foo/bar", "baz", 0, "bat", store.TTLOptionSet{ExpireTime: store.Permanent})
  601. e := timeoutSelect(t, w.EventChan())
  602. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  603. testutil.AssertEqual(t, e.Action, "compareAndSwap")
  604. testutil.AssertEqual(t, e.Node.Key, "/foo/bar")
  605. }
  606. // Ensure that the store can watch in streaming mode.
  607. func TestStoreWatchStream(t *testing.T) {
  608. s := newTestStore(t)
  609. defer s.Close()
  610. var eidx uint64 = 1
  611. w, _ := s.Watch("/foo", false, true, 0)
  612. // first modification
  613. s.Create("/foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  614. e := timeoutSelect(t, w.EventChan())
  615. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  616. testutil.AssertEqual(t, e.Action, "create")
  617. testutil.AssertEqual(t, e.Node.Key, "/foo")
  618. testutil.AssertEqual(t, *e.Node.Value, "bar")
  619. select {
  620. case e = <-w.EventChan():
  621. testutil.AssertNil(t, e)
  622. case <-time.After(100 * time.Millisecond):
  623. }
  624. // second modification
  625. eidx = 2
  626. s.Update("/foo", "baz", store.TTLOptionSet{ExpireTime: store.Permanent})
  627. e = timeoutSelect(t, w.EventChan())
  628. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  629. testutil.AssertEqual(t, e.Action, "update")
  630. testutil.AssertEqual(t, e.Node.Key, "/foo")
  631. testutil.AssertEqual(t, *e.Node.Value, "baz")
  632. select {
  633. case e = <-w.EventChan():
  634. testutil.AssertNil(t, e)
  635. case <-time.After(100 * time.Millisecond):
  636. }
  637. }
  638. // Ensure that the store can watch for hidden keys as long as it's an exact path match.
  639. func TestStoreWatchCreateWithHiddenKey(t *testing.T) {
  640. s := newTestStore(t)
  641. defer s.Close()
  642. var eidx uint64 = 1
  643. w, _ := s.Watch("/_foo", false, false, 0)
  644. s.Create("/_foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  645. e := timeoutSelect(t, w.EventChan())
  646. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  647. testutil.AssertEqual(t, e.Action, "create")
  648. testutil.AssertEqual(t, e.Node.Key, "/_foo")
  649. select {
  650. case e = <-w.EventChan():
  651. testutil.AssertNil(t, e)
  652. case <-time.After(100 * time.Millisecond):
  653. }
  654. }
  655. // Ensure that the store doesn't see hidden key creates without an exact path match in recursive mode.
  656. func TestStoreWatchRecursiveCreateWithHiddenKey(t *testing.T) {
  657. s := newTestStore(t)
  658. defer s.Close()
  659. w, _ := s.Watch("/foo", true, false, 0)
  660. s.Create("/foo/_bar", false, "baz", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  661. e := nbselect(w.EventChan())
  662. testutil.AssertNil(t, e)
  663. w, _ = s.Watch("/foo", true, false, 0)
  664. s.Create("/foo/_baz", true, "", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  665. select {
  666. case e = <-w.EventChan():
  667. testutil.AssertNil(t, e)
  668. case <-time.After(100 * time.Millisecond):
  669. }
  670. s.Create("/foo/_baz/quux", false, "quux", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  671. select {
  672. case e = <-w.EventChan():
  673. testutil.AssertNil(t, e)
  674. case <-time.After(100 * time.Millisecond):
  675. }
  676. }
  677. // Ensure that the store doesn't see hidden key updates.
  678. func TestStoreWatchUpdateWithHiddenKey(t *testing.T) {
  679. s := newTestStore(t)
  680. defer s.Close()
  681. s.Create("/_foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  682. w, _ := s.Watch("/_foo", false, false, 0)
  683. s.Update("/_foo", "baz", store.TTLOptionSet{ExpireTime: store.Permanent})
  684. e := timeoutSelect(t, w.EventChan())
  685. testutil.AssertEqual(t, e.Action, "update")
  686. testutil.AssertEqual(t, e.Node.Key, "/_foo")
  687. e = nbselect(w.EventChan())
  688. testutil.AssertNil(t, e)
  689. }
  690. // Ensure that the store doesn't see hidden key updates without an exact path match in recursive mode.
  691. func TestStoreWatchRecursiveUpdateWithHiddenKey(t *testing.T) {
  692. s := newTestStore(t)
  693. defer s.Close()
  694. s.Create("/foo/_bar", false, "baz", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  695. w, _ := s.Watch("/foo", true, false, 0)
  696. s.Update("/foo/_bar", "baz", store.TTLOptionSet{ExpireTime: store.Permanent})
  697. e := nbselect(w.EventChan())
  698. testutil.AssertNil(t, e)
  699. }
  700. // Ensure that the store can watch for key deletions.
  701. func TestStoreWatchDeleteWithHiddenKey(t *testing.T) {
  702. s := newTestStore(t)
  703. defer s.Close()
  704. var eidx uint64 = 2
  705. s.Create("/_foo", false, "bar", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  706. w, _ := s.Watch("/_foo", false, false, 0)
  707. s.Delete("/_foo", false, false)
  708. e := timeoutSelect(t, w.EventChan())
  709. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  710. testutil.AssertEqual(t, e.Action, "delete")
  711. testutil.AssertEqual(t, e.Node.Key, "/_foo")
  712. e = nbselect(w.EventChan())
  713. testutil.AssertNil(t, e)
  714. }
  715. // Ensure that the store doesn't see hidden key deletes without an exact path match in recursive mode.
  716. func TestStoreWatchRecursiveDeleteWithHiddenKey(t *testing.T) {
  717. s := newTestStore(t)
  718. defer s.Close()
  719. s.Create("/foo/_bar", false, "baz", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  720. w, _ := s.Watch("/foo", true, false, 0)
  721. s.Delete("/foo/_bar", false, false)
  722. e := nbselect(w.EventChan())
  723. testutil.AssertNil(t, e)
  724. }
  725. // Ensure that the store does see hidden key creates if watching deeper than a hidden key in recursive mode.
  726. func TestStoreWatchRecursiveCreateDeeperThanHiddenKey(t *testing.T) {
  727. s := newTestStore(t)
  728. defer s.Close()
  729. var eidx uint64 = 1
  730. w, _ := s.Watch("/_foo/bar", true, false, 0)
  731. s.Create("/_foo/bar/baz", false, "baz", false, store.TTLOptionSet{ExpireTime: store.Permanent})
  732. e := timeoutSelect(t, w.EventChan())
  733. testutil.AssertNotNil(t, e)
  734. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  735. testutil.AssertEqual(t, e.Action, "create")
  736. testutil.AssertEqual(t, e.Node.Key, "/_foo/bar/baz")
  737. }
  738. // Ensure that slow consumers are handled properly.
  739. //
  740. // Since Watcher.EventChan() has a buffer of size 100 we can only queue 100
  741. // event per watcher. If the consumer cannot consume the event on time and
  742. // another event arrives, the channel is closed and event is discarded.
  743. // This test ensures that after closing the channel, the store can continue
  744. // to operate correctly.
  745. func TestStoreWatchSlowConsumer(t *testing.T) {
  746. s := newTestStore(t)
  747. defer s.Close()
  748. s.Watch("/foo", true, true, 0) // stream must be true
  749. // Fill watch channel with 100 events
  750. for i := 1; i <= 100; i++ {
  751. s.Set("/foo", false, string(i), store.TTLOptionSet{ExpireTime: store.Permanent}) // ok
  752. }
  753. // testutil.AssertEqual(t, s.WatcherHub.count, int64(1))
  754. s.Set("/foo", false, "101", store.TTLOptionSet{ExpireTime: store.Permanent}) // ok
  755. // remove watcher
  756. // testutil.AssertEqual(t, s.WatcherHub.count, int64(0))
  757. s.Set("/foo", false, "102", store.TTLOptionSet{ExpireTime: store.Permanent}) // must not panic
  758. }
  759. // Performs a non-blocking select on an event channel.
  760. func nbselect(c <-chan *store.Event) *store.Event {
  761. select {
  762. case e := <-c:
  763. return e
  764. default:
  765. return nil
  766. }
  767. }
  768. // Performs a non-blocking select on an event channel.
  769. func timeoutSelect(t *testing.T, c <-chan *store.Event) *store.Event {
  770. select {
  771. case e := <-c:
  772. return e
  773. case <-time.After(time.Second):
  774. t.Errorf("timed out waiting on event")
  775. return nil
  776. }
  777. }