main_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. "fmt"
  17. "os"
  18. "regexp"
  19. "strings"
  20. "testing"
  21. "time"
  22. "github.com/coreos/etcd/auth"
  23. "github.com/coreos/etcd/integration"
  24. "github.com/coreos/etcd/pkg/testutil"
  25. "golang.org/x/crypto/bcrypt"
  26. )
  27. func init() { auth.BcryptCost = bcrypt.MinCost }
  28. // TestMain sets up an etcd cluster if running the examples.
  29. func TestMain(m *testing.M) {
  30. useCluster := true // default to running all tests
  31. for _, arg := range os.Args {
  32. if strings.HasPrefix(arg, "-test.run=") {
  33. exp := strings.Split(arg, "=")[1]
  34. match, err := regexp.MatchString(exp, "Example")
  35. useCluster = (err == nil && match) || strings.Contains(exp, "Example")
  36. break
  37. }
  38. }
  39. v := 0
  40. if useCluster {
  41. cfg := integration.ClusterConfig{Size: 3}
  42. clus := integration.NewClusterV3(nil, &cfg)
  43. endpoints = make([]string, 3)
  44. for i := range endpoints {
  45. endpoints[i] = clus.Client(i).Endpoints()[0]
  46. }
  47. v = m.Run()
  48. clus.Terminate(nil)
  49. if err := testutil.CheckAfterTest(time.Second); err != nil {
  50. fmt.Fprintf(os.Stderr, "%v", err)
  51. os.Exit(1)
  52. }
  53. } else {
  54. v = m.Run()
  55. }
  56. if v == 0 && testutil.CheckLeakedGoroutine() {
  57. os.Exit(1)
  58. }
  59. os.Exit(v)
  60. }