-
Notifications
You must be signed in to change notification settings - Fork 21
/
ping_test.go
177 lines (148 loc) · 3.72 KB
/
ping_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"bytes"
"context"
"errors"
"fmt"
"net"
"strings"
"testing"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
)
var (
waitTime = 2 * time.Second
errInvalidIP = errors.New("invalid IP address")
)
func waitForIP(ctx context.Context, docker *client.Client, containerID string) (net.IP, error) {
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
ip, err := getIP(ctx, docker, containerID)
if err == errInvalidIP {
time.Sleep(waitTime)
} else if err == nil {
return ip, nil
} else {
return nil, err
}
}
}
func getIP(ctx context.Context, docker *client.Client, containerID string) (net.IP, error) {
res, err := execContainer(ctx, docker, containerID, "zerotier-cli", "listnetworks")
if err != nil {
return nil, err
}
lines := strings.Split(strings.TrimSpace(string(res)), "\n")
parts := strings.Split(lines[len(lines)-1], " ")
if len(lines) < 2 {
return nil, errInvalidIP // force a retry, we haven't joined the network yet
}
if len(parts) != 9 {
return nil, fmt.Errorf("invalid data from listnetworks: line was %d parts: %q", len(parts), strings.Join(parts, " "))
}
ips := strings.Split(parts[8], ",")
ip, _, err := net.ParseCIDR(ips[len(ips)-1])
if ip == nil || err != nil {
return nil, errInvalidIP
}
return ip, nil
}
func execContainer(ctx context.Context, docker *client.Client, containerID string, command ...string) ([]byte, error) {
exec, err := docker.ContainerExecCreate(
ctx,
containerID,
types.ExecConfig{
Cmd: command,
AttachStdout: true,
AttachStderr: true,
},
)
if err != nil {
return nil, err
}
r, err := docker.ContainerExecAttach(ctx, exec.ID, types.ExecStartCheck{})
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(nil)
errChan := make(chan error, 1)
go func() {
if _, err := stdcopy.StdCopy(buf, buf, r.Reader); err != nil {
errChan <- err
return
}
errChan <- nil
}()
if err := docker.ContainerExecStart(ctx, exec.ID, types.ExecStartCheck{}); err != nil {
return nil, err
}
if err := <-errChan; err != nil {
return nil, err
}
res, err := docker.ContainerExecInspect(ctx, exec.ID)
if err != nil {
return nil, err
}
if res.ExitCode != 0 {
return nil, errors.New("invalid exit code")
}
return buf.Bytes(), err
}
func TestDockerPing(t *testing.T) {
tf := getTFTest(t)
tf.Apply("testdata/plans/docker-integration.tf")
var aliceCID, bobCID string
// this extracts the container ids from the state so we can mess with them.
resources := a(tf.State()["resources"])
for _, resource := range resources {
m := h(resource)
if s(m["type"]) == "docker_container" {
name := s(m["name"])
id := s(h(h(a(m["instances"])[0])["attributes"])["id"])
switch name {
case "alice":
aliceCID = id
case "bob":
bobCID = id
default:
t.Fatalf("invalid container name specified: %q", name)
}
}
}
docker, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
t.Cleanup(cancel)
aliceIP, err := waitForIP(ctx, docker, aliceCID)
if err != nil {
t.Fatal(err)
}
bobIP, err := waitForIP(ctx, docker, bobCID)
if err != nil {
t.Fatal(err)
}
for {
time.Sleep(2 * waitTime)
// this basically waits for both pings to succeed bofore the context above times out.
select {
case <-ctx.Done():
t.Fatal(ctx.Err())
default:
}
if _, err := execContainer(ctx, docker, aliceCID, "/bin/ping", "-c", "1", bobIP.String()); err != nil {
continue
}
if _, err := execContainer(ctx, docker, bobCID, "/bin/ping", "-c", "1", aliceIP.String()); err != nil {
continue
}
break
}
}