2023-05-06 14:23:16 +02:00
|
|
|
// apparmor.d - Full set of apparmor profiles
|
2024-02-07 00:16:21 +01:00
|
|
|
// Copyright (C) 2023-2024 Alexandre Pujol <alexandre@pujol.io>
|
2023-05-06 14:23:16 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/roddhjav/apparmor.d/pkg/logging"
|
2024-04-28 01:44:23 +02:00
|
|
|
"github.com/roddhjav/apparmor.d/pkg/paths"
|
2023-05-06 14:23:16 +02:00
|
|
|
)
|
|
|
|
|
2024-10-19 23:50:09 +02:00
|
|
|
const usage = `aa-test [-h] --bootstrap
|
2023-05-06 14:23:16 +02:00
|
|
|
|
|
|
|
Integration tests manager tool for apparmor.d
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-h, --help Show this help message and exit.
|
2024-10-19 23:50:09 +02:00
|
|
|
-b, --bootstrap Download tests using tldr pages and generate Bats tests.
|
2023-05-06 14:23:16 +02:00
|
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
var (
|
|
|
|
help bool
|
|
|
|
bootstrap bool
|
|
|
|
)
|
|
|
|
|
2024-10-19 23:50:09 +02:00
|
|
|
func init() {
|
|
|
|
flag.BoolVar(&help, "h", false, "Show this help message and exit.")
|
|
|
|
flag.BoolVar(&help, "help", false, "Show this help message and exit.")
|
|
|
|
flag.BoolVar(&bootstrap, "b", false, "Download tests using tldr pages and generate Bats tests.")
|
|
|
|
flag.BoolVar(&bootstrap, "bootstrap", false, "Download tests using tldr pages and generate Bats tests.")
|
|
|
|
}
|
|
|
|
|
2023-05-06 14:23:16 +02:00
|
|
|
type Config struct {
|
2024-10-19 23:50:09 +02:00
|
|
|
TestsDir *paths.Path // Default: tests
|
|
|
|
TldrDir *paths.Path // Default: tests/tldr
|
|
|
|
TldrFile *paths.Path // Default: tests/tldr.yml
|
|
|
|
TestsFile *paths.Path // Default: tests/tests.yml
|
2024-10-21 15:13:25 +02:00
|
|
|
BatsDir *paths.Path // Default: tests/bats_dirty
|
2023-05-06 14:23:16 +02:00
|
|
|
}
|
|
|
|
|
2024-10-19 23:50:09 +02:00
|
|
|
func NewConfig() *Config {
|
|
|
|
testsDir := paths.New("tests")
|
2023-05-06 14:23:16 +02:00
|
|
|
cfg := Config{
|
2024-10-19 23:50:09 +02:00
|
|
|
TestsDir: testsDir,
|
|
|
|
TldrDir: testsDir.Join("tldr"),
|
|
|
|
TldrFile: testsDir.Join("tldr.yml"),
|
|
|
|
TestsFile: testsDir.Join("tldr.yml"),
|
|
|
|
BatsDir: testsDir.Join("bats_dirty"),
|
2023-05-06 14:23:16 +02:00
|
|
|
}
|
2024-10-19 23:50:09 +02:00
|
|
|
return &cfg
|
2023-05-06 14:23:16 +02:00
|
|
|
}
|
|
|
|
|
2024-10-19 23:50:09 +02:00
|
|
|
func run() error {
|
|
|
|
logging.Step("Bootstraping tests")
|
|
|
|
cfg := NewConfig()
|
2023-10-01 00:21:26 +02:00
|
|
|
|
2024-10-19 23:50:09 +02:00
|
|
|
tldr := NewTldr(cfg.TldrDir)
|
2023-05-06 14:23:16 +02:00
|
|
|
if err := tldr.Download(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-10-19 23:50:09 +02:00
|
|
|
tests, err := tldr.Parse()
|
2023-05-06 14:23:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-19 23:50:09 +02:00
|
|
|
tests = tests.Filter()
|
2023-05-06 14:23:16 +02:00
|
|
|
|
2024-10-21 15:13:25 +02:00
|
|
|
if err := cfg.BatsDir.RemoveAll(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := cfg.BatsDir.MkdirAll(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-19 23:50:09 +02:00
|
|
|
for _, test := range tests {
|
|
|
|
if err := test.Write(cfg.BatsDir); err != nil {
|
2023-05-06 14:23:16 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-19 23:50:09 +02:00
|
|
|
logging.Bullet("Bats tests directory: %s", cfg.BatsDir)
|
|
|
|
logging.Bullet("Number of tests found %d", len(tests))
|
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)
|
|
|
|
}
|
|
|
|
|
2024-10-19 23:50:09 +02:00
|
|
|
if !bootstrap {
|
2023-05-06 14:23:16 +02:00
|
|
|
flag.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-10-19 23:50:09 +02:00
|
|
|
|
|
|
|
err := run()
|
2023-05-06 14:23:16 +02:00
|
|
|
if err != nil {
|
2024-08-20 20:07:56 +02:00
|
|
|
logging.Fatal("%s", err.Error())
|
2023-05-06 14:23:16 +02:00
|
|
|
}
|
|
|
|
}
|