forked from containerd/containerd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_test.go
163 lines (142 loc) · 3.14 KB
/
client_test.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
package containerd
import (
"bytes"
"context"
"flag"
"fmt"
"os"
"os/exec"
"syscall"
"testing"
"time"
"github.com/containerd/containerd/namespaces"
)
const (
defaultRoot = "/var/lib/containerd-test"
defaultState = "/run/containerd-test"
testImage = "docker.io/library/alpine:latest"
)
var (
address string
noDaemon bool
supportsCriu bool
)
func init() {
flag.StringVar(&address, "address", "/run/containerd/containerd.sock", "The address to the containerd socket for use in the tests")
flag.BoolVar(&noDaemon, "no-daemon", false, "Do not start a dedicated daemon for the tests")
flag.Parse()
}
func testContext() (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(context.Background())
ctx = namespaces.WithNamespace(ctx, "testing")
return ctx, cancel
}
func TestMain(m *testing.M) {
if testing.Short() {
os.Exit(m.Run())
}
// check if criu is installed on the system
_, err := exec.LookPath("criu")
supportsCriu = err == nil
var (
cmd *exec.Cmd
buf = bytes.NewBuffer(nil)
ctx, cancel = testContext()
)
defer cancel()
if !noDaemon {
// setup a new containerd daemon if !testing.Short
cmd = exec.Command("containerd",
"--root", defaultRoot,
"--state", defaultState,
)
cmd.Stderr = buf
if err := cmd.Start(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
client, err := New(address)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := waitForDaemonStart(ctx, client); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// pull a seed image
if _, err = client.Pull(ctx, testImage, WithPullUnpack); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := client.Close(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
// run the test
status := m.Run()
if !noDaemon {
// tear down the daemon and resources created
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
fmt.Fprintln(os.Stderr, err)
}
if _, err := cmd.Process.Wait(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
if err := os.RemoveAll(defaultRoot); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// only print containerd logs if the test failed
if status != 0 {
fmt.Fprintln(os.Stderr, buf.String())
}
}
os.Exit(status)
}
func waitForDaemonStart(ctx context.Context, client *Client) error {
var (
serving bool
err error
)
for i := 0; i < 20; i++ {
serving, err = client.IsServing(ctx)
if serving {
return nil
}
time.Sleep(100 * time.Millisecond)
}
return fmt.Errorf("containerd did not start within 2s: %v", err)
}
func TestNewClient(t *testing.T) {
if testing.Short() {
t.Skip()
}
client, err := New(address)
if err != nil {
t.Fatal(err)
}
if client == nil {
t.Fatal("New() returned nil client")
}
if err := client.Close(); err != nil {
t.Errorf("client closed returned errror %v", err)
}
}
func TestImagePull(t *testing.T) {
if testing.Short() {
t.Skip()
}
ctx, cancel := testContext()
defer cancel()
client, err := New(address)
if err != nil {
t.Fatal(err)
}
defer client.Close()
_, err = client.Pull(ctx, testImage)
if err != nil {
t.Error(err)
return
}
}