]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/config/groups_test.go
tests: go fmt and rewrap
[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", cmp.Diff(tc.exp, res))
198                         }
199                         // Ugly but the simplest way to compare errors (including nil)
200                         if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
201                                 t.Errorf("err = %#v, want %#v",
202                                         err, tc.expErr)
203                         }
204
205                         err = os.Chdir(cwd)
206                         if err != nil {
207                                 t.Fatal(err)
208                         }
209                 })
210         }
211 }
212
213 func TestResolveHostGroups(t *testing.T) {
214         cwd, err := os.Getwd()
215         if err != nil {
216                 t.Fatal(err)
217         }
218         defer os.Chdir(cwd)
219
220         err = os.Chdir("../testdata/project")
221         if err != nil {
222                 t.Fatal(err)
223         }
224         allHosts, err := LoadHosts()
225         if err != nil {
226                 t.Fatal(err)
227         }
228         allGroups, err := LoadGroups(&Config{}, allHosts)
229         if err != nil {
230                 t.Fatal(err)
231         }
232
233         tests := []struct {
234                 name     string
235                 host     string
236                 detected []string
237                 exp      []string
238                 expErr   error
239         }{
240
241                 {
242                         "host1",
243                         "host1.example.org",
244                         nil,
245                         []string{
246                                 "all",
247                                 "group",
248                                 "host1.example.org",
249                                 "remove",
250                         },
251                         nil,
252                 },
253                 {
254                         "host2",
255                         "host2",
256                         nil,
257                         []string{
258                                 "all",
259                                 "group2",
260                                 "host2",
261                         },
262                         nil,
263                 },
264                 {
265                         "host3",
266                         "host3.example.net",
267                         nil,
268                         []string{
269                                 "all",
270                                 "all_except_some",
271                                 "host3.example.net",
272                                 "remove",
273                         },
274                         nil,
275                 },
276                 {
277                         "unknown host",
278                         "unknown",
279                         nil,
280                         []string{
281                                 "all",
282                                 "group2",
283                                 "unknown",
284                         },
285                         nil,
286                 },
287
288                 {
289                         "host1, detected_mips",
290                         "host1.example.org",
291                         []string{
292                                 "detected_mips",
293                         },
294                         []string{
295                                 "all",
296                                 "detected_mips",
297                                 "host1.example.org",
298                                 "remove",
299                         },
300                         nil,
301                 },
302                 {
303                         "host2, detected_linux",
304                         "host2",
305                         []string{
306                                 "detected_linux",
307                         },
308                         []string{
309                                 "all",
310                                 "detected_linux",
311                                 "group2",
312                                 "host2",
313                         },
314                         nil,
315                 },
316         }
317
318         for _, tc := range tests {
319                 t.Run(tc.name, func(t *testing.T) {
320                         res, err := ResolveHostGroups(tc.host, allGroups,
321                                 tc.detected)
322                         if !reflect.DeepEqual(tc.exp, res) {
323                                 t.Errorf("res: %s", cmp.Diff(tc.exp, res))
324                         }
325                         // Ugly but the simplest way to compare errors (including nil)
326                         if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
327                                 t.Errorf("err = %#v, want %#v",
328                                         err, tc.expErr)
329                         }
330                 })
331         }
332 }