v2_http_kv_test.go 27 KB

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