keys_test.go 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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. }
  1040. func TestHTTPKeysAPICreateAction(t *testing.T) {
  1041. act := &setAction{
  1042. Key: "/foo",
  1043. Value: "bar",
  1044. PrevExist: PrevNoExist,
  1045. PrevIndex: 0,
  1046. PrevValue: "",
  1047. TTL: 0,
  1048. }
  1049. kAPI := httpKeysAPI{client: &actionAssertingHTTPClient{t: t, act: act}}
  1050. kAPI.Create(context.Background(), "/foo", "bar")
  1051. }
  1052. func TestHTTPKeysAPIUpdateAction(t *testing.T) {
  1053. act := &setAction{
  1054. Key: "/foo",
  1055. Value: "bar",
  1056. PrevExist: PrevExist,
  1057. PrevIndex: 0,
  1058. PrevValue: "",
  1059. TTL: 0,
  1060. }
  1061. kAPI := httpKeysAPI{client: &actionAssertingHTTPClient{t: t, act: act}}
  1062. kAPI.Update(context.Background(), "/foo", "bar")
  1063. }