members_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. "encoding/json"
  17. "errors"
  18. "net/http"
  19. "net/url"
  20. "reflect"
  21. "testing"
  22. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  23. "github.com/coreos/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. url.URL{Scheme: "https", Host: "127.0.0.1:8081"},
  44. url.URL{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. url.URL{Scheme: "https", Host: "127.0.0.1:8081"},
  68. url.URL{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 TestAssertStatusCode(t *testing.T) {
  101. if err := assertStatusCode(404, 400); err == nil {
  102. t.Errorf("assertStatusCode failed to detect conflict in 400 vs 404")
  103. }
  104. if err := assertStatusCode(404, 400, 404); err != nil {
  105. t.Errorf("assertStatusCode found conflict in (404,400) vs 400: %v", err)
  106. }
  107. }
  108. func TestV2MembersURL(t *testing.T) {
  109. got := v2MembersURL(url.URL{
  110. Scheme: "http",
  111. Host: "foo.example.com:4002",
  112. Path: "/pants",
  113. })
  114. want := &url.URL{
  115. Scheme: "http",
  116. Host: "foo.example.com:4002",
  117. Path: "/pants/v2/members",
  118. }
  119. if !reflect.DeepEqual(want, got) {
  120. t.Fatalf("v2MembersURL got %#v, want %#v", got, want)
  121. }
  122. }
  123. func TestMemberUnmarshal(t *testing.T) {
  124. tests := []struct {
  125. body []byte
  126. wantMember Member
  127. wantError bool
  128. }{
  129. // no URLs, just check ID & Name
  130. {
  131. body: []byte(`{"id": "c", "name": "dungarees"}`),
  132. wantMember: Member{ID: "c", Name: "dungarees", PeerURLs: nil, ClientURLs: nil},
  133. },
  134. // both client and peer URLs
  135. {
  136. body: []byte(`{"peerURLs": ["http://127.0.0.1:2379"], "clientURLs": ["http://127.0.0.1:2379"]}`),
  137. wantMember: Member{
  138. PeerURLs: []string{
  139. "http://127.0.0.1:2379",
  140. },
  141. ClientURLs: []string{
  142. "http://127.0.0.1:2379",
  143. },
  144. },
  145. },
  146. // multiple peer URLs
  147. {
  148. body: []byte(`{"peerURLs": ["http://127.0.0.1:2379", "https://example.com"]}`),
  149. wantMember: Member{
  150. PeerURLs: []string{
  151. "http://127.0.0.1:2379",
  152. "https://example.com",
  153. },
  154. ClientURLs: nil,
  155. },
  156. },
  157. // multiple client URLs
  158. {
  159. body: []byte(`{"clientURLs": ["http://127.0.0.1:2379", "https://example.com"]}`),
  160. wantMember: Member{
  161. PeerURLs: nil,
  162. ClientURLs: []string{
  163. "http://127.0.0.1:2379",
  164. "https://example.com",
  165. },
  166. },
  167. },
  168. // invalid JSON
  169. {
  170. body: []byte(`{"peerU`),
  171. wantError: true,
  172. },
  173. }
  174. for i, tt := range tests {
  175. got := Member{}
  176. err := json.Unmarshal(tt.body, &got)
  177. if tt.wantError != (err != nil) {
  178. t.Errorf("#%d: want error %t, got %v", i, tt.wantError, err)
  179. continue
  180. }
  181. if !reflect.DeepEqual(tt.wantMember, got) {
  182. t.Errorf("#%d: incorrect output: want=%#v, got=%#v", i, tt.wantMember, got)
  183. }
  184. }
  185. }
  186. func TestMemberCollectionUnmarshalFail(t *testing.T) {
  187. mc := &memberCollection{}
  188. if err := mc.UnmarshalJSON([]byte(`{`)); err == nil {
  189. t.Errorf("got nil error")
  190. }
  191. }
  192. func TestMemberCollectionUnmarshal(t *testing.T) {
  193. tests := []struct {
  194. body []byte
  195. want memberCollection
  196. }{
  197. {
  198. body: []byte(`{}`),
  199. want: memberCollection([]Member{}),
  200. },
  201. {
  202. body: []byte(`{"members":[]}`),
  203. want: memberCollection([]Member{}),
  204. },
  205. {
  206. 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"]}]}`),
  207. want: memberCollection(
  208. []Member{
  209. {
  210. ID: "2745e2525fce8fe",
  211. Name: "node3",
  212. PeerURLs: []string{
  213. "http://127.0.0.1:7003",
  214. },
  215. ClientURLs: []string{
  216. "http://127.0.0.1:4003",
  217. },
  218. },
  219. {
  220. ID: "42134f434382925",
  221. Name: "node1",
  222. PeerURLs: []string{
  223. "http://127.0.0.1:2380",
  224. "http://127.0.0.1:7001",
  225. },
  226. ClientURLs: []string{
  227. "http://127.0.0.1:2379",
  228. "http://127.0.0.1:4001",
  229. },
  230. },
  231. {
  232. ID: "94088180e21eb87b",
  233. Name: "node2",
  234. PeerURLs: []string{
  235. "http://127.0.0.1:7002",
  236. },
  237. ClientURLs: []string{
  238. "http://127.0.0.1:4002",
  239. },
  240. },
  241. },
  242. ),
  243. },
  244. }
  245. for i, tt := range tests {
  246. var got memberCollection
  247. err := json.Unmarshal(tt.body, &got)
  248. if err != nil {
  249. t.Errorf("#%d: unexpected error: %v", i, err)
  250. continue
  251. }
  252. if !reflect.DeepEqual(tt.want, got) {
  253. t.Errorf("#%d: incorrect output: want=%#v, got=%#v", i, tt.want, got)
  254. }
  255. }
  256. }
  257. func TestMemberCreateRequestMarshal(t *testing.T) {
  258. req := memberCreateOrUpdateRequest{
  259. PeerURLs: types.URLs([]url.URL{
  260. url.URL{Scheme: "http", Host: "127.0.0.1:8081"},
  261. url.URL{Scheme: "https", Host: "127.0.0.1:8080"},
  262. }),
  263. }
  264. want := []byte(`{"peerURLs":["http://127.0.0.1:8081","https://127.0.0.1:8080"]}`)
  265. got, err := json.Marshal(&req)
  266. if err != nil {
  267. t.Fatalf("Marshal returned unexpected err=%v", err)
  268. }
  269. if !reflect.DeepEqual(want, got) {
  270. t.Fatalf("Failed to marshal memberCreateRequest: want=%s, got=%s", want, got)
  271. }
  272. }
  273. func TestHTTPMembersAPIAddSuccess(t *testing.T) {
  274. wantAction := &membersAPIActionAdd{
  275. peerURLs: types.URLs([]url.URL{
  276. url.URL{Scheme: "http", Host: "127.0.0.1:7002"},
  277. }),
  278. }
  279. mAPI := &httpMembersAPI{
  280. client: &actionAssertingHTTPClient{
  281. t: t,
  282. act: wantAction,
  283. resp: http.Response{
  284. StatusCode: http.StatusCreated,
  285. },
  286. body: []byte(`{"id":"94088180e21eb87b","peerURLs":["http://127.0.0.1:7002"]}`),
  287. },
  288. }
  289. wantResponseMember := &Member{
  290. ID: "94088180e21eb87b",
  291. PeerURLs: []string{"http://127.0.0.1:7002"},
  292. }
  293. m, err := mAPI.Add(context.Background(), "http://127.0.0.1:7002")
  294. if err != nil {
  295. t.Errorf("got non-nil err: %#v", err)
  296. }
  297. if !reflect.DeepEqual(wantResponseMember, m) {
  298. t.Errorf("incorrect Member: want=%#v got=%#v", wantResponseMember, m)
  299. }
  300. }
  301. func TestHTTPMembersAPIAddError(t *testing.T) {
  302. okPeer := "http://example.com:2379"
  303. tests := []struct {
  304. peerURL string
  305. client httpClient
  306. // if wantErr == nil, assert that the returned error is non-nil
  307. // if wantErr != nil, assert that the returned error matches
  308. wantErr error
  309. }{
  310. // malformed peer URL
  311. {
  312. peerURL: ":",
  313. },
  314. // generic httpClient failure
  315. {
  316. peerURL: okPeer,
  317. client: &staticHTTPClient{err: errors.New("fail!")},
  318. },
  319. // unrecognized HTTP status code
  320. {
  321. peerURL: okPeer,
  322. client: &staticHTTPClient{
  323. resp: http.Response{StatusCode: http.StatusTeapot},
  324. },
  325. },
  326. // unmarshal body into membersError on StatusConflict
  327. {
  328. peerURL: okPeer,
  329. client: &staticHTTPClient{
  330. resp: http.Response{
  331. StatusCode: http.StatusConflict,
  332. },
  333. body: []byte(`{"message":"fail!"}`),
  334. },
  335. wantErr: membersError{Message: "fail!"},
  336. },
  337. // fail to unmarshal body on StatusConflict
  338. {
  339. peerURL: okPeer,
  340. client: &staticHTTPClient{
  341. resp: http.Response{
  342. StatusCode: http.StatusConflict,
  343. },
  344. body: []byte(`{"`),
  345. },
  346. },
  347. // fail to unmarshal body on StatusCreated
  348. {
  349. peerURL: okPeer,
  350. client: &staticHTTPClient{
  351. resp: http.Response{
  352. StatusCode: http.StatusCreated,
  353. },
  354. body: []byte(`{"id":"XX`),
  355. },
  356. },
  357. }
  358. for i, tt := range tests {
  359. mAPI := &httpMembersAPI{client: tt.client}
  360. m, err := mAPI.Add(context.Background(), tt.peerURL)
  361. if err == nil {
  362. t.Errorf("#%d: got nil err", i)
  363. }
  364. if tt.wantErr != nil && !reflect.DeepEqual(tt.wantErr, err) {
  365. t.Errorf("#%d: incorrect error: want=%#v got=%#v", i, tt.wantErr, err)
  366. }
  367. if m != nil {
  368. t.Errorf("#%d: got non-nil Member", i)
  369. }
  370. }
  371. }
  372. func TestHTTPMembersAPIRemoveSuccess(t *testing.T) {
  373. wantAction := &membersAPIActionRemove{
  374. memberID: "94088180e21eb87b",
  375. }
  376. mAPI := &httpMembersAPI{
  377. client: &actionAssertingHTTPClient{
  378. t: t,
  379. act: wantAction,
  380. resp: http.Response{
  381. StatusCode: http.StatusNoContent,
  382. },
  383. },
  384. }
  385. if err := mAPI.Remove(context.Background(), "94088180e21eb87b"); err != nil {
  386. t.Errorf("got non-nil err: %#v", err)
  387. }
  388. }
  389. func TestHTTPMembersAPIRemoveFail(t *testing.T) {
  390. tests := []httpClient{
  391. // generic error
  392. &staticHTTPClient{
  393. err: errors.New("fail!"),
  394. },
  395. // unexpected HTTP status code
  396. &staticHTTPClient{
  397. resp: http.Response{
  398. StatusCode: http.StatusInternalServerError,
  399. },
  400. },
  401. }
  402. for i, tt := range tests {
  403. mAPI := &httpMembersAPI{client: tt}
  404. if err := mAPI.Remove(context.Background(), "94088180e21eb87b"); err == nil {
  405. t.Errorf("#%d: got nil err", i)
  406. }
  407. }
  408. }
  409. func TestHTTPMembersAPIListSuccess(t *testing.T) {
  410. wantAction := &membersAPIActionList{}
  411. mAPI := &httpMembersAPI{
  412. client: &actionAssertingHTTPClient{
  413. t: t,
  414. act: wantAction,
  415. resp: http.Response{
  416. StatusCode: http.StatusOK,
  417. },
  418. body: []byte(`{"members":[{"id":"94088180e21eb87b","name":"node2","peerURLs":["http://127.0.0.1:7002"],"clientURLs":["http://127.0.0.1:4002"]}]}`),
  419. },
  420. }
  421. wantResponseMembers := []Member{
  422. Member{
  423. ID: "94088180e21eb87b",
  424. Name: "node2",
  425. PeerURLs: []string{"http://127.0.0.1:7002"},
  426. ClientURLs: []string{"http://127.0.0.1:4002"},
  427. },
  428. }
  429. m, err := mAPI.List(context.Background())
  430. if err != nil {
  431. t.Errorf("got non-nil err: %#v", err)
  432. }
  433. if !reflect.DeepEqual(wantResponseMembers, m) {
  434. t.Errorf("incorrect Members: want=%#v got=%#v", wantResponseMembers, m)
  435. }
  436. }
  437. func TestHTTPMembersAPIListError(t *testing.T) {
  438. tests := []httpClient{
  439. // generic httpClient failure
  440. &staticHTTPClient{err: errors.New("fail!")},
  441. // unrecognized HTTP status code
  442. &staticHTTPClient{
  443. resp: http.Response{StatusCode: http.StatusTeapot},
  444. },
  445. // fail to unmarshal body on StatusOK
  446. &staticHTTPClient{
  447. resp: http.Response{
  448. StatusCode: http.StatusOK,
  449. },
  450. body: []byte(`[{"id":"XX`),
  451. },
  452. }
  453. for i, tt := range tests {
  454. mAPI := &httpMembersAPI{client: tt}
  455. ms, err := mAPI.List(context.Background())
  456. if err == nil {
  457. t.Errorf("#%d: got nil err", i)
  458. }
  459. if ms != nil {
  460. t.Errorf("#%d: got non-nil Member slice", i)
  461. }
  462. }
  463. }