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/>.
25 "github.com/google/go-cmp/cmp"
28 func TestLoadGroups(t *testing.T) {
29 cwd, err := os.Getwd()
35 err = os.Chdir("../testdata/project")
39 hosts, err := LoadHosts()
52 exp map[string][]string
57 "../testdata/project",
84 "all_except_some:remove": {
101 "../testdata/project",
103 GroupOrder: []string{
110 fmt.Errorf("config.yaml: group_order: group \"does-not-exist\" does not exist"),
113 "../testdata/project",
115 GroupOrder: []string{
122 fmt.Errorf("config.yaml: group_order: invalid group name \"special:group\""),
125 "../testdata/project",
127 GroupOrder: []string{
134 fmt.Errorf("config.yaml: group_order: invalid group name \"group:remove\""),
138 "../testdata/group-invalid-all",
142 fmt.Errorf("groups.yaml: group \"all\": conflict with pre-defined group \"all\""),
145 "../testdata/group-invalid-all-remove",
149 fmt.Errorf("groups.yaml: group \"all:remove\": conflict with pre-defined group \"all:remove\""),
152 "../testdata/group-invalid-conflict",
156 fmt.Errorf("groups.yaml: group \"host2\": 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 \"1group2\": group \"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)
197 if !reflect.DeepEqual(tc.exp, res) {
198 t.Errorf("res: %s", cmp.Diff(tc.exp, res))
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",
209 func TestResolveHostGroups(t *testing.T) {
210 cwd, err := os.Getwd()
216 err = os.Chdir("../testdata/project")
220 allHosts, err := LoadHosts()
224 allGroups, err := LoadGroups(&Config{}, allHosts)
285 "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 if !reflect.DeepEqual(tc.exp, res) {
319 t.Errorf("res: %s", cmp.Diff(tc.exp, res))
321 // Ugly but the simplest way to compare errors (including nil)
322 if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
323 t.Errorf("err = %#v, want %#v",