apparmor.d/cmd/aa-test/main.go

176 lines
4.4 KiB
Go
Raw Normal View History

// 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"
"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.
-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
`
const defaultScenariosFile = "scenarios.yml"
var (
help bool
bootstrap bool
run bool
list bool
2023-09-10 13:21:55 +02:00
cfg Config
)
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
}
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")
return cfg
}
func init() {
2023-09-10 13:21:55 +02:00
cfg = NewConfig()
files, _ := aa.MagicRoot.ReadDir(paths.FilterOutDirectories())
for _, path := range files {
2023-09-10 13:21:55 +02:00
cfg.Profiles.Add(path)
}
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.")
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-09-10 13:21:55 +02:00
func testDownload() error {
tldr := integration.NewTldr(cfg.TldrDir)
if err := tldr.Download(); err != nil {
return err
}
2023-09-10 13:21:55 +02:00
tSuite, err := tldr.Parse(cfg.Profiles)
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 {
return err
}
2023-09-10 13:21:55 +02:00
logging.Bullet("Default scenarios saved: %s", cfg.TldrFile)
// 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-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 {
return err
}
2023-09-10 13:21:55 +02:00
logging.Bullet("Tests with profile saved: %s", testWithProfilePath)
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))
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
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 {
return err
}
2023-09-10 13:21:55 +02:00
cfgPath := cfg.ScenariosDir.Join("integration.yml")
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
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
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)
} 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)
}
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()
} else if run || list {
2023-09-10 13:21:55 +02:00
err = testRun(list)
} else {
flag.Usage()
os.Exit(1)
}
if err != nil {
logging.Fatal(err.Error())
}
}