2019-11-02 14:32:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/otiai10/copy"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/internal/suites"
|
|
|
|
"github.com/authelia/authelia/internal/utils"
|
2019-11-02 14:32:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var tmpDirectory = "/tmp/authelia/suites/"
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
func init() {
|
2019-11-17 10:47:07 +00:00
|
|
|
log.SetLevel(log.InfoLevel)
|
2019-11-02 14:32:58 +00: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 16:49:52 +00:00
|
|
|
setupTimeoutCmd := &cobra.Command{
|
|
|
|
Use: "timeout [suite]",
|
|
|
|
Short: "Run the OnSetupTimeout callback when setup times out",
|
|
|
|
Run: setupTimeoutSuite,
|
|
|
|
}
|
|
|
|
|
2020-03-03 07:18:25 +00:00
|
|
|
errorCmd := &cobra.Command{
|
|
|
|
Use: "error [suite]",
|
|
|
|
Short: "Run the OnError callback when some tests fail",
|
|
|
|
Run: runErrorCallback,
|
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
stopCmd := &cobra.Command{
|
|
|
|
Use: "teardown [suite]",
|
|
|
|
Short: "Teardown the suite environment",
|
|
|
|
Run: teardownSuite,
|
|
|
|
}
|
|
|
|
|
|
|
|
rootCmd.AddCommand(startCmd)
|
2019-11-30 16:49:52 +00:00
|
|
|
rootCmd.AddCommand(setupTimeoutCmd)
|
2020-03-03 07:18:25 +00:00
|
|
|
rootCmd.AddCommand(errorCmd)
|
2019-11-02 14:32:58 +00:00
|
|
|
rootCmd.AddCommand(stopCmd)
|
2020-03-03 07:18:25 +00:00
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createRunningSuiteFile(suite string) error {
|
2020-05-05 07:57:30 +00:00
|
|
|
return ioutil.WriteFile(runningSuiteFile, []byte(suite), 0600)
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 10:47:07 +00:00
|
|
|
suiteResourcePath := cwd + "/internal/suites/" + suiteName
|
2019-11-02 14:32:58 +00: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 16:49:52 +00: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 07:18:25 +00: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 14:32:58 +00: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 14:53:16 +00:00
|
|
|
if err := s.TearDown(suiteTmpDirectory); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-11-02 14:32:58 +00:00
|
|
|
|
2019-11-16 14:53:16 +00:00
|
|
|
if err := os.RemoveAll(suiteTmpDirectory); err != nil {
|
2019-11-02 14:32:58 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-11-16 14:53:16 +00:00
|
|
|
if err := removeRunningSuiteFile(); err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
log.Info("Environment has been cleaned!")
|
|
|
|
}
|