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"
26 "ruderich.org/simon/safcm/cmd/safcm/config"
29 func TestHostsToSync(t *testing.T) {
30 cwd, err := os.Getwd()
36 err = os.Chdir("testdata/project")
40 _, allHosts, allGroups, err := LoadBaseFiles()
60 []string{"unknown-host/group"},
62 fmt.Errorf("hosts/groups not found: \"unknown-host/group\""),
69 allHosts.Map["host2"],
74 "host: multiple names",
75 []string{"host2", "host1.example.org"},
77 allHosts.Map["host1.example.org"],
78 allHosts.Map["host2"],
83 "host: multiple identical names",
84 []string{"host2", "host2"},
86 allHosts.Map["host2"],
91 "host: multiple names, including unknown",
92 []string{"host2", "unknown-host"},
94 fmt.Errorf("hosts/groups not found: \"unknown-host\""),
97 "host: multiple names, including unknowns",
98 []string{"host2", "unknown-host", "unknown-host-2"},
100 fmt.Errorf("hosts/groups not found: \"unknown-host\" \"unknown-host-2\""),
104 "group: single name",
107 allHosts.Map["host1.example.org"],
112 "group: multiple names",
113 []string{"group", "group2"},
115 allHosts.Map["host1.example.org"],
116 allHosts.Map["host2"],
121 "group: multiple identical names",
122 []string{"group", "group2", "group"},
124 allHosts.Map["host1.example.org"],
125 allHosts.Map["host2"],
130 "group: multiple names, including unknown",
131 []string{"group", "group2", "unknown-group"},
133 fmt.Errorf("hosts/groups not found: \"unknown-group\""),
139 allHosts.Map["host1.example.org"],
140 allHosts.Map["host2"],
141 allHosts.Map["host3.example.net"],
148 []string{"all", "group2"},
150 allHosts.Map["host1.example.org"],
151 allHosts.Map["host2"],
152 allHosts.Map["host3.example.net"],
158 []string{"all", "group2", "host2"},
160 allHosts.Map["host1.example.org"],
161 allHosts.Map["host2"],
162 allHosts.Map["host3.example.net"],
168 for _, tc := range tests {
169 t.Run(tc.name, func(t *testing.T) {
170 res, err := hostsToSync(tc.names, allHosts, allGroups)
171 if !reflect.DeepEqual(tc.exp, res) {
172 t.Errorf("res: %s", cmp.Diff(tc.exp, res))
174 // Ugly but the simplest way to compare errors (including nil)
175 if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
176 t.Errorf("err = %#v, want %#v",