keys_test.go 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  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. var expiration time.Time
  433. expiration.UnmarshalText([]byte("2015-04-07T04:40:23.044979686Z"))
  434. tests := []struct {
  435. hdr string
  436. body string
  437. wantRes *Response
  438. wantErr bool
  439. }{
  440. // Neither PrevNode or Node
  441. {
  442. hdr: "1",
  443. body: `{"action":"delete"}`,
  444. wantRes: &Response{Action: "delete", Index: 1},
  445. wantErr: false,
  446. },
  447. // PrevNode
  448. {
  449. hdr: "15",
  450. body: `{"action":"delete", "prevNode": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
  451. wantRes: &Response{
  452. Action: "delete",
  453. Index: 15,
  454. Node: nil,
  455. PrevNode: &Node{
  456. Key: "/foo",
  457. Value: "bar",
  458. ModifiedIndex: 12,
  459. CreatedIndex: 10,
  460. },
  461. },
  462. wantErr: false,
  463. },
  464. // Node
  465. {
  466. hdr: "15",
  467. body: `{"action":"get", "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10, "ttl": 10, "expiration": "2015-04-07T04:40:23.044979686Z"}}`,
  468. wantRes: &Response{
  469. Action: "get",
  470. Index: 15,
  471. Node: &Node{
  472. Key: "/foo",
  473. Value: "bar",
  474. ModifiedIndex: 12,
  475. CreatedIndex: 10,
  476. TTL: 10,
  477. Expiration: &expiration,
  478. },
  479. PrevNode: nil,
  480. },
  481. wantErr: false,
  482. },
  483. // Node Dir
  484. {
  485. hdr: "15",
  486. body: `{"action":"get", "node": {"key": "/foo", "dir": true, "modifiedIndex": 12, "createdIndex": 10}}`,
  487. wantRes: &Response{
  488. Action: "get",
  489. Index: 15,
  490. Node: &Node{
  491. Key: "/foo",
  492. Dir: true,
  493. ModifiedIndex: 12,
  494. CreatedIndex: 10,
  495. },
  496. PrevNode: nil,
  497. },
  498. wantErr: false,
  499. },
  500. // PrevNode and Node
  501. {
  502. hdr: "15",
  503. body: `{"action":"update", "prevNode": {"key": "/foo", "value": "baz", "modifiedIndex": 10, "createdIndex": 10}, "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
  504. wantRes: &Response{
  505. Action: "update",
  506. Index: 15,
  507. PrevNode: &Node{
  508. Key: "/foo",
  509. Value: "baz",
  510. ModifiedIndex: 10,
  511. CreatedIndex: 10,
  512. },
  513. Node: &Node{
  514. Key: "/foo",
  515. Value: "bar",
  516. ModifiedIndex: 12,
  517. CreatedIndex: 10,
  518. },
  519. },
  520. wantErr: false,
  521. },
  522. // Garbage in body
  523. {
  524. hdr: "",
  525. body: `garbage`,
  526. wantRes: nil,
  527. wantErr: true,
  528. },
  529. // non-integer index
  530. {
  531. hdr: "poo",
  532. body: `{}`,
  533. wantRes: nil,
  534. wantErr: true,
  535. },
  536. }
  537. for i, tt := range tests {
  538. h := make(http.Header)
  539. h.Add("X-Etcd-Index", tt.hdr)
  540. res, err := unmarshalSuccessfulKeysResponse(h, []byte(tt.body))
  541. if tt.wantErr != (err != nil) {
  542. t.Errorf("#%d: wantErr=%t, err=%v", i, tt.wantErr, err)
  543. }
  544. if (res == nil) != (tt.wantRes == nil) {
  545. t.Errorf("#%d: received res=%#v, but expected res=%#v", i, res, tt.wantRes)
  546. continue
  547. } else if tt.wantRes == nil {
  548. // expected and successfully got nil response
  549. continue
  550. }
  551. if res.Action != tt.wantRes.Action {
  552. t.Errorf("#%d: Action=%s, expected %s", i, res.Action, tt.wantRes.Action)
  553. }
  554. if res.Index != tt.wantRes.Index {
  555. t.Errorf("#%d: Index=%d, expected %d", i, res.Index, tt.wantRes.Index)
  556. }
  557. if !reflect.DeepEqual(res.Node, tt.wantRes.Node) {
  558. t.Errorf("#%d: Node=%v, expected %v", i, res.Node, tt.wantRes.Node)
  559. }
  560. }
  561. }
  562. func TestUnmarshalFailedKeysResponse(t *testing.T) {
  563. body := []byte(`{"errorCode":100,"message":"Key not found","cause":"/foo","index":18}`)
  564. wantErr := Error{
  565. Code: 100,
  566. Message: "Key not found",
  567. Cause: "/foo",
  568. Index: uint64(18),
  569. }
  570. gotErr := unmarshalFailedKeysResponse(body)
  571. if !reflect.DeepEqual(wantErr, gotErr) {
  572. t.Errorf("unexpected error: want=%#v got=%#v", wantErr, gotErr)
  573. }
  574. }
  575. func TestUnmarshalFailedKeysResponseBadJSON(t *testing.T) {
  576. err := unmarshalFailedKeysResponse([]byte(`{"er`))
  577. if err == nil {
  578. t.Errorf("got nil error")
  579. } else if _, ok := err.(Error); ok {
  580. t.Errorf("error is of incorrect type *Error: %#v", err)
  581. }
  582. }
  583. func TestHTTPWatcherNextWaitAction(t *testing.T) {
  584. initAction := waitAction{
  585. Prefix: "/pants",
  586. Key: "/foo/bar",
  587. Recursive: true,
  588. WaitIndex: 19,
  589. }
  590. client := &actionAssertingHTTPClient{
  591. t: t,
  592. act: &initAction,
  593. resp: http.Response{
  594. StatusCode: http.StatusOK,
  595. Header: http.Header{"X-Etcd-Index": []string{"42"}},
  596. },
  597. 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}}`),
  598. }
  599. wantResponse := &Response{
  600. Action: "update",
  601. Node: &Node{Key: "/pants/foo/bar/baz", Value: "snarf", CreatedIndex: uint64(19), ModifiedIndex: uint64(21)},
  602. PrevNode: &Node{Key: "/pants/foo/bar/baz", Value: "snazz", CreatedIndex: uint64(19), ModifiedIndex: uint64(20)},
  603. Index: uint64(42),
  604. }
  605. wantNextWait := waitAction{
  606. Prefix: "/pants",
  607. Key: "/foo/bar",
  608. Recursive: true,
  609. WaitIndex: 22,
  610. }
  611. watcher := &httpWatcher{
  612. client: client,
  613. nextWait: initAction,
  614. }
  615. resp, err := watcher.Next(context.Background())
  616. if err != nil {
  617. t.Errorf("non-nil error: %#v", err)
  618. }
  619. if !reflect.DeepEqual(wantResponse, resp) {
  620. t.Errorf("received incorrect Response: want=%#v got=%#v", wantResponse, resp)
  621. }
  622. if !reflect.DeepEqual(wantNextWait, watcher.nextWait) {
  623. t.Errorf("nextWait incorrect: want=%#v got=%#v", wantNextWait, watcher.nextWait)
  624. }
  625. }
  626. func TestHTTPWatcherNextFail(t *testing.T) {
  627. tests := []httpClient{
  628. // generic HTTP client failure
  629. &staticHTTPClient{
  630. err: errors.New("fail!"),
  631. },
  632. // unusable status code
  633. &staticHTTPClient{
  634. resp: http.Response{
  635. StatusCode: http.StatusTeapot,
  636. },
  637. },
  638. // etcd Error response
  639. &staticHTTPClient{
  640. resp: http.Response{
  641. StatusCode: http.StatusNotFound,
  642. },
  643. body: []byte(`{"errorCode":100,"message":"Key not found","cause":"/foo","index":18}`),
  644. },
  645. }
  646. for i, tt := range tests {
  647. act := waitAction{
  648. Prefix: "/pants",
  649. Key: "/foo/bar",
  650. Recursive: true,
  651. WaitIndex: 19,
  652. }
  653. watcher := &httpWatcher{
  654. client: tt,
  655. nextWait: act,
  656. }
  657. resp, err := watcher.Next(context.Background())
  658. if err == nil {
  659. t.Errorf("#%d: expected non-nil error", i)
  660. }
  661. if resp != nil {
  662. t.Errorf("#%d: expected nil Response, got %#v", i, resp)
  663. }
  664. if !reflect.DeepEqual(act, watcher.nextWait) {
  665. t.Errorf("#%d: nextWait changed: want=%#v got=%#v", i, act, watcher.nextWait)
  666. }
  667. }
  668. }
  669. func TestHTTPKeysAPIWatcherAction(t *testing.T) {
  670. tests := []struct {
  671. key string
  672. opts *WatcherOptions
  673. want waitAction
  674. }{
  675. {
  676. key: "/foo",
  677. opts: nil,
  678. want: waitAction{
  679. Key: "/foo",
  680. Recursive: false,
  681. WaitIndex: 0,
  682. },
  683. },
  684. {
  685. key: "/foo",
  686. opts: &WatcherOptions{
  687. Recursive: false,
  688. AfterIndex: 0,
  689. },
  690. want: waitAction{
  691. Key: "/foo",
  692. Recursive: false,
  693. WaitIndex: 0,
  694. },
  695. },
  696. {
  697. key: "/foo",
  698. opts: &WatcherOptions{
  699. Recursive: true,
  700. AfterIndex: 0,
  701. },
  702. want: waitAction{
  703. Key: "/foo",
  704. Recursive: true,
  705. WaitIndex: 0,
  706. },
  707. },
  708. {
  709. key: "/foo",
  710. opts: &WatcherOptions{
  711. Recursive: false,
  712. AfterIndex: 19,
  713. },
  714. want: waitAction{
  715. Key: "/foo",
  716. Recursive: false,
  717. WaitIndex: 20,
  718. },
  719. },
  720. }
  721. for i, tt := range tests {
  722. kAPI := &httpKeysAPI{
  723. client: &staticHTTPClient{err: errors.New("fail!")},
  724. }
  725. want := &httpWatcher{
  726. client: &staticHTTPClient{err: errors.New("fail!")},
  727. nextWait: tt.want,
  728. }
  729. got := kAPI.Watcher(tt.key, tt.opts)
  730. if !reflect.DeepEqual(want, got) {
  731. t.Errorf("#%d: incorrect watcher: want=%#v got=%#v", i, want, got)
  732. }
  733. }
  734. }
  735. func TestHTTPKeysAPISetAction(t *testing.T) {
  736. tests := []struct {
  737. key string
  738. value string
  739. opts *SetOptions
  740. wantAction httpAction
  741. }{
  742. // nil SetOptions
  743. {
  744. key: "/foo",
  745. value: "bar",
  746. opts: nil,
  747. wantAction: &setAction{
  748. Key: "/foo",
  749. Value: "bar",
  750. PrevValue: "",
  751. PrevIndex: 0,
  752. PrevExist: PrevIgnore,
  753. TTL: 0,
  754. },
  755. },
  756. // empty SetOptions
  757. {
  758. key: "/foo",
  759. value: "bar",
  760. opts: &SetOptions{},
  761. wantAction: &setAction{
  762. Key: "/foo",
  763. Value: "bar",
  764. PrevValue: "",
  765. PrevIndex: 0,
  766. PrevExist: PrevIgnore,
  767. TTL: 0,
  768. },
  769. },
  770. // populated SetOptions
  771. {
  772. key: "/foo",
  773. value: "bar",
  774. opts: &SetOptions{
  775. PrevValue: "baz",
  776. PrevIndex: 13,
  777. PrevExist: PrevExist,
  778. TTL: time.Minute,
  779. },
  780. wantAction: &setAction{
  781. Key: "/foo",
  782. Value: "bar",
  783. PrevValue: "baz",
  784. PrevIndex: 13,
  785. PrevExist: PrevExist,
  786. TTL: time.Minute,
  787. },
  788. },
  789. }
  790. for i, tt := range tests {
  791. client := &actionAssertingHTTPClient{t: t, num: i, act: tt.wantAction}
  792. kAPI := httpKeysAPI{client: client}
  793. kAPI.Set(context.Background(), tt.key, tt.value, tt.opts)
  794. }
  795. }
  796. func TestHTTPKeysAPISetError(t *testing.T) {
  797. tests := []httpClient{
  798. // generic HTTP client failure
  799. &staticHTTPClient{
  800. err: errors.New("fail!"),
  801. },
  802. // unusable status code
  803. &staticHTTPClient{
  804. resp: http.Response{
  805. StatusCode: http.StatusTeapot,
  806. },
  807. },
  808. // etcd Error response
  809. &staticHTTPClient{
  810. resp: http.Response{
  811. StatusCode: http.StatusInternalServerError,
  812. },
  813. body: []byte(`{"errorCode":300,"message":"Raft internal error","cause":"/foo","index":18}`),
  814. },
  815. }
  816. for i, tt := range tests {
  817. kAPI := httpKeysAPI{client: tt}
  818. resp, err := kAPI.Set(context.Background(), "/foo", "bar", nil)
  819. if err == nil {
  820. t.Errorf("#%d: received nil error", i)
  821. }
  822. if resp != nil {
  823. t.Errorf("#%d: received non-nil Response: %#v", i, resp)
  824. }
  825. }
  826. }
  827. func TestHTTPKeysAPISetResponse(t *testing.T) {
  828. client := &staticHTTPClient{
  829. resp: http.Response{
  830. StatusCode: http.StatusOK,
  831. Header: http.Header{"X-Etcd-Index": []string{"21"}},
  832. },
  833. 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}}`),
  834. }
  835. wantResponse := &Response{
  836. Action: "set",
  837. Node: &Node{Key: "/pants/foo/bar/baz", Value: "snarf", CreatedIndex: uint64(21), ModifiedIndex: uint64(21)},
  838. PrevNode: &Node{Key: "/pants/foo/bar/baz", Value: "snazz", CreatedIndex: uint64(19), ModifiedIndex: uint64(20)},
  839. Index: uint64(21),
  840. }
  841. kAPI := &httpKeysAPI{client: client, prefix: "/pants"}
  842. resp, err := kAPI.Set(context.Background(), "/foo/bar/baz", "snarf", nil)
  843. if err != nil {
  844. t.Errorf("non-nil error: %#v", err)
  845. }
  846. if !reflect.DeepEqual(wantResponse, resp) {
  847. t.Errorf("incorrect Response: want=%#v got=%#v", wantResponse, resp)
  848. }
  849. }
  850. func TestHTTPKeysAPIGetAction(t *testing.T) {
  851. tests := []struct {
  852. key string
  853. opts *GetOptions
  854. wantAction httpAction
  855. }{
  856. // nil GetOptions
  857. {
  858. key: "/foo",
  859. opts: nil,
  860. wantAction: &getAction{
  861. Key: "/foo",
  862. Sorted: false,
  863. Recursive: false,
  864. },
  865. },
  866. // empty GetOptions
  867. {
  868. key: "/foo",
  869. opts: &GetOptions{},
  870. wantAction: &getAction{
  871. Key: "/foo",
  872. Sorted: false,
  873. Recursive: false,
  874. },
  875. },
  876. // populated GetOptions
  877. {
  878. key: "/foo",
  879. opts: &GetOptions{
  880. Sort: true,
  881. Recursive: true,
  882. },
  883. wantAction: &getAction{
  884. Key: "/foo",
  885. Sorted: true,
  886. Recursive: true,
  887. },
  888. },
  889. }
  890. for i, tt := range tests {
  891. client := &actionAssertingHTTPClient{t: t, num: i, act: tt.wantAction}
  892. kAPI := httpKeysAPI{client: client}
  893. kAPI.Get(context.Background(), tt.key, tt.opts)
  894. }
  895. }
  896. func TestHTTPKeysAPIGetError(t *testing.T) {
  897. tests := []httpClient{
  898. // generic HTTP client failure
  899. &staticHTTPClient{
  900. err: errors.New("fail!"),
  901. },
  902. // unusable status code
  903. &staticHTTPClient{
  904. resp: http.Response{
  905. StatusCode: http.StatusTeapot,
  906. },
  907. },
  908. // etcd Error response
  909. &staticHTTPClient{
  910. resp: http.Response{
  911. StatusCode: http.StatusInternalServerError,
  912. },
  913. body: []byte(`{"errorCode":300,"message":"Raft internal error","cause":"/foo","index":18}`),
  914. },
  915. }
  916. for i, tt := range tests {
  917. kAPI := httpKeysAPI{client: tt}
  918. resp, err := kAPI.Get(context.Background(), "/foo", nil)
  919. if err == nil {
  920. t.Errorf("#%d: received nil error", i)
  921. }
  922. if resp != nil {
  923. t.Errorf("#%d: received non-nil Response: %#v", i, resp)
  924. }
  925. }
  926. }
  927. func TestHTTPKeysAPIGetResponse(t *testing.T) {
  928. client := &staticHTTPClient{
  929. resp: http.Response{
  930. StatusCode: http.StatusOK,
  931. Header: http.Header{"X-Etcd-Index": []string{"42"}},
  932. },
  933. 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}]}}`),
  934. }
  935. wantResponse := &Response{
  936. Action: "get",
  937. Node: &Node{
  938. Key: "/pants/foo/bar",
  939. Nodes: []*Node{
  940. &Node{Key: "/pants/foo/bar/baz", Value: "snarf", CreatedIndex: 21, ModifiedIndex: 25},
  941. },
  942. CreatedIndex: uint64(19),
  943. ModifiedIndex: uint64(25),
  944. },
  945. Index: uint64(42),
  946. }
  947. kAPI := &httpKeysAPI{client: client, prefix: "/pants"}
  948. resp, err := kAPI.Get(context.Background(), "/foo/bar", &GetOptions{Recursive: true})
  949. if err != nil {
  950. t.Errorf("non-nil error: %#v", err)
  951. }
  952. if !reflect.DeepEqual(wantResponse, resp) {
  953. t.Errorf("incorrect Response: want=%#v got=%#v", wantResponse, resp)
  954. }
  955. }
  956. func TestHTTPKeysAPIDeleteAction(t *testing.T) {
  957. tests := []struct {
  958. key string
  959. value string
  960. opts *DeleteOptions
  961. wantAction httpAction
  962. }{
  963. // nil DeleteOptions
  964. {
  965. key: "/foo",
  966. opts: nil,
  967. wantAction: &deleteAction{
  968. Key: "/foo",
  969. PrevValue: "",
  970. PrevIndex: 0,
  971. Recursive: false,
  972. },
  973. },
  974. // empty DeleteOptions
  975. {
  976. key: "/foo",
  977. opts: &DeleteOptions{},
  978. wantAction: &deleteAction{
  979. Key: "/foo",
  980. PrevValue: "",
  981. PrevIndex: 0,
  982. Recursive: false,
  983. },
  984. },
  985. // populated DeleteOptions
  986. {
  987. key: "/foo",
  988. opts: &DeleteOptions{
  989. PrevValue: "baz",
  990. PrevIndex: 13,
  991. Recursive: true,
  992. },
  993. wantAction: &deleteAction{
  994. Key: "/foo",
  995. PrevValue: "baz",
  996. PrevIndex: 13,
  997. Recursive: true,
  998. },
  999. },
  1000. }
  1001. for i, tt := range tests {
  1002. client := &actionAssertingHTTPClient{t: t, num: i, act: tt.wantAction}
  1003. kAPI := httpKeysAPI{client: client}
  1004. kAPI.Delete(context.Background(), tt.key, tt.opts)
  1005. }
  1006. }
  1007. func TestHTTPKeysAPIDeleteError(t *testing.T) {
  1008. tests := []httpClient{
  1009. // generic HTTP client failure
  1010. &staticHTTPClient{
  1011. err: errors.New("fail!"),
  1012. },
  1013. // unusable status code
  1014. &staticHTTPClient{
  1015. resp: http.Response{
  1016. StatusCode: http.StatusTeapot,
  1017. },
  1018. },
  1019. // etcd Error response
  1020. &staticHTTPClient{
  1021. resp: http.Response{
  1022. StatusCode: http.StatusInternalServerError,
  1023. },
  1024. body: []byte(`{"errorCode":300,"message":"Raft internal error","cause":"/foo","index":18}`),
  1025. },
  1026. }
  1027. for i, tt := range tests {
  1028. kAPI := httpKeysAPI{client: tt}
  1029. resp, err := kAPI.Delete(context.Background(), "/foo", nil)
  1030. if err == nil {
  1031. t.Errorf("#%d: received nil error", i)
  1032. }
  1033. if resp != nil {
  1034. t.Errorf("#%d: received non-nil Response: %#v", i, resp)
  1035. }
  1036. }
  1037. }
  1038. func TestHTTPKeysAPIDeleteResponse(t *testing.T) {
  1039. client := &staticHTTPClient{
  1040. resp: http.Response{
  1041. StatusCode: http.StatusOK,
  1042. Header: http.Header{"X-Etcd-Index": []string{"22"}},
  1043. },
  1044. 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}}`),
  1045. }
  1046. wantResponse := &Response{
  1047. Action: "delete",
  1048. Node: &Node{Key: "/pants/foo/bar/baz", Value: "snarf", CreatedIndex: uint64(19), ModifiedIndex: uint64(22)},
  1049. PrevNode: &Node{Key: "/pants/foo/bar/baz", Value: "snazz", CreatedIndex: uint64(19), ModifiedIndex: uint64(20)},
  1050. Index: uint64(22),
  1051. }
  1052. kAPI := &httpKeysAPI{client: client, prefix: "/pants"}
  1053. resp, err := kAPI.Delete(context.Background(), "/foo/bar/baz", nil)
  1054. if err != nil {
  1055. t.Errorf("non-nil error: %#v", err)
  1056. }
  1057. if !reflect.DeepEqual(wantResponse, resp) {
  1058. t.Errorf("incorrect Response: want=%#v got=%#v", wantResponse, resp)
  1059. }
  1060. }
  1061. func TestHTTPKeysAPICreateAction(t *testing.T) {
  1062. act := &setAction{
  1063. Key: "/foo",
  1064. Value: "bar",
  1065. PrevExist: PrevNoExist,
  1066. PrevIndex: 0,
  1067. PrevValue: "",
  1068. TTL: 0,
  1069. }
  1070. kAPI := httpKeysAPI{client: &actionAssertingHTTPClient{t: t, act: act}}
  1071. kAPI.Create(context.Background(), "/foo", "bar")
  1072. }
  1073. func TestHTTPKeysAPIUpdateAction(t *testing.T) {
  1074. act := &setAction{
  1075. Key: "/foo",
  1076. Value: "bar",
  1077. PrevExist: PrevExist,
  1078. PrevIndex: 0,
  1079. PrevValue: "",
  1080. TTL: 0,
  1081. }
  1082. kAPI := httpKeysAPI{client: &actionAssertingHTTPClient{t: t, act: act}}
  1083. kAPI.Update(context.Background(), "/foo", "bar")
  1084. }