test: refractor integration tests.
This commit is contained in:
parent
e381aace56
commit
e71fc00d8e
4 changed files with 116 additions and 116 deletions
|
|
@ -21,84 +21,84 @@ import (
|
|||
"golang.org/x/exp/slices"
|
||||
)
|
||||
|
||||
// Scenario represents of a list of tests for a given program
|
||||
type (
|
||||
Scenario struct {
|
||||
Name string `yaml:"name"`
|
||||
Profiled bool `yaml:"profiled"` // The program is profiled in apparmor.d
|
||||
Root bool `yaml:"root"` // Run the test as user or as root
|
||||
Dependencies []string `yaml:"require"` // Packages required for the tests to run "$(pacman -Qqo Scenario.Name)"
|
||||
Arguments map[string]string `yaml:"arguments"` // Arguments to pass to the program. Sepicific to this scenario
|
||||
Tests []Test `yaml:"tests"`
|
||||
}
|
||||
Test struct {
|
||||
Description string `yaml:"dsc"`
|
||||
Command string `yaml:"cmd"`
|
||||
Stdin []string `yaml:"stdin"`
|
||||
}
|
||||
)
|
||||
// Test represents of a list of tests for a given program
|
||||
type Test struct {
|
||||
Name string `yaml:"name"`
|
||||
Profiled bool `yaml:"profiled"` // The program is profiled in apparmor.d
|
||||
Root bool `yaml:"root"` // Run the test as user or as root
|
||||
Dependencies []string `yaml:"require"` // Packages required for the tests to run "$(pacman -Qqo Scenario.Name)"
|
||||
Arguments map[string]string `yaml:"arguments"` // Arguments to pass to the program, specific to this scenario
|
||||
Commands []Command `yaml:"tests"`
|
||||
}
|
||||
|
||||
func NewScenario() *Scenario {
|
||||
return &Scenario{
|
||||
// Command is a command line to run as part of a test
|
||||
type Command struct {
|
||||
Description string `yaml:"dsc"`
|
||||
Cmd string `yaml:"cmd"`
|
||||
Stdin []string `yaml:"stdin"`
|
||||
}
|
||||
|
||||
func NewTest() *Test {
|
||||
return &Test{
|
||||
Name: "",
|
||||
Profiled: false,
|
||||
Root: false,
|
||||
Dependencies: []string{},
|
||||
Arguments: map[string]string{},
|
||||
Tests: []Test{},
|
||||
Commands: []Command{},
|
||||
}
|
||||
}
|
||||
|
||||
// HasProfile returns true if the program in the scenario is profiled in apparmor.d
|
||||
func (s *Scenario) hasProfile(profiles paths.PathList) bool {
|
||||
func (t *Test) hasProfile(profiles paths.PathList) bool {
|
||||
for _, path := range profiles {
|
||||
if s.Name == path.Base() {
|
||||
if t.Name == path.Base() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *Scenario) installed() bool {
|
||||
if _, err := exec.LookPath(s.Name); err != nil {
|
||||
func (t *Test) installed() bool {
|
||||
if _, err := exec.LookPath(t.Name); err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *Scenario) resolve(in string) string {
|
||||
func (t *Test) resolve(in string) string {
|
||||
res := in
|
||||
for key, value := range s.Arguments {
|
||||
res = strings.ReplaceAll(res, "{{"+key+"}}", value)
|
||||
for key, value := range t.Arguments {
|
||||
res = strings.ReplaceAll(res, "{{ "+key+" }}", value)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// mergeArguments merge the arguments of the scenario with the global arguments
|
||||
// Scenarios arguments have priority over global arguments
|
||||
func (s *Scenario) mergeArguments(args map[string]string) {
|
||||
// Test arguments have priority over global arguments
|
||||
func (t *Test) mergeArguments(args map[string]string) {
|
||||
for key, value := range args {
|
||||
s.Arguments[key] = value
|
||||
t.Arguments[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
// Run the scenarios tests
|
||||
func (s *Scenario) Run(dryRun bool) (ran int, nb int, err error) {
|
||||
func (t *Test) Run(dryRun bool) (ran int, nb int, err error) {
|
||||
nb = 0
|
||||
if s.Profiled && s.installed() {
|
||||
if slices.Contains(Ignore, s.Name) {
|
||||
if t.Profiled && t.installed() {
|
||||
if slices.Contains(Ignore, t.Name) {
|
||||
return 0, nb, err
|
||||
}
|
||||
logging.Step("%s", s.Name)
|
||||
s.mergeArguments(Arguments)
|
||||
for _, test := range s.Tests {
|
||||
cmd := s.resolve(test.Command)
|
||||
logging.Step("%s", t.Name)
|
||||
t.mergeArguments(Arguments)
|
||||
for _, test := range t.Commands {
|
||||
cmd := t.resolve(test.Cmd)
|
||||
if !strings.Contains(cmd, "{{") {
|
||||
nb++
|
||||
if dryRun {
|
||||
logging.Bullet(cmd)
|
||||
} else {
|
||||
cmdErr := s.run(cmd, strings.Join(test.Stdin, "\n"))
|
||||
cmdErr := t.run(cmd, strings.Join(test.Stdin, "\n"))
|
||||
if cmdErr != nil {
|
||||
// TODO: log the error
|
||||
logging.Error("%v", cmdErr)
|
||||
|
|
@ -113,12 +113,12 @@ func (s *Scenario) Run(dryRun bool) (ran int, nb int, err error) {
|
|||
return 0, nb, err
|
||||
}
|
||||
|
||||
func (s *Scenario) run(cmdline string, in string) error {
|
||||
func (t *Test) run(cmdline string, in string) error {
|
||||
// Running the command in a shell ensure it does not run confined under the sudo profile.
|
||||
// The shell is run unconfined and therefore the cmdline can be confined without no-new-privs issue.
|
||||
sufix := " &" // TODO: we need a goroutine here
|
||||
cmd := exec.Command("sh", "-c", cmdline+sufix)
|
||||
if s.Root {
|
||||
if t.Root {
|
||||
cmd = exec.Command("sudo", "sh", "-c", cmdline+sufix)
|
||||
}
|
||||
cmd.Stdin = strings.NewReader(in)
|
||||
|
|
|
|||
|
|
@ -5,24 +5,23 @@
|
|||
package integration
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/arduino/go-paths-helper"
|
||||
"github.com/roddhjav/apparmor.d/pkg/logs"
|
||||
"github.com/roddhjav/apparmor.d/pkg/util"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// TestSuite is the apparmod.d integration tests to run
|
||||
type TestSuite struct {
|
||||
Scenarios []Scenario // List of scenarios to run
|
||||
Ignore []string // Do not run some scenarios
|
||||
Arguments map[string]string // Common arguments used across all scenarios
|
||||
Tests []Test // List of tests to run
|
||||
Ignore []string // Do not run some tests
|
||||
Arguments map[string]string // Common arguments used across all tests
|
||||
}
|
||||
|
||||
// NewScenarios returns a new list of scenarios
|
||||
func NewTestSuite() *TestSuite {
|
||||
return &TestSuite{
|
||||
Scenarios: []Scenario{},
|
||||
Tests: []Test{},
|
||||
Ignore: []string{},
|
||||
Arguments: map[string]string{},
|
||||
}
|
||||
|
|
@ -30,7 +29,7 @@ func NewTestSuite() *TestSuite {
|
|||
|
||||
// Write export the list of scenarios to a file
|
||||
func (t *TestSuite) Write(path *paths.Path) error {
|
||||
jsonString, err := yaml.Marshal(&t.Scenarios)
|
||||
jsonString, err := yaml.Marshal(&t.Tests)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -44,7 +43,15 @@ func (t *TestSuite) Write(path *paths.Path) error {
|
|||
|
||||
// Cleanup a bit
|
||||
res := string(jsonString)
|
||||
res = strings.Replace(res, "- name:", "\n- name:", -1)
|
||||
regClean := util.ToRegexRepl([]string{
|
||||
"- name:", "\n- name:",
|
||||
`(?m)^.*stdin: \[\].*$`, ``,
|
||||
`{{`, `{{ `,
|
||||
`}}`, ` }}`,
|
||||
})
|
||||
for _, aa := range regClean {
|
||||
res = aa.Regex.ReplaceAllLiteralString(res, aa.Repl)
|
||||
}
|
||||
_, err = file.WriteString("---\n" + res)
|
||||
return err
|
||||
}
|
||||
|
|
@ -52,7 +59,7 @@ func (t *TestSuite) Write(path *paths.Path) error {
|
|||
// ReadScenarios import the scenarios from a file
|
||||
func (t *TestSuite) ReadScenarios(path *paths.Path) error {
|
||||
content, _ := path.ReadFile()
|
||||
return yaml.Unmarshal(content, &t.Scenarios)
|
||||
return yaml.Unmarshal(content, &t.Tests)
|
||||
}
|
||||
|
||||
// ReadSettings import the common argument and ignore list from a file
|
||||
|
|
|
|||
|
|
@ -54,10 +54,7 @@ func (t Tldr) Download() error {
|
|||
}
|
||||
|
||||
pages := []string{"tldr-main/pages/linux", "tldr-main/pages/common"}
|
||||
if err := util.ExtratTo(gzPath, t.Dir, pages); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
return util.ExtratTo(gzPath, t.Dir, pages)
|
||||
}
|
||||
|
||||
// Parse the tldr pages and return a list of scenarios
|
||||
|
|
@ -70,31 +67,31 @@ func (t Tldr) Parse(profiles paths.PathList) (*TestSuite, error) {
|
|||
return nil, err
|
||||
}
|
||||
raw := string(content)
|
||||
scenario := &Scenario{
|
||||
t := &Test{
|
||||
Name: strings.TrimSuffix(path.Base(), ".md"),
|
||||
Profiled: false,
|
||||
Root: false,
|
||||
Arguments: map[string]string{},
|
||||
Tests: []Test{},
|
||||
Commands: []Command{},
|
||||
}
|
||||
scenario.Profiled = scenario.hasProfile(profiles)
|
||||
t.Profiled = t.hasProfile(profiles)
|
||||
if strings.Contains(raw, "sudo") {
|
||||
scenario.Root = true
|
||||
t.Root = true
|
||||
}
|
||||
rawTests := strings.Split(raw, "\n-")[1:]
|
||||
for _, test := range rawTests {
|
||||
res := strings.Split(test, "\n")
|
||||
dsc := strings.ReplaceAll(strings.Trim(res[0], " "), ":", "")
|
||||
cmd := strings.Trim(strings.Trim(res[2], "`"), " ")
|
||||
if scenario.Root {
|
||||
if t.Root {
|
||||
cmd = strings.ReplaceAll(cmd, "sudo ", "")
|
||||
}
|
||||
scenario.Tests = append(scenario.Tests, Test{
|
||||
t.Commands = append(t.Commands, Command{
|
||||
Description: dsc,
|
||||
Command: cmd,
|
||||
Cmd: cmd,
|
||||
})
|
||||
}
|
||||
testSuite.Scenarios = append(testSuite.Scenarios, *scenario)
|
||||
testSuite.Tests = append(testSuite.Tests, *t)
|
||||
}
|
||||
return testSuite, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue