1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright (C) 2021-2024 Simon Ruderich
11 "ruderich.org/simon/safcm/cmd/safcm/config"
12 "ruderich.org/simon/safcm/testutil"
15 func TestHostsToSync(t *testing.T) {
16 cwd, err := os.Getwd()
20 defer os.Chdir(cwd) //nolint:errcheck
22 err = os.Chdir("testdata/project")
26 _, allHosts, allGroups, err := LoadBaseFiles()
33 Groups depending on "detected" groups cannot be used to select hosts as these
34 are only available after the hosts were contacted.
52 []string{"unknown-host/group"},
54 fmt.Errorf("hosts/groups not found: \"unknown-host/group\""),
61 allHosts.Map["host2"],
66 "host: multiple names",
67 []string{"host2", "host1.example.org"},
69 allHosts.Map["host1.example.org"],
70 allHosts.Map["host2"],
75 "host: multiple identical names",
76 []string{"host2", "host2"},
78 allHosts.Map["host2"],
83 "host: multiple names, including unknown",
84 []string{"host2", "unknown-host"},
86 fmt.Errorf("hosts/groups not found: \"unknown-host\""),
89 "host: multiple names, including unknowns",
90 []string{"host2", "unknown-host", "unknown-host-2"},
92 fmt.Errorf("hosts/groups not found: \"unknown-host\" \"unknown-host-2\""),
99 allHosts.Map["host1.example.org"],
104 "group: multiple names",
105 []string{"group3", "group2"},
107 allHosts.Map["host1.example.org"],
108 allHosts.Map["host2"],
113 "group: multiple identical names",
114 []string{"group3", "group2", "group3"},
116 allHosts.Map["host1.example.org"],
117 allHosts.Map["host2"],
122 "group: multiple names, including unknown",
123 []string{"group3", "group2", "unknown-group"},
125 fmt.Errorf("hosts/groups not found: \"unknown-group\""),
131 allHosts.Map["host1.example.org"],
132 allHosts.Map["host2"],
133 allHosts.Map["host3.example.net"],
139 "group: single name (detected)",
142 fmt.Errorf(`group "group" depends on "detected" groups` + errMsg),
145 "group: multiple names (detected)",
146 []string{"group", "group2"},
148 fmt.Errorf(`group "group" depends on "detected" groups` + errMsg),
153 []string{"all", "group2"},
155 allHosts.Map["host1.example.org"],
156 allHosts.Map["host2"],
157 allHosts.Map["host3.example.net"],
163 []string{"all", "group2", "host2"},
165 allHosts.Map["host1.example.org"],
166 allHosts.Map["host2"],
167 allHosts.Map["host3.example.net"],
173 for _, tc := range tests {
174 t.Run(tc.name, func(t *testing.T) {
175 res, err := hostsToSync(tc.names, allHosts, allGroups)
176 testutil.AssertEqual(t, "res", res, tc.exp)
177 testutil.AssertErrorEqual(t, "err", err, tc.expErr)