test(integration): update aa-test.
This commit is contained in:
parent
972b08e74c
commit
4d0ccebb21
3 changed files with 67 additions and 31 deletions
|
|
@ -8,12 +8,14 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/arduino/go-paths-helper"
|
"github.com/arduino/go-paths-helper"
|
||||||
"github.com/roddhjav/apparmor.d/pkg/aa"
|
"github.com/roddhjav/apparmor.d/pkg/aa"
|
||||||
"github.com/roddhjav/apparmor.d/pkg/integration"
|
"github.com/roddhjav/apparmor.d/pkg/integration"
|
||||||
"github.com/roddhjav/apparmor.d/pkg/logging"
|
"github.com/roddhjav/apparmor.d/pkg/logging"
|
||||||
|
"github.com/roddhjav/apparmor.d/pkg/prebuild"
|
||||||
)
|
)
|
||||||
|
|
||||||
const usage = `aa-test [-h] [--bootstrap | --run | --list]
|
const usage = `aa-test [-h] [--bootstrap | --run | --list]
|
||||||
|
|
@ -26,6 +28,8 @@ Options:
|
||||||
-r, --run Run a predefined list of tests.
|
-r, --run Run a predefined list of tests.
|
||||||
-l, --list List the configured tests.
|
-l, --list List the configured tests.
|
||||||
-f, --file FILE Set a tests file. Default: tests/tests.yml
|
-f, --file FILE Set a tests file. Default: tests/tests.yml
|
||||||
|
-d, --deps Install tests dependencies.
|
||||||
|
-D, --dryrun Do not do the action, list it.
|
||||||
|
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|
@ -34,6 +38,8 @@ var (
|
||||||
bootstrap bool
|
bootstrap bool
|
||||||
run bool
|
run bool
|
||||||
list bool
|
list bool
|
||||||
|
deps bool
|
||||||
|
dryRun bool
|
||||||
cfg Config
|
cfg Config
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -42,7 +48,8 @@ type Config struct {
|
||||||
ScenariosDir *paths.Path // Default: tests
|
ScenariosDir *paths.Path // Default: tests
|
||||||
TldrFile *paths.Path // Default: tests/tldr.yml
|
TldrFile *paths.Path // Default: tests/tldr.yml
|
||||||
TestsFile *paths.Path // Default: tests/tests.yml
|
TestsFile *paths.Path // Default: tests/tests.yml
|
||||||
Profiles paths.PathList
|
SettingsFile *paths.Path // Default: tests/settings.yml
|
||||||
|
Profiles paths.PathList // List of profiles
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfig() Config {
|
func NewConfig() Config {
|
||||||
|
|
@ -53,9 +60,21 @@ func NewConfig() Config {
|
||||||
}
|
}
|
||||||
cfg.TldrFile = cfg.ScenariosDir.Join("tldr.yml")
|
cfg.TldrFile = cfg.ScenariosDir.Join("tldr.yml")
|
||||||
cfg.TestsFile = cfg.ScenariosDir.Join("tests.yml")
|
cfg.TestsFile = cfg.ScenariosDir.Join("tests.yml")
|
||||||
|
cfg.SettingsFile = cfg.ScenariosDir.Join("settings.yml")
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LoadTestSuite() (*integration.TestSuite, error) {
|
||||||
|
tSuite := integration.NewTestSuite()
|
||||||
|
if err := tSuite.ReadTests(cfg.TestsFile); err != nil {
|
||||||
|
return tSuite, err
|
||||||
|
}
|
||||||
|
if err := tSuite.ReadSettings(cfg.SettingsFile); err != nil {
|
||||||
|
return tSuite, err
|
||||||
|
}
|
||||||
|
return tSuite, nil
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cfg = NewConfig()
|
cfg = NewConfig()
|
||||||
files, _ := aa.MagicRoot.ReadDir(paths.FilterOutDirectories())
|
files, _ := aa.MagicRoot.ReadDir(paths.FilterOutDirectories())
|
||||||
|
|
@ -71,6 +90,10 @@ func init() {
|
||||||
flag.BoolVar(&run, "run", false, "Run a predefined list of tests.")
|
flag.BoolVar(&run, "run", false, "Run a predefined list of tests.")
|
||||||
flag.BoolVar(&list, "l", false, "List the tests to run.")
|
flag.BoolVar(&list, "l", false, "List the tests to run.")
|
||||||
flag.BoolVar(&list, "list", false, "List the tests to run.")
|
flag.BoolVar(&list, "list", false, "List the tests to run.")
|
||||||
|
flag.BoolVar(&deps, "d", false, "Install tests dependencies.")
|
||||||
|
flag.BoolVar(&deps, "deps", false, "Install tests dependencies.")
|
||||||
|
flag.BoolVar(&dryRun, "D", false, "Do not do the action, list it.")
|
||||||
|
flag.BoolVar(&dryRun, "dryrun", false, "Do not do the action, list it.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testDownload() error {
|
func testDownload() error {
|
||||||
|
|
@ -79,7 +102,7 @@ func testDownload() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
tSuite, err := tldr.Parse(cfg.Profiles)
|
tSuite, err := tldr.Parse()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -89,24 +112,31 @@ func testDownload() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
logging.Bullet("Default scenarios saved: %s", cfg.TldrFile)
|
logging.Bullet("Default scenarios saved: %s", cfg.TldrFile)
|
||||||
|
|
||||||
// Scenarios file with only profiled programs
|
|
||||||
tSuiteWithProfile := integration.NewTestSuite()
|
|
||||||
for _, t := range tSuite.Tests {
|
|
||||||
if t.Profiled {
|
|
||||||
tSuiteWithProfile.Tests = append(tSuiteWithProfile.Tests, t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
testWithProfilePath := cfg.TldrFile.Parent().Join(
|
|
||||||
strings.TrimSuffix(cfg.TldrFile.Base(), cfg.TldrFile.Ext()) + ".new.yml")
|
|
||||||
if err := tSuiteWithProfile.Write(testWithProfilePath); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
logging.Bullet("Tests with profile saved: %s", testWithProfilePath)
|
|
||||||
|
|
||||||
logging.Bullet("Number of tests found %d", len(tSuite.Tests))
|
logging.Bullet("Number of tests found %d", len(tSuite.Tests))
|
||||||
logging.Bullet("Number of tests with profiles in apparmor.d %d", len(tSuiteWithProfile.Tests))
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func testDeps(dryRun bool) error {
|
||||||
|
tSuite, err := LoadTestSuite()
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
deps := tSuite.GetDependencies()
|
||||||
|
switch prebuild.Distribution {
|
||||||
|
case "arch":
|
||||||
|
arg := []string{"pacman", "-Sy", "--noconfirm"}
|
||||||
|
arg = append(arg, deps...)
|
||||||
|
cmd := exec.Command("sudo", arg...)
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
if dryRun {
|
||||||
|
fmt.Println(strings.Join(cmd.Args, " "))
|
||||||
|
} else {
|
||||||
|
return cmd.Run()
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -118,16 +148,13 @@ func testRun(dryRun bool) error {
|
||||||
logging.Step("Run tests")
|
logging.Step("Run tests")
|
||||||
}
|
}
|
||||||
|
|
||||||
tSuite := integration.NewTestSuite()
|
tSuite, err := LoadTestSuite()
|
||||||
if err := tSuite.ReadScenarios(cfg.TestsFile); err != nil {
|
if err != nil {
|
||||||
return err
|
return nil
|
||||||
}
|
|
||||||
cfgPath := cfg.ScenariosDir.Join("integration.yml")
|
|
||||||
if err := tSuite.ReadSettings(cfgPath); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
integration.Arguments = tSuite.Arguments
|
integration.Arguments = tSuite.Arguments
|
||||||
integration.Ignore = tSuite.Ignore
|
integration.Ignore = tSuite.Ignore
|
||||||
|
integration.Profiles = cfg.Profiles
|
||||||
nbCmd := 0
|
nbCmd := 0
|
||||||
nbTest := 0
|
nbTest := 0
|
||||||
for _, test := range tSuite.Tests {
|
for _, test := range tSuite.Tests {
|
||||||
|
|
@ -163,6 +190,8 @@ func main() {
|
||||||
err = testDownload()
|
err = testDownload()
|
||||||
} else if run || list {
|
} else if run || list {
|
||||||
err = testRun(list)
|
err = testRun(list)
|
||||||
|
} else if deps {
|
||||||
|
err = testDeps(dryRun)
|
||||||
} else {
|
} else {
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
||||||
|
|
@ -139,4 +139,3 @@ func (t *Test) run(cmdline string, in string) error {
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -106,3 +106,11 @@ func (t *TestSuite) Results() string {
|
||||||
aaLogs := logs.NewApparmorLogs(file, "")
|
aaLogs := logs.NewApparmorLogs(file, "")
|
||||||
return aaLogs.String()
|
return aaLogs.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *TestSuite) GetDependencies() []string {
|
||||||
|
res := []string{}
|
||||||
|
for _, test := range t.Tests {
|
||||||
|
res = append(res, test.Dependencies...)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue