members_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package client
  14. import (
  15. "encoding/json"
  16. "net/http"
  17. "net/url"
  18. "reflect"
  19. "testing"
  20. )
  21. func TestMembersAPIListAction(t *testing.T) {
  22. ep := url.URL{Scheme: "http", Host: "example.com/v2/admin/members"}
  23. wantURL := &url.URL{
  24. Scheme: "http",
  25. Host: "example.com",
  26. Path: "/v2/admin/members",
  27. }
  28. act := &membersAPIActionList{}
  29. got := *act.httpRequest(ep)
  30. err := assertResponse(got, wantURL, http.Header{}, nil)
  31. if err != nil {
  32. t.Errorf(err.Error())
  33. }
  34. }
  35. func TestMembersAPIUnmarshalMember(t *testing.T) {
  36. tests := []struct {
  37. body []byte
  38. wantMember Member
  39. wantError bool
  40. }{
  41. // no URLs, just check ID & Name
  42. {
  43. body: []byte(`{"id": 1, "name": "dungarees"}`),
  44. wantMember: Member{ID: 1, Name: "dungarees", PeerURLs: []url.URL{}, ClientURLs: []url.URL{}},
  45. },
  46. // both client and peer URLs
  47. {
  48. body: []byte(`{"peerURLs": ["http://127.0.0.1:4001"], "clientURLs": ["http://127.0.0.1:4001"]}`),
  49. wantMember: Member{
  50. PeerURLs: []url.URL{
  51. {Scheme: "http", Host: "127.0.0.1:4001"},
  52. },
  53. ClientURLs: []url.URL{
  54. {Scheme: "http", Host: "127.0.0.1:4001"},
  55. },
  56. },
  57. },
  58. // multiple peer URLs
  59. {
  60. body: []byte(`{"peerURLs": ["http://127.0.0.1:4001", "https://example.com"]}`),
  61. wantMember: Member{
  62. PeerURLs: []url.URL{
  63. {Scheme: "http", Host: "127.0.0.1:4001"},
  64. {Scheme: "https", Host: "example.com"},
  65. },
  66. ClientURLs: []url.URL{},
  67. },
  68. },
  69. // multiple client URLs
  70. {
  71. body: []byte(`{"clientURLs": ["http://127.0.0.1:4001", "https://example.com"]}`),
  72. wantMember: Member{
  73. PeerURLs: []url.URL{},
  74. ClientURLs: []url.URL{
  75. {Scheme: "http", Host: "127.0.0.1:4001"},
  76. {Scheme: "https", Host: "example.com"},
  77. },
  78. },
  79. },
  80. // invalid JSON
  81. {
  82. body: []byte(`{"peerU`),
  83. wantError: true,
  84. },
  85. // valid JSON, invalid URL
  86. {
  87. body: []byte(`{"peerURLs": [":"]}`),
  88. wantError: true,
  89. },
  90. }
  91. for i, tt := range tests {
  92. got := Member{}
  93. err := json.Unmarshal(tt.body, &got)
  94. if tt.wantError != (err != nil) {
  95. t.Errorf("#%d: want error %t, got %v", i, tt.wantError, err)
  96. continue
  97. }
  98. if !reflect.DeepEqual(tt.wantMember, got) {
  99. t.Errorf("#%d: incorrect output: want=%#v, got=%#v", i, tt.wantMember, got)
  100. }
  101. }
  102. }
  103. func TestMembersAPIUnmarshalMembers(t *testing.T) {
  104. body := []byte(`{"members":[{"id":176869799018424574,"peerURLs":["http://127.0.0.1:7003"],"name":"node3","clientURLs":["http://127.0.0.1:4003"]},{"id":297577273835923749,"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":10666918107976480891,"peerURLs":["http://127.0.0.1:7002"],"name":"node2","clientURLs":["http://127.0.0.1:4002"]}]}`)
  105. want := membersCollection{
  106. Members: []Member{
  107. {
  108. ID: 176869799018424574,
  109. Name: "node3",
  110. PeerURLs: []url.URL{
  111. {Scheme: "http", Host: "127.0.0.1:7003"},
  112. },
  113. ClientURLs: []url.URL{
  114. {Scheme: "http", Host: "127.0.0.1:4003"},
  115. },
  116. },
  117. {
  118. ID: 297577273835923749,
  119. Name: "node1",
  120. PeerURLs: []url.URL{
  121. {Scheme: "http", Host: "127.0.0.1:2380"},
  122. {Scheme: "http", Host: "127.0.0.1:7001"},
  123. },
  124. ClientURLs: []url.URL{
  125. {Scheme: "http", Host: "127.0.0.1:2379"},
  126. {Scheme: "http", Host: "127.0.0.1:4001"},
  127. },
  128. },
  129. {
  130. ID: 10666918107976480891,
  131. Name: "node2",
  132. PeerURLs: []url.URL{
  133. {Scheme: "http", Host: "127.0.0.1:7002"},
  134. },
  135. ClientURLs: []url.URL{
  136. {Scheme: "http", Host: "127.0.0.1:4002"},
  137. },
  138. },
  139. },
  140. }
  141. got := membersCollection{}
  142. err := json.Unmarshal(body, &got)
  143. if err != nil {
  144. t.Fatalf("Unexpected error: %v", err)
  145. }
  146. if !reflect.DeepEqual(want, got) {
  147. t.Errorf("Incorrect output: want=%#v, got=%#v", want, got)
  148. }
  149. }