store_test.go 31 KB

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