keys_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. )
  25. func TestV2KeysURLHelper(t *testing.T) {
  26. tests := []struct {
  27. endpoint url.URL
  28. prefix string
  29. key string
  30. want url.URL
  31. }{
  32. // key is empty, no problem
  33. {
  34. endpoint: url.URL{Scheme: "http", Host: "example.com", Path: "/v2/keys"},
  35. prefix: "",
  36. key: "",
  37. want: url.URL{Scheme: "http", Host: "example.com", Path: "/v2/keys"},
  38. },
  39. // key is joined to path
  40. {
  41. endpoint: url.URL{Scheme: "http", Host: "example.com", Path: "/v2/keys"},
  42. prefix: "",
  43. key: "/foo/bar",
  44. want: url.URL{Scheme: "http", Host: "example.com", Path: "/v2/keys/foo/bar"},
  45. },
  46. // key is joined to path when path is empty
  47. {
  48. endpoint: url.URL{Scheme: "http", Host: "example.com", Path: ""},
  49. prefix: "",
  50. key: "/foo/bar",
  51. want: url.URL{Scheme: "http", Host: "example.com", Path: "/foo/bar"},
  52. },
  53. // Host field carries through with port
  54. {
  55. endpoint: url.URL{Scheme: "http", Host: "example.com:8080", Path: "/v2/keys"},
  56. prefix: "",
  57. key: "",
  58. want: url.URL{Scheme: "http", Host: "example.com:8080", Path: "/v2/keys"},
  59. },
  60. // Scheme carries through
  61. {
  62. endpoint: url.URL{Scheme: "https", Host: "example.com", Path: "/v2/keys"},
  63. prefix: "",
  64. key: "",
  65. want: url.URL{Scheme: "https", Host: "example.com", Path: "/v2/keys"},
  66. },
  67. // Prefix is applied
  68. {
  69. endpoint: url.URL{Scheme: "https", Host: "example.com", Path: "/foo"},
  70. prefix: "/bar",
  71. key: "/baz",
  72. want: url.URL{Scheme: "https", Host: "example.com", Path: "/foo/bar/baz"},
  73. },
  74. }
  75. for i, tt := range tests {
  76. got := v2KeysURL(tt.endpoint, tt.prefix, tt.key)
  77. if tt.want != *got {
  78. t.Errorf("#%d: want=%#v, got=%#v", i, tt.want, *got)
  79. }
  80. }
  81. }
  82. func TestGetAction(t *testing.T) {
  83. ep := url.URL{Scheme: "http", Host: "example.com/v2/keys"}
  84. baseWantURL := &url.URL{
  85. Scheme: "http",
  86. Host: "example.com",
  87. Path: "/v2/keys/foo/bar",
  88. }
  89. wantHeader := http.Header{}
  90. tests := []struct {
  91. recursive bool
  92. sorted bool
  93. wantQuery string
  94. }{
  95. {
  96. recursive: false,
  97. sorted: false,
  98. wantQuery: "recursive=false&sorted=false",
  99. },
  100. {
  101. recursive: true,
  102. sorted: false,
  103. wantQuery: "recursive=true&sorted=false",
  104. },
  105. {
  106. recursive: false,
  107. sorted: true,
  108. wantQuery: "recursive=false&sorted=true",
  109. },
  110. {
  111. recursive: true,
  112. sorted: true,
  113. wantQuery: "recursive=true&sorted=true",
  114. },
  115. }
  116. for i, tt := range tests {
  117. f := getAction{
  118. Key: "/foo/bar",
  119. Recursive: tt.recursive,
  120. Sorted: tt.sorted,
  121. }
  122. got := *f.HTTPRequest(ep)
  123. wantURL := baseWantURL
  124. wantURL.RawQuery = tt.wantQuery
  125. err := assertRequest(got, "GET", wantURL, wantHeader, nil)
  126. if err != nil {
  127. t.Errorf("#%d: %v", i, err)
  128. }
  129. }
  130. }
  131. func TestWaitAction(t *testing.T) {
  132. ep := url.URL{Scheme: "http", Host: "example.com/v2/keys"}
  133. baseWantURL := &url.URL{
  134. Scheme: "http",
  135. Host: "example.com",
  136. Path: "/v2/keys/foo/bar",
  137. }
  138. wantHeader := http.Header{}
  139. tests := []struct {
  140. afterIndex uint64
  141. recursive bool
  142. wantQuery string
  143. }{
  144. {
  145. recursive: false,
  146. afterIndex: uint64(0),
  147. wantQuery: "recursive=false&wait=true&waitIndex=0",
  148. },
  149. {
  150. recursive: false,
  151. afterIndex: uint64(12),
  152. wantQuery: "recursive=false&wait=true&waitIndex=12",
  153. },
  154. {
  155. recursive: true,
  156. afterIndex: uint64(12),
  157. wantQuery: "recursive=true&wait=true&waitIndex=12",
  158. },
  159. }
  160. for i, tt := range tests {
  161. f := waitAction{
  162. Key: "/foo/bar",
  163. AfterIndex: tt.afterIndex,
  164. Recursive: tt.recursive,
  165. }
  166. got := *f.HTTPRequest(ep)
  167. wantURL := baseWantURL
  168. wantURL.RawQuery = tt.wantQuery
  169. err := assertRequest(got, "GET", wantURL, wantHeader, nil)
  170. if err != nil {
  171. t.Errorf("#%d: %v", i, err)
  172. }
  173. }
  174. }
  175. func TestSetAction(t *testing.T) {
  176. wantHeader := http.Header(map[string][]string{
  177. "Content-Type": []string{"application/x-www-form-urlencoded"},
  178. })
  179. tests := []struct {
  180. act setAction
  181. wantURL string
  182. wantBody string
  183. }{
  184. // default prefix
  185. {
  186. act: setAction{
  187. Prefix: defaultV2KeysPrefix,
  188. Key: "foo",
  189. },
  190. wantURL: "http://example.com/v2/keys/foo",
  191. wantBody: "value=",
  192. },
  193. // non-default prefix
  194. {
  195. act: setAction{
  196. Prefix: "/pfx",
  197. Key: "foo",
  198. },
  199. wantURL: "http://example.com/pfx/foo",
  200. wantBody: "value=",
  201. },
  202. // no prefix
  203. {
  204. act: setAction{
  205. Key: "foo",
  206. },
  207. wantURL: "http://example.com/foo",
  208. wantBody: "value=",
  209. },
  210. // Key with path separators
  211. {
  212. act: setAction{
  213. Prefix: defaultV2KeysPrefix,
  214. Key: "foo/bar/baz",
  215. },
  216. wantURL: "http://example.com/v2/keys/foo/bar/baz",
  217. wantBody: "value=",
  218. },
  219. // Key with leading slash, Prefix with trailing slash
  220. {
  221. act: setAction{
  222. Prefix: "/foo/",
  223. Key: "/bar",
  224. },
  225. wantURL: "http://example.com/foo/bar",
  226. wantBody: "value=",
  227. },
  228. // Key with trailing slash
  229. {
  230. act: setAction{
  231. Key: "/foo/",
  232. },
  233. wantURL: "http://example.com/foo",
  234. wantBody: "value=",
  235. },
  236. // Value is set
  237. {
  238. act: setAction{
  239. Key: "foo",
  240. Value: "baz",
  241. },
  242. wantURL: "http://example.com/foo",
  243. wantBody: "value=baz",
  244. },
  245. // PrevExist set, but still ignored
  246. {
  247. act: setAction{
  248. Key: "foo",
  249. PrevExist: PrevIgnore,
  250. },
  251. wantURL: "http://example.com/foo",
  252. wantBody: "value=",
  253. },
  254. // PrevExist set to true
  255. {
  256. act: setAction{
  257. Key: "foo",
  258. PrevExist: PrevExist,
  259. },
  260. wantURL: "http://example.com/foo?prevExist=true",
  261. wantBody: "value=",
  262. },
  263. // PrevExist set to false
  264. {
  265. act: setAction{
  266. Key: "foo",
  267. PrevExist: PrevNoExist,
  268. },
  269. wantURL: "http://example.com/foo?prevExist=false",
  270. wantBody: "value=",
  271. },
  272. // PrevValue is urlencoded
  273. {
  274. act: setAction{
  275. Key: "foo",
  276. PrevValue: "bar baz",
  277. },
  278. wantURL: "http://example.com/foo?prevValue=bar+baz",
  279. wantBody: "value=",
  280. },
  281. // PrevIndex is set
  282. {
  283. act: setAction{
  284. Key: "foo",
  285. PrevIndex: uint64(12),
  286. },
  287. wantURL: "http://example.com/foo?prevIndex=12",
  288. wantBody: "value=",
  289. },
  290. // TTL is set
  291. {
  292. act: setAction{
  293. Key: "foo",
  294. TTL: 3 * time.Minute,
  295. },
  296. wantURL: "http://example.com/foo",
  297. wantBody: "ttl=180&value=",
  298. },
  299. }
  300. for i, tt := range tests {
  301. u, err := url.Parse(tt.wantURL)
  302. if err != nil {
  303. t.Errorf("#%d: unable to use wantURL fixture: %v", i, err)
  304. }
  305. got := tt.act.HTTPRequest(url.URL{Scheme: "http", Host: "example.com"})
  306. if err := assertRequest(*got, "PUT", u, wantHeader, []byte(tt.wantBody)); err != nil {
  307. t.Errorf("#%d: %v", i, err)
  308. }
  309. }
  310. }
  311. func TestDeleteAction(t *testing.T) {
  312. wantHeader := http.Header(map[string][]string{
  313. "Content-Type": []string{"application/x-www-form-urlencoded"},
  314. })
  315. tests := []struct {
  316. act deleteAction
  317. wantURL string
  318. }{
  319. // default prefix
  320. {
  321. act: deleteAction{
  322. Prefix: defaultV2KeysPrefix,
  323. Key: "foo",
  324. },
  325. wantURL: "http://example.com/v2/keys/foo",
  326. },
  327. // non-default prefix
  328. {
  329. act: deleteAction{
  330. Prefix: "/pfx",
  331. Key: "foo",
  332. },
  333. wantURL: "http://example.com/pfx/foo",
  334. },
  335. // no prefix
  336. {
  337. act: deleteAction{
  338. Key: "foo",
  339. },
  340. wantURL: "http://example.com/foo",
  341. },
  342. // Key with path separators
  343. {
  344. act: deleteAction{
  345. Prefix: defaultV2KeysPrefix,
  346. Key: "foo/bar/baz",
  347. },
  348. wantURL: "http://example.com/v2/keys/foo/bar/baz",
  349. },
  350. // Key with leading slash, Prefix with trailing slash
  351. {
  352. act: deleteAction{
  353. Prefix: "/foo/",
  354. Key: "/bar",
  355. },
  356. wantURL: "http://example.com/foo/bar",
  357. },
  358. // Key with trailing slash
  359. {
  360. act: deleteAction{
  361. Key: "/foo/",
  362. },
  363. wantURL: "http://example.com/foo",
  364. },
  365. // Recursive set to true
  366. {
  367. act: deleteAction{
  368. Key: "foo",
  369. Recursive: true,
  370. },
  371. wantURL: "http://example.com/foo?recursive=true",
  372. },
  373. // PrevValue is urlencoded
  374. {
  375. act: deleteAction{
  376. Key: "foo",
  377. PrevValue: "bar baz",
  378. },
  379. wantURL: "http://example.com/foo?prevValue=bar+baz",
  380. },
  381. // PrevIndex is set
  382. {
  383. act: deleteAction{
  384. Key: "foo",
  385. PrevIndex: uint64(12),
  386. },
  387. wantURL: "http://example.com/foo?prevIndex=12",
  388. },
  389. }
  390. for i, tt := range tests {
  391. u, err := url.Parse(tt.wantURL)
  392. if err != nil {
  393. t.Errorf("#%d: unable to use wantURL fixture: %v", i, err)
  394. }
  395. got := tt.act.HTTPRequest(url.URL{Scheme: "http", Host: "example.com"})
  396. if err := assertRequest(*got, "DELETE", u, wantHeader, nil); err != nil {
  397. t.Errorf("#%d: %v", i, err)
  398. }
  399. }
  400. }
  401. func assertRequest(got http.Request, wantMethod string, wantURL *url.URL, wantHeader http.Header, wantBody []byte) error {
  402. if wantMethod != got.Method {
  403. return fmt.Errorf("want.Method=%#v got.Method=%#v", wantMethod, got.Method)
  404. }
  405. if !reflect.DeepEqual(wantURL, got.URL) {
  406. return fmt.Errorf("want.URL=%#v got.URL=%#v", wantURL, got.URL)
  407. }
  408. if !reflect.DeepEqual(wantHeader, got.Header) {
  409. return fmt.Errorf("want.Header=%#v got.Header=%#v", wantHeader, got.Header)
  410. }
  411. if got.Body == nil {
  412. if wantBody != nil {
  413. return fmt.Errorf("want.Body=%v got.Body=%v", wantBody, got.Body)
  414. }
  415. } else {
  416. if wantBody == nil {
  417. return fmt.Errorf("want.Body=%v got.Body=%s", wantBody, got.Body)
  418. } else {
  419. gotBytes, err := ioutil.ReadAll(got.Body)
  420. if err != nil {
  421. return err
  422. }
  423. if !reflect.DeepEqual(wantBody, gotBytes) {
  424. return fmt.Errorf("want.Body=%s got.Body=%s", wantBody, gotBytes)
  425. }
  426. }
  427. }
  428. return nil
  429. }
  430. func TestUnmarshalSuccessfulResponse(t *testing.T) {
  431. tests := []struct {
  432. indexHeader string
  433. body string
  434. res *Response
  435. expectError bool
  436. }{
  437. // Neither PrevNode or Node
  438. {
  439. "1",
  440. `{"action":"delete"}`,
  441. &Response{Action: "delete", Index: 1},
  442. false,
  443. },
  444. // PrevNode
  445. {
  446. "15",
  447. `{"action":"delete", "prevNode": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
  448. &Response{Action: "delete", Index: 15, PrevNode: &Node{Key: "/foo", Value: "bar", ModifiedIndex: 12, CreatedIndex: 10}},
  449. false,
  450. },
  451. // Node
  452. {
  453. "15",
  454. `{"action":"get", "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
  455. &Response{Action: "get", Index: 15, Node: &Node{Key: "/foo", Value: "bar", ModifiedIndex: 12, CreatedIndex: 10}},
  456. false,
  457. },
  458. // PrevNode and Node
  459. {
  460. "15",
  461. `{"action":"update", "prevNode": {"key": "/foo", "value": "baz", "modifiedIndex": 10, "createdIndex": 10}, "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
  462. &Response{Action: "update", Index: 15, PrevNode: &Node{Key: "/foo", Value: "baz", ModifiedIndex: 10, CreatedIndex: 10}, Node: &Node{Key: "/foo", Value: "bar", ModifiedIndex: 12, CreatedIndex: 10}},
  463. false,
  464. },
  465. // Garbage in body
  466. {
  467. "",
  468. `garbage`,
  469. nil,
  470. true,
  471. },
  472. }
  473. for i, tt := range tests {
  474. h := make(http.Header)
  475. h.Add("X-Etcd-Index", tt.indexHeader)
  476. res, err := unmarshalSuccessfulResponse(h, []byte(tt.body))
  477. if tt.expectError != (err != nil) {
  478. t.Errorf("#%d: expectError=%t, err=%v", i, tt.expectError, err)
  479. }
  480. if (res == nil) != (tt.res == nil) {
  481. t.Errorf("#%d: received res==%v, but expected res==%v", i, res, tt.res)
  482. continue
  483. } else if tt.res == nil {
  484. // expected and successfully got nil response
  485. continue
  486. }
  487. if res.Action != tt.res.Action {
  488. t.Errorf("#%d: Action=%s, expected %s", i, res.Action, tt.res.Action)
  489. }
  490. if res.Index != tt.res.Index {
  491. t.Errorf("#%d: Index=%d, expected %d", i, res.Index, tt.res.Index)
  492. }
  493. if !reflect.DeepEqual(res.Node, tt.res.Node) {
  494. t.Errorf("#%d: Node=%v, expected %v", i, res.Node, tt.res.Node)
  495. }
  496. }
  497. }
  498. func TestUnmarshalErrorResponse(t *testing.T) {
  499. unrecognized := errors.New("test fixture")
  500. tests := []struct {
  501. code int
  502. want error
  503. }{
  504. {http.StatusBadRequest, unrecognized},
  505. {http.StatusUnauthorized, unrecognized},
  506. {http.StatusPaymentRequired, unrecognized},
  507. {http.StatusForbidden, unrecognized},
  508. {http.StatusNotFound, ErrKeyNoExist},
  509. {http.StatusMethodNotAllowed, unrecognized},
  510. {http.StatusNotAcceptable, unrecognized},
  511. {http.StatusProxyAuthRequired, unrecognized},
  512. {http.StatusRequestTimeout, unrecognized},
  513. {http.StatusConflict, unrecognized},
  514. {http.StatusGone, unrecognized},
  515. {http.StatusLengthRequired, unrecognized},
  516. {http.StatusPreconditionFailed, ErrKeyExists},
  517. {http.StatusRequestEntityTooLarge, unrecognized},
  518. {http.StatusRequestURITooLong, unrecognized},
  519. {http.StatusUnsupportedMediaType, unrecognized},
  520. {http.StatusRequestedRangeNotSatisfiable, unrecognized},
  521. {http.StatusExpectationFailed, unrecognized},
  522. {http.StatusTeapot, unrecognized},
  523. {http.StatusInternalServerError, ErrNoLeader},
  524. {http.StatusNotImplemented, unrecognized},
  525. {http.StatusBadGateway, unrecognized},
  526. {http.StatusServiceUnavailable, unrecognized},
  527. {http.StatusGatewayTimeout, ErrTimeout},
  528. {http.StatusHTTPVersionNotSupported, unrecognized},
  529. }
  530. for i, tt := range tests {
  531. want := tt.want
  532. if reflect.DeepEqual(unrecognized, want) {
  533. want = fmt.Errorf("unrecognized HTTP status code %d", tt.code)
  534. }
  535. got := unmarshalErrorResponse(tt.code)
  536. if !reflect.DeepEqual(want, got) {
  537. t.Errorf("#%d: want=%v, got=%v", i, want, got)
  538. }
  539. }
  540. }