-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain_test.go
53 lines (47 loc) · 1.1 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package hcat
import (
"flag"
"io/ioutil"
"log"
"os"
"testing"
"github.com/hashicorp/consul/sdk/testutil"
"github.com/hashicorp/hcat/internal/test"
)
var (
RunExamples = flag.Bool("egs", false, "Run example tests")
Consuladdr string
)
func TestMain(m *testing.M) {
flag.Parse()
cleanup := func() {}
if *RunExamples {
Consuladdr, cleanup = testConsulSetup()
}
retCode := m.Run()
cleanup() // can't defer w/ os.Exit
os.Exit(retCode)
}
// support for running consul as part of integration testing
func testConsulSetup() (string, func()) {
var err error
origStderr := os.Stderr
os.Stderr, err = os.OpenFile(os.DevNull, os.O_WRONLY, 0o666)
if err != nil {
os.Stderr = origStderr
}
tb := &test.TestingTB{}
consul, err := testutil.NewTestServerConfigT(tb,
func(c *testutil.TestServerConfig) {
c.LogLevel = "error"
c.Stdout = ioutil.Discard
c.Stderr = ioutil.Discard
})
if err != nil {
log.Fatalf("failed to start consul server: %v", err)
}
os.Stderr = origStderr
return consul.HTTPAddr, func() { consul.Stop() }
}