Skip to content

Commit

Permalink
fix(#60): Add possibility to run scripts before/after a test group
Browse files Browse the repository at this point in the history
You can run scripts before/after a test group. Just add your commands to the yaks-config.yaml configuration for the test group
  • Loading branch information
christophd committed Apr 20, 2020
1 parent 8cc7bfa commit f7f6457
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 8 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,32 @@ Also we can make use of command line options when using the `yaks` binary.
yaks test hello-world.feature --tag @regression --glue org.citrusframework.yaks
```

## Pre/Post scripts

You can run scripts before/after a test group. Just add your commands to the `yaks-config.yaml` configuration for the test group.

```yaml
config:
namespace:
temporary: false
autoRemove: true
pre:
- script: prepare.sh
- run: echo Start!
post:
- script: finish.sh
- run: echo Bye!
```

The section `pre` runs before a test group and `post` is added after the test group has finished. The post steps are run even if the tests or pre steps fail
for some reason. This ensures that cleanup tasks are performed also in case of errors.

The `script` option provides a file path to bash script to execute. The user has to make sure that the script is executable. If no absolute file path is
given it is assumed to be a file path relative to the current test group directory.

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.

## For YAKS Developers

Requirements:
Expand Down
3 changes: 3 additions & 0 deletions examples/run-scripts/finish.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

echo Test finshed!
3 changes: 3 additions & 0 deletions examples/run-scripts/prepare.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

echo Now preparing the test!
4 changes: 4 additions & 0 deletions examples/run-scripts/test.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Feature: test

Scenario: print message
Given print 'Hello from YAKS!'
10 changes: 10 additions & 0 deletions examples/run-scripts/yaks-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
config:
namespace:
temporary: false
autoRemove: true
pre:
- script: prepare.sh
- run: echo Start!
post:
- script: finish.sh
- run: echo Bye!
19 changes: 13 additions & 6 deletions pkg/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,23 @@ import (

type RunConfig struct {
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"`
}

type RuntimeConfig struct {
Cucumber CucumberConfig
Cucumber CucumberConfig
}

type CucumberConfig struct {
Expand All @@ -50,7 +57,7 @@ type NamespaceConfig struct {
AutoRemove bool `yaml:"autoremove"`
}

func newWithDefaults() *RunConfig {
func NewWithDefaults() *RunConfig {
ns := NamespaceConfig{
AutoRemove: true,
Temporary: false,
Expand All @@ -61,7 +68,7 @@ func newWithDefaults() *RunConfig {
}

func LoadConfig(file string) (*RunConfig, error) {
config := newWithDefaults()
config := NewWithDefaults()
data, err := ioutil.ReadFile(file)
if err != nil && os.IsNotExist(err) {
return config, nil
Expand Down
67 changes: 65 additions & 2 deletions pkg/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io/ioutil"
"net/http"
"os"
"os/exec"
"path"
"regexp"
"strings"
Expand Down Expand Up @@ -147,6 +148,12 @@ func (o *testCmdOptions) runTest(source string) error {
return err
}

baseDir := getBaseDir(source)
defer runSteps(runConfig.Post, baseDir)
if err = runSteps(runConfig.Pre, baseDir); err != nil {
return err
}

_, err = o.createAndRunTest(c, source, runConfig)
return err
}
Expand Down Expand Up @@ -180,6 +187,12 @@ func (o *testCmdOptions) runTestGroup(source string, results *map[string]error)
return err
}

baseDir := getBaseDir(source)
defer runSteps(runConfig.Post, baseDir)
if err = runSteps(runConfig.Pre, baseDir); err != nil {
return err
}

for _, f := range files {
name := path.Join(source, f.Name())
if f.IsDir() && runConfig.Config.Recursive {
Expand All @@ -200,6 +213,19 @@ func (o *testCmdOptions) runTestGroup(source string, results *map[string]error)
return err
}

func getBaseDir(source string) string {
if isRemoteFile(source) {
return ""
}

if isDir(source) {
return source
} else {
dir, _ := path.Split(source);
return dir
}
}

func printSummary(results map[string]error) {
summary := "\n\nTest suite results:\n"
for k, v := range results {
Expand All @@ -214,6 +240,12 @@ func printSummary(results map[string]error) {

func (o *testCmdOptions) getRunConfig(source string) (*config.RunConfig, error) {
var configFile string
var runConfig *config.RunConfig

if isRemoteFile(source) {
return config.NewWithDefaults(), nil
}

if isDir(source) {
// search for config file in given directory
configFile = path.Join(source, ConfigFile)
Expand All @@ -223,8 +255,6 @@ func (o *testCmdOptions) getRunConfig(source string) (*config.RunConfig, error)
configFile = path.Join(dir, ConfigFile);
}

var runConfig *config.RunConfig

runConfig, err := config.LoadConfig(configFile)
if err != nil {
return nil, err
Expand Down Expand Up @@ -492,6 +522,39 @@ func isDir(fileName string) bool {
return false
}

func runSteps(steps []config.StepConfig, baseDir string) error {
for _, step := range steps {
if len(step.Script) > 0 {
var scriptFile string

if len(baseDir) > 0 && !path.IsAbs(step.Script) {
scriptFile = path.Join(baseDir, step.Script)
} else {
scriptFile = step.Script
}

if out, err := exec.Command(scriptFile).Output(); err == nil {
fmt.Printf("Running script %s: \n%s\n", step.Script, out)
} else {
fmt.Printf("Failed to run script %s: \n%s\n", step.Script, err)
return err
}
}

if len(step.Run) > 0 {
tokens := strings.Split(step.Run, " ")
if out, err := exec.Command(tokens[0], tokens[1:]...).Output(); err == nil {
fmt.Printf("Running command %s: \n%s\n", step.Run, out)
} else {
fmt.Printf("Failed to run command %s: \n%s\n", step.Run, err)
return err
}
}
}

return nil
}

func initializeTempNamespace(name string, c client.Client, context context.Context) (metav1.Object, error) {
var obj runtime.Object

Expand Down

0 comments on commit f7f6457

Please sign in to comment.