2016-05-13 06:50:58 +03:00
|
|
|
// Copyright 2016 The etcd Authors
|
2016-04-15 10:18:24 +03:00
|
|
|
//
|
|
|
|
// 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 clientv3_test
|
|
|
|
|
|
|
|
import (
|
2017-03-10 23:21:44 +03:00
|
|
|
"fmt"
|
2016-04-15 10:18:24 +03:00
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-03-10 23:21:44 +03:00
|
|
|
"time"
|
2016-04-15 10:18:24 +03:00
|
|
|
|
2016-07-07 09:32:46 +03:00
|
|
|
"github.com/coreos/etcd/auth"
|
2016-04-15 10:18:24 +03:00
|
|
|
"github.com/coreos/etcd/integration"
|
|
|
|
"github.com/coreos/etcd/pkg/testutil"
|
2016-07-07 09:32:46 +03:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2016-04-15 10:18:24 +03:00
|
|
|
)
|
|
|
|
|
2016-07-07 09:32:46 +03:00
|
|
|
func init() { auth.BcryptCost = bcrypt.MinCost }
|
|
|
|
|
2016-04-15 10:18:24 +03:00
|
|
|
// TestMain sets up an etcd cluster if running the examples.
|
|
|
|
func TestMain(m *testing.M) {
|
2017-05-30 22:23:12 +03:00
|
|
|
useCluster, hasRunArg := false, false // default to running only Test*
|
2016-04-15 10:18:24 +03:00
|
|
|
for _, arg := range os.Args {
|
|
|
|
if strings.HasPrefix(arg, "-test.run=") {
|
|
|
|
exp := strings.Split(arg, "=")[1]
|
|
|
|
match, err := regexp.MatchString(exp, "Example")
|
|
|
|
useCluster = (err == nil && match) || strings.Contains(exp, "Example")
|
2017-05-30 22:23:12 +03:00
|
|
|
hasRunArg = true
|
2016-04-15 10:18:24 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-05-30 22:23:12 +03:00
|
|
|
if !hasRunArg {
|
|
|
|
// force only running Test* if no args given to avoid leak false
|
|
|
|
// positives from having a long-running cluster for the examples.
|
|
|
|
os.Args = append(os.Args, "-test.run=Test")
|
|
|
|
}
|
2016-04-15 10:18:24 +03:00
|
|
|
|
|
|
|
v := 0
|
|
|
|
if useCluster {
|
|
|
|
cfg := integration.ClusterConfig{Size: 3}
|
|
|
|
clus := integration.NewClusterV3(nil, &cfg)
|
|
|
|
endpoints = make([]string, 3)
|
|
|
|
for i := range endpoints {
|
|
|
|
endpoints[i] = clus.Client(i).Endpoints()[0]
|
|
|
|
}
|
|
|
|
v = m.Run()
|
|
|
|
clus.Terminate(nil)
|
2017-03-10 23:21:44 +03:00
|
|
|
if err := testutil.CheckAfterTest(time.Second); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2016-04-15 10:18:24 +03:00
|
|
|
} else {
|
|
|
|
v = m.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
if v == 0 && testutil.CheckLeakedGoroutine() {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(v)
|
|
|
|
}
|