keys_test.go 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. // Copyright 2015 CoreOS, Inc.
  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 client
  15. import (
  16. "errors"
  17. "fmt"
  18. "io/ioutil"
  19. "net/http"
  20. "net/url"
  21. "reflect"
  22. "testing"
  23. "time"
  24. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  25. )
  26. func TestV2KeysURLHelper(t *testing.T) {
  27. tests := []struct {
  28. endpoint url.URL
  29. prefix string
  30. key string
  31. want url.URL
  32. }{
  33. // key is empty, no problem
  34. {
  35. endpoint: url.URL{Scheme: "http", Host: "example.com", Path: "/v2/keys"},
  36. prefix: "",
  37. key: "",
  38. want: url.URL{Scheme: "http", Host: "example.com", Path: "/v2/keys"},
  39. },
  40. // key is joined to path
  41. {
  42. endpoint: url.URL{Scheme: "http", Host: "example.com", Path: "/v2/keys"},
  43. prefix: "",
  44. key: "/foo/bar",
  45. want: url.URL{Scheme: "http", Host: "example.com", Path: "/v2/keys/foo/bar"},
  46. },
  47. // key is joined to path when path is empty
  48. {
  49. endpoint: url.URL{Scheme: "http", Host: "example.com", Path: ""},
  50. prefix: "",
  51. key: "/foo/bar",
  52. want: url.URL{Scheme: "http", Host: "example.com", Path: "/foo/bar"},
  53. },
  54. // Host field carries through with port
  55. {
  56. endpoint: url.URL{Scheme: "http", Host: "example.com:8080", Path: "/v2/keys"},
  57. prefix: "",
  58. key: "",
  59. want: url.URL{Scheme: "http", Host: "example.com:8080", Path: "/v2/keys"},
  60. },
  61. // Scheme carries through
  62. {
  63. endpoint: url.URL{Scheme: "https", Host: "example.com", Path: "/v2/keys"},
  64. prefix: "",
  65. key: "",
  66. want: url.URL{Scheme: "https", Host: "example.com", Path: "/v2/keys"},
  67. },
  68. // Prefix is applied
  69. {
  70. endpoint: url.URL{Scheme: "https", Host: "example.com", Path: "/foo"},
  71. prefix: "/bar",
  72. key: "/baz",
  73. want: url.URL{Scheme: "https", Host: "example.com", Path: "/foo/bar/baz"},
  74. },
  75. }
  76. for i, tt := range tests {
  77. got := v2KeysURL(tt.endpoint, tt.prefix, tt.key)
  78. if tt.want != *got {
  79. t.Errorf("#%d: want=%#v, got=%#v", i, tt.want, *got)
  80. }
  81. }
  82. }
  83. func TestGetAction(t *testing.T) {
  84. ep := url.URL{Scheme: "http", Host: "example.com/v2/keys"}
  85. baseWantURL := &url.URL{
  86. Scheme: "http",
  87. Host: "example.com",
  88. Path: "/v2/keys/foo/bar",
  89. }
  90. wantHeader := http.Header{}
  91. tests := []struct {
  92. recursive bool
  93. sorted bool
  94. wantQuery string
  95. }{
  96. {
  97. recursive: false,
  98. sorted: false,
  99. wantQuery: "recursive=false&sorted=false",
  100. },
  101. {
  102. recursive: true,
  103. sorted: false,
  104. wantQuery: "recursive=true&sorted=false",
  105. },
  106. {
  107. recursive: false,
  108. sorted: true,
  109. wantQuery: "recursive=false&sorted=true",
  110. },
  111. {
  112. recursive: true,
  113. sorted: true,
  114. wantQuery: "recursive=true&sorted=true",
  115. },
  116. }
  117. for i, tt := range tests {
  118. f := getAction{
  119. Key: "/foo/bar",
  120. Recursive: tt.recursive,
  121. Sorted: tt.sorted,
  122. }
  123. got := *f.HTTPRequest(ep)
  124. wantURL := baseWantURL
  125. wantURL.RawQuery = tt.wantQuery
  126. err := assertRequest(got, "GET", wantURL, wantHeader, nil)
  127. if err != nil {
  128. t.Errorf("#%d: %v", i, err)
  129. }
  130. }
  131. }
  132. func TestWaitAction(t *testing.T) {
  133. ep := url.URL{Scheme: "http", Host: "example.com/v2/keys"}
  134. baseWantURL := &url.URL{
  135. Scheme: "http",
  136. Host: "example.com",
  137. Path: "/v2/keys/foo/bar",
  138. }
  139. wantHeader := http.Header{}
  140. tests := []struct {
  141. waitIndex uint64
  142. recursive bool
  143. wantQuery string
  144. }{
  145. {
  146. recursive: false,
  147. waitIndex: uint64(0),
  148. wantQuery: "recursive=false&wait=true&waitIndex=0",
  149. },
  150. {
  151. recursive: false,
  152. waitIndex: uint64(12),
  153. wantQuery: "recursive=false&wait=true&waitIndex=12",
  154. },
  155. {
  156. recursive: true,
  157. waitIndex: uint64(12),
  158. wantQuery: "recursive=true&wait=true&waitIndex=12",
  159. },
  160. }
  161. for i, tt := range tests {
  162. f := waitAction{
  163. Key: "/foo/bar",
  164. WaitIndex: tt.waitIndex,
  165. Recursive: tt.recursive,
  166. }
  167. got := *f.HTTPRequest(ep)
  168. wantURL := baseWantURL
  169. wantURL.RawQuery = tt.wantQuery
  170. err := assertRequest(got, "GET", wantURL, wantHeader, nil)
  171. if err != nil {
  172. t.Errorf("#%d: unexpected error: %#v", i, err)
  173. }
  174. }
  175. }
  176. func TestSetAction(t *testing.T) {
  177. wantHeader := http.Header(map[string][]string{
  178. "Content-Type": []string{"application/x-www-form-urlencoded"},
  179. })
  180. tests := []struct {
  181. act setAction
  182. wantURL string
  183. wantBody string
  184. }{
  185. // default prefix
  186. {
  187. act: setAction{
  188. Prefix: defaultV2KeysPrefix,
  189. Key: "foo",
  190. },
  191. wantURL: "http://example.com/v2/keys/foo",
  192. wantBody: "value=",
  193. },
  194. // non-default prefix
  195. {
  196. act: setAction{
  197. Prefix: "/pfx",
  198. Key: "foo",
  199. },
  200. wantURL: "http://example.com/pfx/foo",
  201. wantBody: "value=",
  202. },
  203. // no prefix
  204. {
  205. act: setAction{
  206. Key: "foo",
  207. },
  208. wantURL: "http://example.com/foo",
  209. wantBody: "value=",
  210. },
  211. // Key with path separators
  212. {
  213. act: setAction{
  214. Prefix: defaultV2KeysPrefix,
  215. Key: "foo/bar/baz",
  216. },
  217. wantURL: "http://example.com/v2/keys/foo/bar/baz",
  218. wantBody: "value=",
  219. },
  220. // Key with leading slash, Prefix with trailing slash
  221. {
  222. act: setAction{
  223. Prefix: "/foo/",
  224. Key: "/bar",
  225. },
  226. wantURL: "http://example.com/foo/bar",
  227. wantBody: "value=",
  228. },
  229. // Key with trailing slash
  230. {
  231. act: setAction{
  232. Key: "/foo/",
  233. },
  234. wantURL: "http://example.com/foo",
  235. wantBody: "value=",
  236. },
  237. // Value is set
  238. {
  239. act: setAction{
  240. Key: "foo",
  241. Value: "baz",
  242. },
  243. wantURL: "http://example.com/foo",
  244. wantBody: "value=baz",
  245. },
  246. // PrevExist set, but still ignored
  247. {
  248. act: setAction{
  249. Key: "foo",
  250. PrevExist: PrevIgnore,
  251. },
  252. wantURL: "http://example.com/foo",
  253. wantBody: "value=",
  254. },
  255. // PrevExist set to true
  256. {
  257. act: setAction{
  258. Key: "foo",
  259. PrevExist: PrevExist,
  260. },
  261. wantURL: "http://example.com/foo?prevExist=true",
  262. wantBody: "value=",
  263. },
  264. // PrevExist set to false
  265. {
  266. act: setAction{
  267. Key: "foo",
  268. PrevExist: PrevNoExist,
  269. },
  270. wantURL: "http://example.com/foo?prevExist=false",
  271. wantBody: "value=",
  272. },
  273. // PrevValue is urlencoded
  274. {
  275. act: setAction{
  276. Key: "foo",
  277. PrevValue: "bar baz",
  278. },
  279. wantURL: "http://example.com/foo?prevValue=bar+baz",
  280. wantBody: "value=",
  281. },
  282. // PrevIndex is set
  283. {
  284. act: setAction{
  285. Key: "foo",
  286. PrevIndex: uint64(12),
  287. },
  288. wantURL: "http://example.com/foo?prevIndex=12",
  289. wantBody: "value=",
  290. },
  291. // TTL is set
  292. {
  293. act: setAction{
  294. Key: "foo",
  295. TTL: 3 * time.Minute,
  296. },
  297. wantURL: "http://example.com/foo",
  298. wantBody: "ttl=180&value=",
  299. },
  300. }
  301. for i, tt := range tests {
  302. u, err := url.Parse(tt.wantURL)
  303. if err != nil {
  304. t.Errorf("#%d: unable to use wantURL fixture: %v", i, err)
  305. }
  306. got := tt.act.HTTPRequest(url.URL{Scheme: "http", Host: "example.com"})
  307. if err := assertRequest(*got, "PUT", u, wantHeader, []byte(tt.wantBody)); err != nil {
  308. t.Errorf("#%d: %v", i, err)
  309. }
  310. }
  311. }
  312. func TestDeleteAction(t *testing.T) {
  313. wantHeader := http.Header(map[string][]string{
  314. "Content-Type": []string{"application/x-www-form-urlencoded"},
  315. })
  316. tests := []struct {
  317. act deleteAction
  318. wantURL string
  319. }{
  320. // default prefix
  321. {
  322. act: deleteAction{
  323. Prefix: defaultV2KeysPrefix,
  324. Key: "foo",
  325. },
  326. wantURL: "http://example.com/v2/keys/foo",
  327. },
  328. // non-default prefix
  329. {
  330. act: deleteAction{
  331. Prefix: "/pfx",
  332. Key: "foo",
  333. },
  334. wantURL: "http://example.com/pfx/foo",
  335. },
  336. // no prefix
  337. {
  338. act: deleteAction{
  339. Key: "foo",
  340. },
  341. wantURL: "http://example.com/foo",
  342. },
  343. // Key with path separators
  344. {
  345. act: deleteAction{
  346. Prefix: defaultV2KeysPrefix,
  347. Key: "foo/bar/baz",
  348. },
  349. wantURL: "http://example.com/v2/keys/foo/bar/baz",
  350. },
  351. // Key with leading slash, Prefix with trailing slash
  352. {
  353. act: deleteAction{
  354. Prefix: "/foo/",
  355. Key: "/bar",
  356. },
  357. wantURL: "http://example.com/foo/bar",
  358. },
  359. // Key with trailing slash
  360. {
  361. act: deleteAction{
  362. Key: "/foo/",
  363. },
  364. wantURL: "http://example.com/foo",
  365. },
  366. // Recursive set to true
  367. {
  368. act: deleteAction{
  369. Key: "foo",
  370. Recursive: true,
  371. },
  372. wantURL: "http://example.com/foo?recursive=true",
  373. },
  374. // PrevValue is urlencoded
  375. {
  376. act: deleteAction{
  377. Key: "foo",
  378. PrevValue: "bar baz",
  379. },
  380. wantURL: "http://example.com/foo?prevValue=bar+baz",
  381. },
  382. // PrevIndex is set
  383. {
  384. act: deleteAction{
  385. Key: "foo",
  386. PrevIndex: uint64(12),
  387. },
  388. wantURL: "http://example.com/foo?prevIndex=12",
  389. },
  390. }
  391. for i, tt := range tests {
  392. u, err := url.Parse(tt.wantURL)
  393. if err != nil {
  394. t.Errorf("#%d: unable to use wantURL fixture: %v", i, err)
  395. }
  396. got := tt.act.HTTPRequest(url.URL{Scheme: "http", Host: "example.com"})
  397. if err := assertRequest(*got, "DELETE", u, wantHeader, nil); err != nil {
  398. t.Errorf("#%d: %v", i, err)
  399. }
  400. }
  401. }
  402. func assertRequest(got http.Request, wantMethod string, wantURL *url.URL, wantHeader http.Header, wantBody []byte) error {
  403. if wantMethod != got.Method {
  404. return fmt.Errorf("want.Method=%#v got.Method=%#v", wantMethod, got.Method)
  405. }
  406. if !reflect.DeepEqual(wantURL, got.URL) {
  407. return fmt.Errorf("want.URL=%#v got.URL=%#v", wantURL, got.URL)
  408. }
  409. if !reflect.DeepEqual(wantHeader, got.Header) {
  410. return fmt.Errorf("want.Header=%#v got.Header=%#v", wantHeader, got.Header)
  411. }
  412. if got.Body == nil {
  413. if wantBody != nil {
  414. return fmt.Errorf("want.Body=%v got.Body=%v", wantBody, got.Body)
  415. }
  416. } else {
  417. if wantBody == nil {
  418. return fmt.Errorf("want.Body=%v got.Body=%s", wantBody, got.Body)
  419. } else {
  420. gotBytes, err := ioutil.ReadAll(got.Body)
  421. if err != nil {
  422. return err
  423. }
  424. if !reflect.DeepEqual(wantBody, gotBytes) {
  425. return fmt.Errorf("want.Body=%s got.Body=%s", wantBody, gotBytes)
  426. }
  427. }
  428. }
  429. return nil
  430. }
  431. func TestUnmarshalSuccessfulResponse(t *testing.T) {
  432. tests := []struct {
  433. hdr string
  434. body string
  435. wantRes *Response
  436. wantErr bool
  437. }{
  438. // Neither PrevNode or Node
  439. {
  440. hdr: "1",
  441. body: `{"action":"delete"}`,
  442. wantRes: &Response{Action: "delete", Index: 1},
  443. wantErr: false,
  444. },
  445. // PrevNode
  446. {
  447. hdr: "15",
  448. body: `{"action":"delete", "prevNode": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
  449. wantRes: &Response{
  450. Action: "delete",
  451. Index: 15,
  452. Node: nil,
  453. PrevNode: &Node{
  454. Key: "/foo",
  455. Value: "bar",
  456. ModifiedIndex: 12,
  457. CreatedIndex: 10,
  458. },
  459. },
  460. wantErr: false,
  461. },
  462. // Node
  463. {
  464. hdr: "15",
  465. body: `{"action":"get", "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
  466. wantRes: &Response{
  467. Action: "get",
  468. Index: 15,
  469. Node: &Node{
  470. Key: "/foo",
  471. Value: "bar",
  472. ModifiedIndex: 12,
  473. CreatedIndex: 10,
  474. },
  475. PrevNode: nil,
  476. },
  477. wantErr: false,
  478. },
  479. // PrevNode and Node
  480. {
  481. hdr: "15",
  482. body: `{"action":"update", "prevNode": {"key": "/foo", "value": "baz", "modifiedIndex": 10, "createdIndex": 10}, "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
  483. wantRes: &Response{
  484. Action: "update",
  485. Index: 15,
  486. PrevNode: &Node{
  487. Key: "/foo",
  488. Value: "baz",
  489. ModifiedIndex: 10,
  490. CreatedIndex: 10,
  491. },
  492. Node: &Node{
  493. Key: "/foo",
  494. Value: "bar",
  495. ModifiedIndex: 12,
  496. CreatedIndex: 10,
  497. },
  498. },
  499. wantErr: false,
  500. },
  501. // Garbage in body
  502. {
  503. hdr: "",
  504. body: `garbage`,
  505. wantRes: nil,
  506. wantErr: true,
  507. },
  508. // non-integer index
  509. {
  510. hdr: "poo",
  511. body: `{}`,
  512. wantRes: nil,
  513. wantErr: true,
  514. },
  515. }
  516. for i, tt := range tests {
  517. h := make(http.Header)
  518. h.Add("X-Etcd-Index", tt.hdr)
  519. res, err := unmarshalSuccessfulKeysResponse(h, []byte(tt.body))
  520. if tt.wantErr != (err != nil) {
  521. t.Errorf("#%d: wantErr=%t, err=%v", i, tt.wantErr, err)
  522. }
  523. if (res == nil) != (tt.wantRes == nil) {
  524. t.Errorf("#%d: received res=%#v, but expected res=%#v", i, res, tt.wantRes)
  525. continue
  526. } else if tt.wantRes == nil {
  527. // expected and successfully got nil response
  528. continue
  529. }
  530. if res.Action != tt.wantRes.Action {
  531. t.Errorf("#%d: Action=%s, expected %s", i, res.Action, tt.wantRes.Action)
  532. }
  533. if res.Index != tt.wantRes.Index {
  534. t.Errorf("#%d: Index=%d, expected %d", i, res.Index, tt.wantRes.Index)
  535. }
  536. if !reflect.DeepEqual(res.Node, tt.wantRes.Node) {
  537. t.Errorf("#%d: Node=%v, expected %v", i, res.Node, tt.wantRes.Node)
  538. }
  539. }
  540. }
  541. func TestUnmarshalFailedKeysResponse(t *testing.T) {
  542. body := []byte(`{"errorCode":100,"message":"Key not found","cause":"/foo","index":18}`)
  543. wantErr := Error{
  544. Code: 100,
  545. Message: "Key not found",
  546. Cause: "/foo",
  547. Index: uint64(18),
  548. }
  549. gotErr := unmarshalFailedKeysResponse(body)
  550. if !reflect.DeepEqual(wantErr, gotErr) {
  551. t.Errorf("unexpected error: want=%#v got=%#v", wantErr, gotErr)
  552. }
  553. }
  554. func TestUnmarshalFailedKeysResponseBadJSON(t *testing.T) {
  555. err := unmarshalFailedKeysResponse([]byte(`{"er`))
  556. if err == nil {
  557. t.Errorf("got nil error")
  558. } else if _, ok := err.(Error); ok {
  559. t.Errorf("error is of incorrect type *Error: %#v", err)
  560. }
  561. }
  562. func TestHTTPWatcherNextWaitAction(t *testing.T) {
  563. initAction := waitAction{
  564. Prefix: "/pants",
  565. Key: "/foo/bar",
  566. Recursive: true,
  567. WaitIndex: 19,
  568. }
  569. client := &actionAssertingHTTPClient{
  570. t: t,
  571. act: &initAction,
  572. resp: http.Response{
  573. StatusCode: http.StatusOK,
  574. Header: http.Header{"X-Etcd-Index": []string{"42"}},
  575. },
  576. body: []byte(`{"action":"update","node":{"key":"/pants/foo/bar/baz","value":"snarf","modifiedIndex":21,"createdIndex":19},"prevNode":{"key":"/pants/foo/bar/baz","value":"snazz","modifiedIndex":20,"createdIndex":19}}`),
  577. }
  578. wantResponse := &Response{
  579. Action: "update",
  580. Node: &Node{Key: "/pants/foo/bar/baz", Value: "snarf", CreatedIndex: uint64(19), ModifiedIndex: uint64(21)},
  581. PrevNode: &Node{Key: "/pants/foo/bar/baz", Value: "snazz", CreatedIndex: uint64(19), ModifiedIndex: uint64(20)},
  582. Index: uint64(42),
  583. }
  584. wantNextWait := waitAction{
  585. Prefix: "/pants",
  586. Key: "/foo/bar",
  587. Recursive: true,
  588. WaitIndex: 22,
  589. }
  590. watcher := &httpWatcher{
  591. client: client,
  592. nextWait: initAction,
  593. }
  594. resp, err := watcher.Next(context.Background())
  595. if err != nil {
  596. t.Errorf("non-nil error: %#v", err)
  597. }
  598. if !reflect.DeepEqual(wantResponse, resp) {
  599. t.Errorf("received incorrect Response: want=%#v got=%#v", wantResponse, resp)
  600. }
  601. if !reflect.DeepEqual(wantNextWait, watcher.nextWait) {
  602. t.Errorf("nextWait incorrect: want=%#v got=%#v", wantNextWait, watcher.nextWait)
  603. }
  604. }
  605. func TestHTTPWatcherNextFail(t *testing.T) {
  606. tests := []httpClient{
  607. // generic HTTP client failure
  608. &staticHTTPClient{
  609. err: errors.New("fail!"),
  610. },
  611. // unusable status code
  612. &staticHTTPClient{
  613. resp: http.Response{
  614. StatusCode: http.StatusTeapot,
  615. },
  616. },
  617. // etcd Error response
  618. &staticHTTPClient{
  619. resp: http.Response{
  620. StatusCode: http.StatusNotFound,
  621. },
  622. body: []byte(`{"errorCode":100,"message":"Key not found","cause":"/foo","index":18}`),
  623. },
  624. }
  625. for i, tt := range tests {
  626. act := waitAction{
  627. Prefix: "/pants",
  628. Key: "/foo/bar",
  629. Recursive: true,
  630. WaitIndex: 19,
  631. }
  632. watcher := &httpWatcher{
  633. client: tt,
  634. nextWait: act,
  635. }
  636. resp, err := watcher.Next(context.Background())
  637. if err == nil {
  638. t.Errorf("#%d: expected non-nil error", i)
  639. }
  640. if resp != nil {
  641. t.Errorf("#%d: expected nil Response, got %#v", i, resp)
  642. }
  643. if !reflect.DeepEqual(act, watcher.nextWait) {
  644. t.Errorf("#%d: nextWait changed: want=%#v got=%#v", i, act, watcher.nextWait)
  645. }
  646. }
  647. }
  648. func TestHTTPKeysAPIWatcherAction(t *testing.T) {
  649. tests := []struct {
  650. key string
  651. opts *WatcherOptions
  652. want waitAction
  653. }{
  654. {
  655. key: "/foo",
  656. opts: nil,
  657. want: waitAction{
  658. Key: "/foo",
  659. Recursive: false,
  660. WaitIndex: 0,
  661. },
  662. },
  663. {
  664. key: "/foo",
  665. opts: &WatcherOptions{
  666. Recursive: false,
  667. AfterIndex: 0,
  668. },
  669. want: waitAction{
  670. Key: "/foo",
  671. Recursive: false,
  672. WaitIndex: 0,
  673. },
  674. },
  675. {
  676. key: "/foo",
  677. opts: &WatcherOptions{
  678. Recursive: true,
  679. AfterIndex: 0,
  680. },
  681. want: waitAction{
  682. Key: "/foo",
  683. Recursive: true,
  684. WaitIndex: 0,
  685. },
  686. },
  687. {
  688. key: "/foo",
  689. opts: &WatcherOptions{
  690. Recursive: false,
  691. AfterIndex: 19,
  692. },
  693. want: waitAction{
  694. Key: "/foo",
  695. Recursive: false,
  696. WaitIndex: 20,
  697. },
  698. },
  699. }
  700. for i, tt := range tests {
  701. kAPI := &httpKeysAPI{
  702. client: &staticHTTPClient{err: errors.New("fail!")},
  703. }
  704. want := &httpWatcher{
  705. client: &staticHTTPClient{err: errors.New("fail!")},
  706. nextWait: tt.want,
  707. }
  708. got := kAPI.Watcher(tt.key, tt.opts)
  709. if !reflect.DeepEqual(want, got) {
  710. t.Errorf("#%d: incorrect watcher: want=%#v got=%#v", i, want, got)
  711. }
  712. }
  713. }
  714. func TestHTTPKeysAPISetAction(t *testing.T) {
  715. tests := []struct {
  716. key string
  717. value string
  718. opts *SetOptions
  719. wantAction httpAction
  720. }{
  721. // nil SetOptions
  722. {
  723. key: "/foo",
  724. value: "bar",
  725. opts: nil,
  726. wantAction: &setAction{
  727. Key: "/foo",
  728. Value: "bar",
  729. PrevValue: "",
  730. PrevIndex: 0,
  731. PrevExist: PrevIgnore,
  732. TTL: 0,
  733. },
  734. },
  735. // empty SetOptions
  736. {
  737. key: "/foo",
  738. value: "bar",
  739. opts: &SetOptions{},
  740. wantAction: &setAction{
  741. Key: "/foo",
  742. Value: "bar",
  743. PrevValue: "",
  744. PrevIndex: 0,
  745. PrevExist: PrevIgnore,
  746. TTL: 0,
  747. },
  748. },
  749. // populated SetOptions
  750. {
  751. key: "/foo",
  752. value: "bar",
  753. opts: &SetOptions{
  754. PrevValue: "baz",
  755. PrevIndex: 13,
  756. PrevExist: PrevExist,
  757. TTL: time.Minute,
  758. },
  759. wantAction: &setAction{
  760. Key: "/foo",
  761. Value: "bar",
  762. PrevValue: "baz",
  763. PrevIndex: 13,
  764. PrevExist: PrevExist,
  765. TTL: time.Minute,
  766. },
  767. },
  768. }
  769. for i, tt := range tests {
  770. client := &actionAssertingHTTPClient{t: t, num: i, act: tt.wantAction}
  771. kAPI := httpKeysAPI{client: client}
  772. kAPI.Set(context.Background(), tt.key, tt.value, tt.opts)
  773. }
  774. }
  775. func TestHTTPKeysAPISetError(t *testing.T) {
  776. tests := []httpClient{
  777. // generic HTTP client failure
  778. &staticHTTPClient{
  779. err: errors.New("fail!"),
  780. },
  781. // unusable status code
  782. &staticHTTPClient{
  783. resp: http.Response{
  784. StatusCode: http.StatusTeapot,
  785. },
  786. },
  787. // etcd Error response
  788. &staticHTTPClient{
  789. resp: http.Response{
  790. StatusCode: http.StatusInternalServerError,
  791. },
  792. body: []byte(`{"errorCode":300,"message":"Raft internal error","cause":"/foo","index":18}`),
  793. },
  794. }
  795. for i, tt := range tests {
  796. kAPI := httpKeysAPI{client: tt}
  797. resp, err := kAPI.Set(context.Background(), "/foo", "bar", nil)
  798. if err == nil {
  799. t.Errorf("#%d: received nil error", i)
  800. }
  801. if resp != nil {
  802. t.Errorf("#%d: received non-nil Response: %#v", i, resp)
  803. }
  804. }
  805. }
  806. func TestHTTPKeysAPISetResponse(t *testing.T) {
  807. client := &staticHTTPClient{
  808. resp: http.Response{
  809. StatusCode: http.StatusOK,
  810. Header: http.Header{"X-Etcd-Index": []string{"21"}},
  811. },
  812. body: []byte(`{"action":"set","node":{"key":"/pants/foo/bar/baz","value":"snarf","modifiedIndex":21,"createdIndex":21},"prevNode":{"key":"/pants/foo/bar/baz","value":"snazz","modifiedIndex":20,"createdIndex":19}}`),
  813. }
  814. wantResponse := &Response{
  815. Action: "set",
  816. Node: &Node{Key: "/pants/foo/bar/baz", Value: "snarf", CreatedIndex: uint64(21), ModifiedIndex: uint64(21)},
  817. PrevNode: &Node{Key: "/pants/foo/bar/baz", Value: "snazz", CreatedIndex: uint64(19), ModifiedIndex: uint64(20)},
  818. Index: uint64(21),
  819. }
  820. kAPI := &httpKeysAPI{client: client, prefix: "/pants"}
  821. resp, err := kAPI.Set(context.Background(), "/foo/bar/baz", "snarf", nil)
  822. if err != nil {
  823. t.Errorf("non-nil error: %#v", err)
  824. }
  825. if !reflect.DeepEqual(wantResponse, resp) {
  826. t.Errorf("incorrect Response: want=%#v got=%#v", wantResponse, resp)
  827. }
  828. }
  829. func TestHTTPKeysAPIGetAction(t *testing.T) {
  830. tests := []struct {
  831. key string
  832. opts *GetOptions
  833. wantAction httpAction
  834. }{
  835. // nil GetOptions
  836. {
  837. key: "/foo",
  838. opts: nil,
  839. wantAction: &getAction{
  840. Key: "/foo",
  841. Sorted: false,
  842. Recursive: false,
  843. },
  844. },
  845. // empty GetOptions
  846. {
  847. key: "/foo",
  848. opts: &GetOptions{},
  849. wantAction: &getAction{
  850. Key: "/foo",
  851. Sorted: false,
  852. Recursive: false,
  853. },
  854. },
  855. // populated GetOptions
  856. {
  857. key: "/foo",
  858. opts: &GetOptions{
  859. Sort: true,
  860. Recursive: true,
  861. },
  862. wantAction: &getAction{
  863. Key: "/foo",
  864. Sorted: true,
  865. Recursive: true,
  866. },
  867. },
  868. }
  869. for i, tt := range tests {
  870. client := &actionAssertingHTTPClient{t: t, num: i, act: tt.wantAction}
  871. kAPI := httpKeysAPI{client: client}
  872. kAPI.Get(context.Background(), tt.key, tt.opts)
  873. }
  874. }
  875. func TestHTTPKeysAPIGetError(t *testing.T) {
  876. tests := []httpClient{
  877. // generic HTTP client failure
  878. &staticHTTPClient{
  879. err: errors.New("fail!"),
  880. },
  881. // unusable status code
  882. &staticHTTPClient{
  883. resp: http.Response{
  884. StatusCode: http.StatusTeapot,
  885. },
  886. },
  887. // etcd Error response
  888. &staticHTTPClient{
  889. resp: http.Response{
  890. StatusCode: http.StatusInternalServerError,
  891. },
  892. body: []byte(`{"errorCode":300,"message":"Raft internal error","cause":"/foo","index":18}`),
  893. },
  894. }
  895. for i, tt := range tests {
  896. kAPI := httpKeysAPI{client: tt}
  897. resp, err := kAPI.Get(context.Background(), "/foo", nil)
  898. if err == nil {
  899. t.Errorf("#%d: received nil error", i)
  900. }
  901. if resp != nil {
  902. t.Errorf("#%d: received non-nil Response: %#v", i, resp)
  903. }
  904. }
  905. }
  906. func TestHTTPKeysAPIGetResponse(t *testing.T) {
  907. client := &staticHTTPClient{
  908. resp: http.Response{
  909. StatusCode: http.StatusOK,
  910. Header: http.Header{"X-Etcd-Index": []string{"42"}},
  911. },
  912. body: []byte(`{"action":"get","node":{"key":"/pants/foo/bar","modifiedIndex":25,"createdIndex":19,"nodes":[{"key":"/pants/foo/bar/baz","value":"snarf","createdIndex":21,"modifiedIndex":25}]}}`),
  913. }
  914. wantResponse := &Response{
  915. Action: "get",
  916. Node: &Node{
  917. Key: "/pants/foo/bar",
  918. Nodes: []*Node{
  919. &Node{Key: "/pants/foo/bar/baz", Value: "snarf", CreatedIndex: 21, ModifiedIndex: 25},
  920. },
  921. CreatedIndex: uint64(19),
  922. ModifiedIndex: uint64(25),
  923. },
  924. Index: uint64(42),
  925. }
  926. kAPI := &httpKeysAPI{client: client, prefix: "/pants"}
  927. resp, err := kAPI.Get(context.Background(), "/foo/bar", &GetOptions{Recursive: true})
  928. if err != nil {
  929. t.Errorf("non-nil error: %#v", err)
  930. }
  931. if !reflect.DeepEqual(wantResponse, resp) {
  932. t.Errorf("incorrect Response: want=%#v got=%#v", wantResponse, resp)
  933. }
  934. }
  935. func TestHTTPKeysAPIDeleteAction(t *testing.T) {
  936. tests := []struct {
  937. key string
  938. value string
  939. opts *DeleteOptions
  940. wantAction httpAction
  941. }{
  942. // nil DeleteOptions
  943. {
  944. key: "/foo",
  945. opts: nil,
  946. wantAction: &deleteAction{
  947. Key: "/foo",
  948. PrevValue: "",
  949. PrevIndex: 0,
  950. Recursive: false,
  951. },
  952. },
  953. // empty DeleteOptions
  954. {
  955. key: "/foo",
  956. opts: &DeleteOptions{},
  957. wantAction: &deleteAction{
  958. Key: "/foo",
  959. PrevValue: "",
  960. PrevIndex: 0,
  961. Recursive: false,
  962. },
  963. },
  964. // populated DeleteOptions
  965. {
  966. key: "/foo",
  967. opts: &DeleteOptions{
  968. PrevValue: "baz",
  969. PrevIndex: 13,
  970. Recursive: true,
  971. },
  972. wantAction: &deleteAction{
  973. Key: "/foo",
  974. PrevValue: "baz",
  975. PrevIndex: 13,
  976. Recursive: true,
  977. },
  978. },
  979. }
  980. for i, tt := range tests {
  981. client := &actionAssertingHTTPClient{t: t, num: i, act: tt.wantAction}
  982. kAPI := httpKeysAPI{client: client}
  983. kAPI.Delete(context.Background(), tt.key, tt.opts)
  984. }
  985. }
  986. func TestHTTPKeysAPIDeleteError(t *testing.T) {
  987. tests := []httpClient{
  988. // generic HTTP client failure
  989. &staticHTTPClient{
  990. err: errors.New("fail!"),
  991. },
  992. // unusable status code
  993. &staticHTTPClient{
  994. resp: http.Response{
  995. StatusCode: http.StatusTeapot,
  996. },
  997. },
  998. // etcd Error response
  999. &staticHTTPClient{
  1000. resp: http.Response{
  1001. StatusCode: http.StatusInternalServerError,
  1002. },
  1003. body: []byte(`{"errorCode":300,"message":"Raft internal error","cause":"/foo","index":18}`),
  1004. },
  1005. }
  1006. for i, tt := range tests {
  1007. kAPI := httpKeysAPI{client: tt}
  1008. resp, err := kAPI.Delete(context.Background(), "/foo", nil)
  1009. if err == nil {
  1010. t.Errorf("#%d: received nil error", i)
  1011. }
  1012. if resp != nil {
  1013. t.Errorf("#%d: received non-nil Response: %#v", i, resp)
  1014. }
  1015. }
  1016. }
  1017. func TestHTTPKeysAPIDeleteResponse(t *testing.T) {
  1018. client := &staticHTTPClient{
  1019. resp: http.Response{
  1020. StatusCode: http.StatusOK,
  1021. Header: http.Header{"X-Etcd-Index": []string{"22"}},
  1022. },
  1023. body: []byte(`{"action":"delete","node":{"key":"/pants/foo/bar/baz","value":"snarf","modifiedIndex":22,"createdIndex":19},"prevNode":{"key":"/pants/foo/bar/baz","value":"snazz","modifiedIndex":20,"createdIndex":19}}`),
  1024. }
  1025. wantResponse := &Response{
  1026. Action: "delete",
  1027. Node: &Node{Key: "/pants/foo/bar/baz", Value: "snarf", CreatedIndex: uint64(19), ModifiedIndex: uint64(22)},
  1028. PrevNode: &Node{Key: "/pants/foo/bar/baz", Value: "snazz", CreatedIndex: uint64(19), ModifiedIndex: uint64(20)},
  1029. Index: uint64(22),
  1030. }
  1031. kAPI := &httpKeysAPI{client: client, prefix: "/pants"}
  1032. resp, err := kAPI.Delete(context.Background(), "/foo/bar/baz", nil)
  1033. if err != nil {
  1034. t.Errorf("non-nil error: %#v", err)
  1035. }
  1036. if !reflect.DeepEqual(wantResponse, resp) {
  1037. t.Errorf("incorrect Response: want=%#v got=%#v", wantResponse, resp)
  1038. }
  1039. }