store_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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/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 /dir as a directory
  107. e, err = s.Set("/dir", true, "", Permanent)
  108. assert.Nil(t, err, "")
  109. assert.Equal(t, e.Action, "set", "")
  110. assert.Equal(t, e.Node.Key, "/dir", "")
  111. assert.True(t, e.Node.Dir, "")
  112. assert.Equal(t, e.Node.Value, "", "")
  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. }
  118. // Ensure that the store can create a new key if it doesn't already exist.
  119. func TestStoreCreateValue(t *testing.T) {
  120. s := newStore()
  121. // Create /foo=bar
  122. e, err := s.Create("/foo", false, "bar", false, Permanent)
  123. assert.Nil(t, err, "")
  124. assert.Equal(t, e.Action, "create", "")
  125. assert.Equal(t, e.Node.Key, "/foo", "")
  126. assert.False(t, e.Node.Dir, "")
  127. assert.Equal(t, e.Node.Value, "bar", "")
  128. assert.Nil(t, e.Node.Nodes, "")
  129. assert.Nil(t, e.Node.Expiration, "")
  130. assert.Equal(t, e.Node.TTL, 0, "")
  131. assert.Equal(t, e.Node.ModifiedIndex, uint64(1), "")
  132. // Create /empty=""
  133. e, err = s.Create("/empty", false, "", false, Permanent)
  134. assert.Nil(t, err, "")
  135. assert.Equal(t, e.Action, "create", "")
  136. assert.Equal(t, e.Node.Key, "/empty", "")
  137. assert.False(t, e.Node.Dir, "")
  138. assert.Equal(t, e.Node.Value, "", "")
  139. assert.Nil(t, e.Node.Nodes, "")
  140. assert.Nil(t, e.Node.Expiration, "")
  141. assert.Equal(t, e.Node.TTL, 0, "")
  142. assert.Equal(t, e.Node.ModifiedIndex, uint64(2), "")
  143. }
  144. // Ensure that the store can create a new directory if it doesn't already exist.
  145. func TestStoreCreateDirectory(t *testing.T) {
  146. s := newStore()
  147. e, err := s.Create("/foo", true, "", false, Permanent)
  148. assert.Nil(t, err, "")
  149. assert.Equal(t, e.Action, "create", "")
  150. assert.Equal(t, e.Node.Key, "/foo", "")
  151. assert.True(t, e.Node.Dir, "")
  152. }
  153. // Ensure that the store fails to create a key if it already exists.
  154. func TestStoreCreateFailsIfExists(t *testing.T) {
  155. s := newStore()
  156. // create /foo as dir
  157. s.Create("/foo", true, "", false, Permanent)
  158. // create /foo as dir again
  159. e, _err := s.Create("/foo", true, "", false, Permanent)
  160. err := _err.(*etcdErr.Error)
  161. assert.Equal(t, err.ErrorCode, etcdErr.EcodeNodeExist, "")
  162. assert.Equal(t, err.Message, "Key already exists", "")
  163. assert.Equal(t, err.Cause, "/foo", "")
  164. assert.Equal(t, err.Index, uint64(1), "")
  165. assert.Nil(t, e, 0, "")
  166. }
  167. // Ensure that the store can update a key if it already exists.
  168. func TestStoreUpdateValue(t *testing.T) {
  169. s := newStore()
  170. // create /foo=bar
  171. s.Create("/foo", false, "bar", false, Permanent)
  172. // update /foo="bzr"
  173. e, err := s.Update("/foo", "baz", Permanent)
  174. assert.Nil(t, err, "")
  175. assert.Equal(t, e.Action, "update", "")
  176. assert.Equal(t, e.Node.Key, "/foo", "")
  177. assert.False(t, e.Node.Dir, "")
  178. assert.Equal(t, e.Node.Value, "baz", "")
  179. assert.Equal(t, e.Node.TTL, 0, "")
  180. assert.Equal(t, e.Node.ModifiedIndex, uint64(2), "")
  181. // check prevNode
  182. assert.Equal(t, e.PrevNode.Key, "/foo", "")
  183. assert.Equal(t, e.PrevNode.Value, "bar", "")
  184. assert.Equal(t, e.PrevNode.TTL, 0, "")
  185. assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(1), "")
  186. e, _ = s.Get("/foo", false, false)
  187. assert.Equal(t, e.Node.Value, "baz", "")
  188. // update /foo=""
  189. e, err = s.Update("/foo", "", 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, "", "")
  195. assert.Equal(t, e.Node.TTL, 0, "")
  196. assert.Equal(t, e.Node.ModifiedIndex, uint64(3), "")
  197. // check prevNode
  198. assert.Equal(t, e.PrevNode.Key, "/foo", "")
  199. assert.Equal(t, e.PrevNode.Value, "baz", "")
  200. assert.Equal(t, e.PrevNode.TTL, 0, "")
  201. assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(2), "")
  202. e, _ = s.Get("/foo", false, false)
  203. assert.Equal(t, e.Node.Value, "", "")
  204. }
  205. // Ensure that the store cannot update a directory.
  206. func TestStoreUpdateFailsIfDirectory(t *testing.T) {
  207. s := newStore()
  208. s.Create("/foo", true, "", false, Permanent)
  209. e, _err := s.Update("/foo", "baz", Permanent)
  210. err := _err.(*etcdErr.Error)
  211. assert.Equal(t, err.ErrorCode, etcdErr.EcodeNotFile, "")
  212. assert.Equal(t, err.Message, "Not a file", "")
  213. assert.Equal(t, err.Cause, "/foo", "")
  214. assert.Nil(t, e, "")
  215. }
  216. // Ensure that the store can update the TTL on a value.
  217. func TestStoreUpdateValueTTL(t *testing.T) {
  218. s := newStore()
  219. c := make(chan bool)
  220. defer func() {
  221. c <- true
  222. }()
  223. go mockSyncService(s.DeleteExpiredKeys, c)
  224. s.Create("/foo", false, "bar", false, Permanent)
  225. _, err := s.Update("/foo", "baz", time.Now().Add(500*time.Millisecond))
  226. e, _ := s.Get("/foo", false, false)
  227. assert.Equal(t, e.Node.Value, "baz", "")
  228. time.Sleep(600 * time.Millisecond)
  229. e, err = s.Get("/foo", false, false)
  230. assert.Nil(t, e, "")
  231. assert.Equal(t, err.(*etcdErr.Error).ErrorCode, etcdErr.EcodeKeyNotFound, "")
  232. }
  233. // Ensure that the store can update the TTL on a directory.
  234. func TestStoreUpdateDirTTL(t *testing.T) {
  235. s := newStore()
  236. c := make(chan bool)
  237. defer func() {
  238. c <- true
  239. }()
  240. go mockSyncService(s.DeleteExpiredKeys, c)
  241. s.Create("/foo", true, "", false, Permanent)
  242. s.Create("/foo/bar", false, "baz", false, Permanent)
  243. _, err := s.Update("/foo", "", time.Now().Add(500*time.Millisecond))
  244. e, _ := s.Get("/foo/bar", false, false)
  245. assert.Equal(t, e.Node.Value, "baz", "")
  246. time.Sleep(600 * time.Millisecond)
  247. e, err = s.Get("/foo/bar", false, false)
  248. assert.Nil(t, e, "")
  249. assert.Equal(t, err.(*etcdErr.Error).ErrorCode, etcdErr.EcodeKeyNotFound, "")
  250. }
  251. // Ensure that the store can delete a value.
  252. func TestStoreDeleteValue(t *testing.T) {
  253. s := newStore()
  254. s.Create("/foo", false, "bar", false, Permanent)
  255. e, err := s.Delete("/foo", false, false)
  256. assert.Nil(t, err, "")
  257. assert.Equal(t, e.Action, "delete", "")
  258. // check pervNode
  259. assert.NotNil(t, e.PrevNode, "")
  260. assert.Equal(t, e.PrevNode.Key, "/foo", "")
  261. assert.Equal(t, e.PrevNode.Value, "bar", "")
  262. }
  263. // Ensure that the store can delete a directory if recursive is specified.
  264. func TestStoreDeleteDiretory(t *testing.T) {
  265. s := newStore()
  266. // create directory /foo
  267. s.Create("/foo", true, "", false, Permanent)
  268. // delete /foo with dir = true and recursive = false
  269. // this should succeed, since the directory is empty
  270. e, err := s.Delete("/foo", true, false)
  271. assert.Nil(t, err, "")
  272. assert.Equal(t, e.Action, "delete", "")
  273. // check pervNode
  274. assert.NotNil(t, e.PrevNode, "")
  275. assert.Equal(t, e.PrevNode.Key, "/foo", "")
  276. assert.Equal(t, e.PrevNode.Dir, true, "")
  277. // create directory /foo and directory /foo/bar
  278. s.Create("/foo/bar", true, "", false, Permanent)
  279. // delete /foo with dir = true and recursive = false
  280. // this should fail, since the directory is not empty
  281. _, err = s.Delete("/foo", true, false)
  282. assert.NotNil(t, err, "")
  283. // delete /foo with dir=false and recursive = true
  284. // this should succeed, since recursive implies dir=true
  285. // and recursively delete should be able to delete all
  286. // items under the given directory
  287. e, err = s.Delete("/foo", false, true)
  288. assert.Nil(t, err, "")
  289. assert.Equal(t, e.Action, "delete", "")
  290. }
  291. // Ensure that the store cannot delete a directory if both of recursive
  292. // and dir are not specified.
  293. func TestStoreDeleteDiretoryFailsIfNonRecursiveAndDir(t *testing.T) {
  294. s := newStore()
  295. s.Create("/foo", true, "", false, Permanent)
  296. e, _err := s.Delete("/foo", false, false)
  297. err := _err.(*etcdErr.Error)
  298. assert.Equal(t, err.ErrorCode, etcdErr.EcodeNotFile, "")
  299. assert.Equal(t, err.Message, "Not a file", "")
  300. assert.Nil(t, e, "")
  301. }
  302. func TestRootRdOnly(t *testing.T) {
  303. s := newStore()
  304. _, err := s.Set("/", true, "", Permanent)
  305. assert.NotNil(t, err, "")
  306. _, err = s.Delete("/", true, true)
  307. assert.NotNil(t, err, "")
  308. _, err = s.Create("/", true, "", false, Permanent)
  309. assert.NotNil(t, err, "")
  310. _, err = s.Update("/", "", Permanent)
  311. assert.NotNil(t, err, "")
  312. _, err = s.CompareAndSwap("/", "", 0, "", Permanent)
  313. assert.NotNil(t, err, "")
  314. }
  315. func TestStoreCompareAndDeletePrevValue(t *testing.T) {
  316. s := newStore()
  317. s.Create("/foo", false, "bar", false, Permanent)
  318. e, err := s.CompareAndDelete("/foo", "bar", 0)
  319. assert.Nil(t, err, "")
  320. assert.Equal(t, e.Action, "compareAndDelete", "")
  321. assert.Equal(t, e.Node.Key, "/foo", "")
  322. // check pervNode
  323. assert.NotNil(t, e.PrevNode, "")
  324. assert.Equal(t, e.PrevNode.Key, "/foo", "")
  325. assert.Equal(t, e.PrevNode.Value, "bar", "")
  326. assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(1), "")
  327. assert.Equal(t, e.PrevNode.CreatedIndex, uint64(1), "")
  328. }
  329. func TestStoreCompareAndDeletePrevValueFailsIfNotMatch(t *testing.T) {
  330. s := newStore()
  331. s.Create("/foo", false, "bar", false, Permanent)
  332. e, _err := s.CompareAndDelete("/foo", "baz", 0)
  333. err := _err.(*etcdErr.Error)
  334. assert.Equal(t, err.ErrorCode, etcdErr.EcodeTestFailed, "")
  335. assert.Equal(t, err.Message, "Compare failed", "")
  336. assert.Nil(t, e, "")
  337. e, _ = s.Get("/foo", false, false)
  338. assert.Equal(t, e.Node.Value, "bar", "")
  339. }
  340. func TestStoreCompareAndDeletePrevIndex(t *testing.T) {
  341. s := newStore()
  342. s.Create("/foo", false, "bar", false, Permanent)
  343. e, err := s.CompareAndDelete("/foo", "", 1)
  344. assert.Nil(t, err, "")
  345. assert.Equal(t, e.Action, "compareAndDelete", "")
  346. // check pervNode
  347. assert.NotNil(t, e.PrevNode, "")
  348. assert.Equal(t, e.PrevNode.Key, "/foo", "")
  349. assert.Equal(t, e.PrevNode.Value, "bar", "")
  350. assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(1), "")
  351. assert.Equal(t, e.PrevNode.CreatedIndex, uint64(1), "")
  352. }
  353. func TestStoreCompareAndDeletePrevIndexFailsIfNotMatch(t *testing.T) {
  354. s := newStore()
  355. s.Create("/foo", false, "bar", false, Permanent)
  356. e, _err := s.CompareAndDelete("/foo", "", 100)
  357. assert.NotNil(t, _err, "")
  358. err := _err.(*etcdErr.Error)
  359. assert.Equal(t, err.ErrorCode, etcdErr.EcodeTestFailed, "")
  360. assert.Equal(t, err.Message, "Compare failed", "")
  361. assert.Nil(t, e, "")
  362. e, _ = s.Get("/foo", false, false)
  363. assert.Equal(t, e.Node.Value, "bar", "")
  364. }
  365. // Ensure that the store cannot delete a directory.
  366. func TestStoreCompareAndDeleteDiretoryFail(t *testing.T) {
  367. s := newStore()
  368. s.Create("/foo", true, "", false, Permanent)
  369. _, _err := s.CompareAndDelete("/foo", "", 0)
  370. assert.NotNil(t, _err, "")
  371. err := _err.(*etcdErr.Error)
  372. assert.Equal(t, err.ErrorCode, etcdErr.EcodeNotFile, "")
  373. }
  374. // Ensure that the store can conditionally update a key if it has a previous value.
  375. func TestStoreCompareAndSwapPrevValue(t *testing.T) {
  376. s := newStore()
  377. s.Create("/foo", false, "bar", false, Permanent)
  378. e, err := s.CompareAndSwap("/foo", "bar", 0, "baz", Permanent)
  379. assert.Nil(t, err, "")
  380. assert.Equal(t, e.Action, "compareAndSwap", "")
  381. assert.Equal(t, e.Node.Value, "baz", "")
  382. // check pervNode
  383. assert.NotNil(t, e.PrevNode, "")
  384. assert.Equal(t, e.PrevNode.Key, "/foo", "")
  385. assert.Equal(t, e.PrevNode.Value, "bar", "")
  386. assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(1), "")
  387. assert.Equal(t, e.PrevNode.CreatedIndex, uint64(1), "")
  388. e, _ = s.Get("/foo", false, false)
  389. assert.Equal(t, e.Node.Value, "baz", "")
  390. }
  391. // Ensure that the store cannot conditionally update a key if it has the wrong previous value.
  392. func TestStoreCompareAndSwapPrevValueFailsIfNotMatch(t *testing.T) {
  393. s := newStore()
  394. s.Create("/foo", false, "bar", false, Permanent)
  395. e, _err := s.CompareAndSwap("/foo", "wrong_value", 0, "baz", Permanent)
  396. err := _err.(*etcdErr.Error)
  397. assert.Equal(t, err.ErrorCode, etcdErr.EcodeTestFailed, "")
  398. assert.Equal(t, err.Message, "Compare failed", "")
  399. assert.Nil(t, e, "")
  400. e, _ = s.Get("/foo", false, false)
  401. assert.Equal(t, e.Node.Value, "bar", "")
  402. }
  403. // Ensure that the store can conditionally update a key if it has a previous index.
  404. func TestStoreCompareAndSwapPrevIndex(t *testing.T) {
  405. s := newStore()
  406. s.Create("/foo", false, "bar", false, Permanent)
  407. e, err := s.CompareAndSwap("/foo", "", 1, "baz", Permanent)
  408. assert.Nil(t, err, "")
  409. assert.Equal(t, e.Action, "compareAndSwap", "")
  410. assert.Equal(t, e.Node.Value, "baz", "")
  411. // check pervNode
  412. assert.NotNil(t, e.PrevNode, "")
  413. assert.Equal(t, e.PrevNode.Key, "/foo", "")
  414. assert.Equal(t, e.PrevNode.Value, "bar", "")
  415. assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(1), "")
  416. assert.Equal(t, e.PrevNode.CreatedIndex, uint64(1), "")
  417. e, _ = s.Get("/foo", false, false)
  418. assert.Equal(t, e.Node.Value, "baz", "")
  419. }
  420. // Ensure that the store cannot conditionally update a key if it has the wrong previous index.
  421. func TestStoreCompareAndSwapPrevIndexFailsIfNotMatch(t *testing.T) {
  422. s := newStore()
  423. s.Create("/foo", false, "bar", false, Permanent)
  424. e, _err := s.CompareAndSwap("/foo", "", 100, "baz", Permanent)
  425. err := _err.(*etcdErr.Error)
  426. assert.Equal(t, err.ErrorCode, etcdErr.EcodeTestFailed, "")
  427. assert.Equal(t, err.Message, "Compare failed", "")
  428. assert.Nil(t, e, "")
  429. e, _ = s.Get("/foo", false, false)
  430. assert.Equal(t, e.Node.Value, "bar", "")
  431. }
  432. // Ensure that the store can watch for key creation.
  433. func TestStoreWatchCreate(t *testing.T) {
  434. s := newStore()
  435. w, _ := s.Watch("/foo", false, false, 0)
  436. c := w.EventChan
  437. s.Create("/foo", false, "bar", false, Permanent)
  438. e := nbselect(c)
  439. assert.Equal(t, e.Action, "create", "")
  440. assert.Equal(t, e.Node.Key, "/foo", "")
  441. e = nbselect(c)
  442. assert.Nil(t, e, "")
  443. }
  444. // Ensure that the store can watch for recursive key creation.
  445. func TestStoreWatchRecursiveCreate(t *testing.T) {
  446. s := newStore()
  447. w, _ := s.Watch("/foo", true, false, 0)
  448. s.Create("/foo/bar", false, "baz", false, Permanent)
  449. e := nbselect(w.EventChan)
  450. assert.Equal(t, e.Action, "create", "")
  451. assert.Equal(t, e.Node.Key, "/foo/bar", "")
  452. }
  453. // Ensure that the store can watch for key updates.
  454. func TestStoreWatchUpdate(t *testing.T) {
  455. s := newStore()
  456. s.Create("/foo", false, "bar", false, Permanent)
  457. w, _ := s.Watch("/foo", false, false, 0)
  458. s.Update("/foo", "baz", Permanent)
  459. e := nbselect(w.EventChan)
  460. assert.Equal(t, e.Action, "update", "")
  461. assert.Equal(t, e.Node.Key, "/foo", "")
  462. }
  463. // Ensure that the store can watch for recursive key updates.
  464. func TestStoreWatchRecursiveUpdate(t *testing.T) {
  465. s := newStore()
  466. s.Create("/foo/bar", false, "baz", false, Permanent)
  467. w, _ := s.Watch("/foo", true, false, 0)
  468. s.Update("/foo/bar", "baz", Permanent)
  469. e := nbselect(w.EventChan)
  470. assert.Equal(t, e.Action, "update", "")
  471. assert.Equal(t, e.Node.Key, "/foo/bar", "")
  472. }
  473. // Ensure that the store can watch for key deletions.
  474. func TestStoreWatchDelete(t *testing.T) {
  475. s := newStore()
  476. s.Create("/foo", false, "bar", false, Permanent)
  477. w, _ := s.Watch("/foo", false, false, 0)
  478. s.Delete("/foo", false, false)
  479. e := nbselect(w.EventChan)
  480. assert.Equal(t, e.Action, "delete", "")
  481. assert.Equal(t, e.Node.Key, "/foo", "")
  482. }
  483. // Ensure that the store can watch for recursive key deletions.
  484. func TestStoreWatchRecursiveDelete(t *testing.T) {
  485. s := newStore()
  486. s.Create("/foo/bar", false, "baz", false, Permanent)
  487. w, _ := s.Watch("/foo", true, false, 0)
  488. s.Delete("/foo/bar", false, false)
  489. e := nbselect(w.EventChan)
  490. assert.Equal(t, e.Action, "delete", "")
  491. assert.Equal(t, e.Node.Key, "/foo/bar", "")
  492. }
  493. // Ensure that the store can watch for CAS updates.
  494. func TestStoreWatchCompareAndSwap(t *testing.T) {
  495. s := newStore()
  496. s.Create("/foo", false, "bar", false, Permanent)
  497. w, _ := s.Watch("/foo", false, false, 0)
  498. s.CompareAndSwap("/foo", "bar", 0, "baz", Permanent)
  499. e := nbselect(w.EventChan)
  500. assert.Equal(t, e.Action, "compareAndSwap", "")
  501. assert.Equal(t, e.Node.Key, "/foo", "")
  502. }
  503. // Ensure that the store can watch for recursive CAS updates.
  504. func TestStoreWatchRecursiveCompareAndSwap(t *testing.T) {
  505. s := newStore()
  506. s.Create("/foo/bar", false, "baz", false, Permanent)
  507. w, _ := s.Watch("/foo", true, false, 0)
  508. s.CompareAndSwap("/foo/bar", "baz", 0, "bat", Permanent)
  509. e := nbselect(w.EventChan)
  510. assert.Equal(t, e.Action, "compareAndSwap", "")
  511. assert.Equal(t, e.Node.Key, "/foo/bar", "")
  512. }
  513. // Ensure that the store can watch for key expiration.
  514. func TestStoreWatchExpire(t *testing.T) {
  515. s := newStore()
  516. stopChan := make(chan bool)
  517. defer func() {
  518. stopChan <- true
  519. }()
  520. go mockSyncService(s.DeleteExpiredKeys, stopChan)
  521. s.Create("/foo", false, "bar", false, time.Now().Add(500*time.Millisecond))
  522. s.Create("/foofoo", false, "barbarbar", false, time.Now().Add(500*time.Millisecond))
  523. w, _ := s.Watch("/", true, false, 0)
  524. c := w.EventChan
  525. e := nbselect(c)
  526. assert.Nil(t, e, "")
  527. time.Sleep(600 * time.Millisecond)
  528. e = nbselect(c)
  529. assert.Equal(t, e.Action, "expire", "")
  530. assert.Equal(t, e.Node.Key, "/foo", "")
  531. w, _ = s.Watch("/", true, false, 4)
  532. e = nbselect(w.EventChan)
  533. assert.Equal(t, e.Action, "expire", "")
  534. assert.Equal(t, e.Node.Key, "/foofoo", "")
  535. }
  536. // Ensure that the store can watch in streaming mode.
  537. func TestStoreWatchStream(t *testing.T) {
  538. s := newStore()
  539. w, _ := s.Watch("/foo", false, true, 0)
  540. // first modification
  541. s.Create("/foo", false, "bar", false, Permanent)
  542. e := nbselect(w.EventChan)
  543. assert.Equal(t, e.Action, "create", "")
  544. assert.Equal(t, e.Node.Key, "/foo", "")
  545. assert.Equal(t, e.Node.Value, "bar", "")
  546. e = nbselect(w.EventChan)
  547. assert.Nil(t, e, "")
  548. // second modification
  549. s.Update("/foo", "baz", Permanent)
  550. e = nbselect(w.EventChan)
  551. assert.Equal(t, e.Action, "update", "")
  552. assert.Equal(t, e.Node.Key, "/foo", "")
  553. assert.Equal(t, e.Node.Value, "baz", "")
  554. e = nbselect(w.EventChan)
  555. assert.Nil(t, e, "")
  556. }
  557. // Ensure that the store can recover from a previously saved state.
  558. func TestStoreRecover(t *testing.T) {
  559. s := newStore()
  560. s.Create("/foo", true, "", false, Permanent)
  561. s.Create("/foo/x", false, "bar", false, Permanent)
  562. s.Create("/foo/y", false, "baz", false, Permanent)
  563. b, err := s.Save()
  564. s2 := newStore()
  565. s2.Recovery(b)
  566. e, err := s.Get("/foo/x", false, false)
  567. assert.Nil(t, err, "")
  568. assert.Equal(t, e.Node.Value, "bar", "")
  569. e, err = s.Get("/foo/y", false, false)
  570. assert.Nil(t, err, "")
  571. assert.Equal(t, e.Node.Value, "baz", "")
  572. }
  573. // Ensure that the store can recover from a previously saved state that includes an expiring key.
  574. func TestStoreRecoverWithExpiration(t *testing.T) {
  575. s := newStore()
  576. c := make(chan bool)
  577. defer func() {
  578. c <- true
  579. }()
  580. go mockSyncService(s.DeleteExpiredKeys, c)
  581. s.Create("/foo", true, "", false, Permanent)
  582. s.Create("/foo/x", false, "bar", false, Permanent)
  583. s.Create("/foo/y", false, "baz", false, time.Now().Add(5*time.Millisecond))
  584. b, err := s.Save()
  585. time.Sleep(10 * time.Millisecond)
  586. s2 := newStore()
  587. c2 := make(chan bool)
  588. defer func() {
  589. c2 <- true
  590. }()
  591. go mockSyncService(s2.DeleteExpiredKeys, c2)
  592. s2.Recovery(b)
  593. time.Sleep(600 * time.Millisecond)
  594. e, err := s.Get("/foo/x", false, false)
  595. assert.Nil(t, err, "")
  596. assert.Equal(t, e.Node.Value, "bar", "")
  597. e, err = s.Get("/foo/y", false, false)
  598. assert.NotNil(t, err, "")
  599. assert.Nil(t, e, "")
  600. }
  601. // Performs a non-blocking select on an event channel.
  602. func nbselect(c <-chan *Event) *Event {
  603. select {
  604. case e := <-c:
  605. return e
  606. default:
  607. return nil
  608. }
  609. }
  610. func mockSyncService(f func(now time.Time), c chan bool) {
  611. ticker := time.Tick(time.Millisecond * 500)
  612. for {
  613. select {
  614. case <-c:
  615. return
  616. case now := <-ticker:
  617. f(now)
  618. }
  619. }
  620. }