]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/config/groups_test.go
cf6cd6e088cd48d3789fb2f618f903eb1e420dcc
[safcm/safcm.git] / cmd / safcm / config / groups_test.go
1 // Copyright (C) 2021  Simon Ruderich
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package config
17
18 import (
19         "fmt"
20         "os"
21         "path/filepath"
22         "reflect"
23         "testing"
24
25         "github.com/google/go-cmp/cmp"
26 )
27
28 func TestLoadGroups(t *testing.T) {
29         cwd, err := os.Getwd()
30         if err != nil {
31                 t.Fatal(err)
32         }
33         defer os.Chdir(cwd)
34
35         err = os.Chdir("../testdata/project")
36         if err != nil {
37                 t.Fatal(err)
38         }
39         hosts, err := LoadHosts()
40         if err != nil {
41                 t.Fatal(err)
42         }
43         err = os.Chdir(cwd)
44         if err != nil {
45                 t.Fatal(err)
46         }
47
48         tests := []struct {
49                 path   string
50                 cfg    *Config
51                 hosts  *Hosts
52                 exp    map[string][]string
53                 expErr error
54         }{
55
56                 {
57                         "../testdata/project",
58                         &Config{
59                                 GroupOrder: []string{
60                                         "detected_linux",
61                                         "detected_freebsd",
62                                 },
63                         },
64                         hosts,
65                         map[string][]string{
66                                 "group": {
67                                         "detected_linux",
68                                         "detected_freebsd",
69                                         "host1.example.org",
70                                 },
71                                 "group:remove": {
72                                         "host2",
73                                         "detected_mips",
74                                 },
75                                 "group2": {
76                                         "all",
77                                 },
78                                 "group2:remove": {
79                                         "remove",
80                                 },
81                                 "all_except_some": {
82                                         "all",
83                                 },
84                                 "all_except_some:remove": {
85                                         "host1.example.org",
86                                         "group2",
87                                 },
88                                 "remove": {
89                                         "host1.example.org",
90                                         "host2",
91                                         "host3.example.net",
92                                 },
93                                 "remove:remove": {
94                                         "host2",
95                                 },
96                         },
97                         nil,
98                 },
99
100                 {
101                         "../testdata/project",
102                         &Config{
103                                 GroupOrder: []string{
104                                         "detected_freebsd",
105                                         "does-not-exist",
106                                 },
107                         },
108                         hosts,
109                         nil,
110                         fmt.Errorf("config.yaml: group_order: group \"does-not-exist\" does not exist"),
111                 },
112                 {
113                         "../testdata/project",
114                         &Config{
115                                 GroupOrder: []string{
116                                         "detected_freebsd",
117                                         "special:group",
118                                 },
119                         },
120                         hosts,
121                         nil,
122                         fmt.Errorf("config.yaml: group_order: invalid group name \"special:group\""),
123                 },
124                 {
125                         "../testdata/project",
126                         &Config{
127                                 GroupOrder: []string{
128                                         "detected_freebsd",
129                                         "group:remove",
130                                 },
131                         },
132                         hosts,
133                         nil,
134                         fmt.Errorf("config.yaml: group_order: invalid group name \"group:remove\""),
135                 },
136
137                 {
138                         "../testdata/group-invalid-all",
139                         &Config{},
140                         hosts,
141                         nil,
142                         fmt.Errorf("groups.yaml: group \"all\": conflict with pre-defined group \"all\""),
143                 },
144                 {
145                         "../testdata/group-invalid-all-remove",
146                         &Config{},
147                         hosts,
148                         nil,
149                         fmt.Errorf("groups.yaml: group \"all:remove\": conflict with pre-defined group \"all:remove\""),
150                 },
151                 {
152                         "../testdata/group-invalid-conflict",
153                         &Config{},
154                         hosts,
155                         nil,
156                         fmt.Errorf("groups.yaml: group \"host2\": conflict with existing host"),
157                 },
158                 {
159                         "../testdata/group-invalid-detected",
160                         &Config{},
161                         &Hosts{},
162                         nil,
163                         fmt.Errorf("groups.yaml: group \"detected_linux\": name must not start with \"detected\" (reserved for detected groups)"),
164                 },
165                 {
166                         "../testdata/group-invalid-member",
167                         &Config{},
168                         &Hosts{},
169                         nil,
170                         fmt.Errorf("groups.yaml: group \"group1\": member \"special:member\" must not contain \":\""),
171                 },
172                 {
173                         "../testdata/group-invalid-missing",
174                         &Config{},
175                         &Hosts{},
176                         nil,
177                         fmt.Errorf("groups.yaml: group \"1group2\": group \"does-not-exist\" not found"),
178                 },
179                 {
180                         "../testdata/group-invalid-name",
181                         &Config{},
182                         &Hosts{},
183                         nil,
184                         fmt.Errorf("groups.yaml: group \"invalid.group.name\": name contains invalid characters (must match ^[a-z0-9_-]+$)"),
185                 },
186         }
187
188         for _, tc := range tests {
189                 t.Run(tc.path, func(t *testing.T) {
190                         err := os.Chdir(filepath.Join(cwd, tc.path))
191                         if err != nil {
192                                 t.Fatal(err)
193                         }
194
195                         res, err := LoadGroups(tc.cfg, tc.hosts)
196
197                         if !reflect.DeepEqual(tc.exp, res) {
198                                 t.Errorf("res: %s", cmp.Diff(tc.exp, res))
199                         }
200                         // Ugly but the simplest way to compare errors (including nil)
201                         if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
202                                 t.Errorf("err = %#v, want %#v",
203                                         err, tc.expErr)
204                         }
205                 })
206         }
207 }
208
209 func TestResolveHostGroups(t *testing.T) {
210         cwd, err := os.Getwd()
211         if err != nil {
212                 t.Fatal(err)
213         }
214         defer os.Chdir(cwd)
215
216         err = os.Chdir("../testdata/project")
217         if err != nil {
218                 t.Fatal(err)
219         }
220         allHosts, err := LoadHosts()
221         if err != nil {
222                 t.Fatal(err)
223         }
224         allGroups, err := LoadGroups(&Config{}, allHosts)
225         if err != nil {
226                 t.Fatal(err)
227         }
228
229         tests := []struct {
230                 name     string
231                 host     string
232                 detected []string
233                 exp      []string
234                 expErr   error
235         }{
236
237                 {
238                         "host1",
239                         "host1.example.org",
240                         nil,
241                         []string{
242                                 "all",
243                                 "group",
244                                 "host1.example.org",
245                                 "remove",
246                         },
247                         nil,
248                 },
249                 {
250                         "host2",
251                         "host2",
252                         nil,
253                         []string{
254                                 "all",
255                                 "group2",
256                                 "host2",
257                         },
258                         nil,
259                 },
260                 {
261                         "host3",
262                         "host3.example.net",
263                         nil,
264                         []string{
265                                 "all",
266                                 "all_except_some",
267                                 "host3.example.net",
268                                 "remove",
269                         },
270                         nil,
271                 },
272                 {
273                         "unknown host",
274                         "unknown",
275                         nil,
276                         []string{
277                                 "all",
278                                 "group2",
279                                 "unknown",
280                         },
281                         nil,
282                 },
283
284                 {
285                         "host1, detected_mips",
286                         "host1.example.org",
287                         []string{
288                                 "detected_mips",
289                         },
290                         []string{
291                                 "all",
292                                 "detected_mips",
293                                 "host1.example.org",
294                                 "remove",
295                         },
296                         nil,
297                 },
298                 {
299                         "host2, detected_linux",
300                         "host2",
301                         []string{
302                                 "detected_linux",
303                         },
304                         []string{
305                                 "all",
306                                 "detected_linux",
307                                 "group2",
308                                 "host2",
309                         },
310                         nil,
311                 },
312         }
313
314         for _, tc := range tests {
315                 t.Run(tc.name, func(t *testing.T) {
316                         res, err := ResolveHostGroups(tc.host, allGroups,
317                                 tc.detected)
318                         if !reflect.DeepEqual(tc.exp, res) {
319                                 t.Errorf("res: %s", cmp.Diff(tc.exp, res))
320                         }
321                         // Ugly but the simplest way to compare errors (including nil)
322                         if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
323                                 t.Errorf("err = %#v, want %#v",
324                                         err, tc.expErr)
325                         }
326                 })
327         }
328 }