members_test.go 12 KB

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