e2e: add an e2e test for TLS/non-TLS on the same port

This commit is contained in:
Xiang Li 2016-03-23 12:05:55 -07:00
parent 900a61b023
commit 9d55420a00
3 changed files with 69 additions and 21 deletions

View File

@ -63,6 +63,13 @@ var (
isPeerTLS: false, isPeerTLS: false,
initialToken: "new", initialToken: "new",
} }
configClientBoth = etcdProcessClusterConfig{
clusterSize: 1,
proxySize: 0,
isClientBoth: true,
isPeerTLS: false,
initialToken: "new",
}
configPeerTLS = etcdProcessClusterConfig{ configPeerTLS = etcdProcessClusterConfig{
clusterSize: 3, clusterSize: 3,
proxySize: 0, proxySize: 0,
@ -107,6 +114,7 @@ func TestBasicOpsClientTLS(t *testing.T) { testBasicOpsPutGet(t, &configClien
func TestBasicOpsProxyNoTLS(t *testing.T) { testBasicOpsPutGet(t, &configWithProxy) } func TestBasicOpsProxyNoTLS(t *testing.T) { testBasicOpsPutGet(t, &configWithProxy) }
func TestBasicOpsProxyTLS(t *testing.T) { testBasicOpsPutGet(t, &configWithProxyTLS) } func TestBasicOpsProxyTLS(t *testing.T) { testBasicOpsPutGet(t, &configWithProxyTLS) }
func TestBasicOpsProxyPeerTLS(t *testing.T) { testBasicOpsPutGet(t, &configWithProxyPeerTLS) } func TestBasicOpsProxyPeerTLS(t *testing.T) { testBasicOpsPutGet(t, &configWithProxyPeerTLS) }
func TestBasicOpsClientBoth(t *testing.T) { testBasicOpsPutGet(t, &configClientBoth) }
func testBasicOpsPutGet(t *testing.T, cfg *etcdProcessClusterConfig) { func testBasicOpsPutGet(t *testing.T, cfg *etcdProcessClusterConfig) {
defer testutil.AfterTest(t) defer testutil.AfterTest(t)
@ -126,14 +134,28 @@ func testBasicOpsPutGet(t *testing.T, cfg *etcdProcessClusterConfig) {
}() }()
expectPut := `{"action":"set","node":{"key":"/testKey","value":"foo","` expectPut := `{"action":"set","node":{"key":"/testKey","value":"foo","`
expectGet := `{"action":"get","node":{"key":"/testKey","value":"foo","`
if cfg.isClientBoth {
if err := cURLPut(epc, "testKey", "foo", expectPut); err != nil { if err := cURLPut(epc, "testKey", "foo", expectPut); err != nil {
t.Fatalf("failed put with curl (%v)", err) t.Fatalf("failed put with curl (%v)", err)
} }
expectGet := `{"action":"get","node":{"key":"/testKey","value":"foo","`
if err := cURLGet(epc, "testKey", expectGet); err != nil { if err := cURLGet(epc, "testKey", expectGet); err != nil {
t.Fatalf("failed get with curl (%v)", err) t.Fatalf("failed get with curl (%v)", err)
} }
if err := cURLGetUseTLS(epc, "testKey", expectGet); err != nil {
t.Fatalf("failed get with curl (%v)", err)
}
} else {
if err := cURLPut(epc, "testKey", "foo", expectPut); err != nil {
t.Fatalf("failed put with curl (%v)", err)
}
if err := cURLGet(epc, "testKey", expectGet); err != nil {
t.Fatalf("failed get with curl (%v)", err)
}
}
} }
// cURLPrefixArgs builds the beginning of a curl command for a given key // cURLPrefixArgs builds the beginning of a curl command for a given key
@ -143,8 +165,20 @@ func cURLPrefixArgs(clus *etcdProcessCluster, key string) []string {
if clus.cfg.isClientTLS { if clus.cfg.isClientTLS {
cmdArgs = append(cmdArgs, "--cacert", caPath, "--cert", certPath, "--key", privateKeyPath) cmdArgs = append(cmdArgs, "--cacert", caPath, "--cert", certPath, "--key", privateKeyPath)
} }
acurl := clus.procs[rand.Intn(clus.cfg.clusterSize)].cfg.acurl acurl := clus.procs[rand.Intn(clus.cfg.clusterSize)].cfg.acurl[0]
keyURL := acurl.String() + "/v2/keys/testKey" keyURL := acurl + "/v2/keys/testKey"
cmdArgs = append(cmdArgs, "-L", keyURL)
return cmdArgs
}
func cURLPrefixArgsUseTLS(clus *etcdProcessCluster, key string) []string {
cmdArgs := []string{"curl"}
if !clus.cfg.isClientBoth {
panic("should not use cURLPrefixArgsUseTLS when serving only TLS or non-TLS")
}
cmdArgs = append(cmdArgs, "--cacert", caPath, "--cert", certPath, "--key", privateKeyPath)
acurl := clus.procs[rand.Intn(clus.cfg.clusterSize)].cfg.acurl[1]
keyURL := acurl + "/v2/keys/testKey"
cmdArgs = append(cmdArgs, "-L", keyURL) cmdArgs = append(cmdArgs, "-L", keyURL)
return cmdArgs return cmdArgs
} }
@ -158,6 +192,10 @@ func cURLGet(clus *etcdProcessCluster, key, expected string) error {
return spawnWithExpectedString(cURLPrefixArgs(clus, key), expected) return spawnWithExpectedString(cURLPrefixArgs(clus, key), expected)
} }
func cURLGetUseTLS(clus *etcdProcessCluster, key, expected string) error {
return spawnWithExpectedString(cURLPrefixArgsUseTLS(clus, key), expected)
}
type etcdProcessCluster struct { type etcdProcessCluster struct {
cfg *etcdProcessClusterConfig cfg *etcdProcessClusterConfig
procs []*etcdProcess procs []*etcdProcess
@ -172,7 +210,7 @@ type etcdProcess struct {
type etcdProcessConfig struct { type etcdProcessConfig struct {
args []string args []string
dataDirPath string dataDirPath string
acurl url.URL acurl []string
isProxy bool isProxy bool
} }
@ -180,6 +218,8 @@ type etcdProcessClusterConfig struct {
clusterSize int clusterSize int
proxySize int proxySize int
isClientTLS bool isClientTLS bool
// serve both TLS and non-TLS
isClientBoth bool
isPeerTLS bool isPeerTLS bool
isPeerAutoTLS bool isPeerAutoTLS bool
initialToken string initialToken string
@ -265,8 +305,16 @@ func (cfg *etcdProcessClusterConfig) etcdProcessConfigs() []*etcdProcessConfig {
etcdCfgs := make([]*etcdProcessConfig, cfg.clusterSize+cfg.proxySize) etcdCfgs := make([]*etcdProcessConfig, cfg.clusterSize+cfg.proxySize)
initialCluster := make([]string, cfg.clusterSize) initialCluster := make([]string, cfg.clusterSize)
for i := 0; i < cfg.clusterSize; i++ { for i := 0; i < cfg.clusterSize; i++ {
var curlstrs []string
port := etcdProcessBasePort + 2*i port := etcdProcessBasePort + 2*i
if !cfg.isClientBoth {
curl := url.URL{Scheme: clientScheme, Host: fmt.Sprintf("localhost:%d", port)} curl := url.URL{Scheme: clientScheme, Host: fmt.Sprintf("localhost:%d", port)}
curlstrs = []string{curl.String()}
} else {
curl := url.URL{Scheme: "http", Host: fmt.Sprintf("localhost:%d", port)}
curltls := url.URL{Scheme: "https", Host: fmt.Sprintf("localhost:%d", port)}
curlstrs = []string{curl.String(), curltls.String()}
}
purl := url.URL{Scheme: peerScheme, Host: fmt.Sprintf("localhost:%d", port+1)} purl := url.URL{Scheme: peerScheme, Host: fmt.Sprintf("localhost:%d", port+1)}
name := fmt.Sprintf("testname%d", i) name := fmt.Sprintf("testname%d", i)
dataDirPath, derr := ioutil.TempDir("", name+".etcd") dataDirPath, derr := ioutil.TempDir("", name+".etcd")
@ -277,8 +325,8 @@ func (cfg *etcdProcessClusterConfig) etcdProcessConfigs() []*etcdProcessConfig {
args := []string{ args := []string{
"--name", name, "--name", name,
"--listen-client-urls", curl.String(), "--listen-client-urls", strings.Join(curlstrs, ","),
"--advertise-client-urls", curl.String(), "--advertise-client-urls", strings.Join(curlstrs, ","),
"--listen-peer-urls", purl.String(), "--listen-peer-urls", purl.String(),
"--initial-advertise-peer-urls", purl.String(), "--initial-advertise-peer-urls", purl.String(),
"--initial-cluster-token", cfg.initialToken, "--initial-cluster-token", cfg.initialToken,
@ -293,7 +341,7 @@ func (cfg *etcdProcessClusterConfig) etcdProcessConfigs() []*etcdProcessConfig {
etcdCfgs[i] = &etcdProcessConfig{ etcdCfgs[i] = &etcdProcessConfig{
args: args, args: args,
dataDirPath: dataDirPath, dataDirPath: dataDirPath,
acurl: curl, acurl: curlstrs,
} }
} }
for i := 0; i < cfg.proxySize; i++ { for i := 0; i < cfg.proxySize; i++ {
@ -314,7 +362,7 @@ func (cfg *etcdProcessClusterConfig) etcdProcessConfigs() []*etcdProcessConfig {
etcdCfgs[cfg.clusterSize+i] = &etcdProcessConfig{ etcdCfgs[cfg.clusterSize+i] = &etcdProcessConfig{
args: args, args: args,
dataDirPath: dataDirPath, dataDirPath: dataDirPath,
acurl: curl, acurl: []string{curl.String()},
isProxy: true, isProxy: true,
} }
} }
@ -328,7 +376,7 @@ func (cfg *etcdProcessClusterConfig) etcdProcessConfigs() []*etcdProcessConfig {
} }
func (cfg *etcdProcessClusterConfig) tlsArgs() (args []string) { func (cfg *etcdProcessClusterConfig) tlsArgs() (args []string) {
if cfg.isClientTLS { if cfg.isClientTLS || cfg.isClientBoth {
tlsClientArgs := []string{ tlsClientArgs := []string{
"--cert-file", certPath, "--cert-file", certPath,
"--key-file", privateKeyPath, "--key-file", privateKeyPath,

View File

@ -226,11 +226,11 @@ func TestCtlV2RoleList(t *testing.T) {
func etcdctlPrefixArgs(clus *etcdProcessCluster) []string { func etcdctlPrefixArgs(clus *etcdProcessCluster) []string {
endpoints := "" endpoints := ""
if proxies := clus.proxies(); len(proxies) != 0 { if proxies := clus.proxies(); len(proxies) != 0 {
endpoints = proxies[0].cfg.acurl.String() endpoints = proxies[0].cfg.acurl[0]
} else if backends := clus.backends(); len(backends) != 0 { } else if backends := clus.backends(); len(backends) != 0 {
es := []string{} es := []string{}
for _, b := range backends { for _, b := range backends {
es = append(es, b.cfg.acurl.String()) es = append(es, b.cfg.acurl[0])
} }
endpoints = strings.Join(es, ",") endpoints = strings.Join(es, ",")
} }

View File

@ -104,7 +104,7 @@ func ctlV3PrefixArgs(clus *etcdProcessCluster, dialTimeout time.Duration) []stri
if backends := clus.backends(); len(backends) != 0 { if backends := clus.backends(); len(backends) != 0 {
es := []string{} es := []string{}
for _, b := range backends { for _, b := range backends {
es = append(es, stripSchema(b.cfg.acurl.String())) es = append(es, stripSchema(b.cfg.acurl[0]))
} }
endpoints = strings.Join(es, ",") endpoints = strings.Join(es, ",")
} }