Skip to content

Commit

Permalink
add cli flag env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenhillier committed Jan 25, 2019
1 parent c449be2 commit 1854c91
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ Define requests in YAML. [See the test spec properties](#test-spec-properties)

#### Complete example

`apitest -f test.yaml -e token=secret123`

```yaml
# test.yaml
environment:
vars:
host: http://localhost:8000 # {{host}} in request URLs will be replaced with this value
token: secret123
headers:
Authorization: Bearer {{token}} # all requests will include this header
host: http://localhost:8000 # {{host}} will be replaced with this value
headers: # all requests will include headers defined here
Authorization: Bearer {{token}} # token was passed with the -e flag.
requests:
- name: Add a comment
url: "{{host}}/comments"
Expand Down Expand Up @@ -110,6 +112,12 @@ requests:

`apitest -f input.yaml`

Arguments:

* `--file` `-f`: specify a file containing test specs. Example: `-f test/test.yaml`
* `--env` `-e`: define variables for the test environment. Example: `-e myvar=test123`
* `--test` `-t`: specify the name of a single test to run (use quotes if the name contains spaces). Example: `-t "Todo list"`

### GitHub Actions

Add a step to your workflow like this:
Expand Down
26 changes: 24 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"log"
"regexp"
"strings"

flag "github.com/spf13/pflag"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -58,8 +59,10 @@ type UserVar struct {
func main() {
var filename string
var testname string
var userVars []string
flag.StringVarP(&filename, "file", "f", "", "yaml file containing a list of test requests")
flag.StringVarP(&testname, "test", "t", "", "the name of a single test to run (use quotes if name has spaces)")
flag.StringSliceVarP(&userVars, "env", "e", []string{}, "variables to add to the test environment")
flag.Parse()

if filename == "" {
Expand All @@ -69,7 +72,12 @@ func main() {
// read in test definitions from a provided yaml file
set, err := readTestDefinition(filename)
if err != nil {
log.Fatalln(err)
log.Fatal(err)
}

err = set.Environment.processEnvVars(userVars)
if err != nil {
log.Fatal(err)
}

// run the set of tests and exit the program.
Expand Down Expand Up @@ -99,7 +107,7 @@ func readTestDefinition(filename string) (TestSet, error) {
}

// process template tags. a period will be pre-pended to the argument: {{ myVar }} becomes {{ .myVar }}
// so that the text/template package can process them.
// so that the text/template package can replace them with variables.
r, err := regexp.Compile(`{{\s*(\w+)\s*}}`)
if err != nil {
return TestSet{}, errors.New("error processing {{ template }} tags. Please double check input file")
Expand Down Expand Up @@ -140,3 +148,17 @@ func runRequests(requests []Request, env Environment, testname string) (int, int
}
return currentRequest, failCount
}

// processEnvVars iterates through userVars provided through the command line
// and puts valid vars (in the form -e var="my var" or -e token=$API_TOKEN)
func (env Environment) processEnvVars(userVars []string) error {

for _, s := range userVars {
pair := strings.Split(s, "=")
if len(pair) != 2 {
return errors.New("Error processing env vars. Usage example: -e myvar=$MYVAR -e anothervar=$MYVAR2")
}
env.Vars[pair[0]] = pair[1]
}
return nil
}

0 comments on commit 1854c91

Please sign in to comment.