1 // Copyright (C) 2021 Simon Ruderich
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.
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.
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/>.
24 "ruderich.org/simon/safcm/testutil"
27 func TestLoadGroups(t *testing.T) {
28 cwd, err := os.Getwd()
34 err = os.Chdir("../testdata/project")
38 hosts, err := LoadHosts()
51 exp map[string][]string
56 "../testdata/project",
89 "all_except_some:remove": {
106 "../testdata/project",
108 GroupOrder: []string{
115 fmt.Errorf("config.yaml: group_order: group \"does-not-exist\" does not exist"),
118 "../testdata/project",
120 GroupOrder: []string{
127 fmt.Errorf("config.yaml: group_order: invalid group name \"special:group\""),
130 "../testdata/project",
132 GroupOrder: []string{
139 fmt.Errorf("config.yaml: group_order: invalid group name \"group:remove\""),
143 "../testdata/group-invalid-all",
147 fmt.Errorf("groups.yaml: group \"all\": conflict with pre-defined group \"all\""),
150 "../testdata/group-invalid-all-remove",
154 fmt.Errorf("groups.yaml: group \"all:remove\": conflict with pre-defined group \"all:remove\""),
157 "../testdata/group-invalid-conflict",
161 fmt.Errorf("groups.yaml: group \"host2\": conflict with existing host"),
164 "../testdata/group-invalid-detected",
168 fmt.Errorf("groups.yaml: group \"detected_linux\": name must not start with \"detected\" (reserved for detected groups)"),
171 "../testdata/group-invalid-member",
175 fmt.Errorf("groups.yaml: group \"group1\": member \"special:member\" must not contain \":\""),
178 "../testdata/group-invalid-missing",
182 fmt.Errorf("groups.yaml: group \"1group2\": group \"does-not-exist\" not found"),
185 "../testdata/group-invalid-name",
189 fmt.Errorf("groups.yaml: group \"invalid.group.name\": name contains invalid characters (must match ^[a-z0-9_-]+$)"),
193 for _, tc := range tests {
194 t.Run(tc.path, func(t *testing.T) {
195 err := os.Chdir(filepath.Join(cwd, tc.path))
200 res, err := LoadGroups(tc.cfg, tc.hosts)
201 testutil.AssertEqual(t, "res", res, tc.exp)
202 testutil.AssertErrorEqual(t, "err", err, tc.expErr)
207 func TestResolveHostGroups(t *testing.T) {
208 cwd, err := os.Getwd()
214 err = os.Chdir("../testdata/project")
218 allHosts, err := LoadHosts()
222 allGroups, err := LoadGroups(&Config{}, allHosts)
284 "host1, detected_mips",
299 "host2, detected_linux",
314 for _, tc := range tests {
315 t.Run(tc.name, func(t *testing.T) {
316 res, err := ResolveHostGroups(tc.host, allGroups,
318 testutil.AssertEqual(t, "res", res, tc.exp)
319 testutil.AssertErrorEqual(t, "err", err, tc.expErr)
324 func TestTransitivelyDetectedGroups(t *testing.T) {
327 groups map[string][]string
351 "detected as direct member",
369 "detected as direct :remove member",
391 "detected as transitive member",
424 "detected as transitive :remove member",
459 for _, tc := range tests {
460 t.Run(tc.name, func(t *testing.T) {
461 res := TransitivelyDetectedGroups(tc.groups)
462 testutil.AssertEqual(t, "res", res, tc.exp)