store_test.go 28 KB

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