]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/config/groups_test.go
68f18bad3bc5ec3d6ff93fa9ec6010ec9951c073
[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         "reflect"
22         "testing"
23
24         "github.com/google/go-cmp/cmp"
25 )
26
27 func TestLoadGroups(t *testing.T) {
28         cwd, err := os.Getwd()
29         if err != nil {
30                 t.Fatal(err)
31         }
32         defer os.Chdir(cwd)
33
34         err = os.Chdir("../testdata/project")
35         if err != nil {
36                 t.Fatal(err)
37         }
38         hosts, err := LoadHosts()
39         if err != nil {
40                 t.Fatal(err)
41         }
42         err = os.Chdir(cwd)
43         if err != nil {
44                 t.Fatal(err)
45         }
46
47         tests := []struct {
48                 path   string
49                 cfg    *Config
50                 hosts  *Hosts
51                 exp    map[string][]string
52                 expErr error
53         }{
54
55                 {
56                         "../testdata/project",
57                         &Config{
58                                 GroupOrder: []string{
59                                         "detected_linux",
60                                         "detected_freebsd",
61                                 },
62                         },
63                         hosts,
64                         map[string][]string{
65                                 "group": {
66                                         "detected_linux",
67                                         "detected_freebsd",
68                                         "host1.example.org",
69                                 },
70                                 "group:remove": {
71                                         "host2",
72                                         "detected_mips",
73                                 },
74                                 "group2": {
75                                         "all",
76                                 },
77                                 "group2:remove": {
78                                         "remove",
79                                 },
80                                 "all_except_some": {
81                                         "all",
82                                 },
83                                 "all_except_some:remove": {
84                                         "host1.example.org",
85                                         "group2",
86                                 },
87                                 "remove": {
88                                         "host1.example.org",
89                                         "host2",
90                                         "host3.example.net",
91                                 },
92                                 "remove:remove": {
93                                         "host2",
94                                 },
95                         },
96                         nil,
97                 },
98
99                 {
100                         "../testdata/project",
101                         &Config{
102                                 GroupOrder: []string{
103                                         "detected_freebsd",
104                                         "does-not-exist",
105                                 },
106                         },
107                         hosts,
108                         nil,
109                         fmt.Errorf("config.yaml: group_order: group \"does-not-exist\" does not exist"),
110                 },
111                 {
112                         "../testdata/project",
113                         &Config{
114                                 GroupOrder: []string{
115                                         "detected_freebsd",
116                                         "special:group",
117                                 },
118                         },
119                         hosts,
120                         nil,
121                         fmt.Errorf("config.yaml: group_order: invalid group name \"special:group\""),
122                 },
123                 {
124                         "../testdata/project",
125                         &Config{
126                                 GroupOrder: []string{
127                                         "detected_freebsd",
128                                         "group:remove",
129                                 },
130                         },
131                         hosts,
132                         nil,
133                         fmt.Errorf("config.yaml: group_order: invalid group name \"group:remove\""),
134                 },
135
136                 {
137                         "../testdata/group-invalid-all",
138                         &Config{},
139                         hosts,
140                         nil,
141                         fmt.Errorf("groups.yaml: group \"all\": conflict with pre-defined group \"all\""),
142                 },
143                 {
144                         "../testdata/group-invalid-all-remove",
145                         &Config{},
146                         hosts,
147                         nil,
148                         fmt.Errorf("groups.yaml: group \"all:remove\": conflict with pre-defined group \"all:remove\""),
149                 },
150                 {
151                         "../testdata/group-invalid-conflict",
152                         &Config{},
153                         hosts,
154                         nil,
155                         fmt.Errorf("groups.yaml: group \"host2\": conflict with existing host"),
156                 },
157                 {
158                         "../testdata/group-invalid-detected",
159                         &Config{},
160                         &Hosts{},
161                         nil,
162                         fmt.Errorf("groups.yaml: group \"detected_linux\": name must not start with \"detected\" (reserved for detected groups)"),
163                 },
164                 {
165                         "../testdata/group-invalid-member",
166                         &Config{},
167                         &Hosts{},
168                         nil,
169                         fmt.Errorf("groups.yaml: group \"group1\": member \"special:member\" must not contain \":\""),
170                 },
171                 {
172                         "../testdata/group-invalid-missing",
173                         &Config{},
174                         &Hosts{},
175                         nil,
176                         fmt.Errorf("groups.yaml: group \"1group2\": group \"does-not-exist\" not found"),
177                 },
178                 {
179                         "../testdata/group-invalid-name",
180                         &Config{},
181                         &Hosts{},
182                         nil,
183                         fmt.Errorf("groups.yaml: group \"invalid.group.name\": name contains invalid characters (must match ^[a-z0-9_-]+$)"),
184                 },
185         }
186
187         for _, tc := range tests {
188                 t.Run(tc.path, func(t *testing.T) {
189                 err := os.Chdir(tc.path)
190                 if err != nil {
191                         t.Fatal(err)
192                 }
193
194                 res, err := LoadGroups(tc.cfg, tc.hosts)
195
196                 if !reflect.DeepEqual(tc.exp, res) {
197                         t.Errorf("res: %s",
198                                 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                 err = os.Chdir(cwd)
207                 if err != nil {
208                         t.Fatal(err)
209                 }
210                 })
211         }
212 }
213
214 func TestResolveHostGroups(t *testing.T) {
215         cwd, err := os.Getwd()
216         if err != nil {
217                 t.Fatal(err)
218         }
219         defer os.Chdir(cwd)
220
221         err = os.Chdir("../testdata/project")
222         if err != nil {
223                 t.Fatal(err)
224         }
225         allHosts, err := LoadHosts()
226         if err != nil {
227                 t.Fatal(err)
228         }
229         allGroups, err := LoadGroups(&Config{}, allHosts)
230         if err != nil {
231                 t.Fatal(err)
232         }
233
234         tests := []struct {
235                 name     string
236                 host     string
237                 detected []string
238                 exp      []string
239                 expErr   error
240         }{
241
242                 {
243                         "host1",
244                         "host1.example.org",
245                         nil,
246                         []string{
247                                 "all",
248                                 "group",
249                                 "host1.example.org",
250                                 "remove",
251                         },
252                         nil,
253                 },
254                 {
255                         "host2",
256                         "host2",
257                         nil,
258                         []string{
259                                 "all",
260                                 "group2",
261                                 "host2",
262                         },
263                         nil,
264                 },
265                 {
266                         "host3",
267                         "host3.example.net",
268                         nil,
269                         []string{
270                                 "all",
271                                 "all_except_some",
272                                 "host3.example.net",
273                                 "remove",
274                         },
275                         nil,
276                 },
277                 {
278                         "unknown host",
279                         "unknown",
280                         nil,
281                         []string{
282                                 "all",
283                                 "group2",
284                                 "unknown",
285                         },
286                         nil,
287                 },
288
289                 {
290                         "host1, detected_mips",
291                         "host1.example.org",
292                         []string{
293                                 "detected_mips",
294                         },
295                         []string{
296                                 "all",
297                                 "detected_mips",
298                                 "host1.example.org",
299                                 "remove",
300                         },
301                         nil,
302                 },
303                 {
304                         "host2, detected_linux",
305                         "host2",
306                         []string{
307                                 "detected_linux",
308                         },
309                         []string{
310                                 "all",
311                                 "detected_linux",
312                                 "group2",
313                                 "host2",
314                         },
315                         nil,
316                 },
317         }
318
319         for _, tc := range tests {
320                 t.Run(tc.name, func(t *testing.T) {
321                 res, err := ResolveHostGroups(tc.host, allGroups, tc.detected)
322                 if !reflect.DeepEqual(tc.exp, res) {
323                         t.Errorf("res: %s",
324                                 cmp.Diff(tc.exp, res))
325                 }
326                 // Ugly but the simplest way to compare errors (including nil)
327                 if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
328                         t.Errorf("err = %#v, want %#v",
329                                 err, tc.expErr)
330                 }
331                 })
332         }
333 }