1 // Copyright (C) 2021-2022 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/>.
27 "ruderich.org/simon/safcm"
28 "ruderich.org/simon/safcm/testutil"
31 func TestHostSyncReq(t *testing.T) {
32 cwd, err := os.Getwd()
36 defer os.Chdir(cwd) //nolint:errcheck
48 // NOTE: Also update MsgSyncReq in safcm-remote test cases
49 // changing the MsgSyncReq struct!
64 Files: map[string]*safcm.File{
68 Mode: fs.ModeDir | 0755 | fs.ModeSetgid,
71 TriggerCommands: []string{
78 Mode: fs.ModeDir | 0755,
85 Mode: 0100 | fs.ModeSetuid | fs.ModeSetgid | fs.ModeSticky,
96 Data: []byte("Welcome to Host ONE\n\n\n\n\n\nall\n\n\nhost1.example.org\n\n\n\n"),
100 Path: "/etc/rc.local",
104 Data: []byte("#!/bin/sh\n"),
105 TriggerCommands: []string{
109 "/etc/resolv.conf": {
111 Path: "/etc/resolv.conf",
117 Data: []byte("nameserver ::1\n"),
118 TriggerCommands: []string{
119 "echo resolv.conf updated",
125 Mode: os.ModeSymlink | 0777,
128 Data: []byte("doesnt-exist"),
138 Commands: []*safcm.Command{
141 Cmd: "echo command one",
145 Cmd: "echo -n command two",
150 "3 false host groups: all group group3 host1.example.org remove",
151 "3 false host group priorities (descending): host1.example.org",
158 "project-conflict-file",
163 "3 false host groups: all dns host1.example.org",
164 "3 false host group priorities (descending): host1.example.org",
166 fmt.Errorf("groups dns and all both provide \"/etc/resolv.conf\"\nUse 'group_priority' in config.yaml to declare preference"),
169 "conflict: file from detected group",
170 "project-conflict-file",
177 "3 false host groups: all detected_other host2.example.org other",
178 "3 false host group priorities (descending): host2.example.org",
180 fmt.Errorf("groups other and all both provide \"/etc/resolv.conf\"\nUse 'group_priority' in config.yaml to declare preference"),
185 "project-conflict-dir",
190 "3 false host groups: all dns host1.example.org",
191 "3 false host group priorities (descending): host1.example.org",
193 fmt.Errorf("groups dns and all both provide \"/etc\"\nUse 'group_priority' in config.yaml to declare preference"),
196 "conflict: dir from detected group",
197 "project-conflict-dir",
204 "3 false host groups: all detected_other host2.example.org other",
205 "3 false host group priorities (descending): host2.example.org",
207 fmt.Errorf("groups other and all both provide \"/etc\"\nUse 'group_priority' in config.yaml to declare preference"),
212 "project-group-cycle",
217 fmt.Errorf("groups.yaml: cycle while expanding group \"group-b\""),
222 "project-group_priority",
226 Groups: []string{"all", "group-b", "group-a", "host1.example.org"},
227 Files: map[string]*safcm.File{
229 OrigGroup: "host1.example.org",
231 Mode: fs.ModeDir | 0755,
236 OrigGroup: "host1.example.org",
238 Mode: fs.ModeDir | 0755,
242 "/etc/dir-to-file": {
243 OrigGroup: "group-a",
244 Path: "/etc/dir-to-file",
248 Data: []byte("dir-to-file: from group-a\n"),
250 "/etc/dir-to-filex": {
251 OrigGroup: "group-b",
252 Path: "/etc/dir-to-filex",
256 Data: []byte("dir-to-filex\n"),
258 "/etc/dir-to-link": {
259 OrigGroup: "group-a",
260 Path: "/etc/dir-to-link",
261 Mode: fs.ModeSymlink | 0777,
264 Data: []byte("target"),
266 "/etc/dir-to-linkx": {
267 OrigGroup: "group-b",
268 Path: "/etc/dir-to-linkx",
272 Data: []byte("dir-to-linkx\n"),
274 "/etc/file-to-dir": {
275 OrigGroup: "group-a",
276 Path: "/etc/file-to-dir",
277 Mode: fs.ModeDir | 0755,
281 "/etc/file-to-dir/file": {
282 OrigGroup: "group-a",
283 Path: "/etc/file-to-dir/file",
287 Data: []byte("file: from group-a\n"),
289 "/etc/file-to-dir/dir": {
290 OrigGroup: "group-a",
291 Path: "/etc/file-to-dir/dir",
292 Mode: fs.ModeDir | 0755,
296 "/etc/file-to-dir/dir/file2": {
297 OrigGroup: "group-a",
298 Path: "/etc/file-to-dir/dir/file2",
302 Data: []byte("file2: from group-a\n"),
305 OrigGroup: "host1.example.org",
310 Data: []byte("motd: from host1\n"),
315 "3 false host groups: all group-a group-b host1.example.org",
316 "3 false host group priorities (descending): host1.example.org group-a group-b all",
317 `4 false files: "/etc": group group-a overwrites triggers from group group-b`,
318 `4 false files: "/etc": group host1.example.org overwrites triggers from group group-a`,
324 "group_priority (single group)",
325 "project-group_priority-single",
329 Groups: []string{"all", "group-b", "group-a", "host1.example.org"},
330 Files: map[string]*safcm.File{
332 OrigGroup: "group-a",
334 Mode: fs.ModeDir | 0755,
339 OrigGroup: "group-a",
344 Data: []byte("file.txt: from group-a\n"),
349 "3 false host groups: all group-a group-b host1.example.org",
350 "3 false host group priorities (descending): host1.example.org group-a",
356 for _, tc := range tests {
357 t.Run(tc.name, func(t *testing.T) {
358 err = os.Chdir(filepath.Join(cwd,
359 "testdata", tc.project))
364 // `safcm fixperms` in case user has strict umask
365 log.SetOutput(io.Discard)
366 err := MainFixperms()
370 log.SetOutput(os.Stderr)
372 cfg, allHosts, allGroups, err := LoadBaseFiles()
379 host: allHosts.Map[tc.host],
382 allGroups: allGroups,
383 logFunc: func(level safcm.LogLevel, escaped bool, msg string) {
384 events = append(events,
385 fmt.Sprintf("%d %v %s", level, escaped, msg))
389 res, err := s.hostSyncReq(tc.detected)
390 testutil.AssertEqual(t, "res", res, tc.exp)
391 testutil.AssertErrorEqual(t, "err", err, tc.expErr)
392 testutil.AssertEqual(t, "events",
393 events, tc.expEvents)