store_test.go 32 KB

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