2019-11-02 21:32:58 +07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2019-12-24 09:14:52 +07:00
|
|
|
"github.com/authelia/authelia/internal/suites"
|
|
|
|
"github.com/authelia/authelia/internal/utils"
|
2019-11-02 21:32:58 +07:00
|
|
|
"github.com/otiai10/copy"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var tmpDirectory = "/tmp/authelia/suites/"
|
|
|
|
|
|
|
|
// runningSuiteFile name of the file containing the currently running suite
|
|
|
|
var runningSuiteFile = ".suite"
|
|
|
|
|
|
|
|
func init() {
|
2019-11-17 17:47:07 +07:00
|
|
|
log.SetLevel(log.InfoLevel)
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
rootCmd := &cobra.Command{
|
|
|
|
Use: "authelia-suites",
|
|
|
|
}
|
|
|
|
|
|
|
|
startCmd := &cobra.Command{
|
|
|
|
Use: "setup [suite]",
|
|
|
|
Short: "Setup the suite environment",
|
|
|
|
Run: setupSuite,
|
|
|
|
}
|
|
|
|
|
2019-11-30 23:49:52 +07:00
|
|
|
setupTimeoutCmd := &cobra.Command{
|
|
|
|
Use: "timeout [suite]",
|
|
|
|
Short: "Run the OnSetupTimeout callback when setup times out",
|
|
|
|
Run: setupTimeoutSuite,
|
|
|
|
}
|
|
|
|
|
2020-03-03 14:18:25 +07:00
|
|
|
errorCmd := &cobra.Command{
|
|
|
|
Use: "error [suite]",
|
|
|
|
Short: "Run the OnError callback when some tests fail",
|
|
|
|
Run: runErrorCallback,
|
|
|
|
}
|
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
stopCmd := &cobra.Command{
|
|
|
|
Use: "teardown [suite]",
|
|
|
|
Short: "Teardown the suite environment",
|
|
|
|
Run: teardownSuite,
|
|
|
|
}
|
|
|
|
|
|
|
|
rootCmd.AddCommand(startCmd)
|
2019-11-30 23:49:52 +07:00
|
|
|
rootCmd.AddCommand(setupTimeoutCmd)
|
2020-03-03 14:18:25 +07:00
|
|
|
rootCmd.AddCommand(errorCmd)
|
2019-11-02 21:32:58 +07:00
|
|
|
rootCmd.AddCommand(stopCmd)
|
2020-03-03 14:18:25 +07:00
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-11-02 21:32:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func createRunningSuiteFile(suite string) error {
|
|
|
|
return ioutil.WriteFile(runningSuiteFile, []byte(suite), 0644)
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeRunningSuiteFile() error {
|
|
|
|
return os.Remove(runningSuiteFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupSuite(cmd *cobra.Command, args []string) {
|
|
|
|
suiteName := args[0]
|
|
|
|
s := suites.GlobalRegistry.Get(suiteName)
|
|
|
|
|
|
|
|
cwd, err := filepath.Abs("./")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-11-17 17:47:07 +07:00
|
|
|
suiteResourcePath := cwd + "/internal/suites/" + suiteName
|
2019-11-02 21:32:58 +07:00
|
|
|
|
|
|
|
exist, err := utils.FileExists(suiteResourcePath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
suiteTmpDirectory := tmpDirectory + suiteName
|
|
|
|
|
|
|
|
if exist {
|
|
|
|
err := copy.Copy(suiteResourcePath, suiteTmpDirectory)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err := os.MkdirAll(suiteTmpDirectory, 0755)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the .suite file
|
|
|
|
if err := createRunningSuiteFile(suiteName); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.SetUp(suiteTmpDirectory)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failure during environment deployment.")
|
|
|
|
teardownSuite(nil, args)
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Environment is ready!")
|
|
|
|
}
|
|
|
|
|
2019-11-30 23:49:52 +07:00
|
|
|
func setupTimeoutSuite(cmd *cobra.Command, args []string) {
|
|
|
|
suiteName := args[0]
|
|
|
|
s := suites.GlobalRegistry.Get(suiteName)
|
|
|
|
|
|
|
|
if s.OnSetupTimeout == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := s.OnSetupTimeout(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 14:18:25 +07:00
|
|
|
func runErrorCallback(cmd *cobra.Command, args []string) {
|
|
|
|
suiteName := args[0]
|
|
|
|
s := suites.GlobalRegistry.Get(suiteName)
|
|
|
|
|
|
|
|
if s.OnError == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := s.OnError(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
func teardownSuite(cmd *cobra.Command, args []string) {
|
|
|
|
if os.Getenv("SKIP_TEARDOWN") != "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s := suites.GlobalRegistry.Get(args[0])
|
|
|
|
|
|
|
|
suiteTmpDirectory := tmpDirectory + args[0]
|
2019-11-16 21:53:16 +07:00
|
|
|
if err := s.TearDown(suiteTmpDirectory); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-11-02 21:32:58 +07:00
|
|
|
|
2019-11-16 21:53:16 +07:00
|
|
|
if err := os.RemoveAll(suiteTmpDirectory); err != nil {
|
2019-11-02 21:32:58 +07:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-11-16 21:53:16 +07:00
|
|
|
if err := removeRunningSuiteFile(); err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
|
2019-11-02 21:32:58 +07:00
|
|
|
log.Info("Environment has been cleaned!")
|
|
|
|
}
|