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-12-24 02:14:52 +00:00
|
|
|
"github.com/authelia/authelia/internal/suites"
|
|
|
|
"github.com/authelia/authelia/internal/utils"
|
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"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
// ErrNoRunningSuite error raised when no suite is running
|
|
|
|
var ErrNoRunningSuite = errors.New("no running suite")
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
// runningSuiteFile name of the file containing the currently running suite
|
|
|
|
var runningSuiteFile = ".suite"
|
2019-10-29 20:54:47 +00:00
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
var headless bool
|
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")
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SuitesListCmd Command for listing the available suites
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
// SuitesSetupCmd Command for setuping a suite environment
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
// SuitesTeardownCmd Command for tearing down a suite environment
|
|
|
|
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
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
// SuitesTestCmd Command for testing a suite
|
|
|
|
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)
|
|
|
|
for _, k := range suites.GlobalRegistry.Suites() {
|
|
|
|
suiteNames = append(suiteNames, k)
|
|
|
|
}
|
|
|
|
sort.Strings(suiteNames)
|
|
|
|
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
|
|
|
}
|
|
|
|
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
|
|
|
}
|
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
|
|
|
|
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()
|
|
|
|
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()
|
|
|
|
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)
|
|
|
|
signalChannel := make(chan os.Signal)
|
|
|
|
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
|
|
|
|
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 {
|
|
|
|
runOnSetupTimeout(suiteName)
|
2019-11-17 10:47:07 +00:00
|
|
|
}
|
2019-11-30 16:49:52 +00:00
|
|
|
teardownSuite(suiteName)
|
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
|
|
|
|
2019-12-08 12:41:29 +00:00
|
|
|
// If suite(s) are provided as argument
|
2019-11-02 14:32:58 +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
|
|
|
}
|
|
|
|
|
2019-12-08 12:41:29 +00:00
|
|
|
suiteNames := strings.Split(suiteArg, ",")
|
|
|
|
if err := runMultipleSuitesTests(suiteNames, 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)
|
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
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
// Default value is 1 minute
|
|
|
|
timeout := "60s"
|
|
|
|
if suite.TestTimeout > 0 {
|
|
|
|
timeout = fmt.Sprintf("%ds", int64(suite.TestTimeout/time.Second))
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|
2019-12-04 07:46:49 +00:00
|
|
|
testCmdLine := fmt.Sprintf("go test -count=1 -v ./internal/suites -timeout %s -run '^(Test%sSuite)$'", timeout, 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()
|
|
|
|
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
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
if withEnv {
|
2019-11-17 10:47:07 +00:00
|
|
|
err := teardownSuite(suiteName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
func runAllSuites() error {
|
|
|
|
log.Info("Start running all suites")
|
|
|
|
for _, s := range listSuites() {
|
|
|
|
if err := runSuiteTests(s, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Info("All suites passed successfully")
|
|
|
|
return nil
|
2019-10-29 20:54:47 +00:00
|
|
|
}
|