1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright (C) 2021-2024 Simon Ruderich
12 "ruderich.org/simon/safcm/testutil"
15 func TestLoadHosts(t *testing.T) {
16 cwd, err := os.Getwd()
20 defer os.Chdir(cwd) //nolint:errcheck
22 sliceToHosts := func(hosts []*Host) *Hosts {
25 Map: make(map[string]*Host),
27 for _, x := range hosts {
40 "../testdata/project",
43 Name: "host1.example.org",
49 Name: "host3.example.net",
56 "../testdata/host-invalid-duplicate",
58 fmt.Errorf("hosts.yaml: host \"host1.example.org\": host name already exists"),
61 "../testdata/host-invalid-all",
63 fmt.Errorf("hosts.yaml: host \"all\": conflict with pre-defined group \"all\""),
66 "../testdata/host-invalid-detected",
68 fmt.Errorf("hosts.yaml: host \"detected\": name must not start with \"detected\" (reserved for detected groups)"),
71 "../testdata/host-invalid-special",
73 fmt.Errorf("hosts.yaml: host \"special:host\": name must not contain \":\""),
77 for _, tc := range tests {
78 t.Run(tc.path, func(t *testing.T) {
79 err := os.Chdir(filepath.Join(cwd, tc.path))
84 res, err := LoadHosts()
85 testutil.AssertEqual(t, "res", res, tc.exp)
86 testutil.AssertErrorEqual(t, "err", err, tc.expErr)