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/>.
27 "ruderich.org/simon/safcm"
28 "ruderich.org/simon/safcm/testutil"
31 func TestHostSyncReq(t *testing.T) {
32 cwd, err := os.Getwd()
49 // NOTE: Also update MsgSyncReq in safcm-remote test cases
50 // when changing anything here!
66 Files: map[string]*safcm.File{
67 "/": &safcm.File{Path: "/",
69 Mode: fs.ModeDir | 0755 | fs.ModeSetgid,
72 TriggerCommands: []string{
79 Mode: fs.ModeDir | 0755,
83 "/etc/.hidden": &safcm.File{
86 Mode: 0100 | fs.ModeSetuid | fs.ModeSetgid | fs.ModeSticky,
91 "/etc/motd": &safcm.File{
97 Data: []byte("Welcome to Host ONE\n\n\n\n\n\nall\n\n\nhost1.example.org\n\n\n\n"),
99 "/etc/rc.local": &safcm.File{
101 Path: "/etc/rc.local",
105 Data: []byte("#!/bin/sh\n"),
106 TriggerCommands: []string{
110 "/etc/resolv.conf": &safcm.File{
112 Path: "/etc/resolv.conf",
118 Data: []byte("nameserver ::1\n"),
119 TriggerCommands: []string{
120 "echo resolv.conf updated",
123 "/etc/test": &safcm.File{
126 Mode: os.ModeSymlink | 0777,
129 Data: []byte("doesnt-exist"),
139 Commands: []*safcm.Command{
141 Cmd: "echo command one",
144 Cmd: "echo -n command two",
149 "host1.example.org: <nil> 3 host groups: all group group3 host1.example.org remove",
150 "host1.example.org: <nil> 3 host group priorities (desc. order): host1.example.org",
156 "project: host1 (log level info)",
169 Files: map[string]*safcm.File{
170 "/": &safcm.File{Path: "/",
172 Mode: fs.ModeDir | 0755 | fs.ModeSetgid,
175 TriggerCommands: []string{
182 Mode: fs.ModeDir | 0755,
186 "/etc/.hidden": &safcm.File{
188 Path: "/etc/.hidden",
189 Mode: 0100 | fs.ModeSetuid | fs.ModeSetgid | fs.ModeSticky,
194 "/etc/motd": &safcm.File{
200 Data: []byte("Welcome to Host ONE\n\n\n\n\n\nall\n\n\nhost1.example.org\n\n\n\n"),
202 "/etc/rc.local": &safcm.File{
204 Path: "/etc/rc.local",
208 Data: []byte("#!/bin/sh\n"),
209 TriggerCommands: []string{
213 "/etc/resolv.conf": &safcm.File{
215 Path: "/etc/resolv.conf",
221 Data: []byte("nameserver ::1\n"),
222 TriggerCommands: []string{
223 "echo resolv.conf updated",
226 "/etc/test": &safcm.File{
229 Mode: os.ModeSymlink | 0777,
232 Data: []byte("doesnt-exist"),
242 Commands: []*safcm.Command{
244 Cmd: "echo command one",
247 Cmd: "echo -n command two",
257 "project-conflict-file",
263 "host1.example.org: <nil> 3 host groups: all dns host1.example.org",
264 "host1.example.org: <nil> 3 host group priorities (desc. order): host1.example.org",
266 fmt.Errorf("groups dns and all both provide file \"/etc/resolv.conf\"\nUse 'group_order' in config.yaml to declare preference"),
269 "conflict: file from detected group",
270 "project-conflict-file",
278 "host2.example.org: <nil> 3 host groups: all detected_other host2.example.org other",
279 "host2.example.org: <nil> 3 host group priorities (desc. order): host2.example.org",
281 fmt.Errorf("groups other and all both provide file \"/etc/resolv.conf\"\nUse 'group_order' in config.yaml to declare preference"),
286 "project-conflict-dir",
292 "host1.example.org: <nil> 3 host groups: all dns host1.example.org",
293 "host1.example.org: <nil> 3 host group priorities (desc. order): host1.example.org",
295 fmt.Errorf("groups dns and all both provide file \"/etc\"\nUse 'group_order' in config.yaml to declare preference"),
298 "conflict: dir from detected group",
299 "project-conflict-dir",
307 "host2.example.org: <nil> 3 host groups: all detected_other host2.example.org other",
308 "host2.example.org: <nil> 3 host group priorities (desc. order): host2.example.org",
310 fmt.Errorf("groups other and all both provide file \"/etc\"\nUse 'group_order' in config.yaml to declare preference"),
315 "project-group-cycle",
321 fmt.Errorf("groups.yaml: cycle while expanding group \"group-b\""),
326 "project-group_order",
331 Groups: []string{"all", "group-b", "group-a", "host1.example.org"},
332 Files: map[string]*safcm.File{
335 Mode: fs.ModeDir | 0755,
338 OrigGroup: "host1.example.org",
342 Mode: fs.ModeDir | 0755,
345 OrigGroup: "host1.example.org",
347 "/etc/dir-to-file": {
348 Path: "/etc/dir-to-file",
352 Data: []byte("dir-to-file: from group-a\n"),
353 OrigGroup: "group-a",
355 "/etc/dir-to-filex": {
356 OrigGroup: "group-b",
357 Path: "/etc/dir-to-filex",
361 Data: []byte("dir-to-filex\n"),
363 "/etc/dir-to-link": {
364 Path: "/etc/dir-to-link",
365 Mode: fs.ModeSymlink | 0777,
368 Data: []byte("target"),
369 OrigGroup: "group-a",
371 "/etc/dir-to-linkx": {
372 OrigGroup: "group-b",
373 Path: "/etc/dir-to-linkx",
377 Data: []byte("dir-to-linkx\n"),
379 "/etc/file-to-dir": {
380 Path: "/etc/file-to-dir",
381 Mode: fs.ModeDir | 0755,
384 OrigGroup: "group-a",
386 "/etc/file-to-dir/file": {
387 Path: "/etc/file-to-dir/file",
391 Data: []byte("file: from group-a\n"),
392 OrigGroup: "group-a",
394 "/etc/file-to-dir/dir": {
395 Path: "/etc/file-to-dir/dir",
396 Mode: fs.ModeDir | 0755,
399 OrigGroup: "group-a",
401 "/etc/file-to-dir/dir/file2": {
402 Path: "/etc/file-to-dir/dir/file2",
406 Data: []byte("file2: from group-a\n"),
407 OrigGroup: "group-a",
414 Data: []byte("motd: from host1\n"),
415 OrigGroup: "host1.example.org",
420 "host1.example.org: <nil> 3 host groups: all group-a group-b host1.example.org",
421 "host1.example.org: <nil> 3 host group priorities (desc. order): host1.example.org group-a group-b all",
422 `host1.example.org: <nil> 4 files: "/etc": group group-a overwrites triggers from group group-b`,
423 `host1.example.org: <nil> 4 files: "/etc": group host1.example.org overwrites triggers from group group-a`,
429 for _, tc := range tests {
430 t.Run(tc.name, func(t *testing.T) {
431 err = os.Chdir(filepath.Join(cwd,
432 "testdata", tc.project))
437 // `safcm fixperms` in case user has strict umask
438 log.SetOutput(io.Discard)
439 err := MainFixperms()
443 log.SetOutput(os.Stderr)
445 cfg, allHosts, allGroups, err := LoadBaseFiles()
449 cfg.LogLevel = tc.level
452 ch := make(chan Event)
453 done := make(chan struct{})
460 if x.ConnEvent.Type != 0 {
461 panic("unexpected ConnEvent")
463 events = append(events,
464 fmt.Sprintf("%s: %v %d %s",
466 x.Error, x.Log.Level,
473 host: allHosts.Map[tc.host],
476 allGroups: allGroups,
480 res, err := s.hostSyncReq(tc.detected)
482 testutil.AssertEqual(t, "res", res, tc.exp)
483 testutil.AssertErrorEqual(t, "err", err, tc.expErr)
487 testutil.AssertEqual(t, "events",
488 events, tc.expEvents)