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 "ruderich.org/simon/safcm"
23 "ruderich.org/simon/safcm/cmd/safcm/config"
24 "ruderich.org/simon/safcm/testutil"
27 func TestFormatFileChanges(t *testing.T) {
31 changes []safcm.FileChange
40 Path: "created: file",
42 New: safcm.FileChangeInfo{
51 Path: "created: link",
53 New: safcm.FileChangeInfo{
54 Mode: fs.ModeSymlink | 0777,
62 Path: "type change: file -> dir",
63 Old: safcm.FileChangeInfo{
70 New: safcm.FileChangeInfo{
71 Mode: fs.ModeDir | 0751,
77 DataDiff: `@@ -1,2 +1 @@
84 Old: safcm.FileChangeInfo{
91 New: safcm.FileChangeInfo{
100 Path: "group change",
101 Old: safcm.FileChangeInfo{
108 New: safcm.FileChangeInfo{
118 Old: safcm.FileChangeInfo{
125 New: safcm.FileChangeInfo{
134 Path: "mode change (setuid)",
135 Old: safcm.FileChangeInfo{
142 New: safcm.FileChangeInfo{
143 Mode: 0755 | fs.ModeSetuid,
151 Path: "content change",
152 Old: safcm.FileChangeInfo{
159 New: safcm.FileChangeInfo{
166 DataDiff: `@@ -1,2 +1,2 @@
173 Path: "multiple changes",
174 Old: safcm.FileChangeInfo{
181 New: safcm.FileChangeInfo{
182 Mode: fs.ModeDir | 0755,
188 DataDiff: `@@ -1,2 +1 @@
195 "created: file": created, file, user(1000) group(2000), 0644
196 "created: link": created, symlink, user(1000) group(2000), 0777
197 "type change: file -> dir": file -> dir
201 "user change": user(1000) group(2000) -> user2(1001) group(2000)
202 "group change": user(1000) group(2000) -> user(1000) group2(2001)
203 "mode change": 0755 -> 0750
204 "mode change (setuid)": 0755 -> 04755
210 "multiple changes": file -> dir, user(1000) group(2000) -> user2(1001) group2(2001), 0644 -> 0755
224 New: safcm.FileChangeInfo{
233 `changed 1 file(s): (dry-run)
234 "file": created, file, user(1000) group(2000), 0644
245 New: safcm.FileChangeInfo{
256 Old: safcm.FileChangeInfo{
263 New: safcm.FileChangeInfo{
274 "\x00": created, invalid type dLDpSc?---------, \x01(-1) \x02(-2), 07777
276 \ No newline at end of file
277 "\x00": file -> invalid type dLDpSc?---------, \x01(-1) \x02(-2) -> \x03(-3) \x04(-4), 0 -> 07777
279 \ No newline at end of file
284 for _, tc := range tests {
285 t.Run(tc.name, func(t *testing.T) {
287 config: &config.Config{
292 res := s.formatFileChanges(tc.changes)
293 testutil.AssertEqual(t, "res", res, tc.exp)
298 func TestFormatPackageChanges(t *testing.T) {
302 changes []safcm.PackageChange
309 []safcm.PackageChange{
317 `installed 2 package(s):
326 []safcm.PackageChange{
334 `installed 2 package(s): (dry-run)
343 []safcm.PackageChange{
348 `installed 1 package(s):
354 for _, tc := range tests {
355 t.Run(tc.name, func(t *testing.T) {
357 config: &config.Config{
362 res := s.formatPackageChanges(tc.changes)
363 testutil.AssertEqual(t, "res", res, tc.exp)
368 func TestFormatServiceChanges(t *testing.T) {
372 changes []safcm.ServiceChange
379 []safcm.ServiceChange{
389 Name: "service-three",
394 `modified 3 service(s):
395 "service-one": started
396 "service-two": enabled
397 "service-three": started, enabled
404 []safcm.ServiceChange{
414 Name: "service-three",
419 `modified 3 service(s): (dry-run)
420 "service-one": started
421 "service-two": enabled
422 "service-three": started, enabled
429 []safcm.ServiceChange{
439 `modified 2 service(s):
441 "\x01": started, enabled
446 for _, tc := range tests {
447 t.Run(tc.name, func(t *testing.T) {
449 config: &config.Config{
454 res := s.formatServiceChanges(tc.changes)
455 testutil.AssertEqual(t, "res", res, tc.exp)
460 func TestFormatCommandChanges(t *testing.T) {
465 changes []safcm.CommandChange
473 []safcm.CommandChange{
475 Command: "fake command",
476 Output: "fake output",
479 Command: "fake command with no output",
482 Command: "fake command with newline",
483 Output: "fake output\n",
486 Command: "fake command with more output",
487 Output: "fake out\nfake put\nfake\n",
490 Command: "fake failed command",
491 Output: "fake output",
495 `executed 5 command(s):
498 > \ No newline at end of file
499 "fake command with no output"
500 "fake command with newline":
502 "fake command with more output":
506 "fake failed command", failed: "fake error":
508 > \ No newline at end of file
516 []safcm.CommandChange{
518 Command: "fake command",
519 Output: "fake output",
522 `executed 1 command(s): (dry-run)
525 > \ No newline at end of file
533 []safcm.CommandChange{
535 Command: "fake command",
536 Output: "fake output",
539 Command: "fake command with no output",
542 Command: "fake command with newline",
543 Output: "fake output\n",
546 Command: "fake command with more output",
547 Output: "fake out\nfake put\nfake\n",
550 Command: "fake failed command",
551 Output: "fake output",
555 `executed 5 command(s), 1 with no output:
558 > \ No newline at end of file
559 "fake command with newline":
561 "fake command with more output":
565 "fake failed command", failed: "fake error":
567 > \ No newline at end of file
572 "quiet (only quiet commands)",
575 []safcm.CommandChange{
577 Command: "fake command with no output",
580 Command: "fake command with no output",
583 `executed 2 command(s), 2 with no output
588 "quiet (quiet with errors)",
591 []safcm.CommandChange{
593 Command: "fake command with no output but error",
597 Command: "fake command with no output",
600 `executed 2 command(s), 1 with no output:
601 "fake command with no output but error", failed: "fake error"
609 []safcm.CommandChange{
611 Command: "fake command",
614 Command: "fake command with no output",
617 Command: "fake command with newline",
620 Command: "fake command with more output",
623 Command: "fake failed command",
626 `executed 5 command(s): (dry-run)
628 "fake command with no output"
629 "fake command with newline"
630 "fake command with more output"
631 "fake failed command"
639 []safcm.CommandChange{
647 `executed 1 command(s):
648 "\x00", trigger for "\x01", failed: "\x03":
650 > \ No newline at end of file
655 for _, tc := range tests {
656 t.Run(tc.name, func(t *testing.T) {
658 config: &config.Config{
664 res := s.formatCommandChanges(tc.changes)
665 testutil.AssertEqual(t, "res", res, tc.exp)