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 TestLoadHosts(t *testing.T) {
28 cwd, err := os.Getwd()
34 sliceToHosts := func(hosts []*Host) *Hosts {
37 Map: make(map[string]*Host),
39 for _, x := range hosts {
52 "../testdata/project",
55 Name: "host1.example.org",
61 Name: "host3.example.net",
68 "../testdata/host-invalid-all",
70 fmt.Errorf("hosts.yaml: host \"all\": conflict with pre-defined group \"all\""),
73 "../testdata/host-invalid-detected",
75 fmt.Errorf("hosts.yaml: host \"detected\": name must not start with \"detected\" (reserved for detected groups)"),
78 "../testdata/host-invalid-special",
80 fmt.Errorf("hosts.yaml: host \"special:host\": name must not contain \":\""),
84 for _, tc := range tests {
85 t.Run(tc.path, func(t *testing.T) {
86 err := os.Chdir(filepath.Join(cwd, tc.path))
91 res, err := LoadHosts()
92 testutil.AssertEqual(t, "res", res, tc.exp)
93 testutil.AssertErrorEqual(t, "err", err, tc.expErr)