From 9ea734e4adc4c472fb4add64737e4e947e952d11 Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Fri, 25 Oct 2024 11:17:21 -0700 Subject: [PATCH] Fix: testrunner: Explicitly check permissions (#410) The file checker takes an `os.FileMode` which contains things other than just permissions. Make the checker explictly call `mode.Perm()` to ensure it has *just* the permissions bits when checking. This fixes an issue when checking permission on a directory. Signed-off-by: Brian Goff --- spec.go | 5 +++-- test/azlinux_test.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/spec.go b/spec.go index d09506201..34c15b274 100644 --- a/spec.go +++ b/spec.go @@ -621,8 +621,9 @@ func (c FileCheckOutput) Check(dt string, mode fs.FileMode, isDir bool, p string return &CheckOutputError{Kind: "mode", Expected: "ModeFile", Actual: "ModeDir", Path: p} } - if c.Permissions != 0 && c.Permissions != mode { - return &CheckOutputError{Kind: "permissions", Expected: c.Permissions.String(), Actual: mode.String(), Path: p} + perm := mode.Perm() + if c.Permissions != 0 && c.Permissions != perm { + return &CheckOutputError{Kind: "permissions", Expected: c.Permissions.String(), Actual: perm.String(), Path: p} } return c.CheckOutput.Check(dt, p) diff --git a/test/azlinux_test.go b/test/azlinux_test.go index 7f8fe467c..40df5579c 100644 --- a/test/azlinux_test.go +++ b/test/azlinux_test.go @@ -2138,6 +2138,18 @@ func testLinuxPackageTestsFail(ctx context.Context, t *testing.T, cfg testLinuxC "/non-existing-file": {}, }, }, + { + Name: "Test that permissions check fails the build", + Files: map[string]dalec.FileCheckOutput{ + "/": {Permissions: 0o644, IsDir: true}, + }, + }, + { + Name: "Test that dir check fails the build", + Files: map[string]dalec.FileCheckOutput{ + "/": {IsDir: false}, + }, + }, }, } @@ -2145,10 +2157,14 @@ func testLinuxPackageTestsFail(ctx context.Context, t *testing.T, cfg testLinuxC sr := newSolveRequest(withSpec(ctx, t, spec), withBuildTarget(cfg.Target.Package)) _, err := client.Solve(ctx, sr) assert.ErrorContains(t, err, "lstat /non-existing-file: no such file or directory") + assert.ErrorContains(t, err, "expected \"/\" permissions \"-rw-r--r--\", got \"-rwxr-xr-x\"") + assert.ErrorContains(t, err, "expected \"/\" mode \"ModeFile\", got \"ModeDir\"") sr = newSolveRequest(withSpec(ctx, t, spec), withBuildTarget(cfg.Target.Container)) _, err = client.Solve(ctx, sr) assert.ErrorContains(t, err, "lstat /non-existing-file: no such file or directory") + assert.ErrorContains(t, err, "expected \"/\" permissions \"-rw-r--r--\", got \"-rwxr-xr-x\"") + assert.ErrorContains(t, err, "expected \"/\" mode \"ModeFile\", got \"ModeDir\"") }) }) @@ -2181,6 +2197,8 @@ func testLinuxPackageTestsFail(ctx context.Context, t *testing.T, cfg testLinuxC Name: "Test that tests fail the build", Files: map[string]dalec.FileCheckOutput{ "/usr/share/test-file": {}, + // Make sure dir permissions are chcked correctly. + "/usr/share": {IsDir: true, Permissions: 0o755}, }, }, },