-
-
Notifications
You must be signed in to change notification settings - Fork 522
/
Copy pathconfig.go
116 lines (94 loc) · 3.51 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package config
import (
"fmt"
"os"
"path/filepath"
"strconv"
"sync"
"time"
"github.com/magiconair/properties"
)
const ReaperDefaultImage = "testcontainers/ryuk:0.7.0"
var (
tcConfig Config
tcConfigOnce *sync.Once = new(sync.Once)
)
// testcontainersConfig {
// Config represents the configuration for Testcontainers
type Config struct {
Host string `properties:"docker.host,default="`
TLSVerify int `properties:"docker.tls.verify,default=0"`
CertPath string `properties:"docker.cert.path,default="`
HubImageNamePrefix string `properties:"hub.image.name.prefix,default="`
RyukDisabled bool `properties:"ryuk.disabled,default=false"`
RyukPrivileged bool `properties:"ryuk.container.privileged,default=false"`
RyukReconnectionTimeout time.Duration `properties:"ryuk.reconnection.timeout,default=10s"`
RyukConnectionTimeout time.Duration `properties:"ryuk.connection.timeout,default=1m"`
RyukVerbose bool `properties:"ryuk.verbose,default=false"`
TestcontainersHost string `properties:"tc.host,default="`
}
// }
// Read reads from testcontainers properties file, if it exists
// it is possible that certain values get overridden when set as environment variables
func Read() Config {
tcConfigOnce.Do(func() {
tcConfig = read()
if tcConfig.RyukDisabled {
ryukDisabledMessage := `
**********************************************************************************************
Ryuk has been disabled for the current execution. This can cause unexpected behavior in your environment.
More on this: https://golang.testcontainers.org/features/garbage_collector/
**********************************************************************************************`
fmt.Println(ryukDisabledMessage)
}
})
return tcConfig
}
// Reset resets the singleton instance of the Config struct,
// allowing to read the configuration again.
// Handy for testing, so do not use it in production code
// This function is not thread-safe
func Reset() {
tcConfigOnce = new(sync.Once)
}
func read() Config {
config := Config{}
applyEnvironmentConfiguration := func(config Config) Config {
ryukDisabledEnv := os.Getenv("TESTCONTAINERS_RYUK_DISABLED")
if parseBool(ryukDisabledEnv) {
config.RyukDisabled = ryukDisabledEnv == "true"
}
hubImageNamePrefix := os.Getenv("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX")
if hubImageNamePrefix != "" {
config.HubImageNamePrefix = hubImageNamePrefix
}
ryukPrivilegedEnv := os.Getenv("TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED")
if parseBool(ryukPrivilegedEnv) {
config.RyukPrivileged = ryukPrivilegedEnv == "true"
}
ryukVerboseEnv := os.Getenv("TESTCONTAINERS_RYUK_VERBOSE")
if parseBool(ryukVerboseEnv) {
config.RyukVerbose = ryukVerboseEnv == "true"
}
return config
}
home, err := os.UserHomeDir()
if err != nil {
return applyEnvironmentConfiguration(config)
}
tcProp := filepath.Join(home, ".testcontainers.properties")
// init from a file
properties, err := properties.LoadFile(tcProp, properties.UTF8)
if err != nil {
return applyEnvironmentConfiguration(config)
}
if err := properties.Decode(&config); err != nil {
fmt.Printf("invalid testcontainers properties file, returning an empty Testcontainers configuration: %v\n", err)
return applyEnvironmentConfiguration(config)
}
return applyEnvironmentConfiguration(config)
}
func parseBool(input string) bool {
_, err := strconv.ParseBool(input)
return err == nil
}