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/>.
22 "github.com/google/go-cmp/cmp"
24 "ruderich.org/simon/safcm"
25 "ruderich.org/simon/safcm/cmd/safcm/config"
28 func TestFormatFileChanges(t *testing.T) {
32 changes []safcm.FileChange
41 Path: "created: file",
43 New: safcm.FileChangeInfo{
52 Path: "created: link",
54 New: safcm.FileChangeInfo{
55 Mode: fs.ModeSymlink | 0777,
63 Path: "type change: file -> dir",
64 Old: safcm.FileChangeInfo{
71 New: safcm.FileChangeInfo{
72 Mode: fs.ModeDir | 0751,
78 DataDiff: `@@ -1,2 +1 @@
85 Old: safcm.FileChangeInfo{
92 New: safcm.FileChangeInfo{
101 Path: "group change",
102 Old: safcm.FileChangeInfo{
109 New: safcm.FileChangeInfo{
119 Old: safcm.FileChangeInfo{
126 New: safcm.FileChangeInfo{
135 Path: "mode change (setuid)",
136 Old: safcm.FileChangeInfo{
143 New: safcm.FileChangeInfo{
144 Mode: 0755 | fs.ModeSetuid,
152 Path: "content change",
153 Old: safcm.FileChangeInfo{
160 New: safcm.FileChangeInfo{
167 DataDiff: `@@ -1,2 +1,2 @@
174 Path: "multiple changes",
175 Old: safcm.FileChangeInfo{
182 New: safcm.FileChangeInfo{
183 Mode: fs.ModeDir | 0755,
189 DataDiff: `@@ -1,2 +1 @@
196 "created: file": created, file, user(1000) group(2000), 0644
197 "created: link": created, symlink, user(1000) group(2000), 0777
198 "type change: file -> dir": file -> dir
202 "user change": user(1000) group(2000) -> user2(1001) group(2000)
203 "group change": user(1000) group(2000) -> user(1000) group2(2001)
204 "mode change": 0755 -> 0750
205 "mode change (setuid)": 0755 -> 04755
211 "multiple changes": file -> dir, user(1000) group(2000) -> user2(1001) group2(2001), 0644 -> 0755
225 New: safcm.FileChangeInfo{
234 `changed 1 file(s): (dry-run)
235 "file": created, file, user(1000) group(2000), 0644
246 New: safcm.FileChangeInfo{
257 Old: safcm.FileChangeInfo{
264 New: safcm.FileChangeInfo{
275 "\x00": created, invalid type dLDpSc?---------, \x01(-1) \x02(-2), 07777
277 \ No newline at end of file
278 "\x00": file -> invalid type dLDpSc?---------, \x01(-1) \x02(-2) -> \x03(-3) \x04(-4), 0 -> 07777
280 \ No newline at end of file
285 for _, tc := range tests {
286 t.Run(tc.name, func(t *testing.T) {
288 config: &config.Config{
293 res := s.formatFileChanges(tc.changes)
296 cmp.Diff(tc.exp, res))
302 func TestFormatPackageChanges(t *testing.T) {
306 changes []safcm.PackageChange
313 []safcm.PackageChange{
321 `installed 2 package(s):
330 []safcm.PackageChange{
338 `installed 2 package(s): (dry-run)
347 []safcm.PackageChange{
352 `installed 1 package(s):
358 for _, tc := range tests {
359 t.Run(tc.name, func(t *testing.T) {
361 config: &config.Config{
366 res := s.formatPackageChanges(tc.changes)
369 cmp.Diff(tc.exp, res))
375 func TestFormatServiceChanges(t *testing.T) {
379 changes []safcm.ServiceChange
386 []safcm.ServiceChange{
396 Name: "service-three",
401 `modified 3 service(s):
402 "service-one": started
403 "service-two": enabled
404 "service-three": started, enabled
411 []safcm.ServiceChange{
421 Name: "service-three",
426 `modified 3 service(s): (dry-run)
427 "service-one": started
428 "service-two": enabled
429 "service-three": started, enabled
436 []safcm.ServiceChange{
446 `modified 2 service(s):
448 "\x01": started, enabled
453 for _, tc := range tests {
454 t.Run(tc.name, func(t *testing.T) {
456 config: &config.Config{
461 res := s.formatServiceChanges(tc.changes)
464 cmp.Diff(tc.exp, res))
470 func TestFormatCommandChanges(t *testing.T) {
475 changes []safcm.CommandChange
483 []safcm.CommandChange{
485 Command: "fake command",
486 Output: "fake output",
489 Command: "fake command with no output",
492 Command: "fake command with newline",
493 Output: "fake output\n",
496 Command: "fake command with more output",
497 Output: "fake out\nfake put\nfake\n",
500 Command: "fake failed command",
501 Output: "fake output",
505 `executed 5 command(s):
508 > \ No newline at end of file
509 "fake command with no output"
510 "fake command with newline":
512 "fake command with more output":
516 "fake failed command", failed: "fake error":
518 > \ No newline at end of file
526 []safcm.CommandChange{
528 Command: "fake command",
529 Output: "fake output",
532 `executed 1 command(s): (dry-run)
535 > \ No newline at end of file
543 []safcm.CommandChange{
545 Command: "fake command",
546 Output: "fake output",
549 Command: "fake command with no output",
552 Command: "fake command with newline",
553 Output: "fake output\n",
556 Command: "fake command with more output",
557 Output: "fake out\nfake put\nfake\n",
560 Command: "fake failed command",
561 Output: "fake output",
565 `executed 5 command(s), 1 with no output:
568 > \ No newline at end of file
569 "fake command with newline":
571 "fake command with more output":
575 "fake failed command", failed: "fake error":
577 > \ No newline at end of file
582 "quiet (only quiet commands)",
585 []safcm.CommandChange{
587 Command: "fake command with no output",
590 Command: "fake command with no output",
593 `executed 2 command(s), 2 with no output
598 "quiet (quiet with errors)",
601 []safcm.CommandChange{
603 Command: "fake command with no output but error",
607 Command: "fake command with no output",
610 `executed 2 command(s), 1 with no output:
611 "fake command with no output but error", failed: "fake error"
619 []safcm.CommandChange{
621 Command: "fake command",
624 Command: "fake command with no output",
627 Command: "fake command with newline",
630 Command: "fake command with more output",
633 Command: "fake failed command",
636 `executed 5 command(s): (dry-run)
638 "fake command with no output"
639 "fake command with newline"
640 "fake command with more output"
641 "fake failed command"
649 []safcm.CommandChange{
657 `executed 1 command(s):
658 "\x00", trigger for "\x01", failed: "\x03":
660 > \ No newline at end of file
665 for _, tc := range tests {
666 t.Run(tc.name, func(t *testing.T) {
668 config: &config.Config{
674 res := s.formatCommandChanges(tc.changes)
677 cmp.Diff(tc.exp, res))