-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #4836.
- Loading branch information
Showing
5 changed files
with
182 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// Copyright 2016 CoreOS, Inc. | ||
// | ||
// 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 e2e | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/coreos/etcd/pkg/fileutil" | ||
"github.com/coreos/etcd/pkg/testutil" | ||
) | ||
|
||
func TestCtlV3SetQuorum(t *testing.T) { | ||
testCtlV3Set(t, &configNoTLS, 3*time.Second, true) | ||
} | ||
|
||
func TestCtlV3SetQuorumZeroTimeout(t *testing.T) { | ||
testCtlV3Set(t, &configNoTLS, 0, true) | ||
} | ||
|
||
func TestCtlV3SetQuorumTimeout(t *testing.T) { | ||
testCtlV3Set(t, &configNoTLS, time.Nanosecond, true) | ||
} | ||
|
||
func TestCtlV3SetPeerTLSQuorum(t *testing.T) { | ||
testCtlV3Set(t, &configPeerTLS, 3*time.Second, true) | ||
} | ||
|
||
func testCtlV3Set(t *testing.T, cfg *etcdProcessClusterConfig, dialTimeout time.Duration, quorum bool) { | ||
defer testutil.AfterTest(t) | ||
|
||
epc := setupCtlV3Test(t, cfg, quorum) | ||
defer func() { | ||
if errC := epc.Close(); errC != nil { | ||
t.Fatalf("error closing etcd processes (%v)", errC) | ||
} | ||
}() | ||
|
||
key, value := "foo", "bar" | ||
|
||
donec := make(chan struct{}) | ||
go func() { | ||
if err := ctlV3Put(epc, key, value, dialTimeout); err != nil { | ||
if dialTimeout > 0 && dialTimeout <= time.Nanosecond && isGRPCTimedout(err) { // timeout expected | ||
donec <- struct{}{} | ||
return | ||
} | ||
t.Fatalf("put error (%v)", err) | ||
} | ||
if err := ctlV3Get(epc, key, value, dialTimeout, quorum); err != nil { | ||
if dialTimeout > 0 && dialTimeout <= time.Nanosecond && isGRPCTimedout(err) { // timeout expected | ||
donec <- struct{}{} | ||
return | ||
} | ||
t.Fatalf("get error (%v)", err) | ||
} | ||
donec <- struct{}{} | ||
}() | ||
|
||
select { | ||
case <-time.After(2*dialTimeout + time.Second): | ||
if dialTimeout > 0 { | ||
t.Fatalf("test timed out for %v", dialTimeout) | ||
} | ||
case <-donec: | ||
} | ||
} | ||
|
||
func ctlV3PrefixArgs(clus *etcdProcessCluster, dialTimeout time.Duration) []string { | ||
if len(clus.proxies()) > 0 { // TODO: add proxy check as in v2 | ||
panic("v3 proxy not implemented") | ||
} | ||
|
||
endpoints := "" | ||
if backends := clus.backends(); len(backends) != 0 { | ||
es := []string{} | ||
for _, b := range backends { | ||
es = append(es, stripSchema(b.cfg.acurl.String())) | ||
} | ||
endpoints = strings.Join(es, ",") | ||
} | ||
cmdArgs := []string{"../bin/etcdctlv3", "--endpoints", endpoints, "--dial-timeout", dialTimeout.String()} | ||
if clus.cfg.isClientTLS { | ||
cmdArgs = append(cmdArgs, "--cacert", caPath, "--cert", certPath, "--key", privateKeyPath) | ||
} | ||
return cmdArgs | ||
} | ||
|
||
func ctlV3Put(clus *etcdProcessCluster, key, value string, dialTimeout time.Duration) error { | ||
cmdArgs := append(ctlV3PrefixArgs(clus, dialTimeout), "put", key, value) | ||
return spawnWithExpectedString(cmdArgs, "OK") | ||
} | ||
|
||
func ctlV3Get(clus *etcdProcessCluster, key, value string, dialTimeout time.Duration, quorum bool) error { | ||
if !quorum { // TODO: add serialized option | ||
panic("serialized option is not implemented") | ||
} | ||
|
||
cmdArgs := append(ctlV3PrefixArgs(clus, dialTimeout), "get", key) | ||
|
||
// TODO: match by value. Currently it prints out both key and value in multi-lines. | ||
return spawnWithExpectedString(cmdArgs, key) | ||
} | ||
|
||
func mustCtlV3(t *testing.T) { | ||
if !fileutil.Exist("../bin/etcdctlv3") { | ||
t.Fatalf("could not find etcdctlv3 binary") | ||
} | ||
} | ||
|
||
func setupCtlV3Test(t *testing.T, cfg *etcdProcessClusterConfig, quorum bool) *etcdProcessCluster { | ||
if !quorum { // TODO: add serialized option | ||
panic("serialized option is not implemented") | ||
} | ||
|
||
mustCtlV3(t) | ||
if !quorum { | ||
cfg = configStandalone(*cfg) | ||
} | ||
cfg.isV3 = true | ||
epc, err := newEtcdProcessCluster(cfg) | ||
if err != nil { | ||
t.Fatalf("could not start etcd process cluster (%v)", err) | ||
} | ||
return epc | ||
} | ||
|
||
func isGRPCTimedout(err error) bool { | ||
return strings.Contains(err.Error(), "grpc: timed out trying to connect") | ||
} | ||
|
||
func stripSchema(s string) string { | ||
if strings.HasPrefix(s, "http://") { | ||
s = strings.Replace(s, "http://", "", -1) | ||
} | ||
if strings.HasPrefix(s, "https://") { | ||
s = strings.Replace(s, "https://", "", -1) | ||
} | ||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters