From ca30acf60212284acae5d1b1a09c0cd4aa3b77f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Samin?= Date: Mon, 23 Nov 2020 11:14:42 +0100 Subject: [PATCH] feat: read venom flags from configuration file (#326) Signed-off-by: francois samin --- .gitignore | 3 +- README.md | 15 ++++++++ cmd/venom/run/cmd.go | 83 ++++++++++++++++++++++++++++++++++++++++---- go.mod | 1 + go.sum | 1 + process.go | 2 +- 6 files changed, 96 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index bac82fde..65bfe372 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,5 @@ profile.coverprofile tests.log *.dump.json *.dump.html -*.jpg \ No newline at end of file +*.jpg +.venomrc \ No newline at end of file diff --git a/README.md b/README.md index 8893bec1..3bb37548 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,21 @@ VENOM_FORMAT=json venom run my-test-suite.yml -v - example: VENOM_VERBOSE=2 is the same as -vv ``` +You can define the venom settings using a configuration file `.venomrc`. This configuration file should be placed in the current directory or in the home directory. + +``` +variables: + - foo=bar +variables_files + - my_var_file.yaml +stop_on_failure: true +format: xml +output_dir: output +verbosity: 3 +``` + +Please note that command line flags overrides the configuration file. Configuration file overrides the Environment variables. + # Docker image venom can be launched inside a docker image with: diff --git a/cmd/venom/run/cmd.go b/cmd/venom/run/cmd.go index 32dc7d91..2b8a1dfa 100644 --- a/cmd/venom/run/cmd.go +++ b/cmd/venom/run/cmd.go @@ -12,12 +12,13 @@ import ( "strings" "time" + "github.com/ghodss/yaml" yml "github.com/ghodss/yaml" + "github.com/mitchellh/go-homedir" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/ovh/venom" - "github.com/ovh/venom/executors/dbfixtures" "github.com/ovh/venom/executors/exec" "github.com/ovh/venom/executors/grpc" @@ -57,6 +58,74 @@ func init() { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(2) } + + if err := initFromConfigFile(); err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + os.Exit(2) + } +} + +func fileExists(filename string) bool { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} + +func initFromConfigFile() error { + if fileExists(".venomrc") { + fi, err := os.Open(".venomrc") + if err != nil { + return err + } + defer fi.Close() + return initFromReader(fi) + } + + home, err := homedir.Dir() + if err != nil { + return err + } + if fileExists(filepath.Join(home, ".venomrc")) { + fi, err := os.Open(filepath.Join(home, ".venomrc")) + if err != nil { + return err + } + defer fi.Close() + return initFromReader(fi) + } + return nil +} + +type ConfigFileData struct { + Variables []string `yaml:"variables"` + VariablesFiles []string `yaml:"variables_files"` + StopOnFailure bool `yaml:"stop_on_failure"` + Format string `yaml:"format"` + OutputDir string `yaml:"output_dir"` + Verbosity int `yaml:"verbosity"` +} + +func initFromReader(reader io.Reader) error { + btes, err := ioutil.ReadAll(reader) + if err != nil { + return err + } + + var configFileData ConfigFileData + if err := yaml.Unmarshal(btes, &configFileData); err != nil { + return err + } + + variables = configFileData.Variables + varFiles = configFileData.VariablesFiles + format = configFileData.Format + stopOnFailure = configFileData.StopOnFailure + outputDir = configFileData.OutputDir + verbose = &configFileData.Verbosity + + return nil } func initFromEnv() error { @@ -91,12 +160,12 @@ func initFromEnv() error { } func displayArg(ctx context.Context) { - venom.Debug(ctx, "arg variables=%v", strings.Join(variables, " ")) - venom.Debug(ctx, "arg varFiles=%v", strings.Join(varFiles, " ")) - venom.Debug(ctx, "arg format=%v", format) - venom.Debug(ctx, "arg stopOnFailure=%v", stopOnFailure) - venom.Debug(ctx, "arg outputDir=%v", outputDir) - venom.Debug(ctx, "arg verbose=%v", *verbose) + venom.Debug(ctx, "Command arg variables=%v", strings.Join(variables, " ")) + venom.Debug(ctx, "Command arg varFiles=%v", strings.Join(varFiles, " ")) + venom.Debug(ctx, "Command arg format=%v", format) + venom.Debug(ctx, "Command arg stopOnFailure=%v", stopOnFailure) + venom.Debug(ctx, "Command arg outputDir=%v", outputDir) + venom.Debug(ctx, "Command arg verbose=%v", *verbose) } // Cmd run diff --git a/go.mod b/go.mod index a7d9f2f5..3f48a49d 100644 --- a/go.mod +++ b/go.mod @@ -26,6 +26,7 @@ require ( github.com/mattn/go-shellwords v1.0.10 github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect github.com/mattn/go-zglob v0.0.3 + github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/mapstructure v1.3.3 github.com/mndrix/tap-go v0.0.0-20171203230836-629fa407e90b github.com/mxk/go-imap v0.0.0-20150429134902-531c36c3f12d // indirect diff --git a/go.sum b/go.sum index 52f7d40b..28158082 100644 --- a/go.sum +++ b/go.sum @@ -472,6 +472,7 @@ github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyex github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI= github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg= diff --git a/process.go b/process.go index 41d2384f..3fdd53d8 100644 --- a/process.go +++ b/process.go @@ -78,7 +78,7 @@ func (v *Venom) Parse(path []string) error { return err } - Debug(context.TODO(), "ts(%s).Vars: %+v", ts.Package, ts.Vars) + Debug(context.TODO(), "Testsuite (%s) variables: %+v", ts.Package, ts.Vars) for k := range ts.Vars { textractedVars = append(textractedVars, k) }