store_test.go 41 KB

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