main_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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/integration"
  21. "github.com/coreos/etcd/pkg/testutil"
  22. )
  23. // TestMain sets up an etcd cluster if running the examples.
  24. func TestMain(m *testing.M) {
  25. useCluster := true // default to running all tests
  26. for _, arg := range os.Args {
  27. if strings.HasPrefix(arg, "-test.run=") {
  28. exp := strings.Split(arg, "=")[1]
  29. match, err := regexp.MatchString(exp, "Example")
  30. useCluster = (err == nil && match) || strings.Contains(exp, "Example")
  31. break
  32. }
  33. }
  34. v := 0
  35. if useCluster {
  36. cfg := integration.ClusterConfig{Size: 3}
  37. clus := integration.NewClusterV3(nil, &cfg)
  38. endpoints = make([]string, 3)
  39. for i := range endpoints {
  40. endpoints[i] = clus.Client(i).Endpoints()[0]
  41. }
  42. v = m.Run()
  43. clus.Terminate(nil)
  44. } else {
  45. v = m.Run()
  46. }
  47. if v == 0 && testutil.CheckLeakedGoroutine() {
  48. os.Exit(1)
  49. }
  50. os.Exit(v)
  51. }