| 1 |
package e2e |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"log" |
| 6 |
"net/http" |
| 7 |
"regexp" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/coreos/etcd/pkg/fileutil" |
| 13 |
) |
| 14 |
|
| 15 |
var discoveryExec = "../bin/discovery" |
| 16 |
|
| 17 |
func init() { |
| 18 |
if !fileutil.Exist(discoveryExec) { |
| 19 |
log.Fatalf("could not find %q", discoveryExec) |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
func TestDiscovery_size_1(t *testing.T) { testDiscovery(t, 1) } |
| 24 |
func TestDiscovery_size_3(t *testing.T) { testDiscovery(t, 3) } |
| 25 |
func TestDiscovery_size_5(t *testing.T) { testDiscovery(t, 5) } |
| 26 |
func TestDiscovery_size_7(t *testing.T) { testDiscovery(t, 7) } |
| 27 |
func testDiscovery(t *testing.T, size int) { |
| 28 |
cfg := defaultConfig |
| 29 |
cfg.execPath = etcdExecLatest |
| 30 |
cfg.clusterSize = size |
| 31 |
epc, err := cfg.NewEtcdProcessCluster() |
| 32 |
if err != nil { |
| 33 |
t.Fatal(err) |
| 34 |
} |
| 35 |
if err = epc.Start(); err != nil { |
| 36 |
t.Fatal(err) |
| 37 |
} |
| 38 |
defer func() { |
| 39 |
if err = epc.Stop(10 * time.Second); err != nil { |
| 40 |
t.Fatal(err) |
| 41 |
} |
| 42 |
}() |
| 43 |
|
| 44 |
|
| 45 |
procs := make([]*discoveryProcess, size) |
| 46 |
errc := make(chan error) |
| 47 |
for i := 0; i < size; i++ { |
| 48 |
dcfg := discoveryProcessConfig{ |
| 49 |
execPath: discoveryExec, |
| 50 |
etcdEp: epc.procs[i].cfg.curl.String(), |
| 51 |
discoveryHost: fmt.Sprintf("https://test-%d.etcd.io", i), |
| 52 |
} |
| 53 |
procs[i] = dcfg.NewDiscoveryProcess() |
| 54 |
go func(dp *discoveryProcess) { |
| 55 |
errc <- dp.Start() |
| 56 |
}(procs[i]) |
| 57 |
} |
| 58 |
for i := 0; i < size; i++ { |
| 59 |
if err = <-errc; err != nil { |
| 60 |
t.Fatal(err) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
for i := 0; i < size; i++ { |
| 67 |
dhost := procs[i].cfg.discoveryHost |
| 68 |
tokenFunc := func(txt string) bool { |
| 69 |
if !strings.HasPrefix(txt, dhost+"/") { |
| 70 |
return false |
| 71 |
} |
| 72 |
token := strings.Replace(txt, dhost+"/", "", 1) |
| 73 |
return isAlphanumeric(token) |
| 74 |
} |
| 75 |
req := cURLReq{ |
| 76 |
timeout: 5 * time.Second, |
| 77 |
endpoint: fmt.Sprintf("http://localhost:%d/new?size=%d", procs[i].cfg.webPort, size), |
| 78 |
method: http.MethodGet, |
| 79 |
expFunc: tokenFunc, |
| 80 |
} |
| 81 |
token, err := req.Send() |
| 82 |
if err != nil { |
| 83 |
t.Fatal(err) |
| 84 |
} |
| 85 |
if !tokenFunc(token) { |
| 86 |
t.Fatalf("unexpected token %q", token) |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
var isAlphanumeric = regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString |