members_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package client
  15. import (
  16. "context"
  17. "encoding/json"
  18. "errors"
  19. "net/http"
  20. "net/url"
  21. "reflect"
  22. "testing"
  23. "go.etcd.io/etcd/pkg/types"
  24. )
  25. func TestMembersAPIActionList(t *testing.T) {
  26. ep := url.URL{Scheme: "http", Host: "example.com"}
  27. act := &membersAPIActionList{}
  28. wantURL := &url.URL{
  29. Scheme: "http",
  30. Host: "example.com",
  31. Path: "/v2/members",
  32. }
  33. got := *act.HTTPRequest(ep)
  34. err := assertRequest(got, "GET", wantURL, http.Header{}, nil)
  35. if err != nil {
  36. t.Error(err.Error())
  37. }
  38. }
  39. func TestMembersAPIActionAdd(t *testing.T) {
  40. ep := url.URL{Scheme: "http", Host: "example.com"}
  41. act := &membersAPIActionAdd{
  42. peerURLs: types.URLs([]url.URL{
  43. {Scheme: "https", Host: "127.0.0.1:8081"},
  44. {Scheme: "http", Host: "127.0.0.1:8080"},
  45. }),
  46. }
  47. wantURL := &url.URL{
  48. Scheme: "http",
  49. Host: "example.com",
  50. Path: "/v2/members",
  51. }
  52. wantHeader := http.Header{
  53. "Content-Type": []string{"application/json"},
  54. }
  55. wantBody := []byte(`{"peerURLs":["https://127.0.0.1:8081","http://127.0.0.1:8080"]}`)
  56. got := *act.HTTPRequest(ep)
  57. err := assertRequest(got, "POST", wantURL, wantHeader, wantBody)
  58. if err != nil {
  59. t.Error(err.Error())
  60. }
  61. }
  62. func TestMembersAPIActionUpdate(t *testing.T) {
  63. ep := url.URL{Scheme: "http", Host: "example.com"}
  64. act := &membersAPIActionUpdate{
  65. memberID: "0xabcd",
  66. peerURLs: types.URLs([]url.URL{
  67. {Scheme: "https", Host: "127.0.0.1:8081"},
  68. {Scheme: "http", Host: "127.0.0.1:8080"},
  69. }),
  70. }
  71. wantURL := &url.URL{
  72. Scheme: "http",
  73. Host: "example.com",
  74. Path: "/v2/members/0xabcd",
  75. }
  76. wantHeader := http.Header{
  77. "Content-Type": []string{"application/json"},
  78. }
  79. wantBody := []byte(`{"peerURLs":["https://127.0.0.1:8081","http://127.0.0.1:8080"]}`)
  80. got := *act.HTTPRequest(ep)
  81. err := assertRequest(got, "PUT", wantURL, wantHeader, wantBody)
  82. if err != nil {
  83. t.Error(err.Error())
  84. }
  85. }
  86. func TestMembersAPIActionRemove(t *testing.T) {
  87. ep := url.URL{Scheme: "http", Host: "example.com"}
  88. act := &membersAPIActionRemove{memberID: "XXX"}
  89. wantURL := &url.URL{
  90. Scheme: "http",
  91. Host: "example.com",
  92. Path: "/v2/members/XXX",
  93. }
  94. got := *act.HTTPRequest(ep)
  95. err := assertRequest(got, "DELETE", wantURL, http.Header{}, nil)
  96. if err != nil {
  97. t.Error(err.Error())
  98. }
  99. }
  100. func TestMembersAPIActionLeader(t *testing.T) {
  101. ep := url.URL{Scheme: "http", Host: "example.com"}
  102. act := &membersAPIActionLeader{}
  103. wantURL := &url.URL{
  104. Scheme: "http",
  105. Host: "example.com",
  106. Path: "/v2/members/leader",
  107. }
  108. got := *act.HTTPRequest(ep)
  109. err := assertRequest(got, "GET", wantURL, http.Header{}, nil)
  110. if err != nil {
  111. t.Error(err.Error())
  112. }
  113. }
  114. func TestAssertStatusCode(t *testing.T) {
  115. if err := assertStatusCode(404, 400); err == nil {
  116. t.Errorf("assertStatusCode failed to detect conflict in 400 vs 404")
  117. }
  118. if err := assertStatusCode(404, 400, 404); err != nil {
  119. t.Errorf("assertStatusCode found conflict in (404,400) vs 400: %v", err)
  120. }
  121. }
  122. func TestV2MembersURL(t *testing.T) {
  123. got := v2MembersURL(url.URL{
  124. Scheme: "http",
  125. Host: "foo.example.com:4002",
  126. Path: "/pants",
  127. })
  128. want := &url.URL{
  129. Scheme: "http",
  130. Host: "foo.example.com:4002",
  131. Path: "/pants/v2/members",
  132. }
  133. if !reflect.DeepEqual(want, got) {
  134. t.Fatalf("v2MembersURL got %#v, want %#v", got, want)
  135. }
  136. }
  137. func TestMemberUnmarshal(t *testing.T) {
  138. tests := []struct {
  139. body []byte
  140. wantMember Member
  141. wantError bool
  142. }{
  143. // no URLs, just check ID & Name
  144. {
  145. body: []byte(`{"id": "c", "name": "dungarees"}`),
  146. wantMember: Member{ID: "c", Name: "dungarees", PeerURLs: nil, ClientURLs: nil},
  147. },
  148. // both client and peer URLs
  149. {
  150. body: []byte(`{"peerURLs": ["http://127.0.0.1:2379"], "clientURLs": ["http://127.0.0.1:2379"]}`),
  151. wantMember: Member{
  152. PeerURLs: []string{
  153. "http://127.0.0.1:2379",
  154. },
  155. ClientURLs: []string{
  156. "http://127.0.0.1:2379",
  157. },
  158. },
  159. },
  160. // multiple peer URLs
  161. {
  162. body: []byte(`{"peerURLs": ["http://127.0.0.1:2379", "https://example.com"]}`),
  163. wantMember: Member{
  164. PeerURLs: []string{
  165. "http://127.0.0.1:2379",
  166. "https://example.com",
  167. },
  168. ClientURLs: nil,
  169. },
  170. },
  171. // multiple client URLs
  172. {
  173. body: []byte(`{"clientURLs": ["http://127.0.0.1:2379", "https://example.com"]}`),
  174. wantMember: Member{
  175. PeerURLs: nil,
  176. ClientURLs: []string{
  177. "http://127.0.0.1:2379",
  178. "https://example.com",
  179. },
  180. },
  181. },
  182. // invalid JSON
  183. {
  184. body: []byte(`{"peerU`),
  185. wantError: true,
  186. },
  187. }
  188. for i, tt := range tests {
  189. got := Member{}
  190. err := json.Unmarshal(tt.body, &got)
  191. if tt.wantError != (err != nil) {
  192. t.Errorf("#%d: want error %t, got %v", i, tt.wantError, err)
  193. continue
  194. }
  195. if !reflect.DeepEqual(tt.wantMember, got) {
  196. t.Errorf("#%d: incorrect output: want=%#v, got=%#v", i, tt.wantMember, got)
  197. }
  198. }
  199. }
  200. func TestMemberCollectionUnmarshalFail(t *testing.T) {
  201. mc := &memberCollection{}
  202. if err := mc.UnmarshalJSON([]byte(`{`)); err == nil {
  203. t.Errorf("got nil error")
  204. }
  205. }
  206. func TestMemberCollectionUnmarshal(t *testing.T) {
  207. tests := []struct {
  208. body []byte
  209. want memberCollection
  210. }{
  211. {
  212. body: []byte(`{}`),
  213. want: memberCollection([]Member{}),
  214. },
  215. {
  216. body: []byte(`{"members":[]}`),
  217. want: memberCollection([]Member{}),
  218. },
  219. {
  220. body: []byte(`{"members":[{"id":"2745e2525fce8fe","peerURLs":["http://127.0.0.1:7003"],"name":"node3","clientURLs":["http://127.0.0.1:4003"]},{"id":"42134f434382925","peerURLs":["http://127.0.0.1:2380","http://127.0.0.1:7001"],"name":"node1","clientURLs":["http://127.0.0.1:2379","http://127.0.0.1:4001"]},{"id":"94088180e21eb87b","peerURLs":["http://127.0.0.1:7002"],"name":"node2","clientURLs":["http://127.0.0.1:4002"]}]}`),
  221. want: memberCollection(
  222. []Member{
  223. {
  224. ID: "2745e2525fce8fe",
  225. Name: "node3",
  226. PeerURLs: []string{
  227. "http://127.0.0.1:7003",
  228. },
  229. ClientURLs: []string{
  230. "http://127.0.0.1:4003",
  231. },
  232. },
  233. {
  234. ID: "42134f434382925",
  235. Name: "node1",
  236. PeerURLs: []string{
  237. "http://127.0.0.1:2380",
  238. "http://127.0.0.1:7001",
  239. },
  240. ClientURLs: []string{
  241. "http://127.0.0.1:2379",
  242. "http://127.0.0.1:4001",
  243. },
  244. },
  245. {
  246. ID: "94088180e21eb87b",
  247. Name: "node2",
  248. PeerURLs: []string{
  249. "http://127.0.0.1:7002",
  250. },
  251. ClientURLs: []string{
  252. "http://127.0.0.1:4002",
  253. },
  254. },
  255. },
  256. ),
  257. },
  258. }
  259. for i, tt := range tests {
  260. var got memberCollection
  261. err := json.Unmarshal(tt.body, &got)
  262. if err != nil {
  263. t.Errorf("#%d: unexpected error: %v", i, err)
  264. continue
  265. }
  266. if !reflect.DeepEqual(tt.want, got) {
  267. t.Errorf("#%d: incorrect output: want=%#v, got=%#v", i, tt.want, got)
  268. }
  269. }
  270. }
  271. func TestMemberCreateRequestMarshal(t *testing.T) {
  272. req := memberCreateOrUpdateRequest{
  273. PeerURLs: types.URLs([]url.URL{
  274. {Scheme: "http", Host: "127.0.0.1:8081"},
  275. {Scheme: "https", Host: "127.0.0.1:8080"},
  276. }),
  277. }
  278. want := []byte(`{"peerURLs":["http://127.0.0.1:8081","https://127.0.0.1:8080"]}`)
  279. got, err := json.Marshal(&req)
  280. if err != nil {
  281. t.Fatalf("Marshal returned unexpected err=%v", err)
  282. }
  283. if !reflect.DeepEqual(want, got) {
  284. t.Fatalf("Failed to marshal memberCreateRequest: want=%s, got=%s", want, got)
  285. }
  286. }
  287. func TestHTTPMembersAPIAddSuccess(t *testing.T) {
  288. wantAction := &membersAPIActionAdd{
  289. peerURLs: types.URLs([]url.URL{
  290. {Scheme: "http", Host: "127.0.0.1:7002"},
  291. }),
  292. }
  293. mAPI := &httpMembersAPI{
  294. client: &actionAssertingHTTPClient{
  295. t: t,
  296. act: wantAction,
  297. resp: http.Response{
  298. StatusCode: http.StatusCreated,
  299. },
  300. body: []byte(`{"id":"94088180e21eb87b","peerURLs":["http://127.0.0.1:7002"]}`),
  301. },
  302. }
  303. wantResponseMember := &Member{
  304. ID: "94088180e21eb87b",
  305. PeerURLs: []string{"http://127.0.0.1:7002"},
  306. }
  307. m, err := mAPI.Add(context.Background(), "http://127.0.0.1:7002")
  308. if err != nil {
  309. t.Errorf("got non-nil err: %#v", err)
  310. }
  311. if !reflect.DeepEqual(wantResponseMember, m) {
  312. t.Errorf("incorrect Member: want=%#v got=%#v", wantResponseMember, m)
  313. }
  314. }
  315. func TestHTTPMembersAPIAddError(t *testing.T) {
  316. okPeer := "http://example.com:2379"
  317. tests := []struct {
  318. peerURL string
  319. client httpClient
  320. // if wantErr == nil, assert that the returned error is non-nil
  321. // if wantErr != nil, assert that the returned error matches
  322. wantErr error
  323. }{
  324. // malformed peer URL
  325. {
  326. peerURL: ":",
  327. },
  328. // generic httpClient failure
  329. {
  330. peerURL: okPeer,
  331. client: &staticHTTPClient{err: errors.New("fail!")},
  332. },
  333. // unrecognized HTTP status code
  334. {
  335. peerURL: okPeer,
  336. client: &staticHTTPClient{
  337. resp: http.Response{StatusCode: http.StatusTeapot},
  338. },
  339. },
  340. // unmarshal body into membersError on StatusConflict
  341. {
  342. peerURL: okPeer,
  343. client: &staticHTTPClient{
  344. resp: http.Response{
  345. StatusCode: http.StatusConflict,
  346. },
  347. body: []byte(`{"message":"fail!"}`),
  348. },
  349. wantErr: membersError{Message: "fail!"},
  350. },
  351. // fail to unmarshal body on StatusConflict
  352. {
  353. peerURL: okPeer,
  354. client: &staticHTTPClient{
  355. resp: http.Response{
  356. StatusCode: http.StatusConflict,
  357. },
  358. body: []byte(`{"`),
  359. },
  360. },
  361. // fail to unmarshal body on StatusCreated
  362. {
  363. peerURL: okPeer,
  364. client: &staticHTTPClient{
  365. resp: http.Response{
  366. StatusCode: http.StatusCreated,
  367. },
  368. body: []byte(`{"id":"XX`),
  369. },
  370. },
  371. }
  372. for i, tt := range tests {
  373. mAPI := &httpMembersAPI{client: tt.client}
  374. m, err := mAPI.Add(context.Background(), tt.peerURL)
  375. if err == nil {
  376. t.Errorf("#%d: got nil err", i)
  377. }
  378. if tt.wantErr != nil && !reflect.DeepEqual(tt.wantErr, err) {
  379. t.Errorf("#%d: incorrect error: want=%#v got=%#v", i, tt.wantErr, err)
  380. }
  381. if m != nil {
  382. t.Errorf("#%d: got non-nil Member", i)
  383. }
  384. }
  385. }
  386. func TestHTTPMembersAPIRemoveSuccess(t *testing.T) {
  387. wantAction := &membersAPIActionRemove{
  388. memberID: "94088180e21eb87b",
  389. }
  390. mAPI := &httpMembersAPI{
  391. client: &actionAssertingHTTPClient{
  392. t: t,
  393. act: wantAction,
  394. resp: http.Response{
  395. StatusCode: http.StatusNoContent,
  396. },
  397. },
  398. }
  399. if err := mAPI.Remove(context.Background(), "94088180e21eb87b"); err != nil {
  400. t.Errorf("got non-nil err: %#v", err)
  401. }
  402. }
  403. func TestHTTPMembersAPIRemoveFail(t *testing.T) {
  404. tests := []httpClient{
  405. // generic error
  406. &staticHTTPClient{
  407. err: errors.New("fail!"),
  408. },
  409. // unexpected HTTP status code
  410. &staticHTTPClient{
  411. resp: http.Response{
  412. StatusCode: http.StatusInternalServerError,
  413. },
  414. },
  415. }
  416. for i, tt := range tests {
  417. mAPI := &httpMembersAPI{client: tt}
  418. if err := mAPI.Remove(context.Background(), "94088180e21eb87b"); err == nil {
  419. t.Errorf("#%d: got nil err", i)
  420. }
  421. }
  422. }
  423. func TestHTTPMembersAPIListSuccess(t *testing.T) {
  424. wantAction := &membersAPIActionList{}
  425. mAPI := &httpMembersAPI{
  426. client: &actionAssertingHTTPClient{
  427. t: t,
  428. act: wantAction,
  429. resp: http.Response{
  430. StatusCode: http.StatusOK,
  431. },
  432. body: []byte(`{"members":[{"id":"94088180e21eb87b","name":"node2","peerURLs":["http://127.0.0.1:7002"],"clientURLs":["http://127.0.0.1:4002"]}]}`),
  433. },
  434. }
  435. wantResponseMembers := []Member{
  436. {
  437. ID: "94088180e21eb87b",
  438. Name: "node2",
  439. PeerURLs: []string{"http://127.0.0.1:7002"},
  440. ClientURLs: []string{"http://127.0.0.1:4002"},
  441. },
  442. }
  443. m, err := mAPI.List(context.Background())
  444. if err != nil {
  445. t.Errorf("got non-nil err: %#v", err)
  446. }
  447. if !reflect.DeepEqual(wantResponseMembers, m) {
  448. t.Errorf("incorrect Members: want=%#v got=%#v", wantResponseMembers, m)
  449. }
  450. }
  451. func TestHTTPMembersAPIListError(t *testing.T) {
  452. tests := []httpClient{
  453. // generic httpClient failure
  454. &staticHTTPClient{err: errors.New("fail!")},
  455. // unrecognized HTTP status code
  456. &staticHTTPClient{
  457. resp: http.Response{StatusCode: http.StatusTeapot},
  458. },
  459. // fail to unmarshal body on StatusOK
  460. &staticHTTPClient{
  461. resp: http.Response{
  462. StatusCode: http.StatusOK,
  463. },
  464. body: []byte(`[{"id":"XX`),
  465. },
  466. }
  467. for i, tt := range tests {
  468. mAPI := &httpMembersAPI{client: tt}
  469. ms, err := mAPI.List(context.Background())
  470. if err == nil {
  471. t.Errorf("#%d: got nil err", i)
  472. }
  473. if ms != nil {
  474. t.Errorf("#%d: got non-nil Member slice", i)
  475. }
  476. }
  477. }
  478. func TestHTTPMembersAPILeaderSuccess(t *testing.T) {
  479. wantAction := &membersAPIActionLeader{}
  480. mAPI := &httpMembersAPI{
  481. client: &actionAssertingHTTPClient{
  482. t: t,
  483. act: wantAction,
  484. resp: http.Response{
  485. StatusCode: http.StatusOK,
  486. },
  487. body: []byte(`{"id":"94088180e21eb87b","name":"node2","peerURLs":["http://127.0.0.1:7002"],"clientURLs":["http://127.0.0.1:4002"]}`),
  488. },
  489. }
  490. wantResponseMember := &Member{
  491. ID: "94088180e21eb87b",
  492. Name: "node2",
  493. PeerURLs: []string{"http://127.0.0.1:7002"},
  494. ClientURLs: []string{"http://127.0.0.1:4002"},
  495. }
  496. m, err := mAPI.Leader(context.Background())
  497. if err != nil {
  498. t.Errorf("err = %v, want %v", err, nil)
  499. }
  500. if !reflect.DeepEqual(wantResponseMember, m) {
  501. t.Errorf("incorrect member: member = %v, want %v", wantResponseMember, m)
  502. }
  503. }
  504. func TestHTTPMembersAPILeaderError(t *testing.T) {
  505. tests := []httpClient{
  506. // generic httpClient failure
  507. &staticHTTPClient{err: errors.New("fail!")},
  508. // unrecognized HTTP status code
  509. &staticHTTPClient{
  510. resp: http.Response{StatusCode: http.StatusTeapot},
  511. },
  512. // fail to unmarshal body on StatusOK
  513. &staticHTTPClient{
  514. resp: http.Response{
  515. StatusCode: http.StatusOK,
  516. },
  517. body: []byte(`[{"id":"XX`),
  518. },
  519. }
  520. for i, tt := range tests {
  521. mAPI := &httpMembersAPI{client: tt}
  522. m, err := mAPI.Leader(context.Background())
  523. if err == nil {
  524. t.Errorf("#%d: err = nil, want not nil", i)
  525. }
  526. if m != nil {
  527. t.Errorf("member slice = %v, want nil", m)
  528. }
  529. }
  530. }