-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
framework.go
377 lines (325 loc) · 10.3 KB
/
framework.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package framework
import (
"bytes"
"flag"
"fmt"
"os/exec"
"strings"
"testing"
"time"
"github.com/golang/glog"
"github.com/google/cadvisor/client"
"github.com/google/cadvisor/client/v2"
)
var host = flag.String("host", "localhost", "Address of the host being tested")
var port = flag.Int("port", 8080, "Port of the application on the host being tested")
var sshOptions = flag.String("ssh-options", "", "Command line options for ssh")
// Integration test framework.
type Framework interface {
// Clean the framework state.
Cleanup()
// The testing.T used by the framework and the current test.
T() *testing.T
// Returns the hostname being tested.
Hostname() HostnameInfo
// Returns the Docker actions for the test framework.
Docker() DockerActions
// Returns the shell actions for the test framework.
Shell() ShellActions
// Returns the cAdvisor actions for the test framework.
Cadvisor() CadvisorActions
}
// Instantiates a Framework. Cleanup *must* be called. Class is thread-compatible.
// All framework actions report fatal errors on the t specified at creation time.
//
// Typical use:
//
// func TestFoo(t *testing.T) {
// fm := framework.New(t)
// defer fm.Cleanup()
// ... actual test ...
// }
func New(t *testing.T) Framework {
// All integration tests are large.
if testing.Short() {
t.Skip("Skipping framework test in short mode")
}
// Try to see if non-localhost hosts are GCE instances.
fm := &realFramework{
hostname: HostnameInfo{
Host: *host,
Port: *port,
},
t: t,
cleanups: make([]func(), 0),
}
fm.shellActions = shellActions{
fm: fm,
}
fm.dockerActions = dockerActions{
fm: fm,
}
return fm
}
const (
Aufs string = "aufs"
Overlay string = "overlay"
DeviceMapper string = "devicemapper"
Unknown string = ""
)
type DockerActions interface {
// Run the no-op pause Docker container and return its ID.
RunPause() string
// Run the specified command in a Docker busybox container and return its ID.
RunBusybox(cmd ...string) string
// Runs a Docker container in the background. Uses the specified DockerRunArgs and command.
// Returns the ID of the new container.
//
// e.g.:
// Run(DockerRunArgs{Image: "busybox"}, "ping", "www.google.com")
// -> docker run busybox ping www.google.com
Run(args DockerRunArgs, cmd ...string) string
RunStress(args DockerRunArgs, cmd ...string) string
Version() []string
StorageDriver() string
}
type ShellActions interface {
// Runs a specified command and arguments. Returns the stdout and stderr.
Run(cmd string, args ...string) (string, string)
RunStress(cmd string, args ...string) (string, string)
}
type CadvisorActions interface {
// Returns a cAdvisor client to the machine being tested.
Client() *client.Client
ClientV2() *v2.Client
}
type realFramework struct {
hostname HostnameInfo
t *testing.T
cadvisorClient *client.Client
cadvisorClientV2 *v2.Client
shellActions shellActions
dockerActions dockerActions
// Cleanup functions to call on Cleanup()
cleanups []func()
}
type shellActions struct {
fm *realFramework
}
type dockerActions struct {
fm *realFramework
}
type HostnameInfo struct {
Host string
Port int
}
// Returns: http://<host>:<port>/
func (self HostnameInfo) FullHostname() string {
return fmt.Sprintf("http://%s:%d/", self.Host, self.Port)
}
func (self *realFramework) T() *testing.T {
return self.t
}
func (self *realFramework) Hostname() HostnameInfo {
return self.hostname
}
func (self *realFramework) Shell() ShellActions {
return self.shellActions
}
func (self *realFramework) Docker() DockerActions {
return self.dockerActions
}
func (self *realFramework) Cadvisor() CadvisorActions {
return self
}
// Call all cleanup functions.
func (self *realFramework) Cleanup() {
for _, cleanupFunc := range self.cleanups {
cleanupFunc()
}
}
// Gets a client to the cAdvisor being tested.
func (self *realFramework) Client() *client.Client {
if self.cadvisorClient == nil {
cadvisorClient, err := client.NewClient(self.Hostname().FullHostname())
if err != nil {
self.t.Fatalf("Failed to instantiate the cAdvisor client: %v", err)
}
self.cadvisorClient = cadvisorClient
}
return self.cadvisorClient
}
// Gets a v2 client to the cAdvisor being tested.
func (self *realFramework) ClientV2() *v2.Client {
if self.cadvisorClientV2 == nil {
cadvisorClientV2, err := v2.NewClient(self.Hostname().FullHostname())
if err != nil {
self.t.Fatalf("Failed to instantiate the cAdvisor client: %v", err)
}
self.cadvisorClientV2 = cadvisorClientV2
}
return self.cadvisorClientV2
}
func (self dockerActions) RunPause() string {
return self.Run(DockerRunArgs{
Image: "kubernetes/pause",
})
}
// Run the specified command in a Docker busybox container.
func (self dockerActions) RunBusybox(cmd ...string) string {
return self.Run(DockerRunArgs{
Image: "busybox",
}, cmd...)
}
type DockerRunArgs struct {
// Image to use.
Image string
// Arguments to the Docker CLI.
Args []string
InnerArgs []string
}
// TODO(vmarmol): Use the Docker remote API.
// TODO(vmarmol): Refactor a set of "RunCommand" actions.
// Runs a Docker container in the background. Uses the specified DockerRunArgs and command.
//
// e.g.:
// RunDockerContainer(DockerRunArgs{Image: "busybox"}, "ping", "www.google.com")
// -> docker run busybox ping www.google.com
func (self dockerActions) Run(args DockerRunArgs, cmd ...string) string {
dockerCommand := append(append([]string{"docker", "run", "-d"}, args.Args...), args.Image)
dockerCommand = append(dockerCommand, cmd...)
output, _ := self.fm.Shell().Run("sudo", dockerCommand...)
// The last line is the container ID.
elements := strings.Fields(output)
containerId := elements[len(elements)-1]
self.fm.cleanups = append(self.fm.cleanups, func() {
self.fm.Shell().Run("sudo", "docker", "rm", "-f", containerId)
})
return containerId
}
func (self dockerActions) Version() []string {
dockerCommand := []string{"docker", "version", "-f", "'{{.Server.Version}}'"}
output, _ := self.fm.Shell().Run("sudo", dockerCommand...)
output = strings.TrimSpace(output)
ret := strings.Split(output, ".")
if len(ret) != 3 {
self.fm.T().Fatalf("invalid version %v", output)
}
return ret
}
func (self dockerActions) StorageDriver() string {
dockerCommand := []string{"docker", "info"}
output, _ := self.fm.Shell().Run("sudo", dockerCommand...)
if len(output) < 1 {
self.fm.T().Fatalf("failed to find docker storage driver - %v", output)
}
for _, line := range strings.Split(output, "\n") {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "Storage Driver: ") {
idx := strings.LastIndex(line, ": ") + 2
driver := line[idx:]
switch driver {
case Aufs:
return Aufs
case Overlay:
return Overlay
case DeviceMapper:
return DeviceMapper
default:
return Unknown
}
}
}
self.fm.T().Fatalf("failed to find docker storage driver from info - %v", output)
return Unknown
}
func (self dockerActions) RunStress(args DockerRunArgs, cmd ...string) string {
dockerCommand := append(append(append(append([]string{"docker", "run", "-m=4M", "-d", "-t", "-i"}, args.Args...), args.Image), args.InnerArgs...), cmd...)
output, _ := self.fm.Shell().RunStress("sudo", dockerCommand...)
// The last line is the container ID.
if len(output) < 1 {
self.fm.T().Fatalf("need 1 arguments in output %v to get the name but have %v", output, len(output))
}
elements := strings.Fields(output)
containerId := elements[len(elements)-1]
self.fm.cleanups = append(self.fm.cleanups, func() {
self.fm.Shell().Run("sudo", "docker", "rm", "-f", containerId)
})
return containerId
}
func (self shellActions) wrapSsh(command string, args ...string) *exec.Cmd {
cmd := []string{self.fm.Hostname().Host, "--", "sh", "-c", "\"", command}
cmd = append(cmd, args...)
cmd = append(cmd, "\"")
if *sshOptions != "" {
cmd = append(strings.Split(*sshOptions, " "), cmd...)
}
return exec.Command("ssh", cmd...)
}
func (self shellActions) Run(command string, args ...string) (string, string) {
var cmd *exec.Cmd
if self.fm.Hostname().Host == "localhost" {
// Just run locally.
cmd = exec.Command(command, args...)
} else {
// We must SSH to the remote machine and run the command.
cmd = self.wrapSsh(command, args...)
}
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
glog.Infof("About to run - %v", cmd.Args)
err := cmd.Run()
if err != nil {
self.fm.T().Fatalf("Failed to run %q %v in %q with error: %q. Stdout: %q, Stderr: %s", command, args, self.fm.Hostname().Host, err, stdout.String(), stderr.String())
return "", ""
}
return stdout.String(), stderr.String()
}
func (self shellActions) RunStress(command string, args ...string) (string, string) {
var cmd *exec.Cmd
if self.fm.Hostname().Host == "localhost" {
// Just run locally.
cmd = exec.Command(command, args...)
} else {
// We must SSH to the remote machine and run the command.
cmd = self.wrapSsh(command, args...)
}
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
self.fm.T().Logf("Ran %q %v in %q and received error: %q. Stdout: %q, Stderr: %s", command, args, self.fm.Hostname().Host, err, stdout.String(), stderr.String())
return stdout.String(), stderr.String()
}
return stdout.String(), stderr.String()
}
// Runs retryFunc until no error is returned. After dur time the last error is returned.
// Note that the function does not timeout the execution of retryFunc when the limit is reached.
func RetryForDuration(retryFunc func() error, dur time.Duration) error {
waitUntil := time.Now().Add(dur)
var err error
for time.Now().Before(waitUntil) {
err = retryFunc()
if err == nil {
return nil
}
}
return err
}