From b639c988909828ce7043c705e357d6a055c85f28 Mon Sep 17 00:00:00 2001 From: Oliver Feldmann Date: Wed, 11 Jan 2023 11:59:09 +0100 Subject: [PATCH] [newmanExecute] Allow env vars in the runOptions (#3966) * Allow env vars in the runOptions * Add documentation * Regenerate for documentation * Fix documentation --- cmd/newmanExecute.go | 6 +++++- cmd/newmanExecute_generated.go | 2 +- cmd/newmanExecute_test.go | 15 +++++++++++++++ documentation/docs/steps/newmanExecute.md | 14 ++++++++++++++ resources/metadata/newmanExecute.yaml | 2 +- 5 files changed, 36 insertions(+), 3 deletions(-) diff --git a/cmd/newmanExecute.go b/cmd/newmanExecute.go index 0158e3304..db1be0311 100644 --- a/cmd/newmanExecute.go +++ b/cmd/newmanExecute.go @@ -163,7 +163,11 @@ func resolveTemplate(config *newmanExecuteOptions, collection string) ([]string, } for _, runOption := range config.RunOptions { - templ, err := template.New("template").Parse(runOption) + templ, err := template.New("template").Funcs(template.FuncMap{ + "getenv": func(varName string) string { + return os.Getenv(varName) + }, + }).Parse(runOption) if err != nil { log.SetErrorCategory(log.ErrorConfiguration) return nil, errors.Wrap(err, "could not parse newman command template") diff --git a/cmd/newmanExecute_generated.go b/cmd/newmanExecute_generated.go index eeb3d48e6..de540ade4 100644 --- a/cmd/newmanExecute_generated.go +++ b/cmd/newmanExecute_generated.go @@ -202,7 +202,7 @@ func NewmanExecuteCommand() *cobra.Command { func addNewmanExecuteFlags(cmd *cobra.Command, stepConfig *newmanExecuteOptions) { cmd.Flags().StringVar(&stepConfig.NewmanCollection, "newmanCollection", `**/*.postman_collection.json`, "The test collection that should be executed. This could also be a file pattern.") cmd.Flags().StringVar(&stepConfig.NewmanRunCommand, "newmanRunCommand", os.Getenv("PIPER_newmanRunCommand"), "+++ Deprecated +++ Please use list parameter `runOptions` instead.") - cmd.Flags().StringSliceVar(&stepConfig.RunOptions, "runOptions", []string{`run`, `{{.NewmanCollection}}`, `--reporters`, `cli,junit,html`, `--reporter-junit-export`, `target/newman/TEST-{{.CollectionDisplayName}}.xml`, `--reporter-html-export`, `target/newman/TEST-{{.CollectionDisplayName}}.html`}, "The newman command that will be executed inside the docker container.") + cmd.Flags().StringSliceVar(&stepConfig.RunOptions, "runOptions", []string{`run`, `{{.NewmanCollection}}`, `--reporters`, `cli,junit,html`, `--reporter-junit-export`, `target/newman/TEST-{{.CollectionDisplayName}}.xml`, `--reporter-html-export`, `target/newman/TEST-{{.CollectionDisplayName}}.html`}, "The newman command that will be executed inside the docker container. Env vars can be passed via template as in \"{{getenv MY_ENV_VAR}}\".") cmd.Flags().StringVar(&stepConfig.NewmanInstallCommand, "newmanInstallCommand", `npm install newman newman-reporter-html --global --quiet`, "The shell command that will be executed inside the docker container to install Newman.") cmd.Flags().StringVar(&stepConfig.NewmanEnvironment, "newmanEnvironment", os.Getenv("PIPER_newmanEnvironment"), "Specify an environment file path or URL. Environments provide a set of variables that one can use within collections.") cmd.Flags().StringVar(&stepConfig.NewmanGlobals, "newmanGlobals", os.Getenv("PIPER_newmanGlobals"), "Specify the file path or URL for global variables. Global variables are similar to environment variables but have a lower precedence and can be overridden by environment variables having the same name.") diff --git a/cmd/newmanExecute_test.go b/cmd/newmanExecute_test.go index b16d4e44e..aa1a2373a 100644 --- a/cmd/newmanExecute_test.go +++ b/cmd/newmanExecute_test.go @@ -1,6 +1,8 @@ package cmd import ( + "github.com/google/uuid" + "os" "path/filepath" "strings" "testing" @@ -230,6 +232,19 @@ func TestResolveTemplate(t *testing.T) { assert.Equal(t, []string{"this", "is", "my", "fancy", "command", "theDisplayName"}, cmd) }) + t.Run("get environment variable", func(t *testing.T) { + t.Parallel() + + temporaryEnvVarName := uuid.New().String() + os.Setenv(temporaryEnvVarName, "myEnvVar") + defer os.Unsetenv(temporaryEnvVarName) + config := newmanExecuteOptions{RunOptions: []string{"this", "is", "my", "fancy", "command", "with", "--env-var", "{{getenv \"" + temporaryEnvVarName + "\"}}"}} + + cmd, err := resolveTemplate(&config, "collectionsDisplayName") + assert.NoError(t, err) + assert.Equal(t, []string{"this", "is", "my", "fancy", "command", "with", "--env-var", "myEnvVar"}, cmd) + }) + t.Run("error when parameter cannot be resolved", func(t *testing.T) { t.Parallel() diff --git a/documentation/docs/steps/newmanExecute.md b/documentation/docs/steps/newmanExecute.md index 44e473988..a005dedef 100644 --- a/documentation/docs/steps/newmanExecute.md +++ b/documentation/docs/steps/newmanExecute.md @@ -46,6 +46,20 @@ If the following error occurs during the pipeline run, the `newmanRunCommand` is Referencing `newmanEnvironment` and `newmanGlobals` in the runOptions is redundant now. Both parameters are added to runCommand using `newmanEnvironment` and `newmanGlobals` from config when configured and not referenced by go templating using `"--environment", "{{`{{.Config.NewmanEnvironment}}`}}"` and `"--globals", "{{`{{.Config.NewmanGlobals}}`}}"` as shown above. +## Passing Credentials + +If you need to pass additional credentials you can do so via environment +variables. This is done via templating in the `runOptions`, as per this example: + +```yaml +runOptions: [ + {{`"run", "{{.NewmanCollection}}",`}} + {{`"--environment", "{{.Config.NewmanEnvironment}}",`}} + {{`"--env-var", "username={{getenv \"PIPER_TESTCREDENTIAL_USERNAME\"}}",`}} + {{`"--env-var", "password={{getenv \"PIPER_TESTCREDENTIAL_PASSWORD\"}}"`}} +] +``` + ## Example Pipeline step: diff --git a/resources/metadata/newmanExecute.yaml b/resources/metadata/newmanExecute.yaml index f3e560e41..08df0e935 100644 --- a/resources/metadata/newmanExecute.yaml +++ b/resources/metadata/newmanExecute.yaml @@ -25,7 +25,7 @@ spec: - STEPS type: string - name: runOptions - description: The newman command that will be executed inside the docker container. + description: The newman command that will be executed inside the docker container. Env vars can be passed via template as in "{{getenv MY_ENV_VAR}}". scope: - PARAMETERS - STAGES