diff --git a/pkg/prebuild/prepare/core_test.go b/pkg/prebuild/prepare/core_test.go index 3d856cb3..75e5c348 100644 --- a/pkg/prebuild/prepare/core_test.go +++ b/pkg/prebuild/prepare/core_test.go @@ -5,10 +5,104 @@ package prepare import ( + "os" + "os/exec" "slices" "testing" + + "github.com/arduino/go-paths-helper" + "github.com/roddhjav/apparmor.d/pkg/prebuild/cfg" ) +func chdirGitRoot() { + cmd := exec.Command("git", "rev-parse", "--show-toplevel") + out, err := cmd.Output() + if err != nil { + panic(err) + } + root := string(out)[0 : len(out)-1] + if err := os.Chdir(root); err != nil { + panic(err) + } +} + +func TestTask_Apply(t *testing.T) { + tests := []struct { + name string + task Task + want string + wantErr bool + wantFiles paths.PathList + wantNoFiles paths.PathList + }{ + { + name: "synchronise", + task: Tasks["synchronise"], + wantErr: false, + wantFiles: paths.PathList{cfg.RootApparmord.Join("/groups/_full/systemd")}, + }, + { + name: "ignore", + task: Tasks["ignore"], + wantErr: false, + want: "dists/ignore/main.ignore", + }, + { + name: "merge", + task: Tasks["merge"], + wantErr: false, + wantFiles: paths.PathList{cfg.RootApparmord.Join("aa-log")}, + }, + { + name: "configure", + task: Tasks["configure"], + wantErr: false, + }, + { + name: "setflags", + task: Tasks["setflags"], + wantErr: false, + want: "dists/flags/main.flags", + }, + { + name: "systemd-default", + task: Tasks["systemd-default"], + wantErr: false, + wantFiles: paths.PathList{cfg.Root.Join("systemd/system/dbus.service")}, + }, + { + name: "systemd-early", + task: Tasks["systemd-early"], + wantErr: false, + wantFiles: paths.PathList{cfg.Root.Join("systemd/system/pcscd.service")}, + }, + { + name: "fsp", + task: Tasks["fsp"], + wantErr: false, + wantFiles: paths.PathList{cfg.RootApparmord.Join("systemd")}, + }, + } + chdirGitRoot() + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.task.Apply() + if (err != nil) != tt.wantErr { + t.Errorf("Task.Apply() error = %v, wantErr %v", err, tt.wantErr) + return + } + if tt.want != "" && !slices.Contains(got, tt.want) { + t.Errorf("Task.Apply() = %v, want %v", got, tt.want) + } + for _, file := range tt.wantFiles { + if file.NotExist() { + t.Errorf("Task.Apply() = %v, want %v", file, "exist") + } + } + }) + } +} + func TestRegister(t *testing.T) { tests := []struct { name string @@ -22,7 +116,6 @@ func TestRegister(t *testing.T) { }, } for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { Register(tt.names...) for _, name := range tt.names {