main_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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, hasRunArg := false, false // default to running only Test*
  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. hasRunArg = true
  37. break
  38. }
  39. }
  40. if !hasRunArg {
  41. // force only running Test* if no args given to avoid leak false
  42. // positives from having a long-running cluster for the examples.
  43. os.Args = append(os.Args, "-test.run=Test")
  44. }
  45. var v int
  46. if useCluster {
  47. cfg := integration.ClusterConfig{Size: 3}
  48. clus := integration.NewClusterV3(nil, &cfg)
  49. endpoints = make([]string, 3)
  50. for i := range endpoints {
  51. endpoints[i] = clus.Client(i).Endpoints()[0]
  52. }
  53. v = m.Run()
  54. clus.Terminate(nil)
  55. if err := testutil.CheckAfterTest(time.Second); err != nil {
  56. fmt.Fprintf(os.Stderr, "%v", err)
  57. os.Exit(1)
  58. }
  59. } else {
  60. v = m.Run()
  61. }
  62. if v == 0 && testutil.CheckLeakedGoroutine() {
  63. os.Exit(1)
  64. }
  65. os.Exit(v)
  66. }