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
|
// Copyright 2020 The gVisor Authors.
//
// 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 base holds utility methods common to the base tests.
package base
import (
"context"
"net"
"testing"
"time"
"gvisor.dev/gvisor/pkg/test/dockerutil"
"gvisor.dev/gvisor/test/benchmarks/harness"
)
// ServerArgs wraps args for startServers and runServerWorkload.
type ServerArgs struct {
Machine harness.Machine
Port int
RunOpts dockerutil.RunOpts
Cmd []string
}
// StartServers starts b.N containers defined by 'runOpts' and 'cmd' and uses
// 'machine' to check that each is up.
func StartServers(ctx context.Context, b *testing.B, args ServerArgs) []*dockerutil.Container {
b.Helper()
servers := make([]*dockerutil.Container, 0, b.N)
// Create N servers and wait until each of them is serving.
for i := 0; i < b.N; i++ {
server := args.Machine.GetContainer(ctx, b)
servers = append(servers, server)
if err := server.Spawn(ctx, args.RunOpts, args.Cmd...); err != nil {
CleanUpContainers(ctx, servers)
b.Fatalf("failed to spawn node instance: %v", err)
}
// Get the container IP.
servingIP, err := server.FindIP(ctx, false)
if err != nil {
CleanUpContainers(ctx, servers)
b.Fatalf("failed to get ip from server: %v", err)
}
// Wait until the server is up.
if err := harness.WaitUntilServing(ctx, args.Machine, servingIP, args.Port); err != nil {
CleanUpContainers(ctx, servers)
b.Fatalf("failed to wait for serving")
}
}
return servers
}
// CleanUpContainers cleans up a slice of containers.
func CleanUpContainers(ctx context.Context, containers []*dockerutil.Container) {
for _, c := range containers {
if c != nil {
c.CleanUp(ctx)
}
}
}
// RedisInstance returns a Redis container and its reachable IP.
func RedisInstance(ctx context.Context, b *testing.B, machine harness.Machine) (*dockerutil.Container, net.IP) {
b.Helper()
// Spawn a redis instance for the app to use.
redis := machine.GetNativeContainer(ctx, b)
if err := redis.Spawn(ctx, dockerutil.RunOpts{
Image: "benchmarks/redis",
}); err != nil {
redis.CleanUp(ctx)
b.Fatalf("failed to spwan redis instance: %v", err)
}
if out, err := redis.WaitForOutput(ctx, "Ready to accept connections", 3*time.Second); err != nil {
redis.CleanUp(ctx)
b.Fatalf("failed to start redis server: %v %s", err, out)
}
redisIP, err := redis.FindIP(ctx, false)
if err != nil {
redis.CleanUp(ctx)
b.Fatalf("failed to get IP from redis instance: %v", err)
}
return redis, redisIP
}
|