/
itutils.go
317 lines (296 loc) · 7.99 KB
/
itutils.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package utils
import (
"bytes"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"os"
"os/exec"
"strings"
"testing"
"text/template"
"time"
"github.com/ThomasRooney/gexpect"
)
// Deis points to the CLI used to run tests.
var Deis = "deis "
// DeisTestConfig allows tests to be repeated against different
// targets, with different example apps, using specific credentials, and so on.
type DeisTestConfig struct {
AuthKey string
Hosts string
Domain string
SSHKey string
ClusterName string
UserName string
Password string
Email string
ExampleApp string
AppName string
ProcessNum string
ImageID string
Version string
AppUser string
}
// randomApp is used for the test run if DEIS_TEST_APP isn't set
var randomApp = GetRandomApp()
// GetGlobalConfig returns a test configuration object.
func GetGlobalConfig() *DeisTestConfig {
authKey := os.Getenv("DEIS_TEST_AUTH_KEY")
if authKey == "" {
authKey = "deis"
}
hosts := os.Getenv("DEIS_TEST_HOSTS")
if hosts == "" {
hosts = "172.17.8.100"
}
domain := os.Getenv("DEIS_TEST_DOMAIN")
if domain == "" {
domain = "local.deisapp.com"
}
sshKey := os.Getenv("DEIS_TEST_SSH_KEY")
if sshKey == "" {
sshKey = "~/.vagrant.d/insecure_private_key"
}
exampleApp := os.Getenv("DEIS_TEST_APP")
if exampleApp == "" {
exampleApp = randomApp
}
var envCfg = DeisTestConfig{
AuthKey: authKey,
Hosts: hosts,
Domain: domain,
SSHKey: sshKey,
ClusterName: "dev",
UserName: "test",
Password: "asdf1234",
Email: "test@test.co.nz",
ExampleApp: exampleApp,
AppName: "sample",
ProcessNum: "2",
ImageID: "buildtest",
Version: "2",
AppUser: "test1",
}
return &envCfg
}
func doCurl(url string) ([]byte, error) {
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if !strings.Contains(string(body), "Powered by") {
return nil, fmt.Errorf("App not started (%d)\nBody: (%s)", response.StatusCode, string(body))
}
return body, nil
}
// Curl connects to a Deis endpoint to see if the example app is running.
func Curl(t *testing.T, params *DeisTestConfig) {
CurlWithFail(t, params, false, "")
}
// CurlWithFail connects to a Deis endpoint to see if the example app is running.
func CurlWithFail(t *testing.T, params *DeisTestConfig, failFlag bool, expect string) {
url := "http://" + params.AppName + "." + params.Domain
// FIXME: try the curl a few times
for i := 0; i < 20; i++ {
body, err := doCurl(url)
if err == nil {
fmt.Println(string(body))
return
}
time.Sleep(1 * time.Second)
}
// once more to fail with an error
body, err := doCurl(url)
switch failFlag {
case true:
if err != nil {
if strings.Contains(string(err.Error()), expect) {
fmt.Println("(Error expected...ok) " + expect)
} else {
t.Fatal(err)
}
} else {
if strings.Contains(string(body), expect) {
fmt.Println("(Error expected...ok) " + expect)
} else {
t.Fatal(err)
}
}
case false:
if err != nil {
t.Fatal(err)
} else {
fmt.Println(string(body))
}
}
}
// AuthCancel tests whether `deis auth:cancel` destroys a user's account.
func AuthCancel(t *testing.T, params *DeisTestConfig) {
fmt.Println("deis auth:cancel")
child, err := gexpect.Spawn(Deis + " auth:cancel")
if err != nil {
t.Fatalf("command not started\n%v", err)
}
fmt.Println("username:")
err = child.Expect("username:")
if err != nil {
t.Fatalf("expect username failed\n%v", err)
}
child.SendLine(params.UserName)
fmt.Print("password:")
err = child.Expect("password:")
if err != nil {
t.Fatalf("expect password failed\n%v", err)
}
child.SendLine(params.Password)
err = child.ExpectRegex("(y/n)")
if err != nil {
t.Fatalf("expect cancel \n%v", err)
}
child.SendLine("y")
err = child.Expect("Account cancelled")
if err != nil {
t.Fatalf("command executiuon failed\n%v", err)
}
child.Close()
}
// AuthPasswd tests whether `deis auth:passwd` updates a user's password.
func AuthPasswd(t *testing.T, params *DeisTestConfig, password string) {
fmt.Println("deis auth:passwd")
child, err := gexpect.Spawn(Deis + " auth:passwd")
if err != nil {
t.Fatalf("command not started\n%v", err)
}
fmt.Println("current password:")
err = child.Expect("current password: ")
if err != nil {
t.Fatalf("expect password failed\n%v", err)
}
child.SendLine(params.Password)
fmt.Println("new password:")
err = child.Expect("new password: ")
if err != nil {
t.Fatalf("expect password failed\n%v", err)
}
child.SendLine(password)
fmt.Println("new password (confirm):")
err = child.Expect("new password (confirm): ")
if err != nil {
t.Fatalf("expect password failed\n%v", err)
}
child.SendLine(password)
err = child.Expect("Password change succeeded")
if err != nil {
t.Fatalf("command executiuon failed\n%v", err)
}
child.Close()
}
// CheckList executes a command and optionally tests whether its output does
// or does not contain a given string.
func CheckList(
t *testing.T, cmd string, params interface{}, contain string, notflag bool) {
var cmdBuf bytes.Buffer
tmpl := template.Must(template.New("cmd").Parse(cmd))
if err := tmpl.Execute(&cmdBuf, params); err != nil {
t.Fatal(err)
}
cmdString := cmdBuf.String()
fmt.Println(cmdString)
var cmdl *exec.Cmd
if strings.Contains(cmd, "cat") {
cmdl = exec.Command("sh", "-c", cmdString)
} else {
cmdl = exec.Command("sh", "-c", Deis+cmdString)
}
stdout, _, err := RunCommandWithStdoutStderr(cmdl)
if err != nil {
t.Fatal(err)
}
if strings.Contains(stdout.String(), contain) == notflag {
if notflag {
t.Fatalf(
"Didn't expect '%s' in command output:\n%v", contain, stdout)
}
t.Fatalf("Expected '%s' in command output:\n%v", contain, stdout)
}
}
// Execute takes command string and parameters required to execute the command,
// a failflag to check whether the command is expected to fail, and an expect
// string to check whether the command has failed according to failflag.
//
// If failflag is true and the command failed, check the stdout and stderr for
// the expect string.
func Execute(t *testing.T, cmd string, params interface{}, failFlag bool, expect string) {
var cmdBuf bytes.Buffer
tmpl := template.Must(template.New("cmd").Parse(cmd))
if err := tmpl.Execute(&cmdBuf, params); err != nil {
t.Fatal(err)
}
cmdString := cmdBuf.String()
fmt.Println(cmdString)
var cmdl *exec.Cmd
if strings.Contains(cmd, "git") {
cmdl = exec.Command("sh", "-c", cmdString)
} else {
cmdl = exec.Command("sh", "-c", Deis+cmdString)
}
switch failFlag {
case true:
if stdout, stderr, err := RunCommandWithStdoutStderr(cmdl); err != nil {
if strings.Contains(stdout.String(), expect) || strings.Contains(stderr.String(), expect) {
fmt.Println("(Error expected...ok)")
} else {
t.Fatal(err)
}
} else {
if strings.Contains(stdout.String(), expect) || strings.Contains(stderr.String(), expect) {
fmt.Println("(Error expected...ok)" + expect)
} else {
t.Fatal(err)
}
}
case false:
if _, _, err := RunCommandWithStdoutStderr(cmdl); err != nil {
t.Fatal(err)
} else {
fmt.Println("ok")
}
}
}
// AppsDestroyTest destroys a Deis app and checks that it was successful.
func AppsDestroyTest(t *testing.T, params *DeisTestConfig) {
cmd := "apps:destroy --app={{.AppName}} --confirm={{.AppName}}"
if err := Chdir(params.ExampleApp); err != nil {
t.Fatal(err)
}
Execute(t, cmd, params, false, "")
if err := Chdir(".."); err != nil {
t.Fatal(err)
}
if err := Rmdir(params.ExampleApp); err != nil {
t.Fatal(err)
}
}
// GetRandomApp returns a known working example app at random for testing.
func GetRandomApp() string {
rand.Seed(int64(time.Now().Unix()))
apps := []string{
"example-clojure-ring",
// "example-dart",
"example-dockerfile-python",
"example-go",
"example-java-jetty",
"example-nodejs-express",
// "example-php",
"example-play",
"example-python-django",
"example-python-flask",
"example-ruby-sinatra",
"example-scala",
}
return apps[rand.Intn(len(apps))]
}