v2_http_kv_test.go 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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. var watchResp *http.Response
  728. c := make(chan bool)
  729. go func() {
  730. watchResp, _ = tc.Get(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?wait=true"))
  731. c <- true
  732. }()
  733. // Make sure response didn't fire early.
  734. time.Sleep(1 * time.Millisecond)
  735. // Set a value.
  736. v := url.Values{}
  737. v.Set("value", "XXX")
  738. resp, _ := tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  739. resp.Body.Close()
  740. select {
  741. case <-c:
  742. case <-time.After(time.Millisecond):
  743. t.Fatal("cannot get watch result")
  744. }
  745. body := tc.ReadBodyJSON(watchResp)
  746. w := map[string]interface{}{
  747. "node": map[string]interface{}{
  748. "key": "/foo/bar",
  749. "value": "XXX",
  750. "modifiedIndex": float64(3),
  751. },
  752. "action": "set",
  753. }
  754. if err := checkBody(body, w); err != nil {
  755. t.Error(err)
  756. }
  757. }
  758. func TestV2WatchWithIndex(t *testing.T) {
  759. cl := NewCluster(t, 1)
  760. cl.Launch(t)
  761. defer cl.Terminate(t)
  762. u := cl.URL(0)
  763. tc := NewTestClient()
  764. var body map[string]interface{}
  765. c := make(chan bool, 1)
  766. go func() {
  767. resp, _ := tc.Get(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar?wait=true&waitIndex=4"))
  768. body = tc.ReadBodyJSON(resp)
  769. c <- true
  770. }()
  771. select {
  772. case <-c:
  773. t.Fatal("should not get the watch result")
  774. case <-time.After(time.Millisecond):
  775. }
  776. // Set a value (before given index).
  777. v := url.Values{}
  778. v.Set("value", "XXX")
  779. resp, _ := tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  780. resp.Body.Close()
  781. select {
  782. case <-c:
  783. t.Fatal("should not get the watch result")
  784. case <-time.After(time.Millisecond):
  785. }
  786. // Set a value (before given index).
  787. resp, _ = tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar"), v)
  788. resp.Body.Close()
  789. select {
  790. case <-c:
  791. case <-time.After(time.Second):
  792. t.Fatal("cannot get watch result")
  793. }
  794. w := map[string]interface{}{
  795. "node": map[string]interface{}{
  796. "key": "/foo/bar",
  797. "value": "XXX",
  798. "modifiedIndex": float64(4),
  799. },
  800. "action": "set",
  801. }
  802. if err := checkBody(body, w); err != nil {
  803. t.Error(err)
  804. }
  805. }
  806. func TestV2WatchKeyInDir(t *testing.T) {
  807. cl := NewCluster(t, 1)
  808. cl.Launch(t)
  809. defer cl.Terminate(t)
  810. u := cl.URL(0)
  811. tc := NewTestClient()
  812. var body map[string]interface{}
  813. c := make(chan bool)
  814. // Create an expiring directory
  815. v := url.Values{}
  816. v.Set("dir", "true")
  817. v.Set("ttl", "1")
  818. resp, _ := tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/keyindir"), v)
  819. resp.Body.Close()
  820. // Create a permanent node within the directory
  821. v = url.Values{}
  822. v.Set("value", "XXX")
  823. resp, _ = tc.PutForm(fmt.Sprintf("%s%s", u, "/v2/keys/keyindir/bar"), v)
  824. resp.Body.Close()
  825. go func() {
  826. // Expect a notification when watching the node
  827. resp, _ := tc.Get(fmt.Sprintf("%s%s", u, "/v2/keys/keyindir/bar?wait=true"))
  828. body = tc.ReadBodyJSON(resp)
  829. c <- true
  830. }()
  831. select {
  832. case <-c:
  833. case <-time.After(time.Millisecond * 1500):
  834. t.Fatal("timed out waiting for watch result")
  835. }
  836. w := map[string]interface{}{
  837. "node": map[string]interface{}{
  838. "key": "/keyindir",
  839. },
  840. "action": "expire",
  841. }
  842. if err := checkBody(body, w); err != nil {
  843. t.Error(err)
  844. }
  845. }
  846. func TestV2Head(t *testing.T) {
  847. cl := NewCluster(t, 1)
  848. cl.Launch(t)
  849. defer cl.Terminate(t)
  850. u := cl.URL(0)
  851. tc := NewTestClient()
  852. v := url.Values{}
  853. v.Set("value", "XXX")
  854. fullURL := fmt.Sprintf("%s%s", u, "/v2/keys/foo/bar")
  855. resp, _ := tc.Head(fullURL)
  856. resp.Body.Close()
  857. if resp.StatusCode != http.StatusNotFound {
  858. t.Errorf("status = %d, want %d", resp.StatusCode, http.StatusNotFound)
  859. }
  860. if resp.ContentLength <= 0 {
  861. t.Errorf("ContentLength = %d, want > 0", resp.ContentLength)
  862. }
  863. resp, _ = tc.PutForm(fullURL, v)
  864. resp.Body.Close()
  865. resp, _ = tc.Head(fullURL)
  866. resp.Body.Close()
  867. if resp.StatusCode != http.StatusOK {
  868. t.Errorf("status = %d, want %d", resp.StatusCode, http.StatusOK)
  869. }
  870. if resp.ContentLength <= 0 {
  871. t.Errorf("ContentLength = %d, want > 0", resp.ContentLength)
  872. }
  873. }
  874. func checkBody(body map[string]interface{}, w map[string]interface{}) error {
  875. if body["node"] != nil {
  876. if w["node"] != nil {
  877. wn := w["node"].(map[string]interface{})
  878. n := body["node"].(map[string]interface{})
  879. for k := range n {
  880. if wn[k] == nil {
  881. delete(n, k)
  882. }
  883. }
  884. body["node"] = n
  885. }
  886. if w["prevNode"] != nil {
  887. wn := w["prevNode"].(map[string]interface{})
  888. n := body["prevNode"].(map[string]interface{})
  889. for k := range n {
  890. if wn[k] == nil {
  891. delete(n, k)
  892. }
  893. }
  894. body["prevNode"] = n
  895. }
  896. }
  897. for k, v := range w {
  898. g := body[k]
  899. if !reflect.DeepEqual(g, v) {
  900. return fmt.Errorf("%v = %+v, want %+v", k, g, v)
  901. }
  902. }
  903. return nil
  904. }
  905. type testHttpClient struct {
  906. *http.Client
  907. }
  908. // Creates a new HTTP client with KeepAlive disabled.
  909. func NewTestClient() *testHttpClient {
  910. tr := &http.Transport{
  911. Dial: (&net.Dialer{Timeout: time.Second}).Dial,
  912. DisableKeepAlives: true,
  913. }
  914. return &testHttpClient{&http.Client{Transport: tr}}
  915. }
  916. // Reads the body from the response and closes it.
  917. func (t *testHttpClient) ReadBody(resp *http.Response) []byte {
  918. if resp == nil {
  919. return []byte{}
  920. }
  921. body, _ := ioutil.ReadAll(resp.Body)
  922. resp.Body.Close()
  923. return body
  924. }
  925. // Reads the body from the response and parses it as JSON.
  926. func (t *testHttpClient) ReadBodyJSON(resp *http.Response) map[string]interface{} {
  927. m := make(map[string]interface{})
  928. b := t.ReadBody(resp)
  929. if err := json.Unmarshal(b, &m); err != nil {
  930. panic(fmt.Sprintf("HTTP body JSON parse error: %v: %s", err, string(b)))
  931. }
  932. return m
  933. }
  934. func (t *testHttpClient) Head(url string) (*http.Response, error) {
  935. return t.send("HEAD", url, "application/json", nil)
  936. }
  937. func (t *testHttpClient) Get(url string) (*http.Response, error) {
  938. return t.send("GET", url, "application/json", nil)
  939. }
  940. func (t *testHttpClient) Post(url string, bodyType string, body io.Reader) (*http.Response, error) {
  941. return t.send("POST", url, bodyType, body)
  942. }
  943. func (t *testHttpClient) PostForm(url string, data url.Values) (*http.Response, error) {
  944. return t.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
  945. }
  946. func (t *testHttpClient) Put(url string, bodyType string, body io.Reader) (*http.Response, error) {
  947. return t.send("PUT", url, bodyType, body)
  948. }
  949. func (t *testHttpClient) PutForm(url string, data url.Values) (*http.Response, error) {
  950. return t.Put(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
  951. }
  952. func (t *testHttpClient) Delete(url string, bodyType string, body io.Reader) (*http.Response, error) {
  953. return t.send("DELETE", url, bodyType, body)
  954. }
  955. func (t *testHttpClient) DeleteForm(url string, data url.Values) (*http.Response, error) {
  956. return t.Delete(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
  957. }
  958. func (t *testHttpClient) send(method string, url string, bodyType string, body io.Reader) (*http.Response, error) {
  959. req, err := http.NewRequest(method, url, body)
  960. if err != nil {
  961. return nil, err
  962. }
  963. req.Header.Set("Content-Type", bodyType)
  964. return t.Do(req)
  965. }