Skip to content

Commit 0a98ef4

Browse files
committed
Specifying alpine image version in 'Integration-linux' test
playing around WIP: this works WIP: TODO handle single test command input WIP: general pattern finished, need to refactor and optimize and error handle WIP: handling error. TODO: push rec downstream Finished bulk test, would be nice to parse out by file
1 parent 36ed967 commit 0a98ef4

12 files changed

Lines changed: 189 additions & 27 deletions

File tree

examples/commander.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ tests:
1919
it should fail:
2020
command: invalid
2121
stderr: "/bin/sh: 1: invalid: not found"
22-
exit-code: 127
22+
exit-code: 127

examples/environment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ tests:
1212
config:
1313
env:
1414
KEY: local
15-
exit-code: 0
15+
exit-code: 0

examples/minimal_test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
tests:
22
echo hello:
3-
exit-code: 0
3+
exit-code: 0

go.mod298498081.tmp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module github.com/SimonBaeumer/commander
2+
3+
require (
4+
github.com/Microsoft/go-winio v0.4.14 // indirect
5+
github.com/SimonBaeumer/cmd v1.1.0
6+
github.com/antchfx/xmlquery v1.1.0
7+
github.com/antchfx/xpath v1.1.0 // indirect
8+
github.com/docker/distribution v2.7.1+incompatible // indirect
9+
github.com/docker/docker v1.13.1
10+
github.com/docker/go-connections v0.4.0 // indirect
11+
github.com/docker/go-units v0.4.0 // indirect
12+
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e
13+
github.com/magiconair/properties v1.8.1 // indirect
14+
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
15+
github.com/pkg/errors v0.9.1 // indirect
16+
github.com/pmezard/go-difflib v1.0.0
17+
github.com/stretchr/objx v0.2.0 // indirect
18+
github.com/stretchr/testify v1.4.0
19+
github.com/tidwall/gjson v1.3.2
20+
github.com/urfave/cli v1.20.0
21+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
22+
golang.org/x/net v0.0.0-20191014212845-da9a3fd4c582 // indirect
23+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
24+
gopkg.in/yaml.v2 v2.2.4
25+
)
26+
27+
go 1.13

go.mod427131847.tmp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module github.com/SimonBaeumer/commander
2+
3+
require (
4+
github.com/Microsoft/go-winio v0.4.14 // indirect
5+
github.com/SimonBaeumer/cmd v1.1.0
6+
github.com/antchfx/xmlquery v1.1.0
7+
github.com/antchfx/xpath v1.1.0 // indirect
8+
github.com/docker/distribution v2.7.1+incompatible // indirect
9+
github.com/docker/docker v1.13.1
10+
github.com/docker/go-connections v0.4.0 // indirect
11+
github.com/docker/go-units v0.4.0 // indirect
12+
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e
13+
github.com/magiconair/properties v1.8.1 // indirect
14+
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
15+
github.com/pkg/errors v0.9.1 // indirect
16+
github.com/pmezard/go-difflib v1.0.0
17+
github.com/stretchr/objx v0.2.0 // indirect
18+
github.com/stretchr/testify v1.4.0
19+
github.com/tidwall/gjson v1.3.2
20+
github.com/urfave/cli v1.20.0
21+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
22+
golang.org/x/net v0.0.0-20191014212845-da9a3fd4c582 // indirect
23+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
24+
gopkg.in/yaml.v2 v2.2.4
25+
)
26+
27+
go 1.13

go.mod939984059.tmp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module github.com/SimonBaeumer/commander
2+
3+
require (
4+
github.com/Microsoft/go-winio v0.4.14 // indirect
5+
github.com/SimonBaeumer/cmd v1.1.0
6+
github.com/antchfx/xmlquery v1.1.0
7+
github.com/antchfx/xpath v1.1.0 // indirect
8+
github.com/docker/distribution v2.7.1+incompatible // indirect
9+
github.com/docker/docker v1.13.1
10+
github.com/docker/go-connections v0.4.0 // indirect
11+
github.com/docker/go-units v0.4.0 // indirect
12+
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e
13+
github.com/magiconair/properties v1.8.1 // indirect
14+
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
15+
github.com/pkg/errors v0.9.1 // indirect
16+
github.com/pmezard/go-difflib v1.0.0
17+
github.com/stretchr/objx v0.2.0 // indirect
18+
github.com/stretchr/testify v1.4.0
19+
github.com/tidwall/gjson v1.3.2
20+
github.com/urfave/cli v1.20.0
21+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
22+
golang.org/x/net v0.0.0-20191014212845-da9a3fd4c582 // indirect
23+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
24+
gopkg.in/yaml.v2 v2.2.4
25+
)
26+
27+
go 1.13

integration/linux/docker.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ tests:
2020
BUG_REPORT_URL="https://bugs.alpinelinux.org/"
2121
2222
"id -u":
23-
stdout: "1001"
23+
stdout: "1001"

pkg/app/test_command.go

Lines changed: 73 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,110 @@ package app
22

33
import (
44
"fmt"
5-
"github.com/SimonBaeumer/commander/pkg/output"
6-
"github.com/SimonBaeumer/commander/pkg/runtime"
7-
"github.com/SimonBaeumer/commander/pkg/suite"
85
"io/ioutil"
96
"log"
107
"os"
8+
"sync"
9+
10+
"github.com/SimonBaeumer/commander/pkg/output"
11+
"github.com/SimonBaeumer/commander/pkg/runtime"
12+
"github.com/SimonBaeumer/commander/pkg/suite"
1113
)
1214

1315
// TestCommand executes the test argument
1416
// file is the path to the configuration file
1517
// title ist the title of test which should be executed, if empty it will execute all tests
1618
// ctx holds the command flags
17-
func TestCommand(file string, title string, ctx AddCommandContext) error {
19+
func TestCommand(input string, title string, ctx AddCommandContext) error {
1820
if ctx.Verbose == true {
1921
log.SetOutput(os.Stdout)
2022
}
2123

22-
if file == "" {
23-
file = CommanderFile
24+
out := output.NewCliOutput(!ctx.NoColor)
25+
26+
if input == "" {
27+
input = CommanderFile
28+
}
29+
inputType, err := os.Stat(input)
30+
if err != nil {
31+
return fmt.Errorf("Error " + err.Error())
32+
}
33+
34+
var results <-chan runtime.TestResult
35+
if inputType.IsDir() {
36+
fmt.Println("Starting test against directory: " + input + "...")
37+
fmt.Println("")
38+
39+
results, err = testDir(input, title)
40+
41+
} else {
42+
fmt.Println("Starting test file " + input + "...")
43+
fmt.Println("")
44+
results, err = testFile(input, title)
2445
}
2546

26-
fmt.Println("Starting test file " + file + "...")
27-
fmt.Println("")
28-
content, err := ioutil.ReadFile(file)
2947
if err != nil {
3048
return fmt.Errorf("Error " + err.Error())
3149
}
3250

51+
if !out.Start(results) {
52+
return fmt.Errorf("Test suite failed, use --verbose for more detailed output")
53+
}
54+
55+
return nil
56+
}
57+
58+
func testDir(directory string, title string) (<-chan runtime.TestResult, error) {
59+
files, err := ioutil.ReadDir(directory)
60+
if err != nil {
61+
return nil, fmt.Errorf("Error " + err.Error())
62+
}
63+
results := make(chan runtime.TestResult)
64+
65+
var wg sync.WaitGroup
66+
for _, f := range files {
67+
wg.Add(1)
68+
go func(f os.FileInfo) {
69+
defer wg.Done()
70+
fileResults, err := testFile(directory+"/"+f.Name(), title)
71+
if err != nil {
72+
results <- runtime.TestResult{FileError: err}
73+
return
74+
}
75+
for bb := range fileResults {
76+
results <- bb //fan in results
77+
}
78+
}(f)
79+
}
80+
81+
go func(ch chan runtime.TestResult) {
82+
wg.Wait()
83+
close(results)
84+
}(results)
85+
86+
return results, nil
87+
}
88+
89+
func testFile(input string, title string) (<-chan runtime.TestResult, error) {
90+
content, err := ioutil.ReadFile(input)
91+
if err != nil {
92+
return nil, fmt.Errorf("Error " + err.Error())
93+
}
94+
3395
var s suite.Suite
3496
s = suite.ParseYAML(content)
3597
tests := s.GetTests()
3698
// Filter tests if test title was given
3799
if title != "" {
38100
test, err := s.GetTestByTitle(title)
39101
if err != nil {
40-
return err
102+
return nil, fmt.Errorf("Error " + err.Error())
41103
}
42104
tests = []runtime.TestCase{test}
43105
}
44106

45107
r := runtime.NewRuntime(s.Nodes...)
46-
47108
results := r.Start(tests)
48-
out := output.NewCliOutput(!ctx.NoColor)
49-
if !out.Start(results) {
50-
return fmt.Errorf("Test suite failed, use --verbose for more detailed output")
51-
}
52109

53-
return nil
110+
return results, nil
54111
}

pkg/app/test_command_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package app
22

33
import (
44
"bytes"
5-
"github.com/stretchr/testify/assert"
65
"io"
76
"log"
87
"os"
98
"runtime"
109
"sync"
1110
"testing"
11+
12+
"github.com/stretchr/testify/assert"
1213
)
1314

1415
func Test_TestCommand_Verbose(t *testing.T) {
@@ -31,7 +32,7 @@ func Test_TestCommand(t *testing.T) {
3132
if runtime.GOOS == "windows" {
3233
assert.Contains(t, err.Error(), "Error open commander.yaml:")
3334
} else {
34-
assert.Equal(t, "Error open commander.yaml: no such file or directory", err.Error())
35+
assert.Equal(t, "Error stat commander.yaml: no such file or directory", err.Error())
3536
}
3637
}
3738

@@ -41,7 +42,17 @@ func Test_TestCommand_ShouldUseCustomFile(t *testing.T) {
4142
if runtime.GOOS == "windows" {
4243
assert.Contains(t, err.Error(), "Error open my-test.yaml: ")
4344
} else {
44-
assert.Equal(t, "Error open my-test.yaml: no such file or directory", err.Error())
45+
assert.Equal(t, "Error stat my-test.yaml: no such file or directory", err.Error())
46+
}
47+
}
48+
49+
func Test_TestCommand_Dir(t *testing.T) {
50+
err := TestCommand("../../examples", "echo hello", AddCommandContext{})
51+
52+
if runtime.GOOS == "windows" {
53+
assert.Contains(t, err.Error(), "Error open my-test.yaml: ")
54+
} else {
55+
assert.Equal(t, "Error stat my-test.yaml: no such file or directory", err.Error())
4556
}
4657
}
4758

pkg/output/cli.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,16 @@ func (w *OutputWriter) Start(results <-chan runtime.TestResult) bool {
3535
au = aurora.NewAurora(false)
3636
}
3737

38+
fileErrors := make([]error, 0)
3839
failed := 0
3940
testResults := []runtime.TestResult{}
4041
start := time.Now()
41-
4242
for r := range results {
43+
if r.FileError != nil {
44+
fileErrors = append(fileErrors, r.FileError)
45+
continue
46+
}
47+
4348
testResults = append(testResults, r)
4449
if r.ValidationResult.Success {
4550
str := fmt.Sprintf("✓ [%s] %s", r.Node, r.TestCase.Title)
@@ -63,6 +68,11 @@ func (w *OutputWriter) Start(results <-chan runtime.TestResult) bool {
6368
w.printFailures(testResults)
6469
}
6570

71+
w.fprintf("")
72+
for _, e := range fileErrors {
73+
fmt.Println(e)
74+
}
75+
6676
w.fprintf("")
6777
w.fprintf(fmt.Sprintf("Duration: %.3fs", duration.Seconds()))
6878
summary := fmt.Sprintf("Count: %d, Failed: %d", len(testResults), failed)

0 commit comments

Comments
 (0)