Skip to content

Commit a7ee645

Browse files
committed
Add unit tests for runtime
1 parent 9bb1d65 commit a7ee645

9 files changed

Lines changed: 63 additions & 56 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# v2.0.0
2+
3+
- Added `nodes` which allow remote execution of tests
4+
- Added `SSHExecutor` and `LocalExecutor`
5+
- Removed `concurrent` argument from `test` command
6+
17
# v1.3.0
28

39
- Added `xml` assertion to `stdout` and `stderr`

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -653,11 +653,11 @@ Enables ssh tests in unit test suite and sets the credentials for the target hos
653653
`COMMANDER_SSH_TEST` must be set to `1` to enable ssh tests.
654654
655655
```
656-
COMMANDER_TEST_SSH=1
657-
COMMANDER_TEST_SSH_HOST=localhost:2222
658-
COMMANDER_TEST_SSH_PASS=password
659-
COMMANDER_TEST_SSH_USER=root
660-
COMMANDER_TEST_SSH_IDENTITY_FILE=~/.ssh/id_rsa
656+
export COMMANDER_TEST_SSH=1
657+
export COMMANDER_TEST_SSH_HOST=localhost:2222
658+
export COMMANDER_TEST_SSH_PASS=pass
659+
export COMMANDER_TEST_SSH_USER=root
660+
export COMMANDER_TEST_SSH_IDENTITY_FILE=integration/containers/ssh/.ssh/id_rsa
661661
```
662662
663663
## Misc

cmd/commander/commander.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ func createTestCommand() cli.Command {
4848
Usage: "Execute the test suite, by default it will use the commander.yaml from your current directory",
4949
ArgsUsage: "[file] [title]",
5050
Flags: []cli.Flag{
51-
cli.IntFlag{
52-
Name: "concurrent",
53-
EnvVar: "COMMANDER_CONCURRENT",
54-
Usage: "Set the max amount of tests which should run concurrently",
55-
},
5651
cli.BoolFlag{
5752
Name: "no-color",
5853
EnvVar: "COMMANDER_NO_COLOR",

pkg/app/test_command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestCommand(file string, title string, ctx AddCommandContext) error {
4444

4545
r := runtime.NewRuntime(s.Nodes...)
4646

47-
results := r.Start(tests, ctx.Concurrent)
47+
results := r.Start(tests)
4848
out := output.NewCliOutput(!ctx.NoColor)
4949
if !out.Start(results) {
5050
return fmt.Errorf("Test suite failed, use --verbose for more detailed output")

pkg/runtime/docker_executor.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

pkg/runtime/runtime.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ const (
1818
LineCount = "LineCount"
1919
)
2020

21-
const WorkerCountMultiplicator = 5
22-
2321
// Result status codes
2422
const (
2523
//Success status
@@ -131,8 +129,7 @@ type TestResult struct {
131129
}
132130

133131
// Start starts the given test suite and executes all tests
134-
// maxConcurrent configures the amount of go routines which will be started
135-
func (r *Runtime) Start(tests []TestCase, maxConcurrent int) <-chan TestResult {
132+
func (r *Runtime) Start(tests []TestCase) <-chan TestResult {
136133
in := make(chan TestCase)
137134
out := make(chan TestResult)
138135

@@ -213,23 +210,6 @@ func (r *Runtime) getExecutor(node string) Executor {
213210
return LocalExecutor{}
214211
}
215212

216-
func execTest(t TestCase) TestResult {
217-
result := TestResult{}
218-
for i := 1; i <= t.Command.GetRetries(); i++ {
219-
220-
e := LocalExecutor{}
221-
result = e.Execute(t)
222-
223-
result.Tries = i
224-
if result.ValidationResult.Success {
225-
break
226-
}
227-
228-
executeRetryInterval(t)
229-
}
230-
return result
231-
}
232-
233213
func executeRetryInterval(t TestCase) {
234214
if t.Command.GetRetries() > 1 && t.Command.Interval != "" {
235215
interval, err := time.ParseDuration(t.Command.Interval)

pkg/runtime/runtime_test.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ import (
66
"time"
77
)
88

9-
const SingleConcurrent = 1
9+
func Test_NewRuntime(t *testing.T) {
10+
runtime := NewRuntime(Node{Name: "test"}, Node{Name: "test2"})
11+
12+
assert.Len(t, runtime.Nodes, 3)
13+
}
1014

1115
func TestRuntime_Start(t *testing.T) {
1216
s := getExampleTestSuite()
1317
r := Runtime{}
14-
got := r.Start(s, SingleConcurrent)
18+
got := r.Start(s)
1519

1620
assert.IsType(t, make(<-chan TestResult), got)
1721

@@ -30,7 +34,7 @@ func TestRuntime_WithRetries(t *testing.T) {
3034
s[0].Command.Cmd = "echo fail"
3135

3236
r := Runtime{}
33-
got := r.Start(s, 1)
37+
got := r.Start(s)
3438

3539
var counter = 0
3640
for r := range got {
@@ -50,7 +54,7 @@ func TestRuntime_WithRetriesAndInterval(t *testing.T) {
5054

5155
start := time.Now()
5256
r := Runtime{}
53-
got := r.Start(s, 0)
57+
got := r.Start(s)
5458

5559
var counter = 0
5660
for r := range got {
@@ -64,6 +68,26 @@ func TestRuntime_WithRetriesAndInterval(t *testing.T) {
6468
assert.True(t, duration.Seconds() > 0.15, "Retry interval did not work")
6569
}
6670

71+
func Test_Runtime_getExecutor(t *testing.T) {
72+
r := NewRuntime(
73+
Node{Name: "ssh-host", Type: "ssh"},
74+
Node{Name: "localhost", Type: "local"},
75+
Node{Name: "default", Type: ""},
76+
)
77+
78+
// If empty string set as type use local executor
79+
n := r.getExecutor("default")
80+
assert.IsType(t, LocalExecutor{}, n)
81+
82+
n = nil
83+
n = r.getExecutor("localhost")
84+
assert.IsType(t, LocalExecutor{}, n)
85+
86+
n = nil
87+
n = r.getExecutor("ssh-host")
88+
assert.IsType(t, SSHExecutor{}, n)
89+
}
90+
6791
func getExampleTestSuite() []TestCase {
6892
tests := []TestCase{
6993
{

pkg/runtime/ssh_executor_linux_test.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,14 @@ type SSHExecutorTestEnv struct {
1616

1717
var SSHTestEnv SSHExecutorTestEnv
1818

19-
func TestMain(m *testing.M) {
20-
v := os.Getenv("COMMANDER_SSH_TEST")
21-
if v != "1" {
22-
log.Println("Skip ssh_executor_test, set env COMMANDER_SSH_TEST to 1")
23-
return
24-
}
25-
19+
func createExecutor() SSHExecutor {
2620
SSHTestEnv = SSHExecutorTestEnv{
2721
Host: os.Getenv("COMMANDER_TEST_SSH_HOST"),
2822
Pass: os.Getenv("COMMANDER_TEST_SSH_PASS"),
2923
User: os.Getenv("COMMANDER_TEST_SSH_USER"),
3024
IdentityFile: os.Getenv("COMMANDER_TEST_SSH_IDENTITY_FILE"),
3125
}
3226

33-
m.Run()
34-
}
35-
36-
func createExecutor() SSHExecutor {
3727
s := SSHExecutor{
3828
Host: SSHTestEnv.Host,
3929
User: SSHTestEnv.User,
@@ -44,6 +34,10 @@ func createExecutor() SSHExecutor {
4434
}
4535

4636
func Test_SSHExecutor(t *testing.T) {
37+
if !isSSHTestsEnabled() {
38+
return
39+
}
40+
4741
s := createExecutor()
4842

4943
test := TestCase{
@@ -62,6 +56,10 @@ func Test_SSHExecutor(t *testing.T) {
6256
}
6357

6458
func Test_SSHExecutor_WithDir(t *testing.T) {
59+
if !isSSHTestsEnabled() {
60+
return
61+
}
62+
6563
s := createExecutor()
6664

6765
test := TestCase{
@@ -81,6 +79,10 @@ func Test_SSHExecutor_WithDir(t *testing.T) {
8179
}
8280

8381
func Test_SSHExecutor_ExitCode(t *testing.T) {
82+
if !isSSHTestsEnabled() {
83+
return
84+
}
85+
8486
s := createExecutor()
8587

8688
test := TestCase{
@@ -94,3 +96,12 @@ func Test_SSHExecutor_ExitCode(t *testing.T) {
9496
assert.False(t, got.ValidationResult.Success)
9597
assert.Equal(t, 2, got.TestCase.Result.ExitCode)
9698
}
99+
100+
func isSSHTestsEnabled() bool {
101+
v := os.Getenv("COMMANDER_SSH_TEST")
102+
if v != "1" {
103+
log.Println("Skip ssh_executor_test, set env COMMANDER_SSH_TEST to 1")
104+
return false
105+
}
106+
return true
107+
}

0 commit comments

Comments
 (0)