Skip to content

Commit

Permalink
Fix: testrunner: Explicitly check permissions (#410)
Browse files Browse the repository at this point in the history
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 <cpuguy83@gmail.com>
  • Loading branch information
cpuguy83 authored Oct 25, 2024
1 parent ca4a299 commit 9ea734e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions test/azlinux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2138,17 +2138,33 @@ 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},
},
},
},
}

testEnv.RunTest(ctx, t, func(ctx context.Context, client gwclient.Client) {
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\"")
})
})

Expand Down Expand Up @@ -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},
},
},
},
Expand Down

0 comments on commit 9ea734e

Please sign in to comment.