Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interfaces/builtin, testutil: skip unit test when apparmor_parser is not in PATH #14951

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions interfaces/builtin/docker_support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
package builtin_test

import (
"errors"
"os/exec"

"github.com/snapcore/snapd/strutil"
. "gopkg.in/check.v1"

Expand Down Expand Up @@ -868,6 +871,9 @@ ptrace (read, trace) peer=unconfined,

// Profile existing profile
expectedHash, err := testutil.AppArmorParseAndHashHelper("#include <tunables/global> \nprofile docker_support {" + privilegedProfile + "}")
if err != nil && errors.Is(err, exec.ErrNotFound) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The left hand side is redundant

c.Skip(err.Error())
}
c.Assert(err, IsNil)

// Profile generated using GenerateAAREExclusionPatterns
Expand Down
16 changes: 10 additions & 6 deletions testutil/apparmor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,27 @@
)

func AppArmorParseAndHashHelper(profile string) (string, error) {
p, err := exec.LookPath("apparmor_parser")
if err != nil {
return "", fmt.Errorf("cannot find apparmor_parser in $PATH: %w", err)
}

Check warning on line 34 in testutil/apparmor.go

View check run for this annotation

Codecov / codecov/patch

testutil/apparmor.go#L31-L34

Added lines #L31 - L34 were not covered by tests
// Create app_armor parser command with arguments to only return the compiled
// policy to stdout. The profile is not cached or loaded.
apparmorParser := exec.Command("apparmor_parser", "-QKS")
apparmorParser := exec.Command(p, "-QKS")

Check warning on line 37 in testutil/apparmor.go

View check run for this annotation

Codecov / codecov/patch

testutil/apparmor.go#L37

Added line #L37 was not covered by tests

// Get stdin and stdout to pipe the command
apparmorParserStdin, err := apparmorParser.StdinPipe()
if err != nil {
return "Error creating stdin pipe for apparmor_parser", err
return "", fmt.Errorf("cannot create stdin pipe for apparmor_parser: %w", err)

Check warning on line 42 in testutil/apparmor.go

View check run for this annotation

Codecov / codecov/patch

testutil/apparmor.go#L42

Added line #L42 was not covered by tests
}
apparmorParserStdout, err := apparmorParser.StdoutPipe()
if err != nil {
return "Error creating stdout pipe for apparmor_parser", err
return "", fmt.Errorf("cannot create stdout pipe for apparmor_parser: %w", err)

Check warning on line 46 in testutil/apparmor.go

View check run for this annotation

Codecov / codecov/patch

testutil/apparmor.go#L46

Added line #L46 was not covered by tests
}

// Start apparmor_parser command
if err := apparmorParser.Start(); err != nil {
return "Error starting apparmor_parser", err
return "", fmt.Errorf("cannot start apparmor_parser: %w", err)

Check warning on line 51 in testutil/apparmor.go

View check run for this annotation

Codecov / codecov/patch

testutil/apparmor.go#L51

Added line #L51 was not covered by tests
}

// Write apparmor profile to apparmor_parser stdin
Expand All @@ -60,9 +64,9 @@
// Get apparmor_parser command output
if err := apparmorParser.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
return fmt.Sprintf("apparmor_parser command exited with status code %d", exiterr.ExitCode()), err
return "", fmt.Errorf("apparmor_parser command exited with status code %v", exiterr.ExitCode())

Check warning on line 67 in testutil/apparmor.go

View check run for this annotation

Codecov / codecov/patch

testutil/apparmor.go#L67

Added line #L67 was not covered by tests
} else {
return "Error waiting for apparmor_parser command", err
return "", fmt.Errorf("cannot wait() for apparmor_parser process: %w", err)

Check warning on line 69 in testutil/apparmor.go

View check run for this annotation

Codecov / codecov/patch

testutil/apparmor.go#L69

Added line #L69 was not covered by tests
}
}

Expand Down
Loading