v2_http_test.go 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. package etcd
  2. // Ensures that a value can be retrieve for a given key.
  3. import (
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "testing"
  8. "time"
  9. "github.com/coreos/etcd/third_party/github.com/stretchr/testify/assert"
  10. )
  11. // Ensures that a directory is created
  12. //
  13. // $ curl -X PUT localhost:4001/v2/keys/foo/bar?dir=true
  14. //
  15. func TestV2SetDirectory(t *testing.T) {
  16. es, hs := buildCluster(1)
  17. u := hs[0].URL
  18. resp, err := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo?dir=true"), url.Values{})
  19. assert.Equal(t, resp.StatusCode, http.StatusCreated)
  20. body := ReadBody(resp)
  21. assert.Nil(t, err, "")
  22. assert.Equal(t, string(body), `{"action":"set","node":{"key":"/foo","dir":true,"modifiedIndex":2,"createdIndex":2}}`, "")
  23. es[0].Stop()
  24. hs[0].Close()
  25. afterTest(t)
  26. }
  27. // Ensures that a time-to-live is added to a key.
  28. //
  29. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d ttl=20
  30. //
  31. func TestV2SetKeyWithTTL(t *testing.T) {
  32. es, hs := buildCluster(1)
  33. u := hs[0].URL
  34. t0 := time.Now()
  35. v := url.Values{}
  36. v.Set("value", "XXX")
  37. v.Set("ttl", "20")
  38. resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  39. assert.Equal(t, resp.StatusCode, http.StatusCreated)
  40. body := ReadBodyJSON(resp)
  41. node := body["node"].(map[string]interface{})
  42. assert.Equal(t, node["ttl"], 20, "")
  43. // Make sure the expiration date is correct.
  44. expiration, _ := time.Parse(time.RFC3339Nano, node["expiration"].(string))
  45. assert.Equal(t, expiration.Sub(t0)/time.Second, 20, "")
  46. es[0].Stop()
  47. hs[0].Close()
  48. afterTest(t)
  49. }
  50. // Ensures that an invalid time-to-live is returned as an error.
  51. //
  52. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d ttl=bad_ttl
  53. //
  54. func TestV2SetKeyWithBadTTL(t *testing.T) {
  55. es, hs := buildCluster(1)
  56. u := hs[0].URL
  57. v := url.Values{}
  58. v.Set("value", "XXX")
  59. v.Set("ttl", "bad_ttl")
  60. resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  61. assert.Equal(t, resp.StatusCode, http.StatusBadRequest)
  62. body := ReadBodyJSON(resp)
  63. assert.Equal(t, body["errorCode"], 202, "")
  64. assert.Equal(t, body["message"], "The given TTL in POST form is not a number", "")
  65. assert.Equal(t, body["cause"], "Update", "")
  66. es[0].Stop()
  67. hs[0].Close()
  68. afterTest(t)
  69. }
  70. // Ensures that a key is conditionally set if it previously did not exist.
  71. //
  72. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevExist=false
  73. //
  74. func TestV2CreateKeySuccess(t *testing.T) {
  75. es, hs := buildCluster(1)
  76. u := hs[0].URL
  77. v := url.Values{}
  78. v.Set("value", "XXX")
  79. v.Set("prevExist", "false")
  80. resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  81. assert.Equal(t, resp.StatusCode, http.StatusCreated)
  82. body := ReadBodyJSON(resp)
  83. node := body["node"].(map[string]interface{})
  84. assert.Equal(t, node["value"], "XXX", "")
  85. es[0].Stop()
  86. hs[0].Close()
  87. afterTest(t)
  88. }
  89. // Ensures that a key is not conditionally set because it previously existed.
  90. //
  91. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevExist=false
  92. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevExist=false -> fail
  93. //
  94. func TestV2CreateKeyFail(t *testing.T) {
  95. es, hs := buildCluster(1)
  96. u := hs[0].URL
  97. v := url.Values{}
  98. v.Set("value", "XXX")
  99. v.Set("prevExist", "false")
  100. fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar")
  101. resp, _ := PutForm(fullURL, v)
  102. assert.Equal(t, resp.StatusCode, http.StatusCreated)
  103. ReadBody(resp)
  104. resp, _ = PutForm(fullURL, v)
  105. assert.Equal(t, resp.StatusCode, http.StatusPreconditionFailed)
  106. body := ReadBodyJSON(resp)
  107. assert.Equal(t, body["errorCode"], 105, "")
  108. assert.Equal(t, body["message"], "Key already exists", "")
  109. assert.Equal(t, body["cause"], "/foo/bar", "")
  110. es[0].Stop()
  111. hs[0].Close()
  112. afterTest(t)
  113. }
  114. // Ensures that a key is conditionally set only if it previously did exist.
  115. //
  116. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  117. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevExist=true
  118. //
  119. func TestV2UpdateKeySuccess(t *testing.T) {
  120. es, hs := buildCluster(1)
  121. u := hs[0].URL
  122. v := url.Values{}
  123. v.Set("value", "XXX")
  124. fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar")
  125. resp, _ := PutForm(fullURL, v)
  126. assert.Equal(t, resp.StatusCode, http.StatusCreated)
  127. ReadBody(resp)
  128. v.Set("value", "YYY")
  129. v.Set("prevExist", "true")
  130. resp, _ = PutForm(fullURL, v)
  131. assert.Equal(t, resp.StatusCode, http.StatusOK)
  132. body := ReadBodyJSON(resp)
  133. assert.Equal(t, body["action"], "update", "")
  134. es[0].Stop()
  135. hs[0].Close()
  136. afterTest(t)
  137. }
  138. // Ensures that a key is not conditionally set if it previously did not exist.
  139. //
  140. // $ curl -X PUT localhost:4001/v2/keys/foo?dir=true
  141. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevExist=true
  142. //
  143. func TestV2UpdateKeyFailOnValue(t *testing.T) {
  144. es, hs := buildCluster(1)
  145. u := hs[0].URL
  146. v := url.Values{}
  147. resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo?dir=true"), v)
  148. resp.Body.Close()
  149. assert.Equal(t, resp.StatusCode, http.StatusCreated)
  150. v.Set("value", "YYY")
  151. v.Set("prevExist", "true")
  152. resp, _ = PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  153. assert.Equal(t, resp.StatusCode, http.StatusNotFound)
  154. body := ReadBodyJSON(resp)
  155. assert.Equal(t, body["errorCode"], 100, "")
  156. assert.Equal(t, body["message"], "Key not found", "")
  157. assert.Equal(t, body["cause"], "/foo/bar", "")
  158. es[0].Stop()
  159. hs[0].Close()
  160. afterTest(t)
  161. }
  162. // Ensures that a key is not conditionally set if it previously did not exist.
  163. //
  164. // $ curl -X PUT localhost:4001/v2/keys/foo -d value=YYY -d prevExist=true -> fail
  165. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevExist=true -> fail
  166. //
  167. func TestV2UpdateKeyFailOnMissingDirectory(t *testing.T) {
  168. es, hs := buildCluster(1)
  169. u := hs[0].URL
  170. v := url.Values{}
  171. v.Set("value", "YYY")
  172. v.Set("prevExist", "true")
  173. resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), v)
  174. assert.Equal(t, resp.StatusCode, http.StatusNotFound)
  175. body := ReadBodyJSON(resp)
  176. assert.Equal(t, body["errorCode"], 100, "")
  177. assert.Equal(t, body["message"], "Key not found", "")
  178. assert.Equal(t, body["cause"], "/foo", "")
  179. resp, _ = PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  180. assert.Equal(t, resp.StatusCode, http.StatusNotFound)
  181. body = ReadBodyJSON(resp)
  182. assert.Equal(t, body["errorCode"], 100, "")
  183. assert.Equal(t, body["message"], "Key not found", "")
  184. assert.Equal(t, body["cause"], "/foo", "")
  185. es[0].Stop()
  186. hs[0].Close()
  187. afterTest(t)
  188. }
  189. // Ensures that a key could update TTL.
  190. //
  191. // $ curl -X PUT localhost:4001/v2/keys/foo -d value=XXX
  192. // $ curl -X PUT localhost:4001/v2/keys/foo -d value=XXX -d ttl=1000 -d prevExist=true
  193. // $ curl -X PUT localhost:4001/v2/keys/foo -d value=XXX -d ttl= -d prevExist=true
  194. //
  195. func TestV2UpdateKeySuccessWithTTL(t *testing.T) {
  196. es, hs := buildCluster(1)
  197. u := hs[0].URL
  198. v := url.Values{}
  199. v.Set("value", "XXX")
  200. resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), v)
  201. assert.Equal(t, resp.StatusCode, http.StatusCreated)
  202. node := (ReadBodyJSON(resp)["node"]).(map[string]interface{})
  203. createdIndex := node["createdIndex"]
  204. v.Set("ttl", "1000")
  205. v.Set("prevExist", "true")
  206. resp, _ = PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), v)
  207. assert.Equal(t, resp.StatusCode, http.StatusOK)
  208. node = (ReadBodyJSON(resp)["node"]).(map[string]interface{})
  209. assert.Equal(t, node["value"], "XXX", "")
  210. assert.Equal(t, node["ttl"], 1000, "")
  211. assert.NotEqual(t, node["expiration"], "", "")
  212. assert.Equal(t, node["createdIndex"], createdIndex, "")
  213. v.Del("ttl")
  214. resp, _ = PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), v)
  215. assert.Equal(t, resp.StatusCode, http.StatusOK)
  216. node = (ReadBodyJSON(resp)["node"]).(map[string]interface{})
  217. assert.Equal(t, node["value"], "XXX", "")
  218. assert.Equal(t, node["ttl"], nil, "")
  219. assert.Equal(t, node["expiration"], nil, "")
  220. assert.Equal(t, node["createdIndex"], createdIndex, "")
  221. es[0].Stop()
  222. hs[0].Close()
  223. afterTest(t)
  224. }
  225. // Ensures that a key is set only if the previous index matches.
  226. //
  227. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  228. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevIndex=1
  229. //
  230. func TestV2SetKeyCASOnIndexSuccess(t *testing.T) {
  231. es, hs := buildCluster(1)
  232. u := hs[0].URL
  233. v := url.Values{}
  234. v.Set("value", "XXX")
  235. fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar")
  236. resp, _ := PutForm(fullURL, v)
  237. assert.Equal(t, resp.StatusCode, http.StatusCreated)
  238. ReadBody(resp)
  239. v.Set("value", "YYY")
  240. v.Set("prevIndex", "2")
  241. resp, _ = PutForm(fullURL, v)
  242. assert.Equal(t, resp.StatusCode, http.StatusOK)
  243. body := ReadBodyJSON(resp)
  244. assert.Equal(t, body["action"], "compareAndSwap", "")
  245. node := body["node"].(map[string]interface{})
  246. assert.Equal(t, node["value"], "YYY", "")
  247. assert.Equal(t, node["modifiedIndex"], 3, "")
  248. es[0].Stop()
  249. hs[0].Close()
  250. afterTest(t)
  251. }
  252. // Ensures that a key is not set if the previous index does not match.
  253. //
  254. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  255. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevIndex=10
  256. //
  257. func TestV2SetKeyCASOnIndexFail(t *testing.T) {
  258. es, hs := buildCluster(1)
  259. u := hs[0].URL
  260. v := url.Values{}
  261. v.Set("value", "XXX")
  262. fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar")
  263. resp, _ := PutForm(fullURL, v)
  264. assert.Equal(t, resp.StatusCode, http.StatusCreated)
  265. ReadBody(resp)
  266. v.Set("value", "YYY")
  267. v.Set("prevIndex", "10")
  268. resp, _ = PutForm(fullURL, v)
  269. assert.Equal(t, resp.StatusCode, http.StatusPreconditionFailed)
  270. body := ReadBodyJSON(resp)
  271. assert.Equal(t, body["errorCode"], 101, "")
  272. assert.Equal(t, body["message"], "Compare failed", "")
  273. assert.Equal(t, body["cause"], "[10 != 2]", "")
  274. assert.Equal(t, body["index"], 2, "")
  275. es[0].Stop()
  276. hs[0].Close()
  277. afterTest(t)
  278. }
  279. // Ensures that an error is thrown if an invalid previous index is provided.
  280. //
  281. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevIndex=bad_index
  282. //
  283. func TestV2SetKeyCASWithInvalidIndex(t *testing.T) {
  284. es, hs := buildCluster(1)
  285. u := hs[0].URL
  286. v := url.Values{}
  287. v.Set("value", "YYY")
  288. v.Set("prevIndex", "bad_index")
  289. resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  290. assert.Equal(t, resp.StatusCode, http.StatusBadRequest)
  291. body := ReadBodyJSON(resp)
  292. assert.Equal(t, body["errorCode"], 203, "")
  293. assert.Equal(t, body["message"], "The given index in POST form is not a number", "")
  294. assert.Equal(t, body["cause"], "CompareAndSwap", "")
  295. es[0].Stop()
  296. hs[0].Close()
  297. afterTest(t)
  298. }
  299. // Ensures that a key is set only if the previous value matches.
  300. //
  301. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  302. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevValue=XXX
  303. //
  304. func TestV2SetKeyCASOnValueSuccess(t *testing.T) {
  305. es, hs := buildCluster(1)
  306. u := hs[0].URL
  307. v := url.Values{}
  308. v.Set("value", "XXX")
  309. fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar")
  310. resp, _ := PutForm(fullURL, v)
  311. assert.Equal(t, resp.StatusCode, http.StatusCreated)
  312. ReadBody(resp)
  313. v.Set("value", "YYY")
  314. v.Set("prevValue", "XXX")
  315. resp, _ = PutForm(fullURL, v)
  316. assert.Equal(t, resp.StatusCode, http.StatusOK)
  317. body := ReadBodyJSON(resp)
  318. assert.Equal(t, body["action"], "compareAndSwap", "")
  319. node := body["node"].(map[string]interface{})
  320. assert.Equal(t, node["value"], "YYY", "")
  321. assert.Equal(t, node["modifiedIndex"], 3, "")
  322. es[0].Stop()
  323. hs[0].Close()
  324. afterTest(t)
  325. }
  326. // Ensures that a key is not set if the previous value does not match.
  327. //
  328. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  329. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevValue=AAA
  330. //
  331. func TestV2SetKeyCASOnValueFail(t *testing.T) {
  332. es, hs := buildCluster(1)
  333. u := hs[0].URL
  334. v := url.Values{}
  335. v.Set("value", "XXX")
  336. fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar")
  337. resp, _ := PutForm(fullURL, v)
  338. assert.Equal(t, resp.StatusCode, http.StatusCreated)
  339. ReadBody(resp)
  340. v.Set("value", "YYY")
  341. v.Set("prevValue", "AAA")
  342. resp, _ = PutForm(fullURL, v)
  343. assert.Equal(t, resp.StatusCode, http.StatusPreconditionFailed)
  344. body := ReadBodyJSON(resp)
  345. assert.Equal(t, body["errorCode"], 101, "")
  346. assert.Equal(t, body["message"], "Compare failed", "")
  347. assert.Equal(t, body["cause"], "[AAA != XXX]", "")
  348. assert.Equal(t, body["index"], 2, "")
  349. es[0].Stop()
  350. hs[0].Close()
  351. afterTest(t)
  352. }
  353. // Ensures that an error is returned if a blank prevValue is set.
  354. //
  355. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX -d prevValue=
  356. //
  357. func TestV2SetKeyCASWithMissingValueFails(t *testing.T) {
  358. es, hs := buildCluster(1)
  359. u := hs[0].URL
  360. v := url.Values{}
  361. v.Set("value", "XXX")
  362. v.Set("prevValue", "")
  363. resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  364. assert.Equal(t, resp.StatusCode, http.StatusBadRequest)
  365. body := ReadBodyJSON(resp)
  366. assert.Equal(t, body["errorCode"], 201, "")
  367. assert.Equal(t, body["message"], "PrevValue is Required in POST form", "")
  368. assert.Equal(t, body["cause"], "CompareAndSwap", "")
  369. es[0].Stop()
  370. hs[0].Close()
  371. afterTest(t)
  372. }
  373. // Ensures that a key is not set if both previous value and index do not match.
  374. //
  375. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  376. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevValue=AAA -d prevIndex=4
  377. //
  378. func TestV2SetKeyCASOnValueAndIndexFail(t *testing.T) {
  379. es, hs := buildCluster(1)
  380. u := hs[0].URL
  381. v := url.Values{}
  382. v.Set("value", "XXX")
  383. fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar")
  384. resp, _ := PutForm(fullURL, v)
  385. assert.Equal(t, resp.StatusCode, http.StatusCreated)
  386. ReadBody(resp)
  387. v.Set("value", "YYY")
  388. v.Set("prevValue", "AAA")
  389. v.Set("prevIndex", "4")
  390. resp, _ = PutForm(fullURL, v)
  391. assert.Equal(t, resp.StatusCode, http.StatusPreconditionFailed)
  392. body := ReadBodyJSON(resp)
  393. assert.Equal(t, body["errorCode"], 101, "")
  394. assert.Equal(t, body["message"], "Compare failed", "")
  395. assert.Equal(t, body["cause"], "[AAA != XXX] [4 != 2]", "")
  396. assert.Equal(t, body["index"], 2, "")
  397. es[0].Stop()
  398. hs[0].Close()
  399. afterTest(t)
  400. }
  401. // Ensures that a key is not set if previous value match but index does not.
  402. //
  403. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  404. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevValue=XXX -d prevIndex=4
  405. //
  406. func TestV2SetKeyCASOnValueMatchAndIndexFail(t *testing.T) {
  407. es, hs := buildCluster(1)
  408. u := hs[0].URL
  409. v := url.Values{}
  410. v.Set("value", "XXX")
  411. fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar")
  412. resp, _ := PutForm(fullURL, v)
  413. assert.Equal(t, resp.StatusCode, http.StatusCreated)
  414. ReadBody(resp)
  415. v.Set("value", "YYY")
  416. v.Set("prevValue", "XXX")
  417. v.Set("prevIndex", "4")
  418. resp, _ = PutForm(fullURL, v)
  419. assert.Equal(t, resp.StatusCode, http.StatusPreconditionFailed)
  420. body := ReadBodyJSON(resp)
  421. assert.Equal(t, body["errorCode"], 101, "")
  422. assert.Equal(t, body["message"], "Compare failed", "")
  423. assert.Equal(t, body["cause"], "[4 != 2]", "")
  424. assert.Equal(t, body["index"], 2, "")
  425. es[0].Stop()
  426. hs[0].Close()
  427. afterTest(t)
  428. }
  429. // Ensures that a key is not set if previous index matches but value does not.
  430. //
  431. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  432. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY -d prevValue=AAA -d prevIndex=3
  433. //
  434. func TestV2SetKeyCASOnIndexMatchAndValueFail(t *testing.T) {
  435. es, hs := buildCluster(1)
  436. u := hs[0].URL
  437. v := url.Values{}
  438. v.Set("value", "XXX")
  439. fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar")
  440. resp, _ := PutForm(fullURL, v)
  441. assert.Equal(t, resp.StatusCode, http.StatusCreated)
  442. ReadBody(resp)
  443. v.Set("value", "YYY")
  444. v.Set("prevValue", "AAA")
  445. v.Set("prevIndex", "2")
  446. resp, _ = PutForm(fullURL, v)
  447. assert.Equal(t, resp.StatusCode, http.StatusPreconditionFailed)
  448. body := ReadBodyJSON(resp)
  449. assert.Equal(t, body["errorCode"], 101, "")
  450. assert.Equal(t, body["message"], "Compare failed", "")
  451. assert.Equal(t, body["cause"], "[AAA != XXX]", "")
  452. assert.Equal(t, body["index"], 2, "")
  453. es[0].Stop()
  454. hs[0].Close()
  455. afterTest(t)
  456. }
  457. // Ensure that we can set an empty value
  458. //
  459. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=
  460. //
  461. func TestV2SetKeyCASWithEmptyValueSuccess(t *testing.T) {
  462. es, hs := buildCluster(1)
  463. u := hs[0].URL
  464. v := url.Values{}
  465. v.Set("value", "")
  466. resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  467. assert.Equal(t, resp.StatusCode, http.StatusCreated)
  468. body := ReadBody(resp)
  469. assert.Equal(t, string(body), `{"action":"set","node":{"key":"/foo/bar","value":"","modifiedIndex":2,"createdIndex":2}}`)
  470. es[0].Stop()
  471. hs[0].Close()
  472. afterTest(t)
  473. }
  474. func TestV2SetKey(t *testing.T) {
  475. es, hs := buildCluster(1)
  476. u := hs[0].URL
  477. v := url.Values{}
  478. v.Set("value", "XXX")
  479. resp, err := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  480. assert.Equal(t, resp.StatusCode, http.StatusCreated)
  481. body := ReadBody(resp)
  482. assert.Nil(t, err, "")
  483. assert.Equal(t, string(body), `{"action":"set","node":{"key":"/foo/bar","value":"XXX","modifiedIndex":2,"createdIndex":2}}`, "")
  484. resp.Body.Close()
  485. es[0].Stop()
  486. hs[0].Close()
  487. afterTest(t)
  488. }
  489. func TestV2SetKeyRedirect(t *testing.T) {
  490. es, hs := buildCluster(3)
  491. waitCluster(t, es)
  492. u := hs[1].URL
  493. ru := fmt.Sprintf("%s%s", hs[0].URL, "/v2/keys/foo/bar")
  494. v := url.Values{}
  495. v.Set("value", "XXX")
  496. resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  497. assert.Equal(t, resp.StatusCode, http.StatusTemporaryRedirect)
  498. location, err := resp.Location()
  499. if err != nil {
  500. t.Errorf("want err = %, want nil", err)
  501. }
  502. if location.String() != ru {
  503. t.Errorf("location = %v, want %v", location.String(), ru)
  504. }
  505. resp.Body.Close()
  506. for i := range es {
  507. es[len(es)-i-1].Stop()
  508. }
  509. for i := range hs {
  510. hs[len(hs)-i-1].Close()
  511. }
  512. afterTest(t)
  513. }
  514. // Ensures that a key is deleted.
  515. //
  516. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  517. // $ curl -X DELETE localhost:4001/v2/keys/foo/bar
  518. //
  519. func TestV2DeleteKey(t *testing.T) {
  520. es, hs := buildCluster(1)
  521. u := hs[0].URL
  522. v := url.Values{}
  523. v.Set("value", "XXX")
  524. resp, err := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  525. resp.Body.Close()
  526. ReadBody(resp)
  527. resp, err = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), url.Values{})
  528. assert.Equal(t, resp.StatusCode, http.StatusOK)
  529. body := ReadBody(resp)
  530. assert.Nil(t, err, "")
  531. assert.Equal(t, string(body), `{"action":"delete","node":{"key":"/foo/bar","modifiedIndex":3,"createdIndex":2},"prevNode":{"key":"/foo/bar","value":"XXX","modifiedIndex":2,"createdIndex":2}}`, "")
  532. resp.Body.Close()
  533. es[0].Stop()
  534. hs[0].Close()
  535. afterTest(t)
  536. }
  537. // Ensures that an empty directory is deleted when dir is set.
  538. //
  539. // $ curl -X PUT localhost:4001/v2/keys/foo?dir=true
  540. // $ curl -X DELETE localhost:4001/v2/keys/foo ->fail
  541. // $ curl -X DELETE localhost:4001/v2/keys/foo?dir=true
  542. //
  543. func TestV2DeleteEmptyDirectory(t *testing.T) {
  544. es, hs := buildCluster(1)
  545. u := hs[0].URL
  546. resp, err := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo?dir=true"), url.Values{})
  547. resp.Body.Close()
  548. resp, err = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), url.Values{})
  549. assert.Equal(t, resp.StatusCode, http.StatusForbidden)
  550. bodyJson := ReadBodyJSON(resp)
  551. assert.Equal(t, bodyJson["errorCode"], 102, "")
  552. resp, err = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo?dir=true"), url.Values{})
  553. assert.Equal(t, resp.StatusCode, http.StatusOK)
  554. body := ReadBody(resp)
  555. assert.Nil(t, err, "")
  556. assert.Equal(t, string(body), `{"action":"delete","node":{"key":"/foo","dir":true,"modifiedIndex":3,"createdIndex":2},"prevNode":{"key":"/foo","dir":true,"modifiedIndex":2,"createdIndex":2}}`, "")
  557. es[0].Stop()
  558. hs[0].Close()
  559. afterTest(t)
  560. }
  561. // Ensures that a not-empty directory is deleted when dir is set.
  562. //
  563. // $ curl -X PUT localhost:4001/v2/keys/foo/bar?dir=true
  564. // $ curl -X DELETE localhost:4001/v2/keys/foo?dir=true ->fail
  565. // $ curl -X DELETE localhost:4001/v2/keys/foo?dir=true&recursive=true
  566. //
  567. func TestV2DeleteNonEmptyDirectory(t *testing.T) {
  568. es, hs := buildCluster(1)
  569. u := hs[0].URL
  570. resp, err := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?dir=true"), url.Values{})
  571. ReadBody(resp)
  572. resp, err = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo?dir=true"), url.Values{})
  573. assert.Equal(t, resp.StatusCode, http.StatusForbidden)
  574. bodyJson := ReadBodyJSON(resp)
  575. assert.Equal(t, bodyJson["errorCode"], 108, "")
  576. resp, err = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo?dir=true&recursive=true"), url.Values{})
  577. assert.Equal(t, resp.StatusCode, http.StatusOK)
  578. body := ReadBody(resp)
  579. assert.Nil(t, err, "")
  580. assert.Equal(t, string(body), `{"action":"delete","node":{"key":"/foo","dir":true,"modifiedIndex":3,"createdIndex":2},"prevNode":{"key":"/foo","dir":true,"modifiedIndex":2,"createdIndex":2}}`, "")
  581. es[0].Stop()
  582. hs[0].Close()
  583. afterTest(t)
  584. }
  585. // Ensures that a directory is deleted when recursive is set.
  586. //
  587. // $ curl -X PUT localhost:4001/v2/keys/foo?dir=true
  588. // $ curl -X DELETE localhost:4001/v2/keys/foo?recursive=true
  589. //
  590. func TestV2DeleteDirectoryRecursiveImpliesDir(t *testing.T) {
  591. es, hs := buildCluster(1)
  592. u := hs[0].URL
  593. resp, err := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo?dir=true"), url.Values{})
  594. ReadBody(resp)
  595. resp, err = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo?recursive=true"), url.Values{})
  596. assert.Equal(t, resp.StatusCode, http.StatusOK)
  597. body := ReadBody(resp)
  598. assert.Nil(t, err, "")
  599. assert.Equal(t, string(body), `{"action":"delete","node":{"key":"/foo","dir":true,"modifiedIndex":3,"createdIndex":2},"prevNode":{"key":"/foo","dir":true,"modifiedIndex":2,"createdIndex":2}}`, "")
  600. es[0].Stop()
  601. hs[0].Close()
  602. afterTest(t)
  603. }
  604. // Ensures that a key is deleted if the previous index matches
  605. //
  606. // $ curl -X PUT localhost:4001/v2/keys/foo -d value=XXX
  607. // $ curl -X DELETE localhost:4001/v2/keys/foo?prevIndex=3
  608. //
  609. func TestV2DeleteKeyCADOnIndexSuccess(t *testing.T) {
  610. es, hs := buildCluster(1)
  611. u := hs[0].URL
  612. v := url.Values{}
  613. v.Set("value", "XXX")
  614. resp, err := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), v)
  615. ReadBody(resp)
  616. resp, err = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo?prevIndex=2"), url.Values{})
  617. assert.Nil(t, err, "")
  618. body := ReadBodyJSON(resp)
  619. assert.Equal(t, body["action"], "compareAndDelete", "")
  620. node := body["node"].(map[string]interface{})
  621. assert.Equal(t, node["key"], "/foo", "")
  622. assert.Equal(t, node["modifiedIndex"], 3, "")
  623. es[0].Stop()
  624. hs[0].Close()
  625. afterTest(t)
  626. }
  627. // Ensures that a key is not deleted if the previous index does not match
  628. //
  629. // $ curl -X PUT localhost:4001/v2/keys/foo -d value=XXX
  630. // $ curl -X DELETE localhost:4001/v2/keys/foo?prevIndex=100
  631. //
  632. func TestV2DeleteKeyCADOnIndexFail(t *testing.T) {
  633. es, hs := buildCluster(1)
  634. u := hs[0].URL
  635. v := url.Values{}
  636. v.Set("value", "XXX")
  637. resp, err := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), v)
  638. ReadBody(resp)
  639. resp, err = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo?prevIndex=100"), url.Values{})
  640. assert.Nil(t, err, "")
  641. body := ReadBodyJSON(resp)
  642. assert.Equal(t, body["errorCode"], 101)
  643. es[0].Stop()
  644. hs[0].Close()
  645. afterTest(t)
  646. }
  647. // Ensures that an error is thrown if an invalid previous index is provided.
  648. //
  649. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  650. // $ curl -X DELETE localhost:4001/v2/keys/foo/bar?prevIndex=bad_index
  651. //
  652. func TestV2DeleteKeyCADWithInvalidIndex(t *testing.T) {
  653. es, hs := buildCluster(1)
  654. u := hs[0].URL
  655. v := url.Values{}
  656. v.Set("value", "XXX")
  657. resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  658. ReadBody(resp)
  659. resp, _ = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?prevIndex=bad_index"), v)
  660. body := ReadBodyJSON(resp)
  661. assert.Equal(t, body["errorCode"], 203)
  662. es[0].Stop()
  663. hs[0].Close()
  664. afterTest(t)
  665. }
  666. // Ensures that a key is deleted only if the previous value matches.
  667. //
  668. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  669. // $ curl -X DELETE localhost:4001/v2/keys/foo/bar?prevValue=XXX
  670. //
  671. func TestV2DeleteKeyCADOnValueSuccess(t *testing.T) {
  672. es, hs := buildCluster(1)
  673. u := hs[0].URL
  674. v := url.Values{}
  675. v.Set("value", "XXX")
  676. resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  677. ReadBody(resp)
  678. resp, _ = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?prevValue=XXX"), v)
  679. body := ReadBodyJSON(resp)
  680. assert.Equal(t, body["action"], "compareAndDelete", "")
  681. node := body["node"].(map[string]interface{})
  682. assert.Equal(t, node["modifiedIndex"], 3, "")
  683. es[0].Stop()
  684. hs[0].Close()
  685. afterTest(t)
  686. }
  687. // Ensures that a key is not deleted if the previous value does not match.
  688. //
  689. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  690. // $ curl -X DELETE localhost:4001/v2/keys/foo/bar?prevValue=YYY
  691. //
  692. func TestV2DeleteKeyCADOnValueFail(t *testing.T) {
  693. es, hs := buildCluster(1)
  694. u := hs[0].URL
  695. v := url.Values{}
  696. v.Set("value", "XXX")
  697. resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  698. ReadBody(resp)
  699. resp, _ = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?prevValue=YYY"), v)
  700. body := ReadBodyJSON(resp)
  701. assert.Equal(t, body["errorCode"], 101)
  702. es[0].Stop()
  703. hs[0].Close()
  704. afterTest(t)
  705. }
  706. // Ensures that an error is thrown if an invalid previous value is provided.
  707. //
  708. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  709. // $ curl -X DELETE localhost:4001/v2/keys/foo/bar?prevIndex=
  710. //
  711. func TestV2DeleteKeyCADWithInvalidValue(t *testing.T) {
  712. es, hs := buildCluster(1)
  713. u := hs[0].URL
  714. v := url.Values{}
  715. v.Set("value", "XXX")
  716. resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  717. ReadBody(resp)
  718. resp, _ = DeleteForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?prevValue="), v)
  719. body := ReadBodyJSON(resp)
  720. assert.Equal(t, body["errorCode"], 201)
  721. es[0].Stop()
  722. hs[0].Close()
  723. afterTest(t)
  724. }
  725. // Ensures a unique value is added to the key's children.
  726. //
  727. // $ curl -X POST localhost:4001/v2/keys/foo/bar
  728. // $ curl -X POST localhost:4001/v2/keys/foo/bar
  729. // $ curl -X POST localhost:4001/v2/keys/foo/baz
  730. //
  731. func TestV2CreateUnique(t *testing.T) {
  732. es, hs := buildCluster(1)
  733. u := hs[0].URL
  734. // POST should add index to list.
  735. fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar")
  736. resp, _ := PostForm(fullURL, nil)
  737. assert.Equal(t, resp.StatusCode, http.StatusCreated)
  738. body := ReadBodyJSON(resp)
  739. assert.Equal(t, body["action"], "create", "")
  740. node := body["node"].(map[string]interface{})
  741. assert.Equal(t, node["key"], "/foo/bar/2", "")
  742. assert.Nil(t, node["dir"], "")
  743. assert.Equal(t, node["modifiedIndex"], 2, "")
  744. // Second POST should add next index to list.
  745. resp, _ = PostForm(fullURL, nil)
  746. assert.Equal(t, resp.StatusCode, http.StatusCreated)
  747. body = ReadBodyJSON(resp)
  748. node = body["node"].(map[string]interface{})
  749. assert.Equal(t, node["key"], "/foo/bar/3", "")
  750. // POST to a different key should add index to that list.
  751. resp, _ = PostForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/baz"), nil)
  752. assert.Equal(t, resp.StatusCode, http.StatusCreated)
  753. body = ReadBodyJSON(resp)
  754. node = body["node"].(map[string]interface{})
  755. assert.Equal(t, node["key"], "/foo/baz/4", "")
  756. es[0].Stop()
  757. hs[0].Close()
  758. afterTest(t)
  759. }
  760. //
  761. // $ curl localhost:4001/v2/keys/foo/bar -> fail
  762. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  763. // $ curl localhost:4001/v2/keys/foo/bar
  764. //
  765. func TestV2GetKey(t *testing.T) {
  766. es, hs := buildCluster(1)
  767. u := hs[0].URL
  768. v := url.Values{}
  769. v.Set("value", "XXX")
  770. fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar")
  771. resp, _ := Get(fullURL)
  772. resp.Body.Close()
  773. resp, _ = PutForm(fullURL, v)
  774. resp.Body.Close()
  775. resp, _ = Get(fullURL)
  776. assert.Equal(t, resp.Header.Get("Content-Type"), "application/json")
  777. assert.Equal(t, resp.StatusCode, http.StatusOK)
  778. body := ReadBodyJSON(resp)
  779. resp.Body.Close()
  780. assert.Equal(t, body["action"], "get", "")
  781. node := body["node"].(map[string]interface{})
  782. assert.Equal(t, node["key"], "/foo/bar", "")
  783. assert.Equal(t, node["value"], "XXX", "")
  784. assert.Equal(t, node["modifiedIndex"], 2, "")
  785. es[0].Stop()
  786. hs[0].Close()
  787. afterTest(t)
  788. }
  789. // Ensures that a directory of values can be recursively retrieved for a given key.
  790. //
  791. // $ curl -X PUT localhost:4001/v2/keys/foo/x -d value=XXX
  792. // $ curl -X PUT localhost:4001/v2/keys/foo/y/z -d value=YYY
  793. // $ curl localhost:4001/v2/keys/foo -d recursive=true
  794. //
  795. func TestV2GetKeyRecursively(t *testing.T) {
  796. es, hs := buildCluster(1)
  797. u := hs[0].URL
  798. v := url.Values{}
  799. v.Set("value", "XXX")
  800. v.Set("ttl", "10")
  801. resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/x"), v)
  802. ReadBody(resp)
  803. v.Set("value", "YYY")
  804. resp, _ = PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/y/z"), v)
  805. ReadBody(resp)
  806. resp, _ = Get(fmt.Sprintf("%s%s", u, "/v2/keys/foo?recursive=true"))
  807. assert.Equal(t, resp.StatusCode, http.StatusOK)
  808. body := ReadBodyJSON(resp)
  809. assert.Equal(t, body["action"], "get", "")
  810. node := body["node"].(map[string]interface{})
  811. assert.Equal(t, node["key"], "/foo", "")
  812. assert.Equal(t, node["dir"], true, "")
  813. assert.Equal(t, node["modifiedIndex"], 2, "")
  814. assert.Equal(t, len(node["nodes"].([]interface{})), 2, "")
  815. node0 := node["nodes"].([]interface{})[0].(map[string]interface{})
  816. assert.Equal(t, node0["key"], "/foo/x", "")
  817. assert.Equal(t, node0["value"], "XXX", "")
  818. assert.Equal(t, node0["ttl"], 10, "")
  819. node1 := node["nodes"].([]interface{})[1].(map[string]interface{})
  820. assert.Equal(t, node1["key"], "/foo/y", "")
  821. assert.Equal(t, node1["dir"], true, "")
  822. node2 := node1["nodes"].([]interface{})[0].(map[string]interface{})
  823. assert.Equal(t, node2["key"], "/foo/y/z", "")
  824. assert.Equal(t, node2["value"], "YYY", "")
  825. es[0].Stop()
  826. hs[0].Close()
  827. afterTest(t)
  828. }
  829. // Ensures that a watcher can wait for a value to be set and return it to the client.
  830. //
  831. // $ curl localhost:4001/v2/keys/foo/bar?wait=true
  832. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  833. //
  834. func TestV2WatchKey(t *testing.T) {
  835. es, hs := buildCluster(1)
  836. u := hs[0].URL
  837. // There exists a little gap between etcd ready to serve and
  838. // it actually serves the first request, which means the response
  839. // delay could be a little bigger.
  840. // This test is time sensitive, so it does one request to ensure
  841. // that the server is working.
  842. resp, _ := Get(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"))
  843. resp.Body.Close()
  844. var watchResp *http.Response
  845. c := make(chan bool)
  846. go func() {
  847. watchResp, _ = Get(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?wait=true"))
  848. c <- true
  849. }()
  850. // Make sure response didn't fire early.
  851. time.Sleep(1 * time.Millisecond)
  852. // Set a value.
  853. v := url.Values{}
  854. v.Set("value", "XXX")
  855. resp, _ = PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  856. ReadBody(resp)
  857. // A response should follow from the GET above.
  858. time.Sleep(1 * time.Millisecond)
  859. select {
  860. case <-c:
  861. default:
  862. t.Fatal("cannot get watch result")
  863. }
  864. body := ReadBodyJSON(watchResp)
  865. watchResp.Body.Close()
  866. assert.NotNil(t, body, "")
  867. assert.Equal(t, body["action"], "set", "")
  868. node := body["node"].(map[string]interface{})
  869. assert.Equal(t, node["key"], "/foo/bar", "")
  870. assert.Equal(t, node["value"], "XXX", "")
  871. assert.Equal(t, node["modifiedIndex"], 2, "")
  872. es[0].Stop()
  873. hs[0].Close()
  874. afterTest(t)
  875. }
  876. // Ensures that a watcher can wait for a value to be set after a given index.
  877. //
  878. // $ curl localhost:4001/v2/keys/foo/bar?wait=true&waitIndex=3
  879. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  880. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY
  881. //
  882. func TestV2WatchKeyWithIndex(t *testing.T) {
  883. es, hs := buildCluster(1)
  884. u := hs[0].URL
  885. var body map[string]interface{}
  886. c := make(chan bool)
  887. go func() {
  888. resp, _ := Get(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?wait=true&waitIndex=3"))
  889. body = ReadBodyJSON(resp)
  890. c <- true
  891. }()
  892. // Make sure response didn't fire early.
  893. time.Sleep(1 * time.Millisecond)
  894. assert.Nil(t, body, "")
  895. // Set a value (before given index).
  896. v := url.Values{}
  897. v.Set("value", "XXX")
  898. resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  899. ReadBody(resp)
  900. // Make sure response didn't fire early.
  901. time.Sleep(1 * time.Millisecond)
  902. assert.Nil(t, body, "")
  903. // Set a value (before given index).
  904. v.Set("value", "YYY")
  905. resp, _ = PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  906. ReadBody(resp)
  907. // A response should follow from the GET above.
  908. time.Sleep(1 * time.Millisecond)
  909. select {
  910. case <-c:
  911. default:
  912. t.Fatal("cannot get watch result")
  913. }
  914. assert.NotNil(t, body, "")
  915. assert.Equal(t, body["action"], "set", "")
  916. node := body["node"].(map[string]interface{})
  917. assert.Equal(t, node["key"], "/foo/bar", "")
  918. assert.Equal(t, node["value"], "YYY", "")
  919. assert.Equal(t, node["modifiedIndex"], 3, "")
  920. es[0].Stop()
  921. hs[0].Close()
  922. afterTest(t)
  923. }
  924. // Ensures that a watcher can wait for a value to be set after a given index.
  925. //
  926. // $ curl localhost:4001/v2/keys/keyindir/bar?wait=true
  927. // $ curl -X PUT localhost:4001/v2/keys/keyindir -d dir=true -d ttl=1
  928. // $ curl -X PUT localhost:4001/v2/keys/keyindir/bar -d value=YYY
  929. //
  930. func TestV2WatchKeyInDir(t *testing.T) {
  931. es, hs := buildCluster(1)
  932. u := hs[0].URL
  933. var body map[string]interface{}
  934. c := make(chan bool)
  935. // Set a value (before given index).
  936. v := url.Values{}
  937. v.Set("dir", "true")
  938. v.Set("ttl", "1")
  939. resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/keyindir"), v)
  940. ReadBody(resp)
  941. // Set a value (before given index).
  942. v = url.Values{}
  943. v.Set("value", "XXX")
  944. resp, _ = PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/keyindir/bar"), v)
  945. ReadBody(resp)
  946. go func() {
  947. resp, _ := Get(fmt.Sprintf("%s%s", u, "/v2/keys/keyindir/bar?wait=true"))
  948. body = ReadBodyJSON(resp)
  949. c <- true
  950. }()
  951. // wait for expiration, we do have a up to 500 millisecond delay
  952. time.Sleep(time.Second + time.Millisecond*500)
  953. select {
  954. case <-c:
  955. default:
  956. t.Fatal("cannot get watch result")
  957. }
  958. assert.NotNil(t, body, "")
  959. assert.Equal(t, body["action"], "expire", "")
  960. node := body["node"].(map[string]interface{})
  961. assert.Equal(t, node["key"], "/keyindir", "")
  962. es[0].Stop()
  963. hs[0].Close()
  964. afterTest(t)
  965. }
  966. // Ensures that HEAD could work.
  967. //
  968. // $ curl -I localhost:4001/v2/keys/foo/bar -> fail
  969. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  970. // $ curl -I localhost:4001/v2/keys/foo/bar
  971. //
  972. func TestV2HeadKey(t *testing.T) {
  973. es, hs := buildCluster(1)
  974. u := hs[0].URL
  975. v := url.Values{}
  976. v.Set("value", "XXX")
  977. fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar")
  978. resp, _ := Head(fullURL)
  979. assert.Equal(t, resp.StatusCode, http.StatusNotFound)
  980. assert.Equal(t, resp.ContentLength, -1)
  981. resp, _ = PutForm(fullURL, v)
  982. ReadBody(resp)
  983. resp, _ = Head(fullURL)
  984. assert.Equal(t, resp.StatusCode, http.StatusOK)
  985. assert.Equal(t, resp.ContentLength, -1)
  986. es[0].Stop()
  987. hs[0].Close()
  988. afterTest(t)
  989. }