main_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. "go.etcd.io/etcd/integration"
  23. "go.etcd.io/etcd/pkg/testutil"
  24. )
  25. // TestMain sets up an etcd cluster if running the examples.
  26. func TestMain(m *testing.M) {
  27. useCluster, hasRunArg := false, false // default to running only Test*
  28. for _, arg := range os.Args {
  29. if strings.HasPrefix(arg, "-test.run=") {
  30. exp := strings.Split(arg, "=")[1]
  31. match, err := regexp.MatchString(exp, "Example")
  32. useCluster = (err == nil && match) || strings.Contains(exp, "Example")
  33. hasRunArg = true
  34. break
  35. }
  36. }
  37. if !hasRunArg {
  38. // force only running Test* if no args given to avoid leak false
  39. // positives from having a long-running cluster for the examples.
  40. os.Args = append(os.Args, "-test.run=Test")
  41. }
  42. var v int
  43. if useCluster {
  44. cfg := integration.ClusterConfig{Size: 3}
  45. clus := integration.NewClusterV3(nil, &cfg)
  46. endpoints = make([]string, 3)
  47. for i := range endpoints {
  48. endpoints[i] = clus.Client(i).Endpoints()[0]
  49. }
  50. v = m.Run()
  51. clus.Terminate(nil)
  52. if err := testutil.CheckAfterTest(time.Second); err != nil {
  53. fmt.Fprintf(os.Stderr, "%v", err)
  54. os.Exit(1)
  55. }
  56. } else {
  57. v = m.Run()
  58. }
  59. if v == 0 && testutil.CheckLeakedGoroutine() {
  60. os.Exit(1)
  61. }
  62. os.Exit(v)
  63. }