-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhostvars_test.go
158 lines (135 loc) · 3.45 KB
/
hostvars_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
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
148
149
150
151
152
153
154
155
156
157
158
package ansible
import (
"errors"
"fmt"
"os"
"strings"
"testing"
yaml "gopkg.in/yaml.v3"
)
func TestNewHostVarsFile(t *testing.T) {
expected := readVarsYml(t, "testdata/host_vars/setup.host/vars.yml")
expected["__cache_domain"] = "setup.host"
expected["__cache_base"] = ""
actual, err := NewHostVarsFile("testdata/host_vars/setup.host/vars.yml")
if err != nil {
t.Error("file parsing failed", err)
}
if actual == nil {
t.Error("parsed vars.yml is nil")
}
t.Log("actual:")
for k, v := range actual {
t.Log(k, "=", v)
}
if len(expected) != len(actual) {
t.Error("length is not expected. Expected:", len(expected), "actual:", len(actual))
}
for k, v := range expected {
equalRecursive(t, v, actual[k])
}
}
func TestNewHostVars_NoExists(t *testing.T) {
_, err := NewHostVarsFile("testdata/not-exists")
if err == nil {
t.Error("error is nil")
}
if !errors.Is(err, os.ErrNotExist) {
t.Error("error is not os.ErrNotExists, it is", err)
}
}
func TestHostVarsHasTODOs(t *testing.T) {
tests := map[bool]string{
true: "ToDo",
false: "any value",
}
for expected, input := range tests {
t.Run(input, func(t *testing.T) {
hv := HostVars{"key": input}
actual := hv.HasTODOs()
if expected != actual {
t.Error("value is invalid. Expected: '" + fmt.Sprintf("%t", expected) + "', actual: '" + fmt.Sprintf("%t", actual) + "'")
}
})
}
}
func TestString(t *testing.T) {
tests := map[string]any{
"valid": "valid",
"": false,
}
for expected, input := range tests {
t.Run(expected, func(t *testing.T) {
hv := HostVars{"key": input}
actual := hv.String("key")
if expected != actual {
t.Error("value is invalid. Expected: '" + expected + "', actual: '" + actual + "'")
}
})
}
}
func TestStringSlice(t *testing.T) {
tests := map[string]any{
"valid,valid": []any{"valid", "valid"},
"": "invalid",
}
for expected, input := range tests {
t.Run(expected, func(t *testing.T) {
hv := HostVars{"key": input}
actual := hv.StringSlice("key")
actualStr := strings.Join(actual, ",")
if expected != actualStr {
t.Error("value is invalid. Expected: '" + expected + "', actual: '" + actualStr + "'")
}
})
}
}
func TestMaintenanceEnabled(t *testing.T) {
tests := map[bool]string{
true: "any value",
false: "false",
}
for expected, input := range tests {
t.Run(input, func(t *testing.T) {
hv := HostVars{"etke_service_maintenance_enabled": input}
actual := hv.MaintenanceEnabled()
if expected != actual {
t.Error("value is invalid. Expected: '" + fmt.Sprintf("%t", expected) + "', actual: '" + fmt.Sprintf("%t", actual) + "'")
}
})
}
}
func readVarsYml(t *testing.T, filepath string) map[string]any {
t.Helper()
input, err := os.ReadFile(filepath)
if err != nil {
t.Error(err)
}
var vars map[string]any
if err := yaml.Unmarshal(input, &vars); err != nil {
t.Error(err)
}
return vars
}
func equalRecursive(t *testing.T, expected, actual any) {
t.Helper()
mv, ok := expected.(map[string]any)
if ok {
av := actual.(map[string]any) //nolint:forcetypeassert // that's ok
for k, v := range mv {
equalRecursive(t, v, av[k])
}
return
}
sv, ok := expected.([]any)
if ok {
av := actual.([]any) //nolint:forcetypeassert // that's ok
for i, v := range sv {
equalRecursive(t, v, av[i])
}
return
}
if expected != actual {
t.Error("value is invalid. Expected: '" + fmt.Sprintf("%v", expected) + "', actual: '" + fmt.Sprintf("%v", actual) + "'")
}
}