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