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()
58 []string{"unknown-host/group"},
60 fmt.Errorf("hosts/groups not found: \"unknown-host/group\""),
67 allHosts.Map["host2"],
72 "host: multiple names",
73 []string{"host2", "host1.example.org"},
75 allHosts.Map["host1.example.org"],
76 allHosts.Map["host2"],
81 "host: multiple identical names",
82 []string{"host2", "host2"},
84 allHosts.Map["host2"],
89 "host: multiple names, including unknown",
90 []string{"host2", "unknown-host"},
92 fmt.Errorf("hosts/groups not found: \"unknown-host\""),
95 "host: multiple names, including unknowns",
96 []string{"host2", "unknown-host", "unknown-host-2"},
98 fmt.Errorf("hosts/groups not found: \"unknown-host\" \"unknown-host-2\""),
102 "group: single name",
105 allHosts.Map["host1.example.org"],
110 "group: multiple names",
111 []string{"group", "group2"},
113 allHosts.Map["host1.example.org"],
114 allHosts.Map["host2"],
119 "group: multiple identical names",
120 []string{"group", "group2", "group"},
122 allHosts.Map["host1.example.org"],
123 allHosts.Map["host2"],
128 "group: multiple names, including unknown",
129 []string{"group", "group2", "unknown-group"},
131 fmt.Errorf("hosts/groups not found: \"unknown-group\""),
137 allHosts.Map["host1.example.org"],
138 allHosts.Map["host2"],
139 allHosts.Map["host3.example.net"],
146 []string{"all", "group2"},
148 allHosts.Map["host1.example.org"],
149 allHosts.Map["host2"],
150 allHosts.Map["host3.example.net"],
156 []string{"all", "group2", "host2"},
158 allHosts.Map["host1.example.org"],
159 allHosts.Map["host2"],
160 allHosts.Map["host3.example.net"],
166 for _, tc := range tests {
167 t.Run(tc.name, func(t *testing.T) {
168 res, err := hostsToSync(tc.names, allHosts, allGroups)
169 testutil.AssertEqual(t, "res", res, tc.exp)
170 testutil.AssertErrorEqual(t, "err", err, tc.expErr)