| 1 |
package integration |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"io/ioutil" |
| 9 |
"net/http" |
| 10 |
"path" |
| 11 |
"regexp" |
| 12 |
"strings" |
| 13 |
"sync/atomic" |
| 14 |
"testing" |
| 15 |
"time" |
| 16 |
|
| 17 |
"github.com/coreos/etcd/client" |
| 18 |
) |
| 19 |
|
| 20 |
var basePort int32 = 10000 |
| 21 |
|
| 22 |
func TestHandlersV2_size_1(t *testing.T) { testHandlersV2(t, 1) } |
| 23 |
func TestHandlersV2_size_3(t *testing.T) { testHandlersV2(t, 3) } |
| 24 |
func TestHandlersV2_size_5(t *testing.T) { testHandlersV2(t, 5) } |
| 25 |
func TestHandlersV2_size_7(t *testing.T) { testHandlersV2(t, 7) } |
| 26 |
func TestHandlersV2_size_10(t *testing.T) { testHandlersV2(t, 10) } |
| 27 |
func TestHandlersV2_size_100(t *testing.T) { testHandlersV2(t, 100) } |
| 28 |
func testHandlersV2(t *testing.T, size int) { |
| 29 |
cport := int(atomic.LoadInt32(&basePort)) |
| 30 |
atomic.AddInt32(&basePort, int32(5)) |
| 31 |
|
| 32 |
svs := NewService(t, cport, cport+1, cport+2) |
| 33 |
defer svs.Stop(t) |
| 34 |
|
| 35 |
errc := svs.Start(t) |
| 36 |
select { |
| 37 |
case err := <-errc: |
| 38 |
t.Fatal(err) |
| 39 |
case <-time.After(5 * time.Second): |
| 40 |
|
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
resp, err := http.Get(svs.httpEp + "/health") |
| 46 |
if err != nil { |
| 47 |
t.Fatal(err) |
| 48 |
} |
| 49 |
bts, err := ioutil.ReadAll(resp.Body) |
| 50 |
if err != nil { |
| 51 |
t.Fatal(err) |
| 52 |
} |
| 53 |
gracefulClose(resp) |
| 54 |
if !bytes.Equal(bts, []byte("OK")) { |
| 55 |
t.Fatalf("expected 'OK', got %q", string(bts)) |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
resp, err = http.Get(svs.httpEp + fmt.Sprintf("/new?size=%d", size)) |
| 60 |
if err != nil { |
| 61 |
t.Fatal(err) |
| 62 |
} |
| 63 |
bts, err = ioutil.ReadAll(resp.Body) |
| 64 |
if err != nil { |
| 65 |
t.Fatal(err) |
| 66 |
} |
| 67 |
gracefulClose(resp) |
| 68 |
hostToken := string(bts) |
| 69 |
if !strings.HasPrefix(hostToken, testDiscoveryHost+"/") { |
| 70 |
t.Fatalf("expected %q in prefix, got %q", testDiscoveryHost, hostToken) |
| 71 |
} |
| 72 |
token := strings.Replace(hostToken, testDiscoveryHost+"/", "", 1) |
| 73 |
if !isAlphanumeric(token) { |
| 74 |
t.Fatalf("token %q is not alphanumeric", token) |
| 75 |
} |
| 76 |
if len(token) != 32 { |
| 77 |
t.Fatalf("token %q must be 32-character", token) |
| 78 |
} |
| 79 |
|
| 80 |
var cresp client.Response |
| 81 |
|
| 82 |
|
| 83 |
for i, p := range []string{fmt.Sprintf("/%s", token), fmt.Sprintf("/%s/", token)} { |
| 84 |
resp, err = http.Get(svs.httpEp + p) |
| 85 |
if err != nil { |
| 86 |
t.Fatalf("#%d: %v", i, err) |
| 87 |
} |
| 88 |
if err = json.NewDecoder(resp.Body).Decode(&cresp); err != nil { |
| 89 |
t.Fatalf("#%d: %v", i, err) |
| 90 |
} |
| 91 |
gracefulClose(resp) |
| 92 |
if cresp.Node == nil { |
| 93 |
t.Fatalf("#%d: token response returned <nil> node", i) |
| 94 |
} |
| 95 |
exp := "/" + path.Join("_etcd", "registry", token) |
| 96 |
if cresp.Node.Key != exp { |
| 97 |
t.Fatalf("key expected %q, got %q", exp, cresp.Node.Key) |
| 98 |
} |
| 99 |
if !cresp.Node.Dir { |
| 100 |
t.Fatalf("#%d: node expected directory, got dir %v", i, cresp.Node.Dir) |
| 101 |
} |
| 102 |
if len(cresp.Node.Nodes) > 0 { |
| 103 |
t.Fatalf("#%d: unexpected cluster members found, got %+v", i, cresp.Node.Nodes) |
| 104 |
} |
| 105 |
|
| 106 |
if cresp.Node.CreatedIndex != 6 { |
| 107 |
t.Fatalf("cresp.Node.CreatedIndex expected 6, got %d", cresp.Node.CreatedIndex) |
| 108 |
} |
| 109 |
if cresp.Node.ModifiedIndex != 6 { |
| 110 |
t.Fatalf("cresp.Node.ModifiedIndex expected 6, got %d", cresp.Node.ModifiedIndex) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
resp, err = http.Get(svs.httpEp + fmt.Sprintf("/%s/_config/size", token)) |
| 116 |
if err != nil { |
| 117 |
t.Fatal(err) |
| 118 |
} |
| 119 |
if err = json.NewDecoder(resp.Body).Decode(&cresp); err != nil { |
| 120 |
t.Fatal(err) |
| 121 |
} |
| 122 |
gracefulClose(resp) |
| 123 |
if cresp.Node == nil { |
| 124 |
t.Fatal("token response returned <nil> node") |
| 125 |
} |
| 126 |
if !strings.HasSuffix(cresp.Node.Key, token+"/_config/size") { |
| 127 |
t.Fatalf("node key is missing '/_config/size' in %q, got %q", token, cresp.Node.Key) |
| 128 |
} |
| 129 |
if cresp.Node.Value != fmt.Sprintf("%d", size) { |
| 130 |
t.Fatalf("size expected %d, got %s", size, cresp.Node.Value) |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
for i := 0; i < size; i++ { |
| 137 |
memberID := fmt.Sprintf("id%d", i) |
| 138 |
node := client.Node{ |
| 139 |
Key: "/" + path.Join("_etcd", "registry", token+memberID), |
| 140 |
Value: fmt.Sprintf("%s=http://test.com:%d", memberID, i), |
| 141 |
} |
| 142 |
cresp.Node.Nodes = append(cresp.Node.Nodes, &node) |
| 143 |
} |
| 144 |
bts, err = json.Marshal(&cresp) |
| 145 |
if err != nil { |
| 146 |
t.Fatal(err) |
| 147 |
} |
| 148 |
req, err := http.NewRequest(http.MethodPut, svs.httpEp+fmt.Sprintf("/%s", token), bytes.NewReader(bts)) |
| 149 |
if err != nil { |
| 150 |
t.Fatal(err) |
| 151 |
} |
| 152 |
resp, err = http.DefaultClient.Do(req) |
| 153 |
if err != nil { |
| 154 |
t.Fatal(err) |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
for i, ep := range []string{ |
| 159 |
svs.httpEp + fmt.Sprintf("/%s", token), |
| 160 |
svs.etcdCURL.String() + "/" + path.Join("v2", "keys", "_etcd", "registry", token), |
| 161 |
} { |
| 162 |
resp, err = http.Get(ep) |
| 163 |
if err != nil { |
| 164 |
t.Fatalf("#%d: %v", i, err) |
| 165 |
} |
| 166 |
if err = json.NewDecoder(resp.Body).Decode(&cresp); err != nil { |
| 167 |
t.Fatalf("#%d: %v", i, err) |
| 168 |
} |
| 169 |
gracefulClose(resp) |
| 170 |
if cresp.Node == nil { |
| 171 |
t.Fatalf("#%d: token response returned <nil> node", i) |
| 172 |
} |
| 173 |
exp := "/" + path.Join("_etcd", "registry", token) |
| 174 |
if cresp.Node.Key != exp { |
| 175 |
t.Fatalf("#%d: key expected %q, got %q", i, exp, cresp.Node.Key) |
| 176 |
} |
| 177 |
if !cresp.Node.Dir { |
| 178 |
t.Fatalf("#%d: node expected directory, got dir %v", i, cresp.Node.Dir) |
| 179 |
} |
| 180 |
if len(cresp.Node.Nodes) != size { |
| 181 |
t.Fatalf("#%d: expected %d cluster members found, got %+v", i, size, cresp.Node.Nodes) |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
var isAlphanumeric = regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString |
| 187 |
|
| 188 |
func gracefulClose(resp *http.Response) { |
| 189 |
io.Copy(ioutil.Discard, resp.Body) |
| 190 |
resp.Body.Close() |
| 191 |
} |