store_test.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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. e, err := s.Update("/foo", "", time.Now().Add(500*time.Millisecond))
  260. assert.Equal(t, e.Node.Dir, true, "")
  261. e, _ = s.Get("/foo/bar", false, false)
  262. assert.Equal(t, *e.Node.Value, "baz", "")
  263. time.Sleep(600 * time.Millisecond)
  264. e, err = s.Get("/foo/bar", false, false)
  265. assert.Nil(t, e, "")
  266. assert.Equal(t, err.(*etcdErr.Error).ErrorCode, etcdErr.EcodeKeyNotFound, "")
  267. }
  268. // Ensure that the store can delete a value.
  269. func TestStoreDeleteValue(t *testing.T) {
  270. s := newStore()
  271. s.Create("/foo", false, "bar", false, Permanent)
  272. e, err := s.Delete("/foo", false, false)
  273. assert.Nil(t, err, "")
  274. assert.Equal(t, e.Action, "delete", "")
  275. // check pervNode
  276. assert.NotNil(t, e.PrevNode, "")
  277. assert.Equal(t, e.PrevNode.Key, "/foo", "")
  278. assert.Equal(t, *e.PrevNode.Value, "bar", "")
  279. }
  280. // Ensure that the store can delete a directory if recursive is specified.
  281. func TestStoreDeleteDiretory(t *testing.T) {
  282. s := newStore()
  283. // create directory /foo
  284. s.Create("/foo", true, "", false, Permanent)
  285. // delete /foo with dir = true and recursive = false
  286. // this should succeed, since the directory is empty
  287. e, err := s.Delete("/foo", true, false)
  288. assert.Nil(t, err, "")
  289. assert.Equal(t, e.Action, "delete", "")
  290. // check pervNode
  291. assert.NotNil(t, e.PrevNode, "")
  292. assert.Equal(t, e.PrevNode.Key, "/foo", "")
  293. assert.Equal(t, e.PrevNode.Dir, true, "")
  294. // create directory /foo and directory /foo/bar
  295. s.Create("/foo/bar", true, "", false, Permanent)
  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. assert.NotNil(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. assert.Nil(t, err, "")
  306. assert.Equal(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 TestStoreDeleteDiretoryFailsIfNonRecursiveAndDir(t *testing.T) {
  311. s := newStore()
  312. s.Create("/foo", true, "", false, Permanent)
  313. e, _err := s.Delete("/foo", false, false)
  314. err := _err.(*etcdErr.Error)
  315. assert.Equal(t, err.ErrorCode, etcdErr.EcodeNotFile, "")
  316. assert.Equal(t, err.Message, "Not a file", "")
  317. assert.Nil(t, e, "")
  318. }
  319. func TestRootRdOnly(t *testing.T) {
  320. s := newStore()
  321. _, err := s.Set("/", true, "", Permanent)
  322. assert.NotNil(t, err, "")
  323. _, err = s.Delete("/", true, true)
  324. assert.NotNil(t, err, "")
  325. _, err = s.Create("/", true, "", false, Permanent)
  326. assert.NotNil(t, err, "")
  327. _, err = s.Update("/", "", Permanent)
  328. assert.NotNil(t, err, "")
  329. _, err = s.CompareAndSwap("/", "", 0, "", Permanent)
  330. assert.NotNil(t, err, "")
  331. }
  332. func TestStoreCompareAndDeletePrevValue(t *testing.T) {
  333. s := newStore()
  334. s.Create("/foo", false, "bar", false, Permanent)
  335. e, err := s.CompareAndDelete("/foo", "bar", 0)
  336. assert.Nil(t, err, "")
  337. assert.Equal(t, e.Action, "compareAndDelete", "")
  338. assert.Equal(t, e.Node.Key, "/foo", "")
  339. // check pervNode
  340. assert.NotNil(t, e.PrevNode, "")
  341. assert.Equal(t, e.PrevNode.Key, "/foo", "")
  342. assert.Equal(t, *e.PrevNode.Value, "bar", "")
  343. assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(1), "")
  344. assert.Equal(t, e.PrevNode.CreatedIndex, uint64(1), "")
  345. }
  346. func TestStoreCompareAndDeletePrevValueFailsIfNotMatch(t *testing.T) {
  347. s := newStore()
  348. s.Create("/foo", false, "bar", false, Permanent)
  349. e, _err := s.CompareAndDelete("/foo", "baz", 0)
  350. err := _err.(*etcdErr.Error)
  351. assert.Equal(t, err.ErrorCode, etcdErr.EcodeTestFailed, "")
  352. assert.Equal(t, err.Message, "Compare failed", "")
  353. assert.Nil(t, e, "")
  354. e, _ = s.Get("/foo", false, false)
  355. assert.Equal(t, *e.Node.Value, "bar", "")
  356. }
  357. func TestStoreCompareAndDeletePrevIndex(t *testing.T) {
  358. s := newStore()
  359. s.Create("/foo", false, "bar", false, Permanent)
  360. e, err := s.CompareAndDelete("/foo", "", 1)
  361. assert.Nil(t, err, "")
  362. assert.Equal(t, e.Action, "compareAndDelete", "")
  363. // check pervNode
  364. assert.NotNil(t, e.PrevNode, "")
  365. assert.Equal(t, e.PrevNode.Key, "/foo", "")
  366. assert.Equal(t, *e.PrevNode.Value, "bar", "")
  367. assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(1), "")
  368. assert.Equal(t, e.PrevNode.CreatedIndex, uint64(1), "")
  369. }
  370. func TestStoreCompareAndDeletePrevIndexFailsIfNotMatch(t *testing.T) {
  371. s := newStore()
  372. s.Create("/foo", false, "bar", false, Permanent)
  373. e, _err := s.CompareAndDelete("/foo", "", 100)
  374. assert.NotNil(t, _err, "")
  375. err := _err.(*etcdErr.Error)
  376. assert.Equal(t, err.ErrorCode, etcdErr.EcodeTestFailed, "")
  377. assert.Equal(t, err.Message, "Compare failed", "")
  378. assert.Nil(t, e, "")
  379. e, _ = s.Get("/foo", false, false)
  380. assert.Equal(t, *e.Node.Value, "bar", "")
  381. }
  382. // Ensure that the store cannot delete a directory.
  383. func TestStoreCompareAndDeleteDiretoryFail(t *testing.T) {
  384. s := newStore()
  385. s.Create("/foo", true, "", false, Permanent)
  386. _, _err := s.CompareAndDelete("/foo", "", 0)
  387. assert.NotNil(t, _err, "")
  388. err := _err.(*etcdErr.Error)
  389. assert.Equal(t, err.ErrorCode, etcdErr.EcodeNotFile, "")
  390. }
  391. // Ensure that the store can conditionally update a key if it has a previous value.
  392. func TestStoreCompareAndSwapPrevValue(t *testing.T) {
  393. s := newStore()
  394. s.Create("/foo", false, "bar", false, Permanent)
  395. e, err := s.CompareAndSwap("/foo", "bar", 0, "baz", Permanent)
  396. assert.Nil(t, err, "")
  397. assert.Equal(t, e.Action, "compareAndSwap", "")
  398. assert.Equal(t, *e.Node.Value, "baz", "")
  399. // check pervNode
  400. assert.NotNil(t, e.PrevNode, "")
  401. assert.Equal(t, e.PrevNode.Key, "/foo", "")
  402. assert.Equal(t, *e.PrevNode.Value, "bar", "")
  403. assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(1), "")
  404. assert.Equal(t, e.PrevNode.CreatedIndex, uint64(1), "")
  405. e, _ = s.Get("/foo", false, false)
  406. assert.Equal(t, *e.Node.Value, "baz", "")
  407. }
  408. // Ensure that the store cannot conditionally update a key if it has the wrong previous value.
  409. func TestStoreCompareAndSwapPrevValueFailsIfNotMatch(t *testing.T) {
  410. s := newStore()
  411. s.Create("/foo", false, "bar", false, Permanent)
  412. e, _err := s.CompareAndSwap("/foo", "wrong_value", 0, "baz", Permanent)
  413. err := _err.(*etcdErr.Error)
  414. assert.Equal(t, err.ErrorCode, etcdErr.EcodeTestFailed, "")
  415. assert.Equal(t, err.Message, "Compare failed", "")
  416. assert.Nil(t, e, "")
  417. e, _ = s.Get("/foo", false, false)
  418. assert.Equal(t, *e.Node.Value, "bar", "")
  419. }
  420. // Ensure that the store can conditionally update a key if it has a previous index.
  421. func TestStoreCompareAndSwapPrevIndex(t *testing.T) {
  422. s := newStore()
  423. s.Create("/foo", false, "bar", false, Permanent)
  424. e, err := s.CompareAndSwap("/foo", "", 1, "baz", Permanent)
  425. assert.Nil(t, err, "")
  426. assert.Equal(t, e.Action, "compareAndSwap", "")
  427. assert.Equal(t, *e.Node.Value, "baz", "")
  428. // check pervNode
  429. assert.NotNil(t, e.PrevNode, "")
  430. assert.Equal(t, e.PrevNode.Key, "/foo", "")
  431. assert.Equal(t, *e.PrevNode.Value, "bar", "")
  432. assert.Equal(t, e.PrevNode.ModifiedIndex, uint64(1), "")
  433. assert.Equal(t, e.PrevNode.CreatedIndex, uint64(1), "")
  434. e, _ = s.Get("/foo", false, false)
  435. assert.Equal(t, *e.Node.Value, "baz", "")
  436. }
  437. // Ensure that the store cannot conditionally update a key if it has the wrong previous index.
  438. func TestStoreCompareAndSwapPrevIndexFailsIfNotMatch(t *testing.T) {
  439. s := newStore()
  440. s.Create("/foo", false, "bar", false, Permanent)
  441. e, _err := s.CompareAndSwap("/foo", "", 100, "baz", Permanent)
  442. err := _err.(*etcdErr.Error)
  443. assert.Equal(t, err.ErrorCode, etcdErr.EcodeTestFailed, "")
  444. assert.Equal(t, err.Message, "Compare failed", "")
  445. assert.Nil(t, e, "")
  446. e, _ = s.Get("/foo", false, false)
  447. assert.Equal(t, *e.Node.Value, "bar", "")
  448. }
  449. // Ensure that the store can watch for key creation.
  450. func TestStoreWatchCreate(t *testing.T) {
  451. s := newStore()
  452. w, _ := s.Watch("/foo", false, false, 0)
  453. c := w.EventChan
  454. s.Create("/foo", false, "bar", false, Permanent)
  455. e := nbselect(c)
  456. assert.Equal(t, e.Action, "create", "")
  457. assert.Equal(t, e.Node.Key, "/foo", "")
  458. e = nbselect(c)
  459. assert.Nil(t, e, "")
  460. }
  461. // Ensure that the store can watch for recursive key creation.
  462. func TestStoreWatchRecursiveCreate(t *testing.T) {
  463. s := newStore()
  464. w, _ := s.Watch("/foo", true, false, 0)
  465. s.Create("/foo/bar", false, "baz", false, Permanent)
  466. e := nbselect(w.EventChan)
  467. assert.Equal(t, e.Action, "create", "")
  468. assert.Equal(t, e.Node.Key, "/foo/bar", "")
  469. }
  470. // Ensure that the store can watch for key updates.
  471. func TestStoreWatchUpdate(t *testing.T) {
  472. s := newStore()
  473. s.Create("/foo", false, "bar", false, Permanent)
  474. w, _ := s.Watch("/foo", false, false, 0)
  475. s.Update("/foo", "baz", Permanent)
  476. e := nbselect(w.EventChan)
  477. assert.Equal(t, e.Action, "update", "")
  478. assert.Equal(t, e.Node.Key, "/foo", "")
  479. }
  480. // Ensure that the store can watch for recursive key updates.
  481. func TestStoreWatchRecursiveUpdate(t *testing.T) {
  482. s := newStore()
  483. s.Create("/foo/bar", false, "baz", false, Permanent)
  484. w, _ := s.Watch("/foo", true, false, 0)
  485. s.Update("/foo/bar", "baz", Permanent)
  486. e := nbselect(w.EventChan)
  487. assert.Equal(t, e.Action, "update", "")
  488. assert.Equal(t, e.Node.Key, "/foo/bar", "")
  489. }
  490. // Ensure that the store can watch for key deletions.
  491. func TestStoreWatchDelete(t *testing.T) {
  492. s := newStore()
  493. s.Create("/foo", false, "bar", false, Permanent)
  494. w, _ := s.Watch("/foo", false, false, 0)
  495. s.Delete("/foo", false, false)
  496. e := nbselect(w.EventChan)
  497. assert.Equal(t, e.Action, "delete", "")
  498. assert.Equal(t, e.Node.Key, "/foo", "")
  499. }
  500. // Ensure that the store can watch for recursive key deletions.
  501. func TestStoreWatchRecursiveDelete(t *testing.T) {
  502. s := newStore()
  503. s.Create("/foo/bar", false, "baz", false, Permanent)
  504. w, _ := s.Watch("/foo", true, false, 0)
  505. s.Delete("/foo/bar", false, false)
  506. e := nbselect(w.EventChan)
  507. assert.Equal(t, e.Action, "delete", "")
  508. assert.Equal(t, e.Node.Key, "/foo/bar", "")
  509. }
  510. // Ensure that the store can watch for CAS updates.
  511. func TestStoreWatchCompareAndSwap(t *testing.T) {
  512. s := newStore()
  513. s.Create("/foo", false, "bar", false, Permanent)
  514. w, _ := s.Watch("/foo", false, false, 0)
  515. s.CompareAndSwap("/foo", "bar", 0, "baz", Permanent)
  516. e := nbselect(w.EventChan)
  517. assert.Equal(t, e.Action, "compareAndSwap", "")
  518. assert.Equal(t, e.Node.Key, "/foo", "")
  519. }
  520. // Ensure that the store can watch for recursive CAS updates.
  521. func TestStoreWatchRecursiveCompareAndSwap(t *testing.T) {
  522. s := newStore()
  523. s.Create("/foo/bar", false, "baz", false, Permanent)
  524. w, _ := s.Watch("/foo", true, false, 0)
  525. s.CompareAndSwap("/foo/bar", "baz", 0, "bat", Permanent)
  526. e := nbselect(w.EventChan)
  527. assert.Equal(t, e.Action, "compareAndSwap", "")
  528. assert.Equal(t, e.Node.Key, "/foo/bar", "")
  529. }
  530. // Ensure that the store can watch for key expiration.
  531. func TestStoreWatchExpire(t *testing.T) {
  532. s := newStore()
  533. stopChan := make(chan bool)
  534. defer func() {
  535. stopChan <- true
  536. }()
  537. go mockSyncService(s.DeleteExpiredKeys, stopChan)
  538. s.Create("/foo", false, "bar", false, time.Now().Add(500*time.Millisecond))
  539. s.Create("/foofoo", false, "barbarbar", false, time.Now().Add(500*time.Millisecond))
  540. w, _ := s.Watch("/", true, false, 0)
  541. c := w.EventChan
  542. e := nbselect(c)
  543. assert.Nil(t, e, "")
  544. time.Sleep(600 * time.Millisecond)
  545. e = nbselect(c)
  546. assert.Equal(t, e.Action, "expire", "")
  547. assert.Equal(t, e.Node.Key, "/foo", "")
  548. w, _ = s.Watch("/", true, false, 4)
  549. e = nbselect(w.EventChan)
  550. assert.Equal(t, e.Action, "expire", "")
  551. assert.Equal(t, e.Node.Key, "/foofoo", "")
  552. }
  553. // Ensure that the store can watch in streaming mode.
  554. func TestStoreWatchStream(t *testing.T) {
  555. s := newStore()
  556. w, _ := s.Watch("/foo", false, true, 0)
  557. // first modification
  558. s.Create("/foo", false, "bar", false, Permanent)
  559. e := nbselect(w.EventChan)
  560. assert.Equal(t, e.Action, "create", "")
  561. assert.Equal(t, e.Node.Key, "/foo", "")
  562. assert.Equal(t, *e.Node.Value, "bar", "")
  563. e = nbselect(w.EventChan)
  564. assert.Nil(t, e, "")
  565. // second modification
  566. s.Update("/foo", "baz", Permanent)
  567. e = nbselect(w.EventChan)
  568. assert.Equal(t, e.Action, "update", "")
  569. assert.Equal(t, e.Node.Key, "/foo", "")
  570. assert.Equal(t, *e.Node.Value, "baz", "")
  571. e = nbselect(w.EventChan)
  572. assert.Nil(t, e, "")
  573. }
  574. // Ensure that the store can recover from a previously saved state.
  575. func TestStoreRecover(t *testing.T) {
  576. s := newStore()
  577. s.Create("/foo", true, "", false, Permanent)
  578. s.Create("/foo/x", false, "bar", false, Permanent)
  579. s.Create("/foo/y", false, "baz", false, Permanent)
  580. b, err := s.Save()
  581. s2 := newStore()
  582. s2.Recovery(b)
  583. e, err := s.Get("/foo/x", false, false)
  584. assert.Nil(t, err, "")
  585. assert.Equal(t, *e.Node.Value, "bar", "")
  586. e, err = s.Get("/foo/y", false, false)
  587. assert.Nil(t, err, "")
  588. assert.Equal(t, *e.Node.Value, "baz", "")
  589. }
  590. // Ensure that the store can recover from a previously saved state that includes an expiring key.
  591. func TestStoreRecoverWithExpiration(t *testing.T) {
  592. s := newStore()
  593. c := make(chan bool)
  594. defer func() {
  595. c <- true
  596. }()
  597. go mockSyncService(s.DeleteExpiredKeys, c)
  598. s.Create("/foo", true, "", false, Permanent)
  599. s.Create("/foo/x", false, "bar", false, Permanent)
  600. s.Create("/foo/y", false, "baz", false, time.Now().Add(5*time.Millisecond))
  601. b, err := s.Save()
  602. time.Sleep(10 * time.Millisecond)
  603. s2 := newStore()
  604. c2 := make(chan bool)
  605. defer func() {
  606. c2 <- true
  607. }()
  608. go mockSyncService(s2.DeleteExpiredKeys, c2)
  609. s2.Recovery(b)
  610. time.Sleep(600 * time.Millisecond)
  611. e, err := s.Get("/foo/x", false, false)
  612. assert.Nil(t, err, "")
  613. assert.Equal(t, *e.Node.Value, "bar", "")
  614. e, err = s.Get("/foo/y", false, false)
  615. assert.NotNil(t, err, "")
  616. assert.Nil(t, e, "")
  617. }
  618. // Ensure that the store can watch for hidden keys as long as it's an exact path match.
  619. func TestStoreWatchCreateWithHiddenKey(t *testing.T) {
  620. s := newStore()
  621. w, _ := s.Watch("/_foo", false, false, 0)
  622. s.Create("/_foo", false, "bar", false, Permanent)
  623. e := nbselect(w.EventChan)
  624. assert.Equal(t, e.Action, "create", "")
  625. assert.Equal(t, e.Node.Key, "/_foo", "")
  626. e = nbselect(w.EventChan)
  627. assert.Nil(t, e, "")
  628. }
  629. // Ensure that the store doesn't see hidden key creates without an exact path match in recursive mode.
  630. func TestStoreWatchRecursiveCreateWithHiddenKey(t *testing.T) {
  631. s := newStore()
  632. w, _ := s.Watch("/foo", true, false, 0)
  633. s.Create("/foo/_bar", false, "baz", false, Permanent)
  634. e := nbselect(w.EventChan)
  635. assert.Nil(t, e, "")
  636. w, _ = s.Watch("/foo", true, false, 0)
  637. s.Create("/foo/_baz", true, "", false, Permanent)
  638. e = nbselect(w.EventChan)
  639. assert.Nil(t, e, "")
  640. s.Create("/foo/_baz/quux", false, "quux", false, Permanent)
  641. e = nbselect(w.EventChan)
  642. assert.Nil(t, e, "")
  643. }
  644. // Ensure that the store doesn't see hidden key updates.
  645. func TestStoreWatchUpdateWithHiddenKey(t *testing.T) {
  646. s := newStore()
  647. s.Create("/_foo", false, "bar", false, Permanent)
  648. w, _ := s.Watch("/_foo", false, false, 0)
  649. s.Update("/_foo", "baz", Permanent)
  650. e := nbselect(w.EventChan)
  651. assert.Equal(t, e.Action, "update", "")
  652. assert.Equal(t, e.Node.Key, "/_foo", "")
  653. e = nbselect(w.EventChan)
  654. assert.Nil(t, e, "")
  655. }
  656. // Ensure that the store doesn't see hidden key updates without an exact path match in recursive mode.
  657. func TestStoreWatchRecursiveUpdateWithHiddenKey(t *testing.T) {
  658. s := newStore()
  659. s.Create("/foo/_bar", false, "baz", false, Permanent)
  660. w, _ := s.Watch("/foo", true, false, 0)
  661. s.Update("/foo/_bar", "baz", Permanent)
  662. e := nbselect(w.EventChan)
  663. assert.Nil(t, e, "")
  664. }
  665. // Ensure that the store can watch for key deletions.
  666. func TestStoreWatchDeleteWithHiddenKey(t *testing.T) {
  667. s := newStore()
  668. s.Create("/_foo", false, "bar", false, Permanent)
  669. w, _ := s.Watch("/_foo", false, false, 0)
  670. s.Delete("/_foo", false, false)
  671. e := nbselect(w.EventChan)
  672. assert.Equal(t, e.Action, "delete", "")
  673. assert.Equal(t, e.Node.Key, "/_foo", "")
  674. e = nbselect(w.EventChan)
  675. assert.Nil(t, e, "")
  676. }
  677. // Ensure that the store doesn't see hidden key deletes without an exact path match in recursive mode.
  678. func TestStoreWatchRecursiveDeleteWithHiddenKey(t *testing.T) {
  679. s := newStore()
  680. s.Create("/foo/_bar", false, "baz", false, Permanent)
  681. w, _ := s.Watch("/foo", true, false, 0)
  682. s.Delete("/foo/_bar", false, false)
  683. e := nbselect(w.EventChan)
  684. assert.Nil(t, e, "")
  685. }
  686. // Ensure that the store doesn't see expirations of hidden keys.
  687. func TestStoreWatchExpireWithHiddenKey(t *testing.T) {
  688. s := newStore()
  689. stopChan := make(chan bool)
  690. defer func() {
  691. stopChan <- true
  692. }()
  693. go mockSyncService(s.DeleteExpiredKeys, stopChan)
  694. s.Create("/_foo", false, "bar", false, time.Now().Add(500*time.Millisecond))
  695. s.Create("/foofoo", false, "barbarbar", false, time.Now().Add(1000*time.Millisecond))
  696. w, _ := s.Watch("/", true, false, 0)
  697. c := w.EventChan
  698. e := nbselect(c)
  699. assert.Nil(t, e, "")
  700. time.Sleep(600 * time.Millisecond)
  701. e = nbselect(c)
  702. assert.Nil(t, e, "")
  703. time.Sleep(600 * time.Millisecond)
  704. e = nbselect(c)
  705. assert.Equal(t, e.Action, "expire", "")
  706. assert.Equal(t, e.Node.Key, "/foofoo", "")
  707. }
  708. // Ensure that the store does see hidden key creates if watching deeper than a hidden key in recursive mode.
  709. func TestStoreWatchRecursiveCreateDeeperThanHiddenKey(t *testing.T) {
  710. s := newStore()
  711. w, _ := s.Watch("/_foo/bar", true, false, 0)
  712. s.Create("/_foo/bar/baz", false, "baz", false, Permanent)
  713. e := nbselect(w.EventChan)
  714. assert.NotNil(t, e, "")
  715. assert.Equal(t, e.Action, "create", "")
  716. assert.Equal(t, e.Node.Key, "/_foo/bar/baz", "")
  717. }
  718. // Ensure that slow consumers are handled properly.
  719. //
  720. // Since Watcher.EventChan has a buffer of size 1 we can only queue 1
  721. // event per watcher. If the consumer cannot consume the event on time and
  722. // another event arrives, the channel is closed and event is discarded.
  723. // This test ensures that after closing the channel, the store can continue
  724. // to operate correctly.
  725. func TestStoreWatchSlowConsumer(t *testing.T) {
  726. s := newStore()
  727. s.Watch("/foo", true, true, 0) // stream must be true
  728. s.Set("/foo", false, "1", Permanent) // ok
  729. s.Set("/foo", false, "2", Permanent) // ok
  730. s.Set("/foo", false, "3", Permanent) // must not panic
  731. }
  732. // Performs a non-blocking select on an event channel.
  733. func nbselect(c <-chan *Event) *Event {
  734. select {
  735. case e := <-c:
  736. return e
  737. default:
  738. return nil
  739. }
  740. }
  741. func mockSyncService(f func(now time.Time), c chan bool) {
  742. ticker := time.Tick(time.Millisecond * 500)
  743. for {
  744. select {
  745. case <-c:
  746. return
  747. case now := <-ticker:
  748. f(now)
  749. }
  750. }
  751. }