1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright (C) 2021-2024 Simon Ruderich
12 "ruderich.org/simon/safcm/testutil"
15 func TestLoadGroups(t *testing.T) {
16 cwd, err := os.Getwd()
20 defer os.Chdir(cwd) //nolint:errcheck
22 err = os.Chdir("../testdata/project")
26 hosts, err := LoadHosts()
39 exp map[string][]string
44 "../testdata/project",
46 GroupPriority: []string{
77 "all_except_some:remove": {
94 "../testdata/project",
96 GroupPriority: []string{
103 fmt.Errorf("config.yaml: group_priority: group \"does-not-exist\" does not exist"),
106 "../testdata/project",
108 GroupPriority: []string{
115 fmt.Errorf("config.yaml: group_priority: invalid group name \"special:group\""),
118 "../testdata/project",
120 GroupPriority: []string{
127 fmt.Errorf("config.yaml: group_priority: invalid group name \"group:remove\""),
131 "../testdata/group-invalid-all",
135 fmt.Errorf("groups.yaml: group \"all\": conflict with pre-defined group \"all\""),
138 "../testdata/group-invalid-all-remove",
142 fmt.Errorf("groups.yaml: group \"all:remove\": conflict with pre-defined group \"all:remove\""),
145 "../testdata/group-invalid-conflict",
149 fmt.Errorf("groups.yaml: group \"host2\": conflict with existing host"),
152 "../testdata/group-invalid-conflict-remove",
156 fmt.Errorf("groups.yaml: group \"host2:remove\": conflict with existing host"),
159 "../testdata/group-invalid-detected",
163 fmt.Errorf("groups.yaml: group \"detected_linux\": name must not start with \"detected\" (reserved for detected groups)"),
166 "../testdata/group-invalid-member",
170 fmt.Errorf("groups.yaml: group \"group1\": member \"special:member\" must not contain \":\""),
173 "../testdata/group-invalid-missing",
177 fmt.Errorf("groups.yaml: group \"group2\": member \"does-not-exist\" not found"),
180 "../testdata/group-invalid-name",
184 fmt.Errorf("groups.yaml: group \"invalid.group.name\": name contains invalid characters (must match ^[a-z0-9_-]+$)"),
188 for _, tc := range tests {
189 t.Run(tc.path, func(t *testing.T) {
190 err := os.Chdir(filepath.Join(cwd, tc.path))
195 res, err := LoadGroups(tc.cfg, tc.hosts)
196 testutil.AssertEqual(t, "res", res, tc.exp)
197 testutil.AssertErrorEqual(t, "err", err, tc.expErr)
202 func TestResolveHostGroups(t *testing.T) {
203 cwd, err := os.Getwd()
207 defer os.Chdir(cwd) //nolint:errcheck
209 err = os.Chdir("../testdata/project")
213 allHosts, err := LoadHosts()
217 allGroups, err := LoadGroups(&Config{}, allHosts)
279 "host1, detected_mips",
294 "host2, detected_linux",
309 for _, tc := range tests {
310 t.Run(tc.name, func(t *testing.T) {
311 res, err := ResolveHostGroups(tc.host, allGroups,
313 testutil.AssertEqual(t, "res", res, tc.exp)
314 testutil.AssertErrorEqual(t, "err", err, tc.expErr)
319 func TestTransitivelyDetectedGroups(t *testing.T) {
322 groups map[string][]string
346 "detected as direct member",
364 "detected as direct :remove member",
386 "detected as transitive member",
419 "detected as transitive :remove member",
454 for _, tc := range tests {
455 t.Run(tc.name, func(t *testing.T) {
456 res := TransitivelyDetectedGroups(tc.groups)
457 testutil.AssertEqual(t, "res", res, tc.exp)