2023-05-06 14:23:16 +02:00
|
|
|
// apparmor.d - Full set of apparmor profiles
|
|
|
|
// Copyright (C) 2023 Alexandre Pujol <alexandre@pujol.io>
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/arduino/go-paths-helper"
|
2023-09-10 13:21:55 +02:00
|
|
|
"github.com/roddhjav/apparmor.d/pkg/aa"
|
|
|
|
"github.com/roddhjav/apparmor.d/pkg/integration"
|
2023-05-06 14:23:16 +02:00
|
|
|
"github.com/roddhjav/apparmor.d/pkg/logging"
|
|
|
|
)
|
|
|
|
|
|
|
|
const usage = `aa-test [-h] [--bootstrap | --run | --list]
|
|
|
|
|
|
|
|
Integration tests manager tool for apparmor.d
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-h, --help Show this help message and exit.
|
2023-09-10 13:21:55 +02:00
|
|
|
-b, --bootstrap Bootstrap tests using tldr pages.
|
2023-05-06 14:23:16 +02:00
|
|
|
-r, --run Run a predefined list of tests.
|
|
|
|
-l, --list List the configured tests.
|
2023-09-10 13:21:55 +02:00
|
|
|
-f, --file FILE Set a tests file. Default: tests/tests.yml
|
2023-05-06 14:23:16 +02:00
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
var (
|
|
|
|
help bool
|
|
|
|
bootstrap bool
|
|
|
|
run bool
|
|
|
|
list bool
|
2023-09-10 13:21:55 +02:00
|
|
|
cfg Config
|
2023-05-06 14:23:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2023-09-10 13:21:55 +02:00
|
|
|
TldrDir *paths.Path // Default: tests/tldr
|
|
|
|
ScenariosDir *paths.Path // Default: tests
|
|
|
|
TldrFile *paths.Path // Default: tests/tldr.yml
|
|
|
|
TestsFile *paths.Path // Default: tests/tests.yml
|
|
|
|
Profiles paths.PathList
|
2023-05-06 14:23:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfig() Config {
|
|
|
|
cfg := Config{
|
|
|
|
TldrDir: paths.New("tests/tldr"),
|
|
|
|
ScenariosDir: paths.New("tests/"),
|
|
|
|
Profiles: paths.PathList{},
|
|
|
|
}
|
2023-09-10 13:21:55 +02:00
|
|
|
cfg.TldrFile = cfg.ScenariosDir.Join("tldr.yml")
|
|
|
|
cfg.TestsFile = cfg.ScenariosDir.Join("tests.yml")
|
2023-05-06 14:23:16 +02:00
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2023-09-10 13:21:55 +02:00
|
|
|
cfg = NewConfig()
|
|
|
|
files, _ := aa.MagicRoot.ReadDir(paths.FilterOutDirectories())
|
2023-05-06 14:23:16 +02:00
|
|
|
for _, path := range files {
|
2023-09-10 13:21:55 +02:00
|
|
|
cfg.Profiles.Add(path)
|
2023-05-06 14:23:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
flag.BoolVar(&help, "h", false, "Show this help message and exit.")
|
|
|
|
flag.BoolVar(&help, "help", false, "Show this help message and exit.")
|
2023-09-10 13:21:55 +02:00
|
|
|
flag.BoolVar(&bootstrap, "b", false, "Bootstrap tests using tldr pages.")
|
|
|
|
flag.BoolVar(&bootstrap, "bootstrap", false, "Bootstrap tests using tldr pages.")
|
2023-05-06 14:23:16 +02:00
|
|
|
flag.BoolVar(&run, "r", false, "Run a predefined list of tests.")
|
|
|
|
flag.BoolVar(&run, "run", false, "Run a predefined list of tests.")
|
2023-09-10 13:21:55 +02:00
|
|
|
flag.BoolVar(&list, "l", false, "List the tests to run.")
|
|
|
|
flag.BoolVar(&list, "list", false, "List the tests to run.")
|
2023-05-06 14:23:16 +02:00
|
|
|
}
|
|
|
|
|
2023-09-10 13:21:55 +02:00
|
|
|
func testDownload() error {
|
|
|
|
tldr := integration.NewTldr(cfg.TldrDir)
|
2023-05-06 14:23:16 +02:00
|
|
|
if err := tldr.Download(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-10 13:21:55 +02:00
|
|
|
tSuite, err := tldr.Parse(cfg.Profiles)
|
2023-05-06 14:23:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default bootstraped scenarios file
|
2023-09-10 13:21:55 +02:00
|
|
|
if err := tSuite.Write(cfg.TldrFile); err != nil {
|
2023-05-06 14:23:16 +02:00
|
|
|
return err
|
|
|
|
}
|
2023-09-10 13:21:55 +02:00
|
|
|
logging.Bullet("Default scenarios saved: %s", cfg.TldrFile)
|
2023-05-06 14:23:16 +02:00
|
|
|
|
|
|
|
// Scenarios file with only profiled programs
|
2023-09-10 13:21:55 +02:00
|
|
|
tSuiteWithProfile := integration.NewTestSuite()
|
|
|
|
for _, t := range tSuite.Tests {
|
|
|
|
if t.Profiled {
|
|
|
|
tSuiteWithProfile.Tests = append(tSuiteWithProfile.Tests, t)
|
2023-05-06 14:23:16 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-10 13:21:55 +02:00
|
|
|
|
|
|
|
testWithProfilePath := cfg.TldrFile.Parent().Join(
|
|
|
|
strings.TrimSuffix(cfg.TldrFile.Base(), cfg.TldrFile.Ext()) + ".new.yml")
|
|
|
|
if err := tSuiteWithProfile.Write(testWithProfilePath); err != nil {
|
2023-05-06 14:23:16 +02:00
|
|
|
return err
|
|
|
|
}
|
2023-09-10 13:21:55 +02:00
|
|
|
logging.Bullet("Tests with profile saved: %s", testWithProfilePath)
|
2023-05-06 14:23:16 +02:00
|
|
|
|
2023-09-10 13:21:55 +02:00
|
|
|
logging.Bullet("Number of tests found %d", len(tSuite.Tests))
|
|
|
|
logging.Bullet("Number of tests with profiles in apparmor.d %d", len(tSuiteWithProfile.Tests))
|
2023-05-06 14:23:16 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-10 13:21:55 +02:00
|
|
|
func testRun(dryRun bool) error {
|
|
|
|
// Warning: There is no guarantee that the tests are not destructive
|
2023-05-06 14:23:16 +02:00
|
|
|
if dryRun {
|
|
|
|
logging.Step("List tests")
|
|
|
|
} else {
|
|
|
|
logging.Step("Run tests")
|
|
|
|
}
|
|
|
|
|
2023-09-10 13:21:55 +02:00
|
|
|
tSuite := integration.NewTestSuite()
|
|
|
|
if err := tSuite.ReadScenarios(cfg.TestsFile); err != nil {
|
2023-05-06 14:23:16 +02:00
|
|
|
return err
|
|
|
|
}
|
2023-09-10 13:21:55 +02:00
|
|
|
cfgPath := cfg.ScenariosDir.Join("integration.yml")
|
2023-05-06 14:23:16 +02:00
|
|
|
if err := tSuite.ReadSettings(cfgPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-10 13:21:55 +02:00
|
|
|
integration.Arguments = tSuite.Arguments
|
|
|
|
integration.Ignore = tSuite.Ignore
|
|
|
|
nbCmd := 0
|
2023-05-06 14:23:16 +02:00
|
|
|
nbTest := 0
|
2023-09-10 13:21:55 +02:00
|
|
|
for _, test := range tSuite.Tests {
|
|
|
|
ran, nb, err := test.Run(dryRun)
|
|
|
|
nbTest += ran
|
|
|
|
nbCmd += nb
|
2023-05-06 14:23:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if dryRun {
|
|
|
|
logging.Bullet("Number of tests to run %d", nbTest)
|
2023-09-10 13:21:55 +02:00
|
|
|
logging.Bullet("Number of test commands to run %d", nbCmd)
|
2023-05-06 14:23:16 +02:00
|
|
|
} else {
|
2023-09-10 13:21:55 +02:00
|
|
|
logging.Success("Number of tests ran %d", nbTest)
|
|
|
|
logging.Success("Number of test command to ran %d", nbCmd)
|
2023-05-06 14:23:16 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Usage = func() { fmt.Print(usage) }
|
|
|
|
flag.Parse()
|
|
|
|
if help {
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if bootstrap {
|
|
|
|
logging.Step("Bootstraping tests")
|
2023-09-10 13:21:55 +02:00
|
|
|
err = testDownload()
|
2023-05-06 14:23:16 +02:00
|
|
|
} else if run || list {
|
2023-09-10 13:21:55 +02:00
|
|
|
err = testRun(list)
|
2023-05-06 14:23:16 +02:00
|
|
|
} else {
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
logging.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
}
|