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/>.
23 "ruderich.org/simon/safcm/cmd/safcm/config"
24 "ruderich.org/simon/safcm/testutil"
27 func TestHostsToSync(t *testing.T) {
28 cwd, err := os.Getwd()
34 err = os.Chdir("testdata/project")
38 _, allHosts, allGroups, err := LoadBaseFiles()
45 Groups depending on "detected" groups cannot be used to select hosts as these
46 are only available after the hosts were contacted.
64 []string{"unknown-host/group"},
66 fmt.Errorf("hosts/groups not found: \"unknown-host/group\""),
73 allHosts.Map["host2"],
78 "host: multiple names",
79 []string{"host2", "host1.example.org"},
81 allHosts.Map["host1.example.org"],
82 allHosts.Map["host2"],
87 "host: multiple identical names",
88 []string{"host2", "host2"},
90 allHosts.Map["host2"],
95 "host: multiple names, including unknown",
96 []string{"host2", "unknown-host"},
98 fmt.Errorf("hosts/groups not found: \"unknown-host\""),
101 "host: multiple names, including unknowns",
102 []string{"host2", "unknown-host", "unknown-host-2"},
104 fmt.Errorf("hosts/groups not found: \"unknown-host\" \"unknown-host-2\""),
108 "group: single name",
111 allHosts.Map["host1.example.org"],
116 "group: multiple names",
117 []string{"group3", "group2"},
119 allHosts.Map["host1.example.org"],
120 allHosts.Map["host2"],
125 "group: multiple identical names",
126 []string{"group3", "group2", "group3"},
128 allHosts.Map["host1.example.org"],
129 allHosts.Map["host2"],
134 "group: multiple names, including unknown",
135 []string{"group3", "group2", "unknown-group"},
137 fmt.Errorf("hosts/groups not found: \"unknown-group\""),
143 allHosts.Map["host1.example.org"],
144 allHosts.Map["host2"],
145 allHosts.Map["host3.example.net"],
151 "group: single name (detected)",
154 fmt.Errorf(`group "group" depends on "detected" groups` + errMsg),
157 "group: multiple names (detected)",
158 []string{"group", "group2"},
160 fmt.Errorf(`group "group" depends on "detected" groups` + errMsg),
165 []string{"all", "group2"},
167 allHosts.Map["host1.example.org"],
168 allHosts.Map["host2"],
169 allHosts.Map["host3.example.net"],
175 []string{"all", "group2", "host2"},
177 allHosts.Map["host1.example.org"],
178 allHosts.Map["host2"],
179 allHosts.Map["host3.example.net"],
185 for _, tc := range tests {
186 t.Run(tc.name, func(t *testing.T) {
187 res, err := hostsToSync(tc.names, allHosts, allGroups)
188 testutil.AssertEqual(t, "res", res, tc.exp)
189 testutil.AssertErrorEqual(t, "err", err, tc.expErr)