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 {
287 config: &config.Config{
292 res := s.formatFileChanges(tc.changes)
294 t.Errorf("%s: res: %s", tc.name,
295 cmp.Diff(tc.exp, res))
300 func TestFormatPackageChanges(t *testing.T) {
304 changes []safcm.PackageChange
311 []safcm.PackageChange{
319 `installed 2 package(s):
328 []safcm.PackageChange{
336 `installed 2 package(s): (dry-run)
345 []safcm.PackageChange{
350 `installed 1 package(s):
356 for _, tc := range tests {
358 config: &config.Config{
363 res := s.formatPackageChanges(tc.changes)
365 t.Errorf("%s: res: %s", tc.name,
366 cmp.Diff(tc.exp, res))
371 func TestFormatServiceChanges(t *testing.T) {
375 changes []safcm.ServiceChange
382 []safcm.ServiceChange{
392 Name: "service-three",
397 `modified 3 service(s):
398 "service-one": started
399 "service-two": enabled
400 "service-three": started, enabled
407 []safcm.ServiceChange{
417 Name: "service-three",
422 `modified 3 service(s): (dry-run)
423 "service-one": started
424 "service-two": enabled
425 "service-three": started, enabled
432 []safcm.ServiceChange{
442 `modified 2 service(s):
444 "\x01": started, enabled
449 for _, tc := range tests {
451 config: &config.Config{
456 res := s.formatServiceChanges(tc.changes)
458 t.Errorf("%s: res: %s", tc.name,
459 cmp.Diff(tc.exp, res))
464 func TestFormatCommandChanges(t *testing.T) {
468 changes []safcm.CommandChange
475 []safcm.CommandChange{
477 Command: "fake command",
478 Output: "fake output",
481 Command: "fake command with no output",
484 Command: "fake command with newline",
485 Output: "fake output\n",
488 Command: "fake command with more output",
489 Output: "fake out\nfake put\nfake\n",
492 Command: "fake failed command",
493 Output: "fake output",
497 `executed 5 command(s):
500 > \ No newline at end of file
501 "fake command with no output"
502 "fake command with newline":
504 "fake command with more output":
508 "fake failed command", failed: "fake error":
510 > \ No newline at end of file
517 []safcm.CommandChange{
519 Command: "fake command",
520 Output: "fake output",
523 `executed 1 command(s): (dry-run)
526 > \ No newline at end of file
533 []safcm.CommandChange{
541 `executed 1 command(s):
542 "\x00", trigger for "\x01", failed: "\x03":
544 > \ No newline at end of file
549 for _, tc := range tests {
551 config: &config.Config{
556 res := s.formatCommandChanges(tc.changes)
558 t.Errorf("%s: res: %s", tc.name,
559 cmp.Diff(tc.exp, res))