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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
package tester
import (
"fmt"
"os"
)
// EnvTest Environment variables manager for tests.
type EnvTest struct {
keys []string
values map[string]string
liveTestHook func() bool
liveTestExtraHook func() bool
domain string
domainKey string
}
// NewEnvTest Creates an EnvTest.
func NewEnvTest(keys ...string) *EnvTest {
values := make(map[string]string)
for _, key := range keys {
value := os.Getenv(key)
if value != "" {
values[key] = value
}
}
return &EnvTest{
keys: keys,
values: values,
}
}
// WithDomain Defines the name of the environment variable used to define the domain related to the DNS request.
// If the domain is defined, it was considered mandatory to define a test as a "live" test.
func (e *EnvTest) WithDomain(key string) *EnvTest {
e.domainKey = key
e.domain = os.Getenv(key)
return e
}
// WithLiveTestRequirements Defines the environment variables required to define a test as a "live" test.
// Replaces the default behavior (all keys are required).
func (e *EnvTest) WithLiveTestRequirements(keys ...string) *EnvTest {
var countValuedVars int
for _, key := range keys {
if e.domainKey != key && !e.isManagedKey(key) {
panic(fmt.Sprintf("Unauthorized action, the env var %s is not managed or it's not the key of the domain.", key))
}
if _, ok := e.values[key]; ok {
countValuedVars++
}
}
live := countValuedVars != 0 && len(keys) == countValuedVars
e.liveTestHook = func() bool {
return live
}
return e
}
// WithLiveTestExtra Allows to define an additional condition to flag a test as "live" test.
// This does not replace the default behavior.
func (e *EnvTest) WithLiveTestExtra(extra func() bool) *EnvTest {
e.liveTestExtraHook = extra
return e
}
// GetDomain Gets the domain value associated with the DNS challenge (linked to WithDomain method).
func (e *EnvTest) GetDomain() string {
return e.domain
}
// IsLiveTest Checks whether environment variables allow running a "live" test.
func (e *EnvTest) IsLiveTest() bool {
liveTest := e.liveTestExtra()
if e.liveTestHook != nil {
return liveTest && e.liveTestHook()
}
liveTest = liveTest && len(e.values) == len(e.keys)
if liveTest && e.domainKey != "" && e.domain == "" {
return false
}
return liveTest
}
// RestoreEnv Restores the environment variables to the initial state.
func (e *EnvTest) RestoreEnv() {
for key, value := range e.values {
os.Setenv(key, value)
}
}
// ClearEnv Deletes all environment variables related to the test.
func (e *EnvTest) ClearEnv() {
for _, key := range e.keys {
os.Unsetenv(key)
}
}
// GetValue Gets the stored value of an environment variable.
func (e *EnvTest) GetValue(key string) string {
return e.values[key]
}
func (e *EnvTest) liveTestExtra() bool {
if e.liveTestExtraHook == nil {
return true
}
return e.liveTestExtraHook()
}
// Apply Sets/Unsets environment variables.
// Not related to the main environment variables.
func (e *EnvTest) Apply(envVars map[string]string) {
for key, value := range envVars {
if !e.isManagedKey(key) {
panic(fmt.Sprintf("Unauthorized action, the env var %s is not managed.", key))
}
if value == "" {
os.Unsetenv(key)
} else {
os.Setenv(key, value)
}
}
}
func (e *EnvTest) isManagedKey(varName string) bool {
for _, key := range e.keys {
if key == varName {
return true
}
}
return false
}
|