]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/config/groups_test.go
tests: add and use testutil package to reduce duplication
[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         "testing"
23
24         "ruderich.org/simon/safcm/testutil"
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(filepath.Join(cwd, tc.path))
190                         if err != nil {
191                                 t.Fatal(err)
192                         }
193
194                         res, err := LoadGroups(tc.cfg, tc.hosts)
195                         testutil.AssertEqual(t, "res", res, tc.exp)
196                         testutil.AssertErrorEqual(t, "err", err, tc.expErr)
197                 })
198         }
199 }
200
201 func TestResolveHostGroups(t *testing.T) {
202         cwd, err := os.Getwd()
203         if err != nil {
204                 t.Fatal(err)
205         }
206         defer os.Chdir(cwd)
207
208         err = os.Chdir("../testdata/project")
209         if err != nil {
210                 t.Fatal(err)
211         }
212         allHosts, err := LoadHosts()
213         if err != nil {
214                 t.Fatal(err)
215         }
216         allGroups, err := LoadGroups(&Config{}, allHosts)
217         if err != nil {
218                 t.Fatal(err)
219         }
220
221         tests := []struct {
222                 name     string
223                 host     string
224                 detected []string
225                 exp      []string
226                 expErr   error
227         }{
228
229                 {
230                         "host1",
231                         "host1.example.org",
232                         nil,
233                         []string{
234                                 "all",
235                                 "group",
236                                 "host1.example.org",
237                                 "remove",
238                         },
239                         nil,
240                 },
241                 {
242                         "host2",
243                         "host2",
244                         nil,
245                         []string{
246                                 "all",
247                                 "group2",
248                                 "host2",
249                         },
250                         nil,
251                 },
252                 {
253                         "host3",
254                         "host3.example.net",
255                         nil,
256                         []string{
257                                 "all",
258                                 "all_except_some",
259                                 "host3.example.net",
260                                 "remove",
261                         },
262                         nil,
263                 },
264                 {
265                         "unknown host",
266                         "unknown",
267                         nil,
268                         []string{
269                                 "all",
270                                 "group2",
271                                 "unknown",
272                         },
273                         nil,
274                 },
275
276                 {
277                         "host1, detected_mips",
278                         "host1.example.org",
279                         []string{
280                                 "detected_mips",
281                         },
282                         []string{
283                                 "all",
284                                 "detected_mips",
285                                 "host1.example.org",
286                                 "remove",
287                         },
288                         nil,
289                 },
290                 {
291                         "host2, detected_linux",
292                         "host2",
293                         []string{
294                                 "detected_linux",
295                         },
296                         []string{
297                                 "all",
298                                 "detected_linux",
299                                 "group2",
300                                 "host2",
301                         },
302                         nil,
303                 },
304         }
305
306         for _, tc := range tests {
307                 t.Run(tc.name, func(t *testing.T) {
308                         res, err := ResolveHostGroups(tc.host, allGroups,
309                                 tc.detected)
310                         testutil.AssertEqual(t, "res", res, tc.exp)
311                         testutil.AssertErrorEqual(t, "err", err, tc.expErr)
312                 })
313         }
314 }