-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.go
61 lines (49 loc) · 1.29 KB
/
config.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
54
55
56
57
58
59
60
61
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package testutil
import (
"encoding/json"
"os"
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
const (
configEnvVar = "CONSUL_ECS_CONFIG_JSON"
)
// TempDir creates a temporary directory. A test cleanup removes the directory
// and its contents.
func TempDir(t *testing.T) string {
dir, err := os.MkdirTemp("", "")
require.NoError(t, err)
t.Cleanup(func() {
err := os.RemoveAll(dir)
if err != nil {
t.Logf("warning, failed to cleanup temp dir %s - %s", dir, err)
}
})
return dir
}
// SetECSConfigEnvVar the CONSUL_ECS_CONFIG_JSON environment variable
// to the JSON string of the provided value, with a test cleanup.
func SetECSConfigEnvVar(t *testing.T, val interface{}) {
configBytes, err := json.MarshalIndent(val, "", " ")
require.NoError(t, err)
t.Setenv(configEnvVar, string(configBytes))
t.Logf("%s=%s", configEnvVar, os.Getenv(configEnvVar))
}
// EnterpriseFlag indicates whether or not the test was invoked with the -enterprise
// command line argument.
func EnterpriseFlag() bool {
re := regexp.MustCompile("^-+enterprise$")
for _, a := range os.Args {
if re.Match([]byte(strings.ToLower(a))) {
return true
}
}
return false
}
func BoolPtr(v bool) *bool {
return &v
}