tests: beginning of tests using Docker
This commit is contained in:
@ -30,9 +30,14 @@ func init() {
|
||||
}
|
||||
|
||||
func Backup(cmd *cobra.Command, args []string) {
|
||||
backyConfOpts := backy.NewOpts(cfgFile, backy.AddCommandLists(cmdLists), backy.SetLogFile(logFile), backy.SetCmdStdOut(cmdStdOut))
|
||||
backyConfOpts := backy.NewConfigOptions(configFile,
|
||||
backy.AddCommandLists(cmdLists),
|
||||
backy.SetLogFile(logFile),
|
||||
backy.EnableCommandStdOut(cmdStdOut),
|
||||
backy.SetHostsConfigFile(hostsConfigFile))
|
||||
|
||||
backyConfOpts.InitConfig()
|
||||
backyConfOpts.ReadConfig()
|
||||
backyConfOpts.ParseConfigurationFile()
|
||||
|
||||
backyConfOpts.RunListConfig("")
|
||||
for _, host := range backyConfOpts.Hosts {
|
||||
|
54
cmd/backup_test.go
Normal file
54
cmd/backup_test.go
Normal file
@ -0,0 +1,54 @@
|
||||
package cmd
|
||||
|
||||
// import (
|
||||
// "bufio"
|
||||
// "encoding/json"
|
||||
// "os"
|
||||
// "os/exec"
|
||||
// "strings"
|
||||
// "testing"
|
||||
|
||||
// "github.com/stretchr/testify/assert"
|
||||
// )
|
||||
|
||||
// // TestConfigOptions tests the configuration options for the backy package.
|
||||
// func Test_ErrorHook(t *testing.T) {
|
||||
// configFile := "-f ../../tests/ErrorHook.yml"
|
||||
// logFile := "--log-file=ErrorHook.log"
|
||||
// backyCommand := exec.Command("go", "run", "../../backy.go", configFile, logFile, "backup")
|
||||
// backyCommand.Stderr = os.Stdout
|
||||
// backyCommand.Stdout = os.Stdout
|
||||
// err := backyCommand.Run()
|
||||
// assert.Nil(t, err)
|
||||
// os.Remove("ErrorHook.log")
|
||||
// logFileData, logFileErr := os.ReadFile("ErrorHook.log")
|
||||
// if logFileErr != nil {
|
||||
// assert.FailNow(t, logFileErr.Error())
|
||||
|
||||
// }
|
||||
// var JsonData []map[string]interface{}
|
||||
// jsonScanner := bufio.NewScanner(strings.NewReader(string(logFileData)))
|
||||
|
||||
// for jsonScanner.Scan() {
|
||||
// var jsonDataLine map[string]interface{}
|
||||
// err = json.Unmarshal(jsonScanner.Bytes(), &jsonDataLine)
|
||||
// assert.Nil(t, err)
|
||||
// JsonData = append(JsonData, jsonDataLine)
|
||||
// }
|
||||
// for _, v := range JsonData {
|
||||
// _, ok := v["error"]
|
||||
// if !ok {
|
||||
// assert.FailNow(t, "error does not exist\n")
|
||||
// // return
|
||||
// }
|
||||
// }
|
||||
// // t.Logf("%s", logFileData)
|
||||
// // t.Logf("%v", JsonData)
|
||||
// }
|
||||
|
||||
// func TestBackupErrorHook(t *testing.T) {
|
||||
// logFile = "ErrorHook.log"
|
||||
|
||||
// configFile = "../tests/ErrorHook.yml"
|
||||
|
||||
// }
|
@ -20,7 +20,7 @@ package cmd
|
||||
|
||||
// func config(cmd *cobra.Command, args []string) {
|
||||
|
||||
// opts := backy.NewOpts(cfgFile, backy.cronEnabled())
|
||||
// opts := backy.NewConfigOptions(configFile, backy.cronEnabled())
|
||||
// opts.InitConfig()
|
||||
|
||||
// }
|
||||
|
@ -18,13 +18,14 @@ var (
|
||||
func cron(cmd *cobra.Command, args []string) {
|
||||
parseS3Config()
|
||||
|
||||
opts := backy.NewOpts(cfgFile,
|
||||
opts := backy.NewConfigOptions(configFile,
|
||||
backy.EnableCron(),
|
||||
backy.SetLogFile(logFile),
|
||||
backy.SetCmdStdOut(cmdStdOut))
|
||||
backy.EnableCommandStdOut(cmdStdOut),
|
||||
backy.SetHostsConfigFile(hostsConfigFile))
|
||||
|
||||
opts.InitConfig()
|
||||
opts.ReadConfig()
|
||||
opts.ParseConfigurationFile()
|
||||
|
||||
opts.Cron()
|
||||
}
|
||||
|
@ -32,8 +32,12 @@ func execute(cmd *cobra.Command, args []string) {
|
||||
logging.ExitWithMSG("Please provide a command to run. Pass --help to see options.", 1, nil)
|
||||
}
|
||||
|
||||
opts := backy.NewOpts(cfgFile, backy.AddCommands(args), backy.SetLogFile(logFile), backy.SetCmdStdOut(cmdStdOut))
|
||||
opts := backy.NewConfigOptions(configFile,
|
||||
backy.AddCommands(args),
|
||||
backy.SetLogFile(logFile),
|
||||
backy.EnableCommandStdOut(cmdStdOut),
|
||||
backy.SetHostsConfigFile(hostsConfigFile))
|
||||
opts.InitConfig()
|
||||
opts.ReadConfig()
|
||||
opts.ParseConfigurationFile()
|
||||
opts.ExecuteCmds()
|
||||
}
|
||||
|
17
cmd/host.go
17
cmd/host.go
@ -35,10 +35,13 @@ func init() {
|
||||
// 2. stdin (on command line) (TODO)
|
||||
|
||||
func Host(cmd *cobra.Command, args []string) {
|
||||
backyConfOpts := backy.NewOpts(cfgFile, backy.SetLogFile(logFile), backy.SetCmdStdOut(cmdStdOut))
|
||||
backyConfOpts := backy.NewConfigOptions(configFile,
|
||||
backy.SetLogFile(logFile),
|
||||
backy.EnableCommandStdOut(cmdStdOut),
|
||||
backy.SetHostsConfigFile(hostsConfigFile))
|
||||
backyConfOpts.InitConfig()
|
||||
|
||||
backyConfOpts.ReadConfig()
|
||||
backyConfOpts.ParseConfigurationFile()
|
||||
|
||||
// check CLI input
|
||||
if hostsList == nil {
|
||||
@ -46,14 +49,20 @@ func Host(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
|
||||
for _, h := range hostsList {
|
||||
if backy.IsHostLocal(h) {
|
||||
continue
|
||||
}
|
||||
// check if h exists in the config file
|
||||
_, hostFound := backyConfOpts.Hosts[h]
|
||||
if !hostFound {
|
||||
// check if h exists in the SSH config file
|
||||
hostFoundInConfig, s := backy.CheckIfHostHasHostName(h)
|
||||
hostFoundInConfig, s := backy.DoesHostHaveHostName(h)
|
||||
if !hostFoundInConfig {
|
||||
logging.ExitWithMSG("host "+h+" not found", 1, &backyConfOpts.Logger)
|
||||
}
|
||||
if backyConfOpts.Hosts == nil {
|
||||
backyConfOpts.Hosts = make(map[string]*backy.Host)
|
||||
}
|
||||
// create host with hostname and host
|
||||
backyConfOpts.Hosts[h] = &backy.Host{Host: h, HostName: s}
|
||||
}
|
||||
@ -68,5 +77,5 @@ func Host(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
}
|
||||
|
||||
backyConfOpts.ExecCmdsSSH(cmdList, hostsList)
|
||||
backyConfOpts.ExecCmdsOnHosts(cmdList, hostsList)
|
||||
}
|
||||
|
20
cmd/list.go
20
cmd/list.go
@ -22,13 +22,13 @@ var (
|
||||
Use: "cmds [cmd1 cmd2 cmd3...]",
|
||||
Short: "List commands defined in config file.",
|
||||
Long: "List commands defined in config file",
|
||||
Run: ListCmds,
|
||||
Run: ListCommands,
|
||||
}
|
||||
listCmdLists = &cobra.Command{
|
||||
Use: "lists [list1 list2 ...]",
|
||||
Short: "List lists defined in config file.",
|
||||
Long: "List lists defined in config file",
|
||||
Run: ListCmdLists,
|
||||
Run: ListCommandLists,
|
||||
}
|
||||
)
|
||||
|
||||
@ -40,7 +40,7 @@ func init() {
|
||||
|
||||
}
|
||||
|
||||
func ListCmds(cmd *cobra.Command, args []string) {
|
||||
func ListCommands(cmd *cobra.Command, args []string) {
|
||||
|
||||
// setup based on whats passed in:
|
||||
// - cmds
|
||||
@ -54,17 +54,19 @@ func ListCmds(cmd *cobra.Command, args []string) {
|
||||
|
||||
parseS3Config()
|
||||
|
||||
opts := backy.NewOpts(cfgFile, backy.SetLogFile(logFile))
|
||||
opts := backy.NewConfigOptions(configFile,
|
||||
backy.SetLogFile(logFile),
|
||||
backy.SetHostsConfigFile(hostsConfigFile))
|
||||
|
||||
opts.InitConfig()
|
||||
opts.ReadConfig()
|
||||
opts.ParseConfigurationFile()
|
||||
|
||||
for _, v := range cmdsToList {
|
||||
opts.ListCommand(v)
|
||||
}
|
||||
}
|
||||
|
||||
func ListCmdLists(cmd *cobra.Command, args []string) {
|
||||
func ListCommandLists(cmd *cobra.Command, args []string) {
|
||||
|
||||
parseS3Config()
|
||||
|
||||
@ -74,10 +76,12 @@ func ListCmdLists(cmd *cobra.Command, args []string) {
|
||||
logging.ExitWithMSG("Error: lists subcommand needs lists", 1, nil)
|
||||
}
|
||||
|
||||
opts := backy.NewOpts(cfgFile, backy.SetLogFile(logFile))
|
||||
opts := backy.NewConfigOptions(configFile,
|
||||
backy.SetLogFile(logFile),
|
||||
backy.SetHostsConfigFile(hostsConfigFile))
|
||||
|
||||
opts.InitConfig()
|
||||
opts.ReadConfig()
|
||||
opts.ParseConfigurationFile()
|
||||
|
||||
for _, v := range listsToList {
|
||||
opts.ListCommandList(v)
|
||||
|
14
cmd/root.go
14
cmd/root.go
@ -13,11 +13,12 @@ import (
|
||||
|
||||
var (
|
||||
// Used for flags.
|
||||
cfgFile string
|
||||
verbose bool
|
||||
cmdStdOut bool
|
||||
logFile string
|
||||
s3Endpoint string
|
||||
configFile string
|
||||
hostsConfigFile string
|
||||
verbose bool
|
||||
cmdStdOut bool
|
||||
logFile string
|
||||
s3Endpoint string
|
||||
|
||||
rootCmd = &cobra.Command{
|
||||
Use: "backy",
|
||||
@ -38,7 +39,8 @@ func init() {
|
||||
rootCmd.PersistentFlags().StringVar(&logFile, "log-file", "", "log file to write to")
|
||||
rootCmd.PersistentFlags().BoolVar(&cmdStdOut, "cmdStdOut", false, "Pass to print command output to stdout")
|
||||
|
||||
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "f", "", "config file to read from")
|
||||
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "f", "", "config file to read from")
|
||||
rootCmd.PersistentFlags().StringVar(&hostsConfigFile, "hostsConfig", "", "yaml hosts file to read from")
|
||||
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Sets verbose level")
|
||||
rootCmd.PersistentFlags().StringVar(&s3Endpoint, "s3-endpoint", "", "Sets the S3 endpoint used for config file fetching. Overrides S3_ENDPOINT env variable.")
|
||||
rootCmd.AddCommand(backupCmd, execCmd, cronCmd, versionCmd, listCmd)
|
||||
|
Reference in New Issue
Block a user