-
Notifications
You must be signed in to change notification settings - Fork 11
/
ignitions_test.go
223 lines (195 loc) · 4.51 KB
/
ignitions_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package neco
import (
"fmt"
"os"
"path/filepath"
"reflect"
"regexp"
"sort"
"strings"
"testing"
sabakan "github.com/cybozu-go/sabakan/v3/client"
"github.com/google/go-cmp/cmp"
"sigs.k8s.io/yaml"
)
const (
testRoleDir = "ignitions/roles"
)
func checkForFlag(filestateInDir map[string]bool) error {
for key, value := range filestateInDir {
if !value {
return fmt.Errorf("%s file is not included in the configuration file\n", key)
}
}
return nil
}
func testIgnitionTemplates(path string, filestateInDir map[string]bool) error {
data, err := os.ReadFile(path)
if err != nil {
return err
}
t := &sabakan.TemplateSource{}
err = yaml.Unmarshal(data, t)
if err != nil {
return err
}
if len(t.Include) != 0 {
abs, err := filepath.Abs(filepath.Join(filepath.Dir(path), t.Include))
if err != nil {
return err
}
_, err = os.Stat(abs)
if err != nil {
return err
}
err = testIgnitionTemplates(abs, filestateInDir)
if err != nil {
return err
}
}
if len(t.Passwd) != 0 {
abs, err := filepath.Abs(filepath.Join(filepath.Dir(path), t.Passwd))
if err != nil {
return err
}
_, err = os.Stat(abs)
if err != nil {
return err
}
}
if len(t.Files) != 0 {
filesDir, err := filepath.Abs(filepath.Join(filepath.Dir(path), "files"))
if err != nil {
return err
}
var filelistInYAML []string
for _, f := range t.Files {
realPath := filepath.Join(filesDir, f)
filelistInYAML = append(filelistInYAML, realPath)
}
sort.Strings(filelistInYAML)
err = filepath.Walk(filesDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if _, ok := filestateInDir[path]; !ok {
filestateInDir[path] = false
}
return nil
})
if err != nil {
return err
}
for _, f := range filelistInYAML {
if _, ok := filestateInDir[f]; ok {
filestateInDir[f] = true
} else {
return fmt.Errorf("file in %s and file tree differ\n", f)
}
}
}
// remote_files does not use in Neco.
//if len(t.RemoteFiles) != 0 {
// ...
//}
if len(t.Systemd) != 0 {
systemdDir, err := filepath.Abs(filepath.Join(filepath.Dir(path), "systemd"))
if err != nil {
return err
}
err = filepath.Walk(systemdDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if _, ok := filestateInDir[path]; !ok {
filestateInDir[path] = false
}
return nil
})
if err != nil {
return err
}
var filelistInYAML []string
for _, f := range t.Systemd {
realPath := filepath.Join(systemdDir, f.Name)
filelistInYAML = append(filelistInYAML, realPath)
}
sort.Strings(filelistInYAML)
for _, s := range filelistInYAML {
if _, ok := filestateInDir[s]; ok {
filestateInDir[s] = true
} else {
fmt.Printf("%s is pre-installed service.\n", s)
}
}
}
if len(t.Networkd) != 0 {
networkdDir, err := filepath.Abs(filepath.Join(filepath.Dir(path), "networkd"))
if err != nil {
return err
}
var filelistInYAML []string
for _, f := range t.Networkd {
realPath := filepath.Join(networkdDir, f)
filelistInYAML = append(filelistInYAML, realPath)
}
sort.Strings(filelistInYAML)
var filelistInDir []string
err = filepath.Walk(networkdDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
filelistInDir = append(filelistInDir, path)
return nil
})
if err != nil {
return err
}
sort.Strings(filelistInDir)
if !reflect.DeepEqual(filelistInYAML, filelistInDir) {
return fmt.Errorf("files in %s and file tree %s differ\n%v", path, networkdDir, cmp.Diff(filelistInYAML, filelistInDir))
}
}
return nil
}
func TestNecoIgnitionTemplates(t *testing.T) {
siteYAMLsByRole := make(map[string][]string)
t.Parallel()
err := filepath.Walk(testRoleDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if s, _ := regexp.MatchString(`site(-.*|)\.yml`, info.Name()); s {
role := strings.Split(path, "/")[2]
siteYAMLsByRole[role] = append(siteYAMLsByRole[role], path)
}
return nil
})
if err != nil {
t.Fatal(err)
}
for _, siteYAMLs := range siteYAMLsByRole {
filestateInDir := make(map[string]bool)
for _, sy := range siteYAMLs {
err := testIgnitionTemplates(sy, filestateInDir)
if err != nil {
t.Fatal(err)
}
}
if err := checkForFlag(filestateInDir); err != nil {
t.Fatal(err)
}
}
}