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)
295 t.Errorf("res: %s", cmp.Diff(tc.exp, res))
301 func TestFormatPackageChanges(t *testing.T) {
305 changes []safcm.PackageChange
312 []safcm.PackageChange{
320 `installed 2 package(s):
329 []safcm.PackageChange{
337 `installed 2 package(s): (dry-run)
346 []safcm.PackageChange{
351 `installed 1 package(s):
357 for _, tc := range tests {
358 t.Run(tc.name, func(t *testing.T) {
360 config: &config.Config{
365 res := s.formatPackageChanges(tc.changes)
367 t.Errorf("res: %s", cmp.Diff(tc.exp, res))
373 func TestFormatServiceChanges(t *testing.T) {
377 changes []safcm.ServiceChange
384 []safcm.ServiceChange{
394 Name: "service-three",
399 `modified 3 service(s):
400 "service-one": started
401 "service-two": enabled
402 "service-three": started, enabled
409 []safcm.ServiceChange{
419 Name: "service-three",
424 `modified 3 service(s): (dry-run)
425 "service-one": started
426 "service-two": enabled
427 "service-three": started, enabled
434 []safcm.ServiceChange{
444 `modified 2 service(s):
446 "\x01": started, enabled
451 for _, tc := range tests {
452 t.Run(tc.name, func(t *testing.T) {
454 config: &config.Config{
459 res := s.formatServiceChanges(tc.changes)
461 t.Errorf("res: %s", cmp.Diff(tc.exp, res))
467 func TestFormatCommandChanges(t *testing.T) {
472 changes []safcm.CommandChange
480 []safcm.CommandChange{
482 Command: "fake command",
483 Output: "fake output",
486 Command: "fake command with no output",
489 Command: "fake command with newline",
490 Output: "fake output\n",
493 Command: "fake command with more output",
494 Output: "fake out\nfake put\nfake\n",
497 Command: "fake failed command",
498 Output: "fake output",
502 `executed 5 command(s):
505 > \ No newline at end of file
506 "fake command with no output"
507 "fake command with newline":
509 "fake command with more output":
513 "fake failed command", failed: "fake error":
515 > \ No newline at end of file
523 []safcm.CommandChange{
525 Command: "fake command",
526 Output: "fake output",
529 `executed 1 command(s): (dry-run)
532 > \ No newline at end of file
540 []safcm.CommandChange{
542 Command: "fake command",
543 Output: "fake output",
546 Command: "fake command with no output",
549 Command: "fake command with newline",
550 Output: "fake output\n",
553 Command: "fake command with more output",
554 Output: "fake out\nfake put\nfake\n",
557 Command: "fake failed command",
558 Output: "fake output",
562 `executed 5 command(s), 1 with no output:
565 > \ No newline at end of file
566 "fake command with newline":
568 "fake command with more output":
572 "fake failed command", failed: "fake error":
574 > \ No newline at end of file
579 "quiet (only quiet commands)",
582 []safcm.CommandChange{
584 Command: "fake command with no output",
587 Command: "fake command with no output",
590 `executed 2 command(s), 2 with no output
595 "quiet (quiet with errors)",
598 []safcm.CommandChange{
600 Command: "fake command with no output but error",
604 Command: "fake command with no output",
607 `executed 2 command(s), 1 with no output:
608 "fake command with no output but error", failed: "fake error"
616 []safcm.CommandChange{
618 Command: "fake command",
621 Command: "fake command with no output",
624 Command: "fake command with newline",
627 Command: "fake command with more output",
630 Command: "fake failed command",
633 `executed 5 command(s): (dry-run)
635 "fake command with no output"
636 "fake command with newline"
637 "fake command with more output"
638 "fake failed command"
646 []safcm.CommandChange{
654 `executed 1 command(s):
655 "\x00", trigger for "\x01", failed: "\x03":
657 > \ No newline at end of file
662 for _, tc := range tests {
663 t.Run(tc.name, func(t *testing.T) {
665 config: &config.Config{
671 res := s.formatCommandChanges(tc.changes)
673 t.Errorf("res: %s", cmp.Diff(tc.exp, res))