main_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2017 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 client_test
  15. import (
  16. "fmt"
  17. "net/http"
  18. "os"
  19. "regexp"
  20. "strings"
  21. "testing"
  22. "time"
  23. "go.etcd.io/etcd/integration"
  24. "go.etcd.io/etcd/pkg/testutil"
  25. "go.etcd.io/etcd/pkg/transport"
  26. )
  27. var exampleEndpoints []string
  28. var exampleTransport *http.Transport
  29. // TestMain sets up an etcd cluster if running the examples.
  30. func TestMain(m *testing.M) {
  31. useCluster, hasRunArg := false, false // default to running only Test*
  32. for _, arg := range os.Args {
  33. if strings.HasPrefix(arg, "-test.run=") {
  34. exp := strings.Split(arg, "=")[1]
  35. match, err := regexp.MatchString(exp, "Example")
  36. useCluster = (err == nil && match) || strings.Contains(exp, "Example")
  37. hasRunArg = true
  38. break
  39. }
  40. }
  41. if !hasRunArg {
  42. // force only running Test* if no args given to avoid leak false
  43. // positives from having a long-running cluster for the examples.
  44. os.Args = append(os.Args, "-test.run=Test")
  45. }
  46. var v int
  47. if useCluster {
  48. tr, trerr := transport.NewTransport(transport.TLSInfo{}, time.Second)
  49. if trerr != nil {
  50. fmt.Fprintf(os.Stderr, "%v", trerr)
  51. os.Exit(1)
  52. }
  53. cfg := integration.ClusterConfig{Size: 1}
  54. clus := integration.NewClusterV3(nil, &cfg)
  55. exampleEndpoints = []string{clus.Members[0].URL()}
  56. exampleTransport = tr
  57. v = m.Run()
  58. clus.Terminate(nil)
  59. if err := testutil.CheckAfterTest(time.Second); err != nil {
  60. fmt.Fprintf(os.Stderr, "%v", err)
  61. os.Exit(1)
  62. }
  63. } else {
  64. v = m.Run()
  65. }
  66. if v == 0 && testutil.CheckLeakedGoroutine() {
  67. os.Exit(1)
  68. }
  69. os.Exit(v)
  70. }