Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: argument as environment variables #323

Merged
merged 2 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ It can also output xUnit results files.
* [Variable on Command Line](#variable-on-command-line)
* [Variable Definitions Files](#variable-definitions-files)
* [Environment Variables](#environment-variables)
* [Variable helpers](#variable-helpers)
* [How to use outputs from a test step as input of another test step](#how-to-use-outputs-from-a-test-step-as-input-of-another-test-step)
* [Builtin venom variables](#builtin-venom-variables)
* [Tests Report](#tests-report)
* [Assertion](#assertion)
* [Keywords](#keywords)
* [Debug your testsuites](#debug-your-testsuites)
* [Use venom in CI](#use-venom-in-ci)
* [Hacking](#hacking)
* [License](#license)

Expand Down Expand Up @@ -47,6 +49,23 @@ Flags:
-v, --verbose count verbose. -vv to very verbose and -vvv to very verbose with CPU Profiling
```

You can define the arguments with environment variables:

```bash
venom run my-test-suite.yml --format=json
# is the same as
VENOM_FORMAT=json venom run my-test-suite.yml
```

```
--format - example: VENOM_FORMAT=json
--output-dir - example: VENOM_OUTPUT_DIR=.
--stop-on-failure - example: VENOM_STOP_ON_FAILURE=true
--var - example: VENOM_VAR="foo=bar"
--var-from-file - example: VENOM_VAR_FROM_FILE="fileA.yml fileB.yml"
-v - example: VENOM_VERBOSE=2 is the same as -vv
```

# Docker image

venom can be launched inside a docker image with:
Expand Down Expand Up @@ -181,6 +200,50 @@ $ export VENOM_VAR_foo=bar
$ venom run *.yml
```

### Variable helpers

Helpers available and some examples:

- `abbrev`
- `abbrevboth`
- `trunc`
- `trim`
- `upper`: {{.myvar | upper}}
- `lower`: {{.myvar | lower}}
- `title`
- `untitle`
- `substr`
- `repeat`
- `trimall`
- `trimAll`
- `trimSuffix`
- `trimPrefix`
- `nospace`
- `initials`
- `randAlphaNum`
- `randAlpha`
- `randASCII`
- `randNumeric`
- `swapcase`
- `shuffle`
- `snakecase`
- `camelcase`
- `quote`
- `squote`
- `indent`
- `nindent`
- `replace`: {{.myvar | replace "_" "."}}
- `plural`
- `default`: {{.myvar | default ""}}
- `empty`
- `coalesce`
- `toJSON`
- `toPrettyJSON`
- `b64enc`
- `b64dec` {{.result.bodyjson | b64enc}}
- `escape`: replace ‘_‘, ‘/’, ‘.’ by ‘-’


## How to use outputs from a test step as input of another test step

To be able to reuse a property from a teststep in a following testcase or step, you have to extract the variable, as the following example.
Expand Down
67 changes: 58 additions & 9 deletions cmd/venom/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"runtime/pprof"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -51,6 +52,51 @@ func init() {
Cmd.Flags().BoolVarP(&stopOnFailure, "stop-on-failure", "", false, "Stop running Test Suite on first Test Case failure")
Cmd.PersistentFlags().StringVarP(&outputDir, "output-dir", "", "", "Output Directory: create tests results file inside this directory")
verbose = Cmd.Flags().CountP("verbose", "v", "verbose. -vv to very verbose and -vvv to very verbose with CPU Profiling")

if err := initFromEnv(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(2)
}
}

func initFromEnv() error {
if os.Getenv("VENOM_VAR") != "" {
variables = strings.Split(os.Getenv("VENOM_VAR"), " ")
}
if os.Getenv("VENOM_VAR_FROM_FILE") != "" {
varFiles = strings.Split(os.Getenv("VENOM_VAR_FROM_FILE"), " ")
}
if os.Getenv("VENOM_FORMAT") != "" {
format = os.Getenv("VENOM_FORMAT")
}
if os.Getenv("VENOM_STOP_ON_FAILURE") != "" {
var err error
stopOnFailure, err = strconv.ParseBool(os.Getenv("VENOM_STOP_ON_FAILURE"))
if err != nil {
return fmt.Errorf("invalid value for VENOM_STOP_ON_FAILURE")
}
}
if os.Getenv("VENOM_OUTPUT_DIR") != "" {
outputDir = os.Getenv("VENOM_OUTPUT_DIR")
}
if os.Getenv("VENOM_VERBOSE") != "" {
v, err := strconv.ParseInt(os.Getenv("VENOM_VERBOSE"), 10, 64)
if err != nil {
return fmt.Errorf("invalid value for VENOM_VERBOSE, must be 1, 2 or 3")
}
v2 := int(v)
verbose = &v2
}
return nil
}

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)
}

// Cmd run
Expand Down Expand Up @@ -90,6 +136,12 @@ Notice that variables initialized with -var-from-file argument can be overrided
v.StopOnFailure = stopOnFailure
v.Verbose = *verbose

if err := v.InitLogger(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(2)
return err
}

if v.Verbose == 3 {
fCPU, err := os.Create(filepath.Join(v.OutputDir, "pprof_cpu_profile.prof"))
if err != nil {
Expand All @@ -106,6 +158,9 @@ Notice that variables initialized with -var-from-file argument can be overrided
defer pprof.StopCPUProfile()
}
}
if *verbose >= 2 {
displayArg(context.Background())
}

var readers = []io.Reader{}
for _, f := range varFiles {
Expand All @@ -128,28 +183,22 @@ Notice that variables initialized with -var-from-file argument can be overrided

start := time.Now()

if err := v.Init(); err != nil {
fmt.Println(err)
os.Exit(2)
return err
}

if err := v.Parse(path); err != nil {
fmt.Println(err)
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(2)
return err
}

tests, err := v.Process(context.Background(), path)
if err != nil {
fmt.Println(err)
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(2)
return err
}

elapsed := time.Since(start)
if err := v.OutputResult(*tests, elapsed); err != nil {
fmt.Println(err)
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(2)
return err
}
Expand Down
4 changes: 2 additions & 2 deletions process.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/sirupsen/logrus"
)

// Init initializes venom logger
func (v *Venom) Init() error {
// InitLogger initializes venom logger
func (v *Venom) InitLogger() error {
v.testsuites = []TestSuite{}
if v.Verbose == 0 {
logrus.SetLevel(logrus.WarnLevel)
Expand Down
2 changes: 1 addition & 1 deletion process_teststep.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (v *Venom) RunTestStep(ctx context.Context, e ExecutorRunner, ts *TestSuite
mapResult := GetExecutorResult(result)
mapResultString, _ := executors.DumpString(result)

if v.Verbose == 2 {
if v.Verbose >= 2 {
fdump := dumpFile{
Result: result,
TestStep: step,
Expand Down