/
runner_test.go
148 lines (116 loc) · 3.1 KB
/
runner_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package runner
import (
"os/exec"
"strings"
"testing"
"time"
"github.com/eko/monday/internal/config"
mocks "github.com/eko/monday/internal/tests/mocks/proxy"
"github.com/stretchr/testify/assert"
)
func TestNewRunner(t *testing.T) {
// Given
proxy := &mocks.ProxyInterface{}
project := getMockedProjectWithApplication()
// When
runner := NewRunner(proxy, project)
// Then
assert.IsType(t, new(Runner), runner)
assert.Equal(t, proxy, runner.proxy)
assert.Equal(t, project.Name, runner.projectName)
assert.Equal(t, project.Applications, runner.applications)
}
func TestRunAll(t *testing.T) {
// Given
execCommand = mockExecCommand
proxy := &mocks.ProxyInterface{}
project := getMockedProjectWithApplication()
runner := NewRunner(proxy, project)
// When
runner.RunAll()
// Then
// Wait for goroutine to launch application and be available
for i := 0; i < 50; i++ {
if _, ok := runner.cmds["test-app"]; ok {
break
}
time.Sleep(time.Duration(100 * time.Millisecond))
}
// Check for application to be runned properly
if cmd, ok := runner.cmds["test-app"]; ok {
runCommand := strings.Replace(strings.Join(cmd.Args, " "), "echo <runner>", "runner", -1)
assert.Equal(t, "echo OK Arguments Seems -to=work", runCommand)
} else {
t.Fatal("Cannot retrieve just launched application command execution")
}
}
func TestStop(t *testing.T) {
// Given
execCommand = mockExecCommand
proxy := &mocks.ProxyInterface{}
project := getMockedProjectWithApplication()
runner := NewRunner(proxy, project)
runner.RunAll()
// Wait for goroutine to launch application and be available
for i := 0; i < 50; i++ {
if _, ok := runner.cmds["test-app"]; ok {
break
}
time.Sleep(time.Duration(100 * time.Millisecond))
}
// When
runner.Stop()
// Then
if cmd, ok := runner.cmds["test-app"]; ok {
runCommand := strings.Replace(strings.Join(cmd.Args, " "), "echo <runner>", "runner", -1)
assert.Equal(t, "echo OK Arguments Seems -to=work", runCommand)
assert.True(t, cmd.ProcessState.Exited())
} else {
t.Fatal("Cannot retrieve just launched application command execution")
}
}
func TestSetupAll(t *testing.T) {
// Given
execCommand = mockExecCommand
proxy := &mocks.ProxyInterface{}
project := &config.Project{
Name: "My project name",
Applications: []*config.Application{
&config.Application{
Name: "test-app",
Path: "/unkown/directory",
Setup: []string{
"echo Starting test command setup...",
"echo ...and a second setup command to confirm it works",
},
},
},
}
runner := NewRunner(proxy, project)
// When
runner.SetupAll()
// Then
assert.True(t, hasSetup)
}
func mockExecCommand(command string, args ...string) *exec.Cmd {
args = append([]string{"<runner>"}, args...)
return exec.Command("echo", args...)
}
func getMockedProjectWithApplication() *config.Project {
return &config.Project{
Name: "My project name",
Applications: []*config.Application{
&config.Application{
Name: "test-app",
Path: "/",
Executable: "echo",
Args: []string{
"OK",
"Arguments",
"Seems",
"-to=work",
},
},
},
}
}