2016-07-12 03:02:49 +03:00
|
|
|
// Copyright 2016 The etcd Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2017-07-15 00:33:48 +03:00
|
|
|
"context"
|
2016-07-12 03:02:49 +03:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2017-03-16 05:31:10 +03:00
|
|
|
"path/filepath"
|
2016-07-12 03:02:49 +03:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-07-15 00:33:48 +03:00
|
|
|
"time"
|
2016-07-12 03:02:49 +03:00
|
|
|
|
2017-07-15 00:33:48 +03:00
|
|
|
"github.com/coreos/etcd/clientv3"
|
2016-07-12 03:02:49 +03:00
|
|
|
"github.com/coreos/etcd/embed"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEmbedEtcd(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
cfg embed.Config
|
|
|
|
|
|
|
|
werr string
|
|
|
|
wpeers int
|
|
|
|
wclients int
|
|
|
|
}{
|
|
|
|
{werr: "multiple discovery"},
|
|
|
|
{werr: "advertise-client-urls is required"},
|
|
|
|
{werr: "should be at least"},
|
|
|
|
{werr: "is too long"},
|
|
|
|
{wpeers: 1, wclients: 1},
|
|
|
|
{wpeers: 2, wclients: 1},
|
|
|
|
{wpeers: 1, wclients: 2},
|
2017-01-26 06:22:12 +03:00
|
|
|
{werr: "expected IP"},
|
|
|
|
{werr: "expected IP"},
|
2016-07-12 03:02:49 +03:00
|
|
|
}
|
|
|
|
|
2017-12-07 22:32:45 +03:00
|
|
|
urls := newEmbedURLs(false, 10)
|
2016-07-12 03:02:49 +03:00
|
|
|
|
|
|
|
// setup defaults
|
|
|
|
for i := range tests {
|
|
|
|
tests[i].cfg = *embed.NewConfig()
|
2018-04-17 00:22:29 +03:00
|
|
|
tests[i].cfg.Logger = "zap"
|
2018-04-25 23:23:38 +03:00
|
|
|
tests[i].cfg.LogOutputs = []string{"/dev/null"}
|
2018-04-17 00:22:29 +03:00
|
|
|
tests[i].cfg.Debug = false
|
2016-07-12 03:02:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tests[0].cfg.Durl = "abc"
|
2016-07-16 02:47:32 +03:00
|
|
|
setupEmbedCfg(&tests[1].cfg, []url.URL{urls[0]}, []url.URL{urls[1]})
|
2016-07-12 03:02:49 +03:00
|
|
|
tests[1].cfg.ACUrls = nil
|
|
|
|
tests[2].cfg.TickMs = tests[2].cfg.ElectionMs - 1
|
|
|
|
tests[3].cfg.ElectionMs = 999999
|
2016-07-16 02:47:32 +03:00
|
|
|
setupEmbedCfg(&tests[4].cfg, []url.URL{urls[2]}, []url.URL{urls[3]})
|
|
|
|
setupEmbedCfg(&tests[5].cfg, []url.URL{urls[4]}, []url.URL{urls[5], urls[6]})
|
|
|
|
setupEmbedCfg(&tests[6].cfg, []url.URL{urls[7], urls[8]}, []url.URL{urls[9]})
|
2016-07-12 03:02:49 +03:00
|
|
|
|
2017-01-26 06:22:12 +03:00
|
|
|
dnsURL, _ := url.Parse("http://whatever.test:12345")
|
|
|
|
tests[7].cfg.LCUrls = []url.URL{*dnsURL}
|
|
|
|
tests[8].cfg.LPUrls = []url.URL{*dnsURL}
|
|
|
|
|
2017-03-16 05:31:10 +03:00
|
|
|
dir := filepath.Join(os.TempDir(), fmt.Sprintf("embed-etcd"))
|
2016-07-12 03:02:49 +03:00
|
|
|
os.RemoveAll(dir)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
tests[i].cfg.Dir = dir
|
|
|
|
e, err := embed.StartEtcd(&tests[i].cfg)
|
2016-08-29 09:04:06 +03:00
|
|
|
if e != nil {
|
|
|
|
<-e.Server.ReadyNotify() // wait for e.Server to join the cluster
|
|
|
|
}
|
2016-07-12 03:02:49 +03:00
|
|
|
if tt.werr != "" {
|
|
|
|
if err == nil || !strings.Contains(err.Error(), tt.werr) {
|
|
|
|
t.Errorf("%d: expected error with %q, got %v", i, tt.werr, err)
|
|
|
|
}
|
|
|
|
if e != nil {
|
|
|
|
e.Close()
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%d: expected success, got error %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(e.Peers) != tt.wpeers {
|
|
|
|
t.Errorf("%d: expected %d peers, got %d", i, tt.wpeers, len(e.Peers))
|
|
|
|
}
|
|
|
|
if len(e.Clients) != tt.wclients {
|
2017-01-27 00:57:58 +03:00
|
|
|
t.Errorf("%d: expected %d clients, got %d", i, tt.wclients, len(e.Clients))
|
2016-07-12 03:02:49 +03:00
|
|
|
}
|
|
|
|
e.Close()
|
2017-03-17 20:15:34 +03:00
|
|
|
select {
|
|
|
|
case err := <-e.Err():
|
|
|
|
t.Errorf("#%d: unexpected error on close (%v)", i, err)
|
|
|
|
default:
|
|
|
|
}
|
2016-07-12 03:02:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 22:32:45 +03:00
|
|
|
func TestEmbedEtcdGracefulStopSecure(t *testing.T) { testEmbedEtcdGracefulStop(t, true) }
|
|
|
|
func TestEmbedEtcdGracefulStopInsecure(t *testing.T) { testEmbedEtcdGracefulStop(t, false) }
|
|
|
|
|
|
|
|
// testEmbedEtcdGracefulStop ensures embedded server stops
|
2017-07-15 00:33:48 +03:00
|
|
|
// cutting existing transports.
|
2017-12-07 22:32:45 +03:00
|
|
|
func testEmbedEtcdGracefulStop(t *testing.T, secure bool) {
|
2017-07-15 00:33:48 +03:00
|
|
|
cfg := embed.NewConfig()
|
2017-12-07 22:32:45 +03:00
|
|
|
if secure {
|
|
|
|
cfg.ClientTLSInfo = testTLSInfo
|
|
|
|
cfg.PeerTLSInfo = testTLSInfo
|
|
|
|
}
|
2017-07-15 00:33:48 +03:00
|
|
|
|
2017-12-07 22:32:45 +03:00
|
|
|
urls := newEmbedURLs(secure, 2)
|
2017-07-15 00:33:48 +03:00
|
|
|
setupEmbedCfg(cfg, []url.URL{urls[0]}, []url.URL{urls[1]})
|
|
|
|
|
|
|
|
cfg.Dir = filepath.Join(os.TempDir(), fmt.Sprintf("embed-etcd"))
|
|
|
|
os.RemoveAll(cfg.Dir)
|
|
|
|
defer os.RemoveAll(cfg.Dir)
|
|
|
|
|
|
|
|
e, err := embed.StartEtcd(cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
<-e.Server.ReadyNotify() // wait for e.Server to join the cluster
|
|
|
|
|
2017-12-07 22:32:45 +03:00
|
|
|
clientCfg := clientv3.Config{
|
|
|
|
Endpoints: []string{urls[0].String()},
|
|
|
|
}
|
|
|
|
if secure {
|
|
|
|
clientCfg.TLS, err = testTLSInfo.ClientConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cli, err := clientv3.New(clientCfg)
|
2017-07-15 00:33:48 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
// open watch connection
|
|
|
|
cli.Watch(context.Background(), "foo")
|
|
|
|
|
|
|
|
donec := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
e.Close()
|
|
|
|
close(donec)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case err := <-e.Err():
|
|
|
|
t.Fatal(err)
|
|
|
|
case <-donec:
|
|
|
|
case <-time.After(2*time.Second + e.Server.Cfg.ReqTimeout()):
|
|
|
|
t.Fatalf("took too long to close server")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 22:32:45 +03:00
|
|
|
func newEmbedURLs(secure bool, n int) (urls []url.URL) {
|
|
|
|
scheme := "unix"
|
|
|
|
if secure {
|
|
|
|
scheme = "unixs"
|
|
|
|
}
|
2016-07-16 02:47:32 +03:00
|
|
|
for i := 0; i < n; i++ {
|
2017-12-07 22:32:45 +03:00
|
|
|
u, _ := url.Parse(fmt.Sprintf("%s://localhost:%d%06d", scheme, os.Getpid(), i))
|
2016-07-16 02:47:32 +03:00
|
|
|
urls = append(urls, *u)
|
|
|
|
}
|
2017-11-11 05:28:57 +03:00
|
|
|
return urls
|
2016-07-16 02:47:32 +03:00
|
|
|
}
|
|
|
|
|
2016-07-12 03:02:49 +03:00
|
|
|
func setupEmbedCfg(cfg *embed.Config, curls []url.URL, purls []url.URL) {
|
2018-04-17 00:22:29 +03:00
|
|
|
cfg.Logger = "zap"
|
2018-04-25 23:23:38 +03:00
|
|
|
cfg.LogOutputs = []string{"/dev/null"}
|
2018-04-17 00:22:29 +03:00
|
|
|
cfg.Debug = false
|
|
|
|
|
2016-07-12 03:02:49 +03:00
|
|
|
cfg.ClusterState = "new"
|
|
|
|
cfg.LCUrls, cfg.ACUrls = curls, curls
|
|
|
|
cfg.LPUrls, cfg.APUrls = purls, purls
|
|
|
|
cfg.InitialCluster = ""
|
|
|
|
for i := range purls {
|
|
|
|
cfg.InitialCluster += ",default=" + purls[i].String()
|
|
|
|
}
|
|
|
|
cfg.InitialCluster = cfg.InitialCluster[1:]
|
|
|
|
}
|