v2_http_kv_test.go 24 KB

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