Skip to content

Commit

Permalink
Command Scripts from TestSuite or TestStep (#117)
Browse files Browse the repository at this point in the history
Co-authored-by: Marcin Owsiany <mowsiany@D2iQ.com>
Co-authored-by: Andreas Neumann <aneumann@mesosphere.com>
Signed-off-by: Ken Sipe <kensipe@gmail.com>
  • Loading branch information
3 people committed May 22, 2020
1 parent 8f3d766 commit 5e3968d
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/apis/testharness/v1beta1/test_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ type Command struct {
Command string `json:"command"`
// If set, the `--namespace` flag will be appended to the command with the namespace to use.
Namespaced bool `json:"namespaced"`
// Ability to run a shell script from TestStep (without a script file)
// namespaced and command should not be used with script. namespaced is ignored and command is an error.
// env expansion is depended upon the shell but ENV is passed to the runtime env.
Script string `json:"script"`
// If set, exit failures (`exec.ExitError`) will be ignored. `exec.Error` are NOT ignored.
IgnoreFailure bool `json:"ignoreFailure"`
// If set, the command is run in the background.
Expand Down
14 changes: 14 additions & 0 deletions pkg/test/utils/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,20 @@ func ExpandEnv(c string, env map[string]string) string {
func GetArgs(ctx context.Context, cmd harness.Command, namespace string, env map[string]string) (*exec.Cmd, error) {
argSlice := []string{}

if cmd.Command != "" && cmd.Script != "" {
return nil, errors.New("command and script can not be set in the same configuration")
}
if cmd.Command == "" && cmd.Script == "" {
return nil, errors.New("command or script must be set")
}
if cmd.Script != "" && cmd.Namespaced {
return nil, errors.New("script can not used 'namespaced', use the $NAMESPACE environment variable instead")
}

if cmd.Script != "" {
builtCmd := exec.CommandContext(ctx, "sh", "-c", cmd.Script)
return builtCmd, nil
}
c := ExpandEnv(cmd.Command, env)

argSplit, err := shlex.Split(c)
Expand Down
70 changes: 70 additions & 0 deletions pkg/test/utils/kubernetes_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"bytes"
"context"
"errors"
"io/ioutil"
Expand Down Expand Up @@ -414,3 +415,72 @@ func TestExpandEnv(t *testing.T) {
"EXPAND_ME": "world",
}))
}

func TestRunScript(t *testing.T) {
tests := []struct {
name string
command string
script string
wantedErr bool
expectedStdout bool
}{
{
name: `no script and no command`,
command: "",
script: "",
wantedErr: true,
expectedStdout: false,
},
{
name: `script AND command`,
command: "echo 'hello'",
script: "for i in {1..5}; do echo $NAMESPACE; done",
wantedErr: true,
expectedStdout: false,
},
// failure for script command as a command (reason we need a script script option)
{
name: `command has a failing script command`,
command: "for i in {1..5}; do echo $NAMESPACE; done",
script: "",
wantedErr: true,
expectedStdout: false,
},
{
name: `working script command`,
command: "",
script: "for i in {1..5}; do echo $NAMESPACE; done",
wantedErr: false,
expectedStdout: true,
},
}

for _, tt := range tests {

tt := tt

t.Run(tt.name, func(t *testing.T) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
hcmd := harness.Command{
Command: tt.command,
Script: tt.script,
}

logger := NewTestLogger(t, "")
// script runs with output
_, err := RunCommand(context.TODO(), "", hcmd, "", stdout, stderr, logger, 0)

if tt.wantedErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
if tt.expectedStdout {
assert.True(t, stdout.Len() > 0)
} else {
assert.True(t, stdout.Len() == 0)
}
})
}
}

0 comments on commit 5e3968d

Please sign in to comment.