-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer_adbd_integration_test.go
More file actions
206 lines (178 loc) · 5.84 KB
/
Copy pathcontainer_adbd_integration_test.go
File metadata and controls
206 lines (178 loc) · 5.84 KB
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
package adb_test
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
adb "github.com/dector/adb-go"
)
const containerIntegrationEnv = "ADB_GO_CONTAINER_INTEGRATION"
func TestContainerADBDInstrumentedImplementedFunctionality(t *testing.T) {
if os.Getenv(containerIntegrationEnv) == "" {
t.Skipf("set %s=1 to run instrumented integration tests against containerized adbd", containerIntegrationEnv)
}
testTimeout := 2 * time.Minute
if os.Getenv("ADB_GO_CONTAINER_BUILD") != "" {
testTimeout = 30 * time.Minute
}
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
defer cancel()
runtime := containerRuntime(t)
addr := startContainerADBD(t, ctx, runtime)
client, err := adb.Connect(ctx, addr)
if err != nil {
t.Fatalf("Connect(%q) failed: %v", addr, err)
}
defer client.Close()
t.Run("shell", func(t *testing.T) {
out, err := client.Shell(ctx, "printf adb-go-container-shell-ok")
if err != nil {
t.Fatalf("Shell() failed: %v", err)
}
if got := strings.TrimSpace(string(out)); got != "adb-go-container-shell-ok" {
t.Fatalf("Shell() output = %q, want adb-go-container-shell-ok", got)
}
})
t.Run("shell stream", func(t *testing.T) {
var out bytes.Buffer
if err := client.ShellStream(ctx, "printf adb-go-container-stream-ok", &out); err != nil {
t.Fatalf("ShellStream() failed: %v", err)
}
if got := out.String(); got != "adb-go-container-stream-ok" {
t.Fatalf("ShellStream() output = %q, want adb-go-container-stream-ok", got)
}
})
t.Run("open service", func(t *testing.T) {
stream, err := client.OpenService(ctx, "shell:printf adb-go-container-service-ok")
if err != nil {
t.Fatalf("OpenService() failed: %v", err)
}
defer stream.Close()
var out bytes.Buffer
if _, err := out.ReadFrom(stream); err != nil {
t.Fatalf("ReadFrom(service stream) failed: %v", err)
}
if got := out.String(); got != "adb-go-container-service-ok" {
t.Fatalf("OpenService() output = %q, want adb-go-container-service-ok", got)
}
})
t.Run("push and pull", func(t *testing.T) {
remoteDir := fmt.Sprintf("/tmp/adb-go-container-%d", time.Now().UnixNano())
remotePath := remoteDir + "/roundtrip.txt"
defer client.Shell(context.Background(), "rm -rf "+remoteDir)
if _, err := client.Shell(ctx, "mkdir -p "+remoteDir); err != nil {
t.Fatalf("create remote temp dir failed: %v", err)
}
localDir := t.TempDir()
localPushPath := filepath.Join(localDir, "push.txt")
want := []byte("hello from adb-go containerized adbd\n")
if err := os.WriteFile(localPushPath, want, 0o666); err != nil {
t.Fatalf("WriteFile(push source) failed: %v", err)
}
if err := client.PushFile(ctx, localPushPath, remotePath); err != nil {
t.Fatalf("PushFile() failed: %v", err)
}
localPullPath := filepath.Join(localDir, "pull.txt")
if err := client.PullFile(ctx, remotePath, localPullPath); err != nil {
t.Fatalf("PullFile() failed: %v", err)
}
got, err := os.ReadFile(localPullPath)
if err != nil {
t.Fatalf("ReadFile(pulled file) failed: %v", err)
}
if !bytes.Equal(got, want) {
t.Fatalf("pulled file = %q, want %q", got, want)
}
})
}
func startContainerADBD(t *testing.T, ctx context.Context, runtime string) string {
t.Helper()
image := os.Getenv("ADB_GO_CONTAINER_IMAGE")
if image == "" {
image = "adb-go-linux-adbd"
}
if os.Getenv("ADB_GO_CONTAINER_BUILD") != "" {
runCommand(t, ctx, runtime, "build", "-f", "docker/linux-adbd/Containerfile", "-t", image, "docker/linux-adbd")
}
cid := strings.TrimSpace(runCommand(t, ctx, runtime, "run", "-d", "--rm", "-p", "127.0.0.1::5555", image))
if cid == "" {
t.Fatalf("%s run returned an empty container id", runtime)
}
t.Cleanup(func() {
cleanupCtx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
_ = exec.CommandContext(cleanupCtx, runtime, "rm", "-f", cid).Run()
})
addr := ""
deadline := time.Now().Add(30 * time.Second)
for time.Now().Before(deadline) {
out, err := commandOutput(ctx, runtime, "port", cid, "5555/tcp")
if err == nil {
addr = strings.TrimSpace(out)
if addr != "" {
break
}
}
time.Sleep(100 * time.Millisecond)
}
if addr == "" {
t.Fatalf("%s did not publish adbd port for container %s", runtime, cid)
}
if strings.Contains(addr, "\n") {
addr = strings.Split(addr, "\n")[0]
}
waitCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
var lastErr error
for waitCtx.Err() == nil {
client, err := adb.Connect(waitCtx, addr)
if err == nil {
_ = client.Close()
return addr
}
lastErr = err
time.Sleep(200 * time.Millisecond)
}
t.Fatalf("containerized adbd at %s did not become ready: %v", addr, lastErr)
return ""
}
func containerRuntime(t *testing.T) string {
t.Helper()
if runtime := os.Getenv("ADB_GO_CONTAINER_RUNTIME"); runtime != "" {
if _, err := exec.LookPath(runtime); err != nil {
t.Fatalf("containerized adbd integration requested, but %s was not found in PATH: %v", runtime, err)
}
return runtime
}
if _, err := exec.LookPath("podman"); err == nil {
return "podman"
}
if _, err := exec.LookPath("docker"); err == nil {
return "docker"
}
t.Fatal("containerized adbd integration requested, but neither podman nor docker was found in PATH")
return ""
}
func runCommand(t *testing.T, ctx context.Context, name string, args ...string) string {
t.Helper()
out, err := commandOutput(ctx, name, args...)
if err != nil {
t.Fatal(err)
}
return out
}
func commandOutput(ctx context.Context, name string, args ...string) (string, error) {
cmd := exec.CommandContext(ctx, name, args...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return stdout.String(), fmt.Errorf("%s %s failed: %w\nstdout:\n%s\nstderr:\n%s", name, strings.Join(args, " "), err, stdout.String(), stderr.String())
}
return stdout.String(), nil
}