main_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package clientv3_test
  15. import (
  16. "os"
  17. "regexp"
  18. "strings"
  19. "testing"
  20. "github.com/coreos/etcd/auth"
  21. "github.com/coreos/etcd/integration"
  22. "github.com/coreos/etcd/pkg/testutil"
  23. "golang.org/x/crypto/bcrypt"
  24. )
  25. func init() { auth.BcryptCost = bcrypt.MinCost }
  26. // TestMain sets up an etcd cluster if running the examples.
  27. func TestMain(m *testing.M) {
  28. useCluster := true // default to running all tests
  29. for _, arg := range os.Args {
  30. if strings.HasPrefix(arg, "-test.run=") {
  31. exp := strings.Split(arg, "=")[1]
  32. match, err := regexp.MatchString(exp, "Example")
  33. useCluster = (err == nil && match) || strings.Contains(exp, "Example")
  34. break
  35. }
  36. }
  37. v := 0
  38. if useCluster {
  39. cfg := integration.ClusterConfig{Size: 3}
  40. clus := integration.NewClusterV3(nil, &cfg)
  41. endpoints = make([]string, 3)
  42. for i := range endpoints {
  43. endpoints[i] = clus.Client(i).Endpoints()[0]
  44. }
  45. v = m.Run()
  46. clus.Terminate(nil)
  47. } else {
  48. v = m.Run()
  49. }
  50. if v == 0 && testutil.CheckLeakedGoroutine() {
  51. os.Exit(1)
  52. }
  53. os.Exit(v)
  54. }