Skip to content

Commit

Permalink
Merge pull request #88 from nicolaferraro/fix-cli-3
Browse files Browse the repository at this point in the history
chore(CLI): add name and timeout to the steps
  • Loading branch information
christophd authored Apr 23, 2020
2 parents d862b6e + d38369e commit 55a0604
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,9 @@ config:
pre:
- script: prepare.sh
- run: echo Start!
- run: |
- name: Optional name
timeout: 30m
run: |
echo "Multiline"
echo "Commands are also"
echo "Supported!"
Expand All @@ -463,6 +465,10 @@ given it is assumed to be a file path relative to the current test group directo
With `run` you can add any shell command. At the moment only single line commands are supported here. You can add multiple `run` commands in a `pre`
or `post` section.

Each step can also define a human readable `name` that will be printed before its execution.

By default a step must complete within 30 minutes (`30m`). The timeout can be changed using the `timeout` option in the step declaration (in Golang duration format).

Scripts can leverage the following environment variables that are set automatically by the Yaks runtime:

- **YAKS_NAMESPACE**: always contains the namespace where the tests will be executed, no matter if the namespace is fixed or temporary
Expand Down
24 changes: 13 additions & 11 deletions pkg/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,36 @@ import (
)

type RunConfig struct {
Config Config `yaml:"config"`
Config Config `yaml:"config"`
Pre []StepConfig `yaml:"pre"`
Post []StepConfig `yaml:"post"`
}

type Config struct {
Recursive bool `yaml:"recursive"`
Namespace NamespaceConfig
Runtime RuntimeConfig
Recursive bool `yaml:"recursive"`
Namespace NamespaceConfig
Runtime RuntimeConfig
}

type StepConfig struct {
Run string `yaml:"run"`
Script string `yaml:"script"`
Run string `yaml:"run"`
Script string `yaml:"script"`
Name string `yaml:"name"`
Timeout string `yaml:"timeout"`
}

type RuntimeConfig struct {
Cucumber CucumberConfig
Cucumber CucumberConfig
}

type CucumberConfig struct {
Tags []string `yaml:"tags"`
Glue []string `yaml:"glue"`
Options string `yaml:"options"`
Tags []string `yaml:"tags"`
Glue []string `yaml:"glue"`
Options string `yaml:"options"`
}

type NamespaceConfig struct {
Name string `yaml:"name"`
Name string `yaml:"name"`
Temporary bool `yaml:"temporary"`
AutoRemove bool `yaml:"autoremove"`
}
Expand Down
36 changes: 28 additions & 8 deletions pkg/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ const (
CucumberGlue = "CUCUMBER_GLUE"
CucumberFeatures = "CUCUMBER_FEATURES"
CucumberFilterTags = "CUCUMBER_FILTER_TAGS"

DefaultStepTimeout = "30m"
)

func newCmdTest(rootCmdOptions *RootCmdOptions) *cobra.Command {
Expand Down Expand Up @@ -99,9 +101,9 @@ type testCmdOptions struct {
env []string
tags []string
features []string
glue []string
options string
report report.OutputFormat
glue []string
options string
report report.OutputFormat
}

func (o *testCmdOptions) validateArgs(_ *cobra.Command, args []string) error {
Expand Down Expand Up @@ -240,7 +242,7 @@ func (o *testCmdOptions) runTestGroup(source string, results *v1alpha1.TestResul
}

if len(suiteErrors) > 0 {
results.Errors = append(results.Errors, suiteErrors...)
results.Errors = append(results.Errors, suiteErrors...)
}

return nil
Expand Down Expand Up @@ -544,7 +546,11 @@ func isDir(fileName string) bool {
func runSteps(steps []config.StepConfig, namespace, baseDir string) error {
for idx, step := range steps {
if len(step.Script) > 0 {
if err := runScript(step.Script, fmt.Sprintf("script %s", step.Script), namespace, baseDir); err != nil {
desc := step.Name
if desc == "" {
desc = fmt.Sprintf("script %s", step.Script)
}
if err := runScript(step.Script, desc, namespace, baseDir, step.Timeout); err != nil {
return err
}
}
Expand Down Expand Up @@ -576,7 +582,11 @@ func runSteps(steps []config.StepConfig, namespace, baseDir string) error {
return err
}

if err := runScript(file.Name(), fmt.Sprintf("inline command %d", idx), namespace, baseDir); err != nil {
desc := step.Name
if desc == "" {
desc = fmt.Sprintf("inline command %d", idx)
}
if err := runScript(file.Name(), desc, namespace, baseDir, step.Timeout); err != nil {
return err
}
}
Expand All @@ -585,8 +595,18 @@ func runSteps(steps []config.StepConfig, namespace, baseDir string) error {
return nil
}

func runScript(scriptFile, desc, namespace, baseDir string) error {
command := exec.Command(scriptFile)
func runScript(scriptFile, desc, namespace, baseDir, timeout string) error {
if timeout == "" {
timeout = DefaultStepTimeout
}
actualTimeout, err := time.ParseDuration(timeout)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), actualTimeout)
defer cancel()

command := exec.CommandContext(ctx, scriptFile)

command.Env = os.Environ()
command.Env = append(command.Env, fmt.Sprintf("YAKS_NAMESPACE=%s", namespace))
Expand Down

0 comments on commit 55a0604

Please sign in to comment.