v2_http_kv_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package integration
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "io"
  18. "io/ioutil"
  19. "log"
  20. "net"
  21. "net/http"
  22. "net/url"
  23. "reflect"
  24. "strings"
  25. "testing"
  26. "time"
  27. )
  28. func init() {
  29. log.SetOutput(ioutil.Discard)
  30. }
  31. func TestV2Set(t *testing.T) {
  32. cl := NewCluster(t, 1)
  33. cl.Launch(t)
  34. defer cl.Terminate(t)
  35. u := cl.URL(0)
  36. tc := NewTestClient()
  37. v := url.Values{}
  38. v.Set("value", "bar")
  39. tests := []struct {
  40. relativeURL string
  41. value url.Values
  42. wStatus int
  43. w string
  44. }{
  45. {
  46. "/v2/keys/foo/bar",
  47. v,
  48. http.StatusCreated,
  49. `{"action":"set","node":{"key":"/foo/bar","value":"bar","modifiedIndex":3,"createdIndex":3}}`,
  50. },
  51. {
  52. "/v2/keys/foodir?dir=true",
  53. url.Values{},
  54. http.StatusCreated,
  55. `{"action":"set","node":{"key":"/foodir","dir":true,"modifiedIndex":4,"createdIndex":4}}`,
  56. },
  57. {
  58. "/v2/keys/fooempty",
  59. url.Values(map[string][]string{"value": {""}}),
  60. http.StatusCreated,
  61. `{"action":"set","node":{"key":"/fooempty","value":"","modifiedIndex":5,"createdIndex":5}}`,
  62. },
  63. }
  64. for i, tt := range tests {
  65. resp, err := tc.PutForm(fmt.Sprintf("%s%s", u, tt.relativeURL), tt.value)
  66. if err != nil {
  67. t.Errorf("#%d: err = %v, want nil", i, err)
  68. }
  69. g := string(tc.ReadBody(resp))
  70. w := tt.w + "\n"
  71. if g != w {
  72. t.Errorf("#%d: body = %v, want %v", i, g, w)
  73. }
  74. if resp.StatusCode != tt.wStatus {
  75. t.Errorf("#%d: status = %d, want %d", i, resp.StatusCode, tt.wStatus)
  76. }
  77. }
  78. }
  79. func TestV2CreateUpdate(t *testing.T) {
  80. cl := NewCluster(t, 1)
  81. cl.Launch(t)
  82. defer cl.Terminate(t)
  83. u := cl.URL(0)
  84. tc := NewTestClient()
  85. tests := []struct {
  86. relativeURL string
  87. value url.Values
  88. wStatus int
  89. w map[string]interface{}
  90. }{
  91. // key with ttl
  92. {
  93. "/v2/keys/ttl/foo",
  94. url.Values(map[string][]string{"value": {"XXX"}, "ttl": {"20"}}),
  95. http.StatusCreated,
  96. map[string]interface{}{
  97. "node": map[string]interface{}{
  98. "value": "XXX",
  99. "ttl": float64(20),
  100. },
  101. },
  102. },
  103. // key with bad ttl
  104. {
  105. "/v2/keys/ttl/foo",
  106. url.Values(map[string][]string{"value": {"XXX"}, "ttl": {"bad_ttl"}}),
  107. http.StatusBadRequest,
  108. map[string]interface{}{
  109. "errorCode": float64(202),
  110. "message": "The given TTL in POST form is not a number",
  111. },
  112. },
  113. // create key
  114. {
  115. "/v2/keys/create/foo",
  116. url.Values(map[string][]string{"value": {"XXX"}, "prevExist": {"false"}}),
  117. http.StatusCreated,
  118. map[string]interface{}{
  119. "node": map[string]interface{}{
  120. "value": "XXX",
  121. },
  122. },
  123. },
  124. // created key failed
  125. {
  126. "/v2/keys/create/foo",
  127. url.Values(map[string][]string{"value": {"XXX"}, "prevExist": {"false"}}),
  128. http.StatusPreconditionFailed,
  129. map[string]interface{}{
  130. "errorCode": float64(105),
  131. "message": "Key already exists",
  132. "cause": "/create/foo",
  133. },
  134. },
  135. // update the newly created key with ttl
  136. {
  137. "/v2/keys/create/foo",
  138. url.Values(map[string][]string{"value": {"YYY"}, "prevExist": {"true"}, "ttl": {"20"}}),
  139. http.StatusOK,
  140. map[string]interface{}{
  141. "node": map[string]interface{}{
  142. "value": "YYY",
  143. "ttl": float64(20),
  144. },
  145. "action": "update",
  146. },
  147. },
  148. // update the ttl to none
  149. {
  150. "/v2/keys/create/foo",
  151. url.Values(map[string][]string{"value": {"ZZZ"}, "prevExist": {"true"}}),
  152. http.StatusOK,
  153. map[string]interface{}{
  154. "node": map[string]interface{}{
  155. "value": "ZZZ",
  156. },
  157. "action": "update",
  158. },
  159. },
  160. // update on a non-existing key
  161. {
  162. "/v2/keys/nonexist",
  163. url.Values(map[string][]string{"value": {"XXX"}, "prevExist": {"true"}}),
  164. http.StatusNotFound,
  165. map[string]interface{}{
  166. "errorCode": float64(100),
  167. "message": "Key not found",
  168. "cause": "/nonexist",
  169. },
  170. },
  171. }
  172. for i, tt := range tests {
  173. resp, _ := tc.PutForm(fmt.Sprintf("%s%s", u, tt.relativeURL), tt.value)
  174. if resp.StatusCode != tt.wStatus {
  175. t.Errorf("#%d: status = %d, want %d", i, resp.StatusCode, tt.wStatus)
  176. }
  177. if err := checkBody(tc.ReadBodyJSON(resp), tt.w); err != nil {
  178. t.Errorf("#%d: %v", i, err)
  179. }
  180. }
  181. }
  182. func TestV2CAS(t *testing.T) {
  183. cl := NewCluster(t, 1)
  184. cl.Launch(t)
  185. defer cl.Terminate(t)
  186. u := cl.URL(0)
  187. tc := NewTestClient()
  188. tests := []struct {
  189. relativeURL string
  190. value url.Values
  191. wStatus int
  192. w map[string]interface{}
  193. }{
  194. {
  195. "/v2/keys/cas/foo",
  196. url.Values(map[string][]string{"value": {"XXX"}}),
  197. http.StatusCreated,
  198. nil,
  199. },
  200. {
  201. "/v2/keys/cas/foo",
  202. url.Values(map[string][]string{"value": {"YYY"}, "prevIndex": {"3"}}),
  203. http.StatusOK,
  204. map[string]interface{}{
  205. "node": map[string]interface{}{
  206. "value": "YYY",
  207. "modifiedIndex": float64(4),
  208. },
  209. "action": "compareAndSwap",
  210. },
  211. },
  212. {
  213. "/v2/keys/cas/foo",
  214. url.Values(map[string][]string{"value": {"YYY"}, "prevIndex": {"10"}}),
  215. http.StatusPreconditionFailed,
  216. map[string]interface{}{
  217. "errorCode": float64(101),
  218. "message": "Compare failed",
  219. "cause": "[10 != 4]",
  220. "index": float64(4),
  221. },
  222. },
  223. {
  224. "/v2/keys/cas/foo",
  225. url.Values(map[string][]string{"value": {"YYY"}, "prevIndex": {"bad_index"}}),
  226. http.StatusBadRequest,
  227. map[string]interface{}{
  228. "errorCode": float64(203),
  229. "message": "The given index in POST form is not a number",
  230. },
  231. },
  232. {
  233. "/v2/keys/cas/foo",
  234. url.Values(map[string][]string{"value": {"ZZZ"}, "prevValue": {"YYY"}}),
  235. http.StatusOK,
  236. map[string]interface{}{
  237. "node": map[string]interface{}{
  238. "value": "ZZZ",
  239. },
  240. "action": "compareAndSwap",
  241. },
  242. },
  243. {
  244. "/v2/keys/cas/foo",
  245. url.Values(map[string][]string{"value": {"XXX"}, "prevValue": {"bad_value"}}),
  246. http.StatusPreconditionFailed,
  247. map[string]interface{}{
  248. "errorCode": float64(101),
  249. "message": "Compare failed",
  250. "cause": "[bad_value != ZZZ]",
  251. },
  252. },
  253. // prevValue is required
  254. {
  255. "/v2/keys/cas/foo",
  256. url.Values(map[string][]string{"value": {"XXX"}, "prevValue": {""}}),
  257. http.StatusBadRequest,
  258. map[string]interface{}{
  259. "errorCode": float64(201),
  260. },
  261. },
  262. {
  263. "/v2/keys/cas/foo",
  264. url.Values(map[string][]string{"value": {"XXX"}, "prevValue": {"bad_value"}, "prevIndex": {"100"}}),
  265. http.StatusPreconditionFailed,
  266. map[string]interface{}{
  267. "errorCode": float64(101),
  268. "message": "Compare failed",
  269. "cause": "[bad_value != ZZZ] [100 != 5]",
  270. },
  271. },
  272. {
  273. "/v2/keys/cas/foo",
  274. url.Values(map[string][]string{"value": {"XXX"}, "prevValue": {"ZZZ"}, "prevIndex": {"100"}}),
  275. http.StatusPreconditionFailed,
  276. map[string]interface{}{
  277. "errorCode": float64(101),
  278. "message": "Compare failed",
  279. "cause": "[100 != 5]",
  280. },
  281. },
  282. {
  283. "/v2/keys/cas/foo",
  284. url.Values(map[string][]string{"value": {"XXX"}, "prevValue": {"bad_value"}, "prevIndex": {"5"}}),
  285. http.StatusPreconditionFailed,
  286. map[string]interface{}{
  287. "errorCode": float64(101),
  288. "message": "Compare failed",
  289. "cause": "[bad_value != ZZZ]",
  290. },
  291. },
  292. }
  293. for i, tt := range tests {
  294. resp, _ := tc.PutForm(fmt.Sprintf("%s%s", u, tt.relativeURL), tt.value)
  295. if resp.StatusCode != tt.wStatus {
  296. t.Errorf("#%d: status = %d, want %d", i, resp.StatusCode, tt.wStatus)
  297. }
  298. if err := checkBody(tc.ReadBodyJSON(resp), tt.w); err != nil {
  299. t.Errorf("#%d: %v", i, err)
  300. }
  301. }
  302. }
  303. func TestV2Delete(t *testing.T) {
  304. cl := NewCluster(t, 1)
  305. cl.Launch(t)
  306. defer cl.Terminate(t)
  307. u := cl.URL(0)
  308. tc := NewTestClient()
  309. v := url.Values{}
  310. v.Set("value", "XXX")
  311. resp, err := tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), v)
  312. if err != nil {
  313. t.Error(err)
  314. }
  315. resp.Body.Close()
  316. resp, err = tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/emptydir?dir=true"), v)
  317. if err != nil {
  318. t.Error(err)
  319. }
  320. resp.Body.Close()
  321. resp, err = tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foodir/bar?dir=true"), v)
  322. if err != nil {
  323. t.Error(err)
  324. }
  325. resp.Body.Close()
  326. tests := []struct {
  327. relativeURL string
  328. wStatus int
  329. w map[string]interface{}
  330. }{
  331. {
  332. "/v2/keys/foo",
  333. http.StatusOK,
  334. map[string]interface{}{
  335. "node": map[string]interface{}{
  336. "key": "/foo",
  337. },
  338. "prevNode": map[string]interface{}{
  339. "key": "/foo",
  340. "value": "XXX",
  341. },
  342. "action": "delete",
  343. },
  344. },
  345. {
  346. "/v2/keys/emptydir",
  347. http.StatusForbidden,
  348. map[string]interface{}{
  349. "errorCode": float64(102),
  350. "message": "Not a file",
  351. "cause": "/emptydir",
  352. },
  353. },
  354. {
  355. "/v2/keys/emptydir?dir=true",
  356. http.StatusOK,
  357. nil,
  358. },
  359. {
  360. "/v2/keys/foodir?dir=true",
  361. http.StatusForbidden,
  362. map[string]interface{}{
  363. "errorCode": float64(108),
  364. "message": "Directory not empty",
  365. "cause": "/foodir",
  366. },
  367. },
  368. {
  369. "/v2/keys/foodir?recursive=true",
  370. http.StatusOK,
  371. map[string]interface{}{
  372. "node": map[string]interface{}{
  373. "key": "/foodir",
  374. "dir": true,
  375. },
  376. "prevNode": map[string]interface{}{
  377. "key": "/foodir",
  378. "dir": true,
  379. },
  380. "action": "delete",
  381. },
  382. },
  383. }
  384. for i, tt := range tests {
  385. resp, _ := tc.DeleteForm(fmt.Sprintf("%s%s", u, tt.relativeURL), nil)
  386. if resp.StatusCode != tt.wStatus {
  387. t.Errorf("#%d: status = %d, want %d", i, resp.StatusCode, tt.wStatus)
  388. }
  389. if err := checkBody(tc.ReadBodyJSON(resp), tt.w); err != nil {
  390. t.Errorf("#%d: %v", i, err)
  391. }
  392. }
  393. }
  394. func TestV2CAD(t *testing.T) {
  395. cl := NewCluster(t, 1)
  396. cl.Launch(t)
  397. defer cl.Terminate(t)
  398. u := cl.URL(0)
  399. tc := NewTestClient()
  400. v := url.Values{}
  401. v.Set("value", "XXX")
  402. resp, err := tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo"), v)
  403. if err != nil {
  404. t.Error(err)
  405. }
  406. resp.Body.Close()
  407. resp, err = tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foovalue"), v)
  408. if err != nil {
  409. t.Error(err)
  410. }
  411. resp.Body.Close()
  412. tests := []struct {
  413. relativeURL string
  414. wStatus int
  415. w map[string]interface{}
  416. }{
  417. {
  418. "/v2/keys/foo?prevIndex=100",
  419. http.StatusPreconditionFailed,
  420. map[string]interface{}{
  421. "errorCode": float64(101),
  422. "message": "Compare failed",
  423. "cause": "[100 != 3]",
  424. },
  425. },
  426. {
  427. "/v2/keys/foo?prevIndex=bad_index",
  428. http.StatusBadRequest,
  429. map[string]interface{}{
  430. "errorCode": float64(203),
  431. "message": "The given index in POST form is not a number",
  432. },
  433. },
  434. {
  435. "/v2/keys/foo?prevIndex=3",
  436. http.StatusOK,
  437. map[string]interface{}{
  438. "node": map[string]interface{}{
  439. "key": "/foo",
  440. "modifiedIndex": float64(5),
  441. },
  442. "action": "compareAndDelete",
  443. },
  444. },
  445. {
  446. "/v2/keys/foovalue?prevValue=YYY",
  447. http.StatusPreconditionFailed,
  448. map[string]interface{}{
  449. "errorCode": float64(101),
  450. "message": "Compare failed",
  451. "cause": "[YYY != XXX]",
  452. },
  453. },
  454. {
  455. "/v2/keys/foovalue?prevValue=",
  456. http.StatusBadRequest,
  457. map[string]interface{}{
  458. "errorCode": float64(201),
  459. "cause": `"prevValue" cannot be empty`,
  460. },
  461. },
  462. {
  463. "/v2/keys/foovalue?prevValue=XXX",
  464. http.StatusOK,
  465. map[string]interface{}{
  466. "node": map[string]interface{}{
  467. "key": "/foovalue",
  468. "modifiedIndex": float64(6),
  469. },
  470. "action": "compareAndDelete",
  471. },
  472. },
  473. }
  474. for i, tt := range tests {
  475. resp, _ := tc.DeleteForm(fmt.Sprintf("%s%s", u, tt.relativeURL), nil)
  476. if resp.StatusCode != tt.wStatus {
  477. t.Errorf("#%d: status = %d, want %d", i, resp.StatusCode, tt.wStatus)
  478. }
  479. if err := checkBody(tc.ReadBodyJSON(resp), tt.w); err != nil {
  480. t.Errorf("#%d: %v", i, err)
  481. }
  482. }
  483. }
  484. func TestV2Unique(t *testing.T) {
  485. cl := NewCluster(t, 1)
  486. cl.Launch(t)
  487. defer cl.Terminate(t)
  488. u := cl.URL(0)
  489. tc := NewTestClient()
  490. tests := []struct {
  491. relativeURL string
  492. value url.Values
  493. wStatus int
  494. w map[string]interface{}
  495. }{
  496. {
  497. "/v2/keys/foo",
  498. url.Values(map[string][]string{"value": {"XXX"}}),
  499. http.StatusCreated,
  500. map[string]interface{}{
  501. "node": map[string]interface{}{
  502. "key": "/foo/3",
  503. "value": "XXX",
  504. },
  505. "action": "create",
  506. },
  507. },
  508. {
  509. "/v2/keys/foo",
  510. url.Values(map[string][]string{"value": {"XXX"}}),
  511. http.StatusCreated,
  512. map[string]interface{}{
  513. "node": map[string]interface{}{
  514. "key": "/foo/4",
  515. "value": "XXX",
  516. },
  517. "action": "create",
  518. },
  519. },
  520. {
  521. "/v2/keys/bar",
  522. url.Values(map[string][]string{"value": {"XXX"}}),
  523. http.StatusCreated,
  524. map[string]interface{}{
  525. "node": map[string]interface{}{
  526. "key": "/bar/5",
  527. "value": "XXX",
  528. },
  529. "action": "create",
  530. },
  531. },
  532. }
  533. for i, tt := range tests {
  534. resp, _ := tc.PostForm(fmt.Sprintf("%s%s", u, tt.relativeURL), tt.value)
  535. if resp.StatusCode != tt.wStatus {
  536. t.Errorf("#%d: status = %d, want %d", i, resp.StatusCode, tt.wStatus)
  537. }
  538. if err := checkBody(tc.ReadBodyJSON(resp), tt.w); err != nil {
  539. t.Errorf("#%d: %v", i, err)
  540. }
  541. }
  542. }
  543. func TestV2Get(t *testing.T) {
  544. cl := NewCluster(t, 1)
  545. cl.Launch(t)
  546. defer cl.Terminate(t)
  547. u := cl.URL(0)
  548. tc := NewTestClient()
  549. v := url.Values{}
  550. v.Set("value", "XXX")
  551. resp, err := tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar/zar"), v)
  552. if err != nil {
  553. t.Error(err)
  554. }
  555. resp.Body.Close()
  556. tests := []struct {
  557. relativeURL string
  558. wStatus int
  559. w map[string]interface{}
  560. }{
  561. {
  562. "/v2/keys/foo/bar/zar",
  563. http.StatusOK,
  564. map[string]interface{}{
  565. "node": map[string]interface{}{
  566. "key": "/foo/bar/zar",
  567. "value": "XXX",
  568. },
  569. "action": "get",
  570. },
  571. },
  572. {
  573. "/v2/keys/foo",
  574. http.StatusOK,
  575. map[string]interface{}{
  576. "node": map[string]interface{}{
  577. "key": "/foo",
  578. "dir": true,
  579. "nodes": []interface{}{
  580. map[string]interface{}{
  581. "key": "/foo/bar",
  582. "dir": true,
  583. "createdIndex": float64(3),
  584. "modifiedIndex": float64(3),
  585. },
  586. },
  587. },
  588. "action": "get",
  589. },
  590. },
  591. {
  592. "/v2/keys/foo?recursive=true",
  593. http.StatusOK,
  594. map[string]interface{}{
  595. "node": map[string]interface{}{
  596. "key": "/foo",
  597. "dir": true,
  598. "nodes": []interface{}{
  599. map[string]interface{}{
  600. "key": "/foo/bar",
  601. "dir": true,
  602. "createdIndex": float64(3),
  603. "modifiedIndex": float64(3),
  604. "nodes": []interface{}{
  605. map[string]interface{}{
  606. "key": "/foo/bar/zar",
  607. "value": "XXX",
  608. "createdIndex": float64(3),
  609. "modifiedIndex": float64(3),
  610. },
  611. },
  612. },
  613. },
  614. },
  615. "action": "get",
  616. },
  617. },
  618. }
  619. for i, tt := range tests {
  620. resp, _ := tc.Get(fmt.Sprintf("%s%s", u, tt.relativeURL))
  621. if resp.StatusCode != tt.wStatus {
  622. t.Errorf("#%d: status = %d, want %d", i, resp.StatusCode, tt.wStatus)
  623. }
  624. if resp.Header.Get("Content-Type") != "application/json" {
  625. t.Errorf("#%d: header = %v, want %v", i, resp.Header.Get("Content-Type"), "application/json")
  626. }
  627. if err := checkBody(tc.ReadBodyJSON(resp), tt.w); err != nil {
  628. t.Errorf("#%d: %v", i, err)
  629. }
  630. }
  631. }
  632. func TestV2QuorumGet(t *testing.T) {
  633. cl := NewCluster(t, 1)
  634. cl.Launch(t)
  635. defer cl.Terminate(t)
  636. u := cl.URL(0)
  637. tc := NewTestClient()
  638. v := url.Values{}
  639. v.Set("value", "XXX")
  640. resp, err := tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar/zar?quorum=true"), v)
  641. if err != nil {
  642. t.Error(err)
  643. }
  644. resp.Body.Close()
  645. tests := []struct {
  646. relativeURL string
  647. wStatus int
  648. w map[string]interface{}
  649. }{
  650. {
  651. "/v2/keys/foo/bar/zar",
  652. http.StatusOK,
  653. map[string]interface{}{
  654. "node": map[string]interface{}{
  655. "key": "/foo/bar/zar",
  656. "value": "XXX",
  657. },
  658. "action": "get",
  659. },
  660. },
  661. {
  662. "/v2/keys/foo",
  663. http.StatusOK,
  664. map[string]interface{}{
  665. "node": map[string]interface{}{
  666. "key": "/foo",
  667. "dir": true,
  668. "nodes": []interface{}{
  669. map[string]interface{}{
  670. "key": "/foo/bar",
  671. "dir": true,
  672. "createdIndex": float64(3),
  673. "modifiedIndex": float64(3),
  674. },
  675. },
  676. },
  677. "action": "get",
  678. },
  679. },
  680. {
  681. "/v2/keys/foo?recursive=true",
  682. http.StatusOK,
  683. map[string]interface{}{
  684. "node": map[string]interface{}{
  685. "key": "/foo",
  686. "dir": true,
  687. "nodes": []interface{}{
  688. map[string]interface{}{
  689. "key": "/foo/bar",
  690. "dir": true,
  691. "createdIndex": float64(3),
  692. "modifiedIndex": float64(3),
  693. "nodes": []interface{}{
  694. map[string]interface{}{
  695. "key": "/foo/bar/zar",
  696. "value": "XXX",
  697. "createdIndex": float64(3),
  698. "modifiedIndex": float64(3),
  699. },
  700. },
  701. },
  702. },
  703. },
  704. "action": "get",
  705. },
  706. },
  707. }
  708. for i, tt := range tests {
  709. resp, _ := tc.Get(fmt.Sprintf("%s%s", u, tt.relativeURL))
  710. if resp.StatusCode != tt.wStatus {
  711. t.Errorf("#%d: status = %d, want %d", i, resp.StatusCode, tt.wStatus)
  712. }
  713. if resp.Header.Get("Content-Type") != "application/json" {
  714. t.Errorf("#%d: header = %v, want %v", i, resp.Header.Get("Content-Type"), "application/json")
  715. }
  716. if err := checkBody(tc.ReadBodyJSON(resp), tt.w); err != nil {
  717. t.Errorf("#%d: %v", i, err)
  718. }
  719. }
  720. }
  721. func TestV2Watch(t *testing.T) {
  722. cl := NewCluster(t, 1)
  723. cl.Launch(t)
  724. defer cl.Terminate(t)
  725. u := cl.URL(0)
  726. tc := NewTestClient()
  727. watchResp, _ := tc.Get(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?wait=true"))
  728. // Set a value.
  729. v := url.Values{}
  730. v.Set("value", "XXX")
  731. resp, _ := tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  732. resp.Body.Close()
  733. body := tc.ReadBodyJSON(watchResp)
  734. w := map[string]interface{}{
  735. "node": map[string]interface{}{
  736. "key": "/foo/bar",
  737. "value": "XXX",
  738. "modifiedIndex": float64(3),
  739. },
  740. "action": "set",
  741. }
  742. if err := checkBody(body, w); err != nil {
  743. t.Error(err)
  744. }
  745. }
  746. func TestV2WatchWithIndex(t *testing.T) {
  747. cl := NewCluster(t, 1)
  748. cl.Launch(t)
  749. defer cl.Terminate(t)
  750. u := cl.URL(0)
  751. tc := NewTestClient()
  752. var body map[string]interface{}
  753. c := make(chan bool, 1)
  754. go func() {
  755. resp, _ := tc.Get(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?wait=true&waitIndex=4"))
  756. body = tc.ReadBodyJSON(resp)
  757. c <- true
  758. }()
  759. select {
  760. case <-c:
  761. t.Fatal("should not get the watch result")
  762. case <-time.After(time.Millisecond):
  763. }
  764. // Set a value (before given index).
  765. v := url.Values{}
  766. v.Set("value", "XXX")
  767. resp, _ := tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  768. resp.Body.Close()
  769. select {
  770. case <-c:
  771. t.Fatal("should not get the watch result")
  772. case <-time.After(time.Millisecond):
  773. }
  774. // Set a value (before given index).
  775. resp, _ = tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  776. resp.Body.Close()
  777. select {
  778. case <-c:
  779. case <-time.After(time.Second):
  780. t.Fatal("cannot get watch result")
  781. }
  782. w := map[string]interface{}{
  783. "node": map[string]interface{}{
  784. "key": "/foo/bar",
  785. "value": "XXX",
  786. "modifiedIndex": float64(4),
  787. },
  788. "action": "set",
  789. }
  790. if err := checkBody(body, w); err != nil {
  791. t.Error(err)
  792. }
  793. }
  794. func TestV2WatchKeyInDir(t *testing.T) {
  795. cl := NewCluster(t, 1)
  796. cl.Launch(t)
  797. defer cl.Terminate(t)
  798. u := cl.URL(0)
  799. tc := NewTestClient()
  800. var body map[string]interface{}
  801. c := make(chan bool)
  802. // Create an expiring directory
  803. v := url.Values{}
  804. v.Set("dir", "true")
  805. v.Set("ttl", "1")
  806. resp, _ := tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/keyindir"), v)
  807. resp.Body.Close()
  808. // Create a permanent node within the directory
  809. v = url.Values{}
  810. v.Set("value", "XXX")
  811. resp, _ = tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/keyindir/bar"), v)
  812. resp.Body.Close()
  813. go func() {
  814. // Expect a notification when watching the node
  815. resp, _ := tc.Get(fmt.Sprintf("%s%s", u, "/v2/keys/keyindir/bar?wait=true"))
  816. body = tc.ReadBodyJSON(resp)
  817. c <- true
  818. }()
  819. select {
  820. case <-c:
  821. // 1s ttl + 0.5s sync delay + 1.5s disk and network delay
  822. // We set that long disk and network delay because travis may be slow
  823. // when do system calls.
  824. case <-time.After(3 * time.Second):
  825. t.Fatal("timed out waiting for watch result")
  826. }
  827. w := map[string]interface{}{
  828. "node": map[string]interface{}{
  829. "key": "/keyindir",
  830. },
  831. "action": "expire",
  832. }
  833. if err := checkBody(body, w); err != nil {
  834. t.Error(err)
  835. }
  836. }
  837. func TestV2Head(t *testing.T) {
  838. cl := NewCluster(t, 1)
  839. cl.Launch(t)
  840. defer cl.Terminate(t)
  841. u := cl.URL(0)
  842. tc := NewTestClient()
  843. v := url.Values{}
  844. v.Set("value", "XXX")
  845. fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar")
  846. resp, _ := tc.Head(fullURL)
  847. resp.Body.Close()
  848. if resp.StatusCode != http.StatusNotFound {
  849. t.Errorf("status = %d, want %d", resp.StatusCode, http.StatusNotFound)
  850. }
  851. if resp.ContentLength <= 0 {
  852. t.Errorf("ContentLength = %d, want > 0", resp.ContentLength)
  853. }
  854. resp, _ = tc.PutForm(fullURL, v)
  855. resp.Body.Close()
  856. resp, _ = tc.Head(fullURL)
  857. resp.Body.Close()
  858. if resp.StatusCode != http.StatusOK {
  859. t.Errorf("status = %d, want %d", resp.StatusCode, http.StatusOK)
  860. }
  861. if resp.ContentLength <= 0 {
  862. t.Errorf("ContentLength = %d, want > 0", resp.ContentLength)
  863. }
  864. }
  865. func checkBody(body map[string]interface{}, w map[string]interface{}) error {
  866. if body["node"] != nil {
  867. if w["node"] != nil {
  868. wn := w["node"].(map[string]interface{})
  869. n := body["node"].(map[string]interface{})
  870. for k := range n {
  871. if wn[k] == nil {
  872. delete(n, k)
  873. }
  874. }
  875. body["node"] = n
  876. }
  877. if w["prevNode"] != nil {
  878. wn := w["prevNode"].(map[string]interface{})
  879. n := body["prevNode"].(map[string]interface{})
  880. for k := range n {
  881. if wn[k] == nil {
  882. delete(n, k)
  883. }
  884. }
  885. body["prevNode"] = n
  886. }
  887. }
  888. for k, v := range w {
  889. g := body[k]
  890. if !reflect.DeepEqual(g, v) {
  891. return fmt.Errorf("%v = %+v, want %+v", k, g, v)
  892. }
  893. }
  894. return nil
  895. }
  896. type testHttpClient struct {
  897. *http.Client
  898. }
  899. // Creates a new HTTP client with KeepAlive disabled.
  900. func NewTestClient() *testHttpClient {
  901. tr := &http.Transport{
  902. Dial: (&net.Dialer{Timeout: time.Second}).Dial,
  903. DisableKeepAlives: true,
  904. }
  905. return &testHttpClient{&http.Client{Transport: tr}}
  906. }
  907. // Reads the body from the response and closes it.
  908. func (t *testHttpClient) ReadBody(resp *http.Response) []byte {
  909. if resp == nil {
  910. return []byte{}
  911. }
  912. body, _ := ioutil.ReadAll(resp.Body)
  913. resp.Body.Close()
  914. return body
  915. }
  916. // Reads the body from the response and parses it as JSON.
  917. func (t *testHttpClient) ReadBodyJSON(resp *http.Response) map[string]interface{} {
  918. m := make(map[string]interface{})
  919. b := t.ReadBody(resp)
  920. if err := json.Unmarshal(b, &m); err != nil {
  921. panic(fmt.Sprintf("HTTP body JSON parse error: %v: %s", err, string(b)))
  922. }
  923. return m
  924. }
  925. func (t *testHttpClient) Head(url string) (*http.Response, error) {
  926. return t.send("HEAD", url, "application/json", nil)
  927. }
  928. func (t *testHttpClient) Get(url string) (*http.Response, error) {
  929. return t.send("GET", url, "application/json", nil)
  930. }
  931. func (t *testHttpClient) Post(url string, bodyType string, body io.Reader) (*http.Response, error) {
  932. return t.send("POST", url, bodyType, body)
  933. }
  934. func (t *testHttpClient) PostForm(url string, data url.Values) (*http.Response, error) {
  935. return t.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
  936. }
  937. func (t *testHttpClient) Put(url string, bodyType string, body io.Reader) (*http.Response, error) {
  938. return t.send("PUT", url, bodyType, body)
  939. }
  940. func (t *testHttpClient) PutForm(url string, data url.Values) (*http.Response, error) {
  941. return t.Put(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
  942. }
  943. func (t *testHttpClient) Delete(url string, bodyType string, body io.Reader) (*http.Response, error) {
  944. return t.send("DELETE", url, bodyType, body)
  945. }
  946. func (t *testHttpClient) DeleteForm(url string, data url.Values) (*http.Response, error) {
  947. return t.Delete(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
  948. }
  949. func (t *testHttpClient) send(method string, url string, bodyType string, body io.Reader) (*http.Response, error) {
  950. req, err := http.NewRequest(method, url, body)
  951. if err != nil {
  952. return nil, err
  953. }
  954. req.Header.Set("Content-Type", bodyType)
  955. return t.Do(req)
  956. }