| 1 |
package e2e |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"fmt" |
| 6 |
"log" |
| 7 |
"net/http" |
| 8 |
"os" |
| 9 |
"path" |
| 10 |
"strconv" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/coreos/etcd/client" |
| 16 |
"github.com/coreos/etcd/pkg/expect" |
| 17 |
"github.com/coreos/etcd/pkg/fileutil" |
| 18 |
) |
| 19 |
|
| 20 |
var ( |
| 21 |
etcdExecPrev string |
| 22 |
etcdExecLatest string |
| 23 |
|
| 24 |
etcdctlExecPrev string |
| 25 |
etcdctlExecLatest string |
| 26 |
|
| 27 |
defaultConfig = etcdProcessClusterConfig{ |
| 28 |
execPath: "", |
| 29 |
keepDataDir: false, |
| 30 |
clusterSize: 5, |
| 31 |
initialToken: "test-token", |
| 32 |
} |
| 33 |
) |
| 34 |
|
| 35 |
func init() { |
| 36 |
versions := []string{} |
| 37 |
vs := os.Getenv("ETCD_VERSIONS") |
| 38 |
for _, ver := range strings.Split(vs, " ") { |
| 39 |
versions = append(versions, ver) |
| 40 |
} |
| 41 |
if len(versions) != 2 { |
| 42 |
log.Fatalf("expected 2 etcd versions, got %+v", versions) |
| 43 |
} |
| 44 |
|
| 45 |
etcdExecPrev = fmt.Sprintf("../bin/etcd-%s", versions[0]) |
| 46 |
etcdExecLatest = fmt.Sprintf("../bin/etcd-%s", versions[1]) |
| 47 |
|
| 48 |
etcdctlExecPrev = fmt.Sprintf("../bin/etcdctl-%s", versions[0]) |
| 49 |
etcdctlExecLatest = fmt.Sprintf("../bin/etcdctl-%s", versions[1]) |
| 50 |
|
| 51 |
if !fileutil.Exist(etcdExecPrev) { |
| 52 |
log.Fatalf("could not find %q", etcdExecPrev) |
| 53 |
} |
| 54 |
if !fileutil.Exist(etcdExecLatest) { |
| 55 |
log.Fatalf("could not find %q", etcdExecLatest) |
| 56 |
} |
| 57 |
if !fileutil.Exist(etcdctlExecPrev) { |
| 58 |
log.Fatalf("could not find %q", etcdctlExecPrev) |
| 59 |
} |
| 60 |
if !fileutil.Exist(etcdctlExecLatest) { |
| 61 |
log.Fatalf("could not find %q", etcdctlExecLatest) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func tokenFunc(txt string) bool { |
| 66 |
if !strings.HasPrefix(txt, "http://localhost:") { |
| 67 |
return false |
| 68 |
} |
| 69 |
token := path.Base(txt) |
| 70 |
return isAlphanumeric(token) |
| 71 |
} |
| 72 |
|
| 73 |
func TestE2E_size_1_no_discovery(t *testing.T) { testE2E(t, 1, false) } |
| 74 |
func TestE2E_size_3_no_discovery(t *testing.T) { testE2E(t, 3, false) } |
| 75 |
func TestE2E_size_5_no_discovery(t *testing.T) { testE2E(t, 5, false) } |
| 76 |
func TestE2E_size_7_no_discovery(t *testing.T) { testE2E(t, 7, false) } |
| 77 |
func TestE2E_size_1_discovery(t *testing.T) { testE2E(t, 1, true) } |
| 78 |
func TestE2E_size_3_discovery(t *testing.T) { testE2E(t, 3, true) } |
| 79 |
func TestE2E_size_5_discovery(t *testing.T) { testE2E(t, 5, true) } |
| 80 |
func TestE2E_size_7_discovery(t *testing.T) { testE2E(t, 7, true) } |
| 81 |
func testE2E(t *testing.T, size int, useDiscovery bool) { |
| 82 |
cfg := defaultConfig |
| 83 |
cfg.execPath = etcdExecPrev |
| 84 |
cfg.clusterSize = size |
| 85 |
|
| 86 |
etcdClus1, err := cfg.NewEtcdProcessCluster() |
| 87 |
if err != nil { |
| 88 |
t.Fatal(err) |
| 89 |
} |
| 90 |
if err = etcdClus1.Start(); err != nil { |
| 91 |
t.Fatal(err) |
| 92 |
} |
| 93 |
defer func() { |
| 94 |
if err = etcdClus1.Stop(10 * time.Second); err != nil { |
| 95 |
t.Fatal(err) |
| 96 |
} |
| 97 |
}() |
| 98 |
|
| 99 |
etcdServer := etcdClus1 |
| 100 |
if useDiscovery { |
| 101 |
|
| 102 |
procs := make([]*discoveryProcess, size) |
| 103 |
errc := make(chan error) |
| 104 |
for i := 0; i < size; i++ { |
| 105 |
dcfg := discoveryProcessConfig{ |
| 106 |
execPath: discoveryExec, |
| 107 |
etcdEp: etcdClus1.procs[i].cfg.curl.String(), |
| 108 |
} |
| 109 |
procs[i] = dcfg.NewDiscoveryProcess() |
| 110 |
go func(dp *discoveryProcess) { |
| 111 |
errc <- dp.Start() |
| 112 |
}(procs[i]) |
| 113 |
} |
| 114 |
for i := 0; i < size; i++ { |
| 115 |
if err = <-errc; err != nil { |
| 116 |
t.Fatal(err) |
| 117 |
} |
| 118 |
} |
| 119 |
req := cURLReq{ |
| 120 |
timeout: 5 * time.Second, |
| 121 |
endpoint: fmt.Sprintf("http://localhost:%d/new?size=%d", procs[0].cfg.webPort, size), |
| 122 |
method: http.MethodGet, |
| 123 |
expFunc: tokenFunc, |
| 124 |
} |
| 125 |
var token string |
| 126 |
token, err = req.Send() |
| 127 |
if err != nil { |
| 128 |
t.Fatal(err) |
| 129 |
} |
| 130 |
if !tokenFunc(token) { |
| 131 |
t.Fatalf("unexpected token %q", token) |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
etcdCfg := defaultConfig |
| 136 |
etcdCfg.execPath = etcdExecPrev |
| 137 |
etcdCfg.clusterSize = size |
| 138 |
etcdCfg.discoveryToken = token |
| 139 |
etcdClus2, err := etcdCfg.NewEtcdProcessCluster() |
| 140 |
if err != nil { |
| 141 |
t.Fatal(err) |
| 142 |
} |
| 143 |
if err = etcdClus2.Start(); err != nil { |
| 144 |
t.Fatal(err) |
| 145 |
} |
| 146 |
defer func() { |
| 147 |
if err = etcdClus2.Stop(10 * time.Second); err != nil { |
| 148 |
t.Fatal(err) |
| 149 |
} |
| 150 |
}() |
| 151 |
|
| 152 |
|
| 153 |
ctlv2Discovery := etcdctlCtl{ |
| 154 |
api: 2, |
| 155 |
execPath: etcdctlExecLatest, |
| 156 |
endpoints: etcdClus1.ClientEndpoints(), |
| 157 |
dialTimeout: 7 * time.Second, |
| 158 |
} |
| 159 |
if err = ctlv2Discovery.Get("l", KV{ |
| 160 |
Key: path.Join("_etcd", "registry", path.Base(token), "_config", "size"), |
| 161 |
Val: strconv.Itoa(size), |
| 162 |
}); err != nil { |
| 163 |
t.Fatal(err) |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
var resp *http.Response |
| 168 |
resp, err = http.Get(token) |
| 169 |
if err != nil { |
| 170 |
t.Fatal(err) |
| 171 |
} |
| 172 |
defer resp.Body.Close() |
| 173 |
|
| 174 |
var cresp client.Response |
| 175 |
if err = json.NewDecoder(resp.Body).Decode(&cresp); err != nil { |
| 176 |
t.Fatal(err) |
| 177 |
} |
| 178 |
if len(cresp.Node.Nodes) != size { |
| 179 |
t.Fatalf("len(cresp.Node.Nodes) expected %d, got %d", size, len(cresp.Node.Nodes)) |
| 180 |
} |
| 181 |
|
| 182 |
etcdServer = etcdClus2 |
| 183 |
} |
| 184 |
|
| 185 |
ctlv2 := etcdctlCtl{ |
| 186 |
api: 2, |
| 187 |
execPath: etcdctlExecLatest, |
| 188 |
endpoints: etcdServer.ClientEndpoints(), |
| 189 |
dialTimeout: 7 * time.Second, |
| 190 |
} |
| 191 |
ctlv3 := ctlv2 |
| 192 |
ctlv3.api = 3 |
| 193 |
kvs := []KV{} |
| 194 |
for i := 0; i < 5; i++ { |
| 195 |
kvs = append(kvs, KV{Key: fmt.Sprintf("foo%d", i), Val: fmt.Sprintf("var%d", i)}) |
| 196 |
} |
| 197 |
for _, kv := range kvs { |
| 198 |
if err = ctlv2.Put(kv.Key, kv.Val); err != nil { |
| 199 |
t.Fatal(err) |
| 200 |
} |
| 201 |
if err = ctlv3.Put(kv.Key, kv.Val); err != nil { |
| 202 |
t.Fatal(err) |
| 203 |
} |
| 204 |
} |
| 205 |
time.Sleep(3 * time.Second) |
| 206 |
for _, kv := range kvs { |
| 207 |
if err = ctlv2.Get("l", kv); err != nil { |
| 208 |
t.Fatal(err) |
| 209 |
} |
| 210 |
if err = ctlv3.Get("l", kv); err != nil { |
| 211 |
t.Fatal(err) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
for i := 0; i < etcdServer.cfg.clusterSize; i++ { |
| 217 |
if err = etcdServer.procs[i].Stop(); err != nil { |
| 218 |
t.Fatal(err) |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
etcdServer.procs[i].cfg.execPath = etcdExecLatest |
| 223 |
child, err := expect.NewExpect(etcdServer.procs[i].cfg.execPath, etcdServer.procs[i].cfg.args...) |
| 224 |
if err != nil { |
| 225 |
t.Fatal(err) |
| 226 |
} |
| 227 |
etcdServer.procs[i].proc = child |
| 228 |
etcdServer.procs[i].donec = make(chan struct{}) |
| 229 |
if err = etcdServer.procs[i].waitReady(); err != nil { |
| 230 |
t.Fatal(err) |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
ctlv2 := etcdctlCtl{ |
| 235 |
api: 2, |
| 236 |
execPath: etcdctlExecLatest, |
| 237 |
endpoints: []string{etcdServer.procs[i].cfg.curl.String()}, |
| 238 |
dialTimeout: 7 * time.Second, |
| 239 |
} |
| 240 |
ctlv3 := ctlv2 |
| 241 |
ctlv3.api = 3 |
| 242 |
for _, kv := range kvs { |
| 243 |
if err = ctlv2.Get("s", kv); err != nil { |
| 244 |
t.Fatal(err) |
| 245 |
} |
| 246 |
if err = ctlv3.Get("s", kv); err != nil { |
| 247 |
t.Fatal(err) |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
} |