Skip to content

Commit

Permalink
cmd/snapd-apparmor: Mock AppArmor parser search path in tests
Browse files Browse the repository at this point in the history
This ensures the mocked apparmor_parser (via testutil.MockCommand()) is found
and used during the tests.

Signed-off-by: Alex Murray <[email protected]>
  • Loading branch information
alexmurray committed Sep 21, 2022
1 parent 855550b commit 7d1efc1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/snapd-apparmor/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ var (
IsContainer = isContainer
IsContainerWithInternalPolicy = isContainerWithInternalPolicy
LoadAppArmorProfiles = loadAppArmorProfiles
MockParserSearchPath = mockParserSearchPath
)
5 changes: 4 additions & 1 deletion cmd/snapd-apparmor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ func isWSL() bool {
// container's boot process to experience failed policy loads but the boot
// process should continue without any loss of functionality. This is an
// unsupported configuration that cannot be properly handled by this function.
//
func isContainerWithInternalPolicy() bool {
var appArmorSecurityFSPath = filepath.Join(dirs.GlobalRootDir, "/sys/kernel/security/apparmor")
var nsStackedPath = filepath.Join(appArmorSecurityFSPath, ".ns_stacked")
Expand Down Expand Up @@ -171,3 +170,7 @@ func run() error {

return loadAppArmorProfiles()
}

func mockParserSearchPath(parserSearchPath string) (restore func()) {
return apparmor_sandbox.MockParserSearchPath(parserSearchPath)
}
9 changes: 8 additions & 1 deletion cmd/snapd-apparmor/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ func (s *mainSuite) TestIsContainerWithInternalPolicy(c *C) {
func (s *mainSuite) TestLoadAppArmorProfiles(c *C) {
parserCmd := testutil.MockCommand(c, "apparmor_parser", "")
defer parserCmd.Restore()
restore := snapd_apparmor.MockParserSearchPath(parserCmd.BinDir())
defer restore()
err := snapd_apparmor.LoadAppArmorProfiles()
c.Assert(err, IsNil)
// since no profiles to load the parser should not have been called
Expand Down Expand Up @@ -119,7 +121,10 @@ func (s *mainSuite) TestLoadAppArmorProfiles(c *C) {
profile}})

// test error case
testutil.MockCommand(c, "apparmor_parser", "echo mocked parser failed > /dev/stderr; exit 1")
parserCmd = testutil.MockCommand(c, "apparmor_parser", "echo mocked parser failed > /dev/stderr; exit 1")
defer parserCmd.Restore()
restore = snapd_apparmor.MockParserSearchPath(parserCmd.BinDir())
defer restore()
err = snapd_apparmor.LoadAppArmorProfiles()
c.Check(err.Error(), Equals, "cannot load apparmor profiles: exit status 1\napparmor_parser output:\nmocked parser failed\n")

Expand Down Expand Up @@ -201,6 +206,8 @@ func (s *integrationSuite) SetUpTest(c *C) {
// simulate a single profile to load
s.parserCmd = testutil.MockCommand(c, "apparmor_parser", "")
s.AddCleanup(s.parserCmd.Restore)
restore := snapd_apparmor.MockParserSearchPath(s.parserCmd.BinDir())
s.AddCleanup(restore)
err := os.MkdirAll(dirs.SnapAppArmorDir, 0755)
c.Assert(err, IsNil)
profile := filepath.Join(dirs.SnapAppArmorDir, "foo")
Expand Down

0 comments on commit 7d1efc1

Please sign in to comment.