v2_http_kv_test.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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. // TODO(xiangli): fix the wrong assumption here.
  816. // the order of nodes map cannot be determined.
  817. node0 := node["nodes"].([]interface{})[0].(map[string]interface{})
  818. assert.Equal(t, node0["key"], "/foo/x", "")
  819. assert.Equal(t, node0["value"], "XXX", "")
  820. assert.Equal(t, node0["ttl"], 10, "")
  821. node1 := node["nodes"].([]interface{})[1].(map[string]interface{})
  822. assert.Equal(t, node1["key"], "/foo/y", "")
  823. assert.Equal(t, node1["dir"], true, "")
  824. node2 := node1["nodes"].([]interface{})[0].(map[string]interface{})
  825. assert.Equal(t, node2["key"], "/foo/y/z", "")
  826. assert.Equal(t, node2["value"], "YYY", "")
  827. es[0].Stop()
  828. hs[0].Close()
  829. afterTest(t)
  830. }
  831. // Ensures that a watcher can wait for a value to be set and return it to the client.
  832. //
  833. // $ curl localhost:4001/v2/keys/foo/bar?wait=true
  834. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  835. //
  836. func TestV2WatchKey(t *testing.T) {
  837. es, hs := buildCluster(1)
  838. u := hs[0].URL
  839. // There exists a little gap between etcd ready to serve and
  840. // it actually serves the first request, which means the response
  841. // delay could be a little bigger.
  842. // This test is time sensitive, so it does one request to ensure
  843. // that the server is working.
  844. resp, _ := Get(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"))
  845. resp.Body.Close()
  846. var watchResp *http.Response
  847. c := make(chan bool)
  848. go func() {
  849. watchResp, _ = Get(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?wait=true"))
  850. c <- true
  851. }()
  852. // Make sure response didn't fire early.
  853. time.Sleep(1 * time.Millisecond)
  854. // Set a value.
  855. v := url.Values{}
  856. v.Set("value", "XXX")
  857. resp, _ = PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  858. ReadBody(resp)
  859. // A response should follow from the GET above.
  860. time.Sleep(1 * time.Millisecond)
  861. select {
  862. case <-c:
  863. default:
  864. t.Fatal("cannot get watch result")
  865. }
  866. body := ReadBodyJSON(watchResp)
  867. watchResp.Body.Close()
  868. assert.NotNil(t, body, "")
  869. assert.Equal(t, body["action"], "set", "")
  870. node := body["node"].(map[string]interface{})
  871. assert.Equal(t, node["key"], "/foo/bar", "")
  872. assert.Equal(t, node["value"], "XXX", "")
  873. assert.Equal(t, node["modifiedIndex"], 2, "")
  874. es[0].Stop()
  875. hs[0].Close()
  876. afterTest(t)
  877. }
  878. // Ensures that a watcher can wait for a value to be set after a given index.
  879. //
  880. // $ curl localhost:4001/v2/keys/foo/bar?wait=true&waitIndex=3
  881. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  882. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=YYY
  883. //
  884. func TestV2WatchKeyWithIndex(t *testing.T) {
  885. es, hs := buildCluster(1)
  886. u := hs[0].URL
  887. var body map[string]interface{}
  888. c := make(chan bool)
  889. go func() {
  890. resp, _ := Get(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?wait=true&waitIndex=3"))
  891. body = ReadBodyJSON(resp)
  892. c <- true
  893. }()
  894. // Make sure response didn't fire early.
  895. time.Sleep(1 * time.Millisecond)
  896. assert.Nil(t, body, "")
  897. // Set a value (before given index).
  898. v := url.Values{}
  899. v.Set("value", "XXX")
  900. resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  901. ReadBody(resp)
  902. // Make sure response didn't fire early.
  903. time.Sleep(1 * time.Millisecond)
  904. assert.Nil(t, body, "")
  905. // Set a value (before given index).
  906. v.Set("value", "YYY")
  907. resp, _ = PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  908. ReadBody(resp)
  909. // A response should follow from the GET above.
  910. time.Sleep(1 * time.Millisecond)
  911. select {
  912. case <-c:
  913. default:
  914. t.Fatal("cannot get watch result")
  915. }
  916. assert.NotNil(t, body, "")
  917. assert.Equal(t, body["action"], "set", "")
  918. node := body["node"].(map[string]interface{})
  919. assert.Equal(t, node["key"], "/foo/bar", "")
  920. assert.Equal(t, node["value"], "YYY", "")
  921. assert.Equal(t, node["modifiedIndex"], 3, "")
  922. es[0].Stop()
  923. hs[0].Close()
  924. afterTest(t)
  925. }
  926. // Ensures that a watcher can wait for a value to be set after a given index.
  927. //
  928. // $ curl localhost:4001/v2/keys/keyindir/bar?wait=true
  929. // $ curl -X PUT localhost:4001/v2/keys/keyindir -d dir=true -d ttl=1
  930. // $ curl -X PUT localhost:4001/v2/keys/keyindir/bar -d value=YYY
  931. //
  932. func TestV2WatchKeyInDir(t *testing.T) {
  933. es, hs := buildCluster(1)
  934. u := hs[0].URL
  935. var body map[string]interface{}
  936. c := make(chan bool)
  937. // Set a value (before given index).
  938. v := url.Values{}
  939. v.Set("dir", "true")
  940. v.Set("ttl", "1")
  941. resp, _ := PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/keyindir"), v)
  942. ReadBody(resp)
  943. // Set a value (before given index).
  944. v = url.Values{}
  945. v.Set("value", "XXX")
  946. resp, _ = PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/keyindir/bar"), v)
  947. ReadBody(resp)
  948. go func() {
  949. resp, _ := Get(fmt.Sprintf("%s%s", u, "/v2/keys/keyindir/bar?wait=true"))
  950. body = ReadBodyJSON(resp)
  951. c <- true
  952. }()
  953. // wait for expiration, we do have a up to 500 millisecond delay
  954. time.Sleep(time.Second + time.Millisecond*500)
  955. select {
  956. case <-c:
  957. default:
  958. t.Fatal("cannot get watch result")
  959. }
  960. assert.NotNil(t, body, "")
  961. assert.Equal(t, body["action"], "expire", "")
  962. node := body["node"].(map[string]interface{})
  963. assert.Equal(t, node["key"], "/keyindir", "")
  964. es[0].Stop()
  965. hs[0].Close()
  966. afterTest(t)
  967. }
  968. // Ensures that HEAD could work.
  969. //
  970. // $ curl -I localhost:4001/v2/keys/foo/bar -> fail
  971. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  972. // $ curl -I localhost:4001/v2/keys/foo/bar
  973. //
  974. func TestV2HeadKey(t *testing.T) {
  975. es, hs := buildCluster(1)
  976. u := hs[0].URL
  977. v := url.Values{}
  978. v.Set("value", "XXX")
  979. fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar")
  980. resp, _ := Head(fullURL)
  981. assert.Equal(t, resp.StatusCode, http.StatusNotFound)
  982. assert.Equal(t, resp.ContentLength, -1)
  983. resp, _ = PutForm(fullURL, v)
  984. ReadBody(resp)
  985. resp, _ = Head(fullURL)
  986. assert.Equal(t, resp.StatusCode, http.StatusOK)
  987. assert.Equal(t, resp.ContentLength, -1)
  988. es[0].Stop()
  989. hs[0].Close()
  990. afterTest(t)
  991. }