store_test.go 38 KB

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