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"),
141 "echo -n command two",
145 "host1.example.org: <nil> 3 host groups: all group group3 host1.example.org remove",
146 "host1.example.org: <nil> 3 host group priorities (desc. order): host1.example.org",
152 "project: host1 (log level info)",
165 Files: map[string]*safcm.File{
166 "/": &safcm.File{Path: "/",
168 Mode: fs.ModeDir | 0755 | fs.ModeSetgid,
171 TriggerCommands: []string{
178 Mode: fs.ModeDir | 0755,
182 "/etc/.hidden": &safcm.File{
184 Path: "/etc/.hidden",
185 Mode: 0100 | fs.ModeSetuid | fs.ModeSetgid | fs.ModeSticky,
190 "/etc/motd": &safcm.File{
196 Data: []byte("Welcome to Host ONE\n\n\n\n\n\nall\n\n\nhost1.example.org\n\n\n\n"),
198 "/etc/rc.local": &safcm.File{
200 Path: "/etc/rc.local",
204 Data: []byte("#!/bin/sh\n"),
205 TriggerCommands: []string{
209 "/etc/resolv.conf": &safcm.File{
211 Path: "/etc/resolv.conf",
217 Data: []byte("nameserver ::1\n"),
218 TriggerCommands: []string{
219 "echo resolv.conf updated",
222 "/etc/test": &safcm.File{
225 Mode: os.ModeSymlink | 0777,
228 Data: []byte("doesnt-exist"),
240 "echo -n command two",
249 "project-conflict-file",
255 "host1.example.org: <nil> 3 host groups: all dns host1.example.org",
256 "host1.example.org: <nil> 3 host group priorities (desc. order): host1.example.org",
258 fmt.Errorf("groups dns and all both provide file \"/etc/resolv.conf\"\nUse 'group_order' in config.yaml to declare preference"),
261 "conflict: file from detected group",
262 "project-conflict-file",
270 "host2.example.org: <nil> 3 host groups: all detected_other host2.example.org other",
271 "host2.example.org: <nil> 3 host group priorities (desc. order): host2.example.org",
273 fmt.Errorf("groups other and all both provide file \"/etc/resolv.conf\"\nUse 'group_order' in config.yaml to declare preference"),
278 "project-conflict-dir",
284 "host1.example.org: <nil> 3 host groups: all dns host1.example.org",
285 "host1.example.org: <nil> 3 host group priorities (desc. order): host1.example.org",
287 fmt.Errorf("groups dns and all both provide file \"/etc\"\nUse 'group_order' in config.yaml to declare preference"),
290 "conflict: dir from detected group",
291 "project-conflict-dir",
299 "host2.example.org: <nil> 3 host groups: all detected_other host2.example.org other",
300 "host2.example.org: <nil> 3 host group priorities (desc. order): host2.example.org",
302 fmt.Errorf("groups other and all both provide file \"/etc\"\nUse 'group_order' in config.yaml to declare preference"),
307 "project-group-cycle",
313 fmt.Errorf("groups.yaml: cycle while expanding group \"group-b\""),
318 "project-group_order",
323 Groups: []string{"all", "group-b", "group-a", "host1.example.org"},
324 Files: map[string]*safcm.File{
327 Mode: fs.ModeDir | 0755,
330 OrigGroup: "host1.example.org",
334 Mode: fs.ModeDir | 0755,
337 OrigGroup: "host1.example.org",
339 "/etc/dir-to-file": {
340 Path: "/etc/dir-to-file",
344 Data: []byte("dir-to-file: from group-a\n"),
345 OrigGroup: "group-a",
347 "/etc/dir-to-filex": {
348 OrigGroup: "group-b",
349 Path: "/etc/dir-to-filex",
353 Data: []byte("dir-to-filex\n"),
355 "/etc/dir-to-link": {
356 Path: "/etc/dir-to-link",
357 Mode: fs.ModeSymlink | 0777,
360 Data: []byte("target"),
361 OrigGroup: "group-a",
363 "/etc/dir-to-linkx": {
364 OrigGroup: "group-b",
365 Path: "/etc/dir-to-linkx",
369 Data: []byte("dir-to-linkx\n"),
371 "/etc/file-to-dir": {
372 Path: "/etc/file-to-dir",
373 Mode: fs.ModeDir | 0755,
376 OrigGroup: "group-a",
378 "/etc/file-to-dir/file": {
379 Path: "/etc/file-to-dir/file",
383 Data: []byte("file: from group-a\n"),
384 OrigGroup: "group-a",
386 "/etc/file-to-dir/dir": {
387 Path: "/etc/file-to-dir/dir",
388 Mode: fs.ModeDir | 0755,
391 OrigGroup: "group-a",
393 "/etc/file-to-dir/dir/file2": {
394 Path: "/etc/file-to-dir/dir/file2",
398 Data: []byte("file2: from group-a\n"),
399 OrigGroup: "group-a",
406 Data: []byte("motd: from host1\n"),
407 OrigGroup: "host1.example.org",
412 "host1.example.org: <nil> 3 host groups: all group-a group-b host1.example.org",
413 "host1.example.org: <nil> 3 host group priorities (desc. order): host1.example.org group-a group-b all",
414 `host1.example.org: <nil> 4 files: "/etc": group group-a overwrites triggers from group group-b`,
415 `host1.example.org: <nil> 4 files: "/etc": group host1.example.org overwrites triggers from group group-a`,
421 for _, tc := range tests {
422 t.Run(tc.name, func(t *testing.T) {
423 err = os.Chdir(filepath.Join(cwd,
424 "testdata", tc.project))
429 // `safcm fixperms` in case user has strict umask
430 log.SetOutput(io.Discard)
431 err := MainFixperms()
435 log.SetOutput(os.Stderr)
437 cfg, allHosts, allGroups, err := LoadBaseFiles()
441 cfg.LogLevel = tc.level
444 ch := make(chan Event)
445 done := make(chan struct{})
452 if x.ConnEvent.Type != 0 {
453 panic("unexpected ConnEvent")
455 events = append(events,
456 fmt.Sprintf("%s: %v %d %s",
458 x.Error, x.Log.Level,
465 host: allHosts.Map[tc.host],
468 allGroups: allGroups,
472 res, err := s.hostSyncReq(tc.detected)
474 testutil.AssertEqual(t, "res", res, tc.exp)
475 testutil.AssertErrorEqual(t, "err", err, tc.expErr)
479 testutil.AssertEqual(t, "events",
480 events, tc.expEvents)