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 "github.com/google/go-cmp/cmp"
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",
83 "all_except_some:remove": {
100 "../testdata/project",
102 GroupOrder: []string{
109 fmt.Errorf("config.yaml: group_order: group \"does-not-exist\" does not exist"),
112 "../testdata/project",
114 GroupOrder: []string{
121 fmt.Errorf("config.yaml: group_order: invalid group name \"special:group\""),
124 "../testdata/project",
126 GroupOrder: []string{
133 fmt.Errorf("config.yaml: group_order: invalid group name \"group:remove\""),
137 "../testdata/group-invalid-all",
141 fmt.Errorf("groups.yaml: group \"all\": conflict with pre-defined group \"all\""),
144 "../testdata/group-invalid-all-remove",
148 fmt.Errorf("groups.yaml: group \"all:remove\": conflict with pre-defined group \"all:remove\""),
151 "../testdata/group-invalid-conflict",
155 fmt.Errorf("groups.yaml: group \"host2\": conflict with existing host"),
158 "../testdata/group-invalid-detected",
162 fmt.Errorf("groups.yaml: group \"detected_linux\": name must not start with \"detected\" (reserved for detected groups)"),
165 "../testdata/group-invalid-member",
169 fmt.Errorf("groups.yaml: group \"group1\": member \"special:member\" must not contain \":\""),
172 "../testdata/group-invalid-missing",
176 fmt.Errorf("groups.yaml: group \"1group2\": group \"does-not-exist\" not found"),
179 "../testdata/group-invalid-name",
183 fmt.Errorf("groups.yaml: group \"invalid.group.name\": name contains invalid characters (must match ^[a-z0-9_-]+$)"),
187 for _, tc := range tests {
188 err := os.Chdir(tc.path)
193 res, err := LoadGroups(tc.cfg, tc.hosts)
195 if !reflect.DeepEqual(tc.exp, res) {
196 t.Errorf("%s: res: %s", tc.path,
197 cmp.Diff(tc.exp, res))
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("%s: err = %#v, want %#v",
202 tc.path, err, tc.expErr)
212 func TestResolveHostGroups(t *testing.T) {
213 cwd, err := os.Getwd()
219 err = os.Chdir("../testdata/project")
223 allHosts, err := LoadHosts()
227 allGroups, err := LoadGroups(&Config{}, allHosts)
288 "host1, detected_mips",
302 "host2, detected_linux",
317 for _, tc := range tests {
318 res, err := ResolveHostGroups(tc.host, allGroups, tc.detected)
319 if !reflect.DeepEqual(tc.exp, res) {
320 t.Errorf("%s: res: %s", tc.name,
321 cmp.Diff(tc.exp, res))
323 // Ugly but the simplest way to compare errors (including nil)
324 if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
325 t.Errorf("%s: err = %#v, want %#v",
326 tc.name, err, tc.expErr)