2019-10-29 20:54:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2019-11-02 14:32:58 +00:00
|
|
|
"sort"
|
2019-10-29 20:54:47 +00:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
2019-11-02 14:32:58 +00:00
|
|
|
"time"
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-10-29 20:54:47 +00:00
|
|
|
"github.com/spf13/cobra"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/suites"
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2019-10-29 20:54:47 +00:00
|
|
|
)
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
// ErrNotAvailableSuite error raised when suite is not available.
|
|
|
|
var ErrNotAvailableSuite = errors.New("unavailable suite")
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// ErrNoRunningSuite error raised when no suite is running.
|
2019-11-02 14:32:58 +00:00
|
|
|
var ErrNoRunningSuite = errors.New("no running suite")
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// runningSuiteFile name of the file containing the currently running suite.
|
2019-11-02 14:32:58 +00:00
|
|
|
var runningSuiteFile = ".suite"
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
var headless bool
|
2020-02-04 21:18:02 +00:00
|
|
|
var testPattern string
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
func init() {
|
|
|
|
SuitesTestCmd.Flags().BoolVar(&headless, "headless", false, "Run tests in headless mode")
|
2020-02-04 21:18:02 +00:00
|
|
|
SuitesTestCmd.Flags().StringVar(&testPattern, "test", "", "The single test to run")
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 00:10:00 +00:00
|
|
|
// SuitesListCmd Command for listing the available suites.
|
2019-10-29 20:54:47 +00:00
|
|
|
var SuitesListCmd = &cobra.Command{
|
|
|
|
Use: "list",
|
|
|
|
Short: "List available suites.",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2019-11-02 14:32:58 +00:00
|
|
|
fmt.Println(strings.Join(listSuites(), "\n"))
|
2019-10-29 20:54:47 +00:00
|
|
|
},
|
|
|
|
Args: cobra.ExactArgs(0),
|
|
|
|
}
|
|
|
|
|
2020-01-21 00:10:00 +00:00
|
|
|
// SuitesSetupCmd Command to setup a suite environment.
|
2019-11-02 14:32:58 +00:00
|
|
|
var SuitesSetupCmd = &cobra.Command{
|
|
|
|
Use: "setup [suite]",
|
|
|
|
Short: "Setup a Go suite environment. Suites can be listed using the list command.",
|
2019-10-29 20:54:47 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2019-11-02 14:32:58 +00:00
|
|
|
providedSuite := args[0]
|
|
|
|
runningSuite, err := getRunningSuite()
|
2019-10-29 20:54:47 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2019-11-02 14:32:58 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if runningSuite != "" && runningSuite != providedSuite {
|
|
|
|
log.Fatal("A suite is already running")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := setupSuite(providedSuite); err != nil {
|
|
|
|
log.Fatal(err)
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
},
|
2019-11-02 14:32:58 +00:00
|
|
|
Args: cobra.ExactArgs(1),
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// SuitesTeardownCmd Command for tearing down a suite environment.
|
2019-11-02 14:32:58 +00:00
|
|
|
var SuitesTeardownCmd = &cobra.Command{
|
|
|
|
Use: "teardown [suite]",
|
|
|
|
Short: "Teardown a Go suite environment. Suites can be listed using the list command.",
|
2019-10-29 20:54:47 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2019-11-02 14:32:58 +00:00
|
|
|
var suiteName string
|
|
|
|
if len(args) == 1 {
|
|
|
|
suiteName = args[0]
|
|
|
|
} else {
|
|
|
|
runningSuite, err := getRunningSuite()
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
if err != nil {
|
2019-11-30 16:49:52 +00:00
|
|
|
log.Fatal(err)
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if runningSuite == "" {
|
2019-11-30 16:49:52 +00:00
|
|
|
log.Fatal(ErrNoRunningSuite)
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
suiteName = runningSuite
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := teardownSuite(suiteName); err != nil {
|
2019-11-30 16:49:52 +00:00
|
|
|
log.Fatal(err)
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
2019-11-02 14:32:58 +00:00
|
|
|
},
|
|
|
|
Args: cobra.MaximumNArgs(1),
|
|
|
|
}
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// SuitesTestCmd Command for testing a suite.
|
2019-11-02 14:32:58 +00:00
|
|
|
var SuitesTestCmd = &cobra.Command{
|
|
|
|
Use: "test [suite]",
|
|
|
|
Short: "Test a suite. Suites can be listed using the list command.",
|
|
|
|
Run: testSuite,
|
|
|
|
Args: cobra.MaximumNArgs(1),
|
|
|
|
}
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
func listSuites() []string {
|
|
|
|
suiteNames := make([]string, 0)
|
2020-04-09 01:05:17 +00:00
|
|
|
suiteNames = append(suiteNames, suites.GlobalRegistry.Suites()...)
|
2019-11-02 14:32:58 +00:00
|
|
|
sort.Strings(suiteNames)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
return suiteNames
|
|
|
|
}
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
func checkSuiteAvailable(suite string) error {
|
|
|
|
suites := listSuites()
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
for _, s := range suites {
|
|
|
|
if s == suite {
|
|
|
|
return nil
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
return ErrNotAvailableSuite
|
|
|
|
}
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
func runSuiteSetupTeardown(command string, suite string) error {
|
|
|
|
selectedSuite := suite
|
|
|
|
err := checkSuiteAvailable(selectedSuite)
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == ErrNotAvailableSuite {
|
|
|
|
log.Fatal(errors.New("Suite named " + selectedSuite + " does not exist"))
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
s := suites.GlobalRegistry.Get(selectedSuite)
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2020-11-19 01:50:34 +00:00
|
|
|
if command == "teardown" {
|
|
|
|
if _, err := os.Stat("../../web/.nyc_output"); err == nil {
|
|
|
|
log.Infof("Generating frontend coverage reports for suite %s...", suite)
|
|
|
|
|
|
|
|
cmd := utils.Command("yarn", "report")
|
|
|
|
cmd.Dir = "web"
|
|
|
|
cmd.Env = os.Environ()
|
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:50:58 +00:00
|
|
|
cmd := utils.CommandWithStdout("go", "run", "cmd/authelia-suites/main.go", command, selectedSuite)
|
2019-11-17 10:47:07 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
2019-11-02 14:32:58 +00:00
|
|
|
cmd.Env = os.Environ()
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
return utils.RunCommandWithTimeout(cmd, s.SetUpTimeout)
|
|
|
|
}
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-30 16:49:52 +00:00
|
|
|
func runOnSetupTimeout(suite string) error {
|
|
|
|
cmd := utils.CommandWithStdout("go", "run", "cmd/authelia-suites/main.go", "timeout", suite)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.Env = os.Environ()
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-30 16:49:52 +00:00
|
|
|
return utils.RunCommandWithTimeout(cmd, 15*time.Second)
|
|
|
|
}
|
|
|
|
|
2020-03-03 07:18:25 +00:00
|
|
|
func runOnError(suite string) error {
|
|
|
|
cmd := utils.CommandWithStdout("go", "run", "cmd/authelia-suites/main.go", "error", suite)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.Env = os.Environ()
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-03-03 07:18:25 +00:00
|
|
|
return utils.RunCommandWithTimeout(cmd, 15*time.Second)
|
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
func setupSuite(suiteName string) error {
|
|
|
|
log.Infof("Setup environment for suite %s...", suiteName)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-06-11 00:30:53 +00:00
|
|
|
signalChannel := make(chan os.Signal, 1)
|
2019-11-02 14:32:58 +00:00
|
|
|
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
interrupted := false
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
go func() {
|
|
|
|
<-signalChannel
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
interrupted = true
|
|
|
|
}()
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
if errSetup := runSuiteSetupTeardown("setup", suiteName); errSetup != nil || interrupted {
|
2019-11-30 16:49:52 +00:00
|
|
|
if errSetup == utils.ErrTimeoutReached {
|
2020-12-16 01:47:31 +00:00
|
|
|
err := runOnSetupTimeout(suiteName)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-11-17 10:47:07 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-12-16 01:47:31 +00:00
|
|
|
err := teardownSuite(suiteName)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
return errSetup
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
func teardownSuite(suiteName string) error {
|
|
|
|
log.Infof("Tear down environment for suite %s...", suiteName)
|
|
|
|
return runSuiteSetupTeardown("teardown", suiteName)
|
|
|
|
}
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
func testSuite(cmd *cobra.Command, args []string) {
|
|
|
|
runningSuite, err := getRunningSuite()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// If suite(s) are provided as argument.
|
2020-02-04 21:18:02 +00:00
|
|
|
if len(args) >= 1 {
|
2019-12-08 12:41:29 +00:00
|
|
|
suiteArg := args[0]
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-12-08 12:41:29 +00:00
|
|
|
if runningSuite != "" && suiteArg != runningSuite {
|
|
|
|
log.Fatal(errors.New("Running suite (" + runningSuite + ") is different than suite(s) to be tested (" + suiteArg + "). Shutdown running suite and retry"))
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 21:18:02 +00:00
|
|
|
if err := runMultipleSuitesTests(strings.Split(suiteArg, ","), runningSuite == ""); err != nil {
|
2019-11-02 14:32:58 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if runningSuite != "" {
|
|
|
|
fmt.Println("Running suite (" + runningSuite + ") detected. Run tests of that suite")
|
|
|
|
if err := runSuiteTests(runningSuite, false); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-29 20:54:47 +00:00
|
|
|
} else {
|
2019-11-02 14:32:58 +00:00
|
|
|
fmt.Println("No suite provided therefore all suites will be tested")
|
|
|
|
if err := runAllSuites(); err != nil {
|
|
|
|
log.Fatal(err)
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getRunningSuite() (string, error) {
|
2019-11-02 14:32:58 +00:00
|
|
|
exist, err := utils.FileExists(runningSuiteFile)
|
2019-10-29 20:54:47 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exist {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
b, err := ioutil.ReadFile(runningSuiteFile)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-10-29 20:54:47 +00:00
|
|
|
return string(b), err
|
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
func runSuiteTests(suiteName string, withEnv bool) error {
|
|
|
|
if withEnv {
|
|
|
|
if err := setupSuite(suiteName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
suite := suites.GlobalRegistry.Get(suiteName)
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// Default value is 1 minute.
|
2019-11-02 14:32:58 +00:00
|
|
|
timeout := "60s"
|
|
|
|
if suite.TestTimeout > 0 {
|
|
|
|
timeout = fmt.Sprintf("%ds", int64(suite.TestTimeout/time.Second))
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-02-04 21:18:02 +00:00
|
|
|
testCmdLine := fmt.Sprintf("go test -count=1 -v ./internal/suites -timeout %s ", timeout)
|
|
|
|
|
|
|
|
if testPattern != "" {
|
|
|
|
testCmdLine += fmt.Sprintf("-run '%s'", testPattern)
|
|
|
|
} else {
|
|
|
|
testCmdLine += fmt.Sprintf("-run '^(Test%sSuite)$'", suiteName)
|
|
|
|
}
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
log.Infof("Running tests of suite %s...", suiteName)
|
|
|
|
log.Debugf("Running tests with command: %s", testCmdLine)
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
cmd := utils.CommandWithStdout("bash", "-c", testCmdLine)
|
2019-11-17 10:47:07 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
2019-11-02 14:32:58 +00:00
|
|
|
cmd.Env = os.Environ()
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
if headless {
|
|
|
|
cmd.Env = append(cmd.Env, "HEADLESS=y")
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
testErr := cmd.Run()
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2020-03-03 07:18:25 +00:00
|
|
|
// If the tests failed, run the error hook.
|
|
|
|
if testErr != nil {
|
|
|
|
if err := runOnError(suiteName); err != nil {
|
|
|
|
// Do not return this error to return the test error instead
|
|
|
|
// and not skip the teardown phase.
|
|
|
|
log.Errorf("Error executing OnError callback: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-17 10:47:07 +00:00
|
|
|
|
2020-03-03 07:18:25 +00:00
|
|
|
if withEnv {
|
|
|
|
if err := teardownSuite(suiteName); err != nil {
|
2020-05-02 05:06:39 +00:00
|
|
|
// Do not return this error to return the test error instead.
|
2020-03-03 07:18:25 +00:00
|
|
|
log.Errorf("Error running teardown: %v", err)
|
2019-11-17 10:47:07 +00:00
|
|
|
}
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
return testErr
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
|
2019-12-08 12:41:29 +00:00
|
|
|
func runMultipleSuitesTests(suiteNames []string, withEnv bool) error {
|
|
|
|
for _, suiteName := range suiteNames {
|
|
|
|
if err := runSuiteTests(suiteName, withEnv); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-12-08 12:41:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
func runAllSuites() error {
|
|
|
|
log.Info("Start running all suites")
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
for _, s := range listSuites() {
|
|
|
|
if err := runSuiteTests(s, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
log.Info("All suites passed successfully")
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
return nil
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|