This repository has been archived by the owner on Jun 3, 2021. It is now read-only.
/
shared_feature_context.go
299 lines (264 loc) · 8.79 KB
/
shared_feature_context.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package helpers
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"strings"
"syscall"
"time"
"github.com/DATA-DOG/godog"
"github.com/DATA-DOG/godog/gherkin"
"github.com/Originate/exosphere/src"
"github.com/Originate/exosphere/src/util"
execplus "github.com/Originate/go-execplus"
"github.com/pkg/errors"
)
var childCmdPlus *execplus.CmdPlus
var childOutput string
var appDir string
var templateDir string
func waitWithTimeout(cmdPlus *execplus.CmdPlus, duration time.Duration) error {
done := make(chan error)
go func() { done <- cmdPlus.Wait() }()
select {
case err := <-done:
return err
case <-time.After(duration):
return fmt.Errorf("Timed out after %v, command did not exit. Full output:\n%s", duration, cmdPlus.GetOutput())
}
}
// SharedFeatureContext defines the festure context shared between the sub commands
// nolint: gocyclo
func SharedFeatureContext(s *godog.Suite) {
s.BeforeScenario(func(arg1 interface{}) {
var err error
appDir, err = ioutil.TempDir("", "")
if err != nil {
panic(err)
}
templateDir, err = ioutil.TempDir("", "")
if err != nil {
panic(err)
}
})
s.AfterScenario(func(arg1 interface{}, arg2 error) {
if childCmdPlus != nil {
if err := childCmdPlus.Kill(); err != nil {
panic(err)
}
childCmdPlus = nil
}
if err := cleanApp(appDir); err != nil {
panic(err)
}
})
// Application Setup
s.Step(`^I am in the root directory of a non-exosphere application$`, func() error {
return nil
})
s.Step(`^I am in the root directory of an empty application called "([^"]*)"$`, func(appName string) error {
var err error
appDir, err = createEmptyApp(appName)
return err
})
s.Step(`^I am in the root directory of the "([^"]*)" example application$`, func(name string) error {
return CheckoutApp(appDir, name)
})
s.Step(`^it doesn\'t run any tests$`, func() error {
expectedText := "is not an exosphere application"
if childCmdPlus != nil {
return childCmdPlus.WaitForText(expectedText, time.Minute)
}
return validateTextContains(childOutput, expectedText)
})
s.Step(`^I am in the directory of "([^"]*)" application containing a "([^"]*)" service$`, func(appName, serviceRole string) error {
return CheckoutApp(appDir, "test-app")
})
s.Step(`^my application has the templates:$`, func(table *gherkin.DataTable) error {
for _, row := range table.Rows[1:] {
templateName, gitURL := row.Cells[0].Value, row.Cells[1].Value
command := []string{"exo", "template", "add", templateName, gitURL}
if len(row.Cells) == 3 {
gitTag := row.Cells[2].Value
command = append(command, gitTag)
}
if _, err := util.Run(appDir, command...); err != nil {
return errors.Wrap(err, fmt.Sprintf("Failed to creates the template %s:%s\n", appDir, err))
}
}
return nil
})
s.Step(`^my (?:application|workspace) contains the empty directory "([^"]*)"`, func(directory string) error {
dirPath := path.Join(appDir, directory)
isDir, err := util.DoesDirectoryExist(dirPath)
if err != nil {
return err
}
if !isDir {
return fmt.Errorf("%s is a not a directory", dirPath)
}
fileInfos, err := ioutil.ReadDir(dirPath)
if err != nil {
return err
}
if len(fileInfos) != 0 {
fileNames := []string{}
for _, fileInfo := range fileInfos {
fileNames = append(fileNames, fileInfo.Name())
}
return fmt.Errorf("%s is a not a an empty directory. Contains: %s", dirPath, strings.Join(fileNames, ", "))
}
return nil
})
// Running / Starting a command
s.Step(`^running "([^"]*)" in the terminal$`, func(command string) error {
var err error
childOutput, err = util.Run(appDir, command)
return err
})
s.Step(`^running "([^"]*)" in my application directory$`, func(command string) error {
var err error
childOutput, err = util.Run(appDir, command)
return err
})
s.Step(`^running "([^"]*)" in the "([^"]*)" directory$`, func(command, dirName string) error {
var err error
childOutput, err = util.Run(path.Join(appDir, dirName), command)
return err
})
s.Step(`^starting "([^"]*)" in the terminal$`, func(command string) error {
commandWords, err := util.ParseCommand(command)
if err != nil {
return err
}
childCmdPlus = execplus.NewCmdPlus(commandWords...)
childCmdPlus.SetDir(appDir)
return childCmdPlus.Start()
})
s.Step(`^starting "([^"]*)" in my application directory$`, func(command string) error {
commandWords, err := util.ParseCommand(command)
if err != nil {
return err
}
childCmdPlus = execplus.NewCmdPlus(commandWords...)
childCmdPlus.SetDir(appDir)
return childCmdPlus.Start()
})
s.Step(`^starting "([^"]*)" in the "([^"]*)" directory$`, func(command, dirName string) error {
commandWords, err := util.ParseCommand(command)
if err != nil {
return err
}
childCmdPlus = execplus.NewCmdPlus(commandWords...)
childCmdPlus.SetDir(path.Join(appDir, dirName))
return childCmdPlus.Start()
})
// Entering user input
s.Step(`^entering into the wizard:$`, func(table *gherkin.DataTable) error {
for _, row := range table.Rows[1:] {
if err := enterInput(row); err != nil {
return errors.Wrap(err, fmt.Sprintf("Failed to enter %s into the wizard", row.Cells[1].Value))
}
}
return nil
})
// Verifying output
s.Step(`^it prints "([^"]*)" in the terminal$`, func(text string) error {
if childCmdPlus != nil {
return childCmdPlus.WaitForText(text, time.Minute)
}
return validateTextContains(childOutput, text)
})
s.Step(`^it prints the current version in the terminal$`, func() error {
text := fmt.Sprintf("Exosphere v%s", src.Version)
if childCmdPlus != nil {
return childCmdPlus.WaitForText(text, time.Minute)
}
return validateTextContains(childOutput, text)
})
s.Step(`^it does not print "([^"]*)" in the terminal$`, func(text string) error {
if childCmdPlus != nil {
if err := validateTextContains(childCmdPlus.GetOutput(), text); err == nil {
return fmt.Errorf(validateTextDoesNotContainErrorTemplate, childCmdPlus.GetOutput(), text)
}
return nil
}
return validateTextContains(childOutput, text)
})
s.Step(`^I see:$`, func(expectedText *gherkin.DocString) error {
if childCmdPlus != nil {
return childCmdPlus.WaitForText(expectedText.Content, time.Second*2)
}
return validateTextContains(childOutput, expectedText.Content)
})
s.Step(`^I eventually see "([^"]*)" in the terminal$`, func(expectedText string) error {
return childCmdPlus.WaitForText(expectedText, time.Second)
})
s.Step(`^I eventually see:$`, func(expectedText *gherkin.DocString) error {
return childCmdPlus.WaitForText(expectedText.Content, time.Minute)
})
s.Step(`^(it exits|waiting until the process ends)$`, func() error {
return waitWithTimeout(childCmdPlus, time.Minute)
})
s.Step(`^I stop all running processes$`, func() error {
if childCmdPlus != nil {
err := childCmdPlus.Cmd.Process.Signal(os.Interrupt)
if err != nil {
return err
}
err = waitWithTimeout(childCmdPlus, time.Minute)
if err != nil {
fmt.Println("Command did not exit after 1m (TODO: fix this)")
return childCmdPlus.Kill()
}
}
return nil
})
s.Step(`^it exits with code (\d+)$`, func(expectedExitCode int) error {
actualExitCode := 0
if err := waitWithTimeout(childCmdPlus, time.Minute); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
actualExitCode = status.ExitStatus()
} else {
return fmt.Errorf("Unable to parse Status object: %v", err)
}
} else {
return fmt.Errorf("cmd.Wait: %v", err)
}
}
if actualExitCode != expectedExitCode {
return fmt.Errorf("Exited with code %d instead of %d. Output:\n%s", actualExitCode, expectedExitCode, childCmdPlus.GetOutput())
}
return nil
})
s.Step(`^my workspace contains the file "([^"]*)" with content:$`, func(fileName string, expectedContent *gherkin.DocString) error {
bytes, err := ioutil.ReadFile(path.Join(appDir, fileName))
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Failed to read %s", fileName))
}
return validateTextContains(strings.TrimSpace(string(bytes)), strings.TrimSpace(expectedContent.Content))
})
s.Step(`^my workspace contains the files:$`, func(table *gherkin.DataTable) error {
for _, row := range table.Rows[1:] {
filename := row.Cells[0].Value
hasFile, err := util.DoesFileExist(path.Join(appDir, filename))
if err != nil {
return err
}
if !hasFile {
return fmt.Errorf("Expected %s to exist", filename)
}
}
return nil
})
s.Step(`^my application now contains the file "([^"]*)" with the content:$`, func(fileName string, expectedContent *gherkin.DocString) error {
bytes, err := ioutil.ReadFile(path.Join(appDir, fileName))
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Failed to read %s", fileName))
}
return validateTextContains(strings.TrimSpace(string(bytes)), strings.TrimSpace(expectedContent.Content))
})
}