-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathruntime.go
More file actions
247 lines (212 loc) · 5.61 KB
/
runtime.go
File metadata and controls
247 lines (212 loc) · 5.61 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package runtime
import (
"fmt"
"github.com/SimonBaeumer/cmd"
"log"
"os"
"runtime"
"strings"
"sync"
"time"
)
// Constants for defining the various tested properties
const (
ExitCode = "ExitCode"
Stdout = "Stdout"
Stderr = "Stderr"
LineCount = "LineCount"
)
const WorkerCountMultiplicator = 5
// Result status codes
const (
//Success status
Success ResultStatus = iota
// Failed status
Failed
// Skipped status
Skipped
)
// TestCase represents a test case which will be executed by the runtime
type TestCase struct {
Title string
Command CommandUnderTest
Expected Expected
Result CommandResult
}
//TestConfig represents the configuration for a test
type TestConfig struct {
Env map[string]string
Dir string
Timeout string
Retries int
Interval string
InheritEnv bool
}
// ResultStatus represents the status code of a test result
type ResultStatus int
// CommandResult holds the result for a specific test
type CommandResult struct {
Status ResultStatus
Stdout string
Stderr string
Combined string
ExitCode int
FailureProperties []string
Error error
}
//Expected is the expected output of the command under test
type Expected struct {
Stdout ExpectedOut
Stderr ExpectedOut
LineCount int
ExitCode int
}
//ExpectedOut represents the assertions on stdout and stderr
type ExpectedOut struct {
Contains []string `yaml:"contains,omitempty"`
Lines map[int]string `yaml:"lines,omitempty"`
Exactly string `yaml:"exactly,omitempty"`
LineCount int `yaml:"line-count,omitempty"`
NotContains []string `yaml:"not-contains,omitempty"`
JSON map[string]string `yaml:"json,omitempty"`
XML map[string]string `yaml:"xml,omitempty"`
}
// CommandUnderTest represents the command under test
type CommandUnderTest struct {
Cmd string
InheritEnv bool
Env map[string]string
Dir string
Timeout string
Retries int
Interval string
}
// TestResult represents the TestCase and the ValidationResult
type TestResult struct {
TestCase TestCase
ValidationResult ValidationResult
FailedProperty string
Tries int
}
// Start starts the given test suite and executes all tests
// maxConcurrent configures the amount of go routines which will be started
func Start(tests []TestCase, maxConcurrent int) <-chan TestResult {
in := make(chan TestCase)
out := make(chan TestResult)
go func(tests []TestCase) {
defer close(in)
for _, t := range tests {
in <- t
}
}(tests)
workerCount := maxConcurrent
if maxConcurrent == 0 {
workerCount = runtime.NumCPU() * WorkerCountMultiplicator
}
var wg sync.WaitGroup
for i := 0; i < workerCount; i++ {
wg.Add(1)
go func(tests chan TestCase) {
defer wg.Done()
for t := range tests {
result := TestResult{}
for i := 1; i <= t.Command.GetRetries(); i++ {
result = runTest(t)
result.Tries = i
if result.ValidationResult.Success {
break
}
executeRetryInterval(t)
}
out <- result
}
}(in)
}
go func(results chan TestResult) {
wg.Wait()
close(results)
}(out)
return out
}
func executeRetryInterval(t TestCase) {
if t.Command.GetRetries() > 1 && t.Command.Interval != "" {
interval, err := time.ParseDuration(t.Command.Interval)
if err != nil {
panic(fmt.Sprintf("'%s' interval error: %s", t.Command.Cmd, err))
}
time.Sleep(interval)
}
}
// runTest executes the current test case
func runTest(test TestCase) TestResult {
timeoutOpt, err := createTimeoutOption(test.Command.Timeout)
if err != nil {
test.Result = CommandResult{Error: err}
return TestResult{
TestCase: test,
}
}
envOpt := createEnvVarsOption(test)
// cut = command under test
cut := cmd.NewCommand(
test.Command.Cmd,
cmd.WithWorkingDir(test.Command.Dir),
timeoutOpt,
envOpt)
if err := cut.Execute(); err != nil {
log.Println(test.Title, " failed ", err.Error())
test.Result = CommandResult{
Error: err,
}
return TestResult{
TestCase: test,
}
}
log.Println("title: '"+test.Title+"'", " Command: ", test.Command.Cmd)
log.Println("title: '"+test.Title+"'", " Directory: ", cut.Dir)
log.Println("title: '"+test.Title+"'", " Env: ", cut.Env)
// Write test result
test.Result = CommandResult{
ExitCode: cut.ExitCode(),
Stdout: strings.TrimSpace(strings.Replace(cut.Stdout(), "\r\n", "\n", -1)),
Stderr: strings.TrimSpace(strings.Replace(cut.Stderr(), "\r\n", "\n", -1)),
Combined: strings.TrimSpace(strings.Replace(cut.Combined(), "\r\n", "\n", -1)),
}
log.Println("title: '"+test.Title+"'", " ExitCode: ", test.Result.ExitCode)
log.Println("title: '"+test.Title+"'", " Stdout: ", test.Result.Stdout)
log.Println("title: '"+test.Title+"'", " Stderr: ", test.Result.Stderr)
return Validate(test)
}
func createEnvVarsOption(test TestCase) func(c *cmd.Command) {
return func(c *cmd.Command) {
// Add all env variables from parent process
if test.Command.InheritEnv {
for _, v := range os.Environ() {
split := strings.Split(v, "=")
c.AddEnv(split[0], split[1])
}
}
// Add custom env variables
for k, v := range test.Command.Env {
c.AddEnv(k, v)
}
}
}
func createTimeoutOption(timeout string) (func(c *cmd.Command), error) {
timeoutOpt := cmd.WithoutTimeout
if timeout != "" {
d, err := time.ParseDuration(timeout)
if err != nil {
return func(c *cmd.Command) {}, err
}
timeoutOpt = cmd.WithTimeout(d)
}
return timeoutOpt, nil
}
// GetRetries returns the retries of the command
func (c *CommandUnderTest) GetRetries() int {
if c.Retries == 0 {
return 1
}
return c.Retries
}