-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker_init_test.go
More file actions
306 lines (264 loc) · 8.89 KB
/
docker_init_test.go
File metadata and controls
306 lines (264 loc) · 8.89 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
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
//go:build docker
package main_test
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// TestDockerInitWhisper verifies docker-init.sh launches a whisper container
// and exports expected environment variables when ENABLE_WHISPER=1.
func TestDockerInitWhisper(t *testing.T) {
tmp := t.TempDir()
dockerLog := filepath.Join(tmp, "docker.log")
envFile := filepath.Join(tmp, "env.txt")
// stub docker binary
dockerBin := filepath.Join(tmp, "docker")
if err := os.WriteFile(dockerBin, []byte("#!/bin/sh\necho \"$@\" >>$DOCKER_LOG\n[ \"$1\" = ps ] && exit 0\nexit 0\n"), 0755); err != nil {
t.Fatalf("write docker stub: %v", err)
}
// install subtitle-manager stub to /usr/local/bin
smBin := "/usr/local/bin/subtitle-manager"
if err := os.WriteFile(smBin, []byte("#!/bin/sh\nenv >$ENV_FILE\n"), 0755); err != nil {
t.Fatalf("install subtitle-manager stub: %v", err)
}
t.Cleanup(func() { os.Remove(smBin) })
env := append(os.Environ(),
"PATH="+tmp+":"+os.Getenv("PATH"),
"DOCKER_LOG="+dockerLog,
"ENV_FILE="+envFile,
"ENABLE_WHISPER=1",
"WHISPER_CONTAINER_NAME=test-whisper",
"WHISPER_PORT=12345",
"WHISPER_DEVICE=cpu",
"WHISPER_HEALTH_TIMEOUT=0", // Skip health check in tests
)
cmd := exec.Command("sh", "docker-init.sh")
cmd.Env = env
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("docker-init: %v\n%s", err, string(out))
}
data, err := os.ReadFile(envFile)
if err != nil {
t.Fatalf("read env file: %v", err)
}
envContent := string(data)
if !strings.Contains(envContent, "SM_PROVIDERS_WHISPER_API_URL=http://localhost:12345") {
t.Fatalf("missing whisper url: %s", envContent)
}
if !strings.Contains(envContent, "SM_OPENAI_API_URL=http://localhost:12345/v1") {
t.Fatalf("missing openai url: %s", envContent)
}
logData, err := os.ReadFile(dockerLog)
if err != nil {
t.Fatalf("read docker log: %v", err)
}
if !strings.Contains(string(logData), "run -d --name test-whisper") {
t.Fatalf("docker run not executed: %s", string(logData))
}
}
// TestDockerInitWhisperRetry verifies that docker-init.sh retries on failure
func TestDockerInitWhisperRetry(t *testing.T) {
tmp := t.TempDir()
dockerLog := filepath.Join(tmp, "docker.log")
envFile := filepath.Join(tmp, "env.txt")
// stub docker binary that fails twice then succeeds
dockerBin := filepath.Join(tmp, "docker")
dockerScript := `#!/bin/sh
echo "$@" >>$DOCKER_LOG
if [ "$1" = "ps" ]; then
exit 0
elif [ "$1" = "run" ]; then
# Count number of previous run attempts in the log
attempts=$(grep -c "^run " $DOCKER_LOG || echo 0)
# Attempt 1 and 2 fail, attempt 3 succeeds
if [ $attempts -le 2 ]; then
exit 1 # fail first two attempts
else
exit 0 # succeed on third attempt
fi
else
exit 0
fi`
if err := os.WriteFile(dockerBin, []byte(dockerScript), 0755); err != nil {
t.Fatalf("write docker stub: %v", err)
}
// install subtitle-manager stub
smBin := "/usr/local/bin/subtitle-manager"
if err := os.WriteFile(smBin, []byte("#!/bin/sh\nenv >$ENV_FILE\n"), 0755); err != nil {
t.Fatalf("install subtitle-manager stub: %v", err)
}
t.Cleanup(func() { os.Remove(smBin) })
env := append(os.Environ(),
"PATH="+tmp+":"+os.Getenv("PATH"),
"DOCKER_LOG="+dockerLog,
"ENV_FILE="+envFile,
"ENABLE_WHISPER=1",
"WHISPER_CONTAINER_NAME=test-whisper-retry",
"WHISPER_PORT=12346",
"WHISPER_DEVICE=cpu",
"WHISPER_HEALTH_TIMEOUT=0", // Skip health check in tests
)
cmd := exec.Command("sh", "docker-init.sh")
cmd.Env = env
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("docker-init: %v\n%s", err, string(out))
}
logData, err := os.ReadFile(dockerLog)
if err != nil {
t.Fatalf("read docker log: %v", err)
}
// Should have attempted 3 times
runCount := strings.Count(string(logData), "run -d --name test-whisper-retry")
if runCount != 3 {
t.Fatalf("expected 3 docker run attempts, got %d: %s", runCount, string(logData))
}
}
// TestDockerInitWhisperMaxRetries verifies that docker-init.sh exits after max retries
func TestDockerInitWhisperMaxRetries(t *testing.T) {
tmp := t.TempDir()
dockerLog := filepath.Join(tmp, "docker.log")
// stub docker binary that always fails
dockerBin := filepath.Join(tmp, "docker")
dockerScript := `#!/bin/sh
echo "$@" >>$DOCKER_LOG
if [ "$1" = "ps" ]; then
exit 0
elif [ "$1" = "run" ]; then
exit 1 # always fail
else
exit 0
fi`
if err := os.WriteFile(dockerBin, []byte(dockerScript), 0755); err != nil {
t.Fatalf("write docker stub: %v", err)
}
env := append(os.Environ(),
"PATH="+tmp+":"+os.Getenv("PATH"),
"DOCKER_LOG="+dockerLog,
"ENABLE_WHISPER=1",
"WHISPER_CONTAINER_NAME=test-whisper-fail",
"WHISPER_DEVICE=cpu",
"WHISPER_HEALTH_TIMEOUT=0", // Skip health check in tests
"WHISPER_MAX_RETRIES=2",
)
cmd := exec.Command("sh", "docker-init.sh")
cmd.Env = env
if err := cmd.Run(); err == nil {
t.Fatal("expected docker-init to fail after max retries")
}
logData, err := os.ReadFile(dockerLog)
if err != nil {
t.Fatalf("read docker log: %v", err)
}
// Should have attempted exactly 2 times
runCount := strings.Count(string(logData), "run -d --name test-whisper-fail")
if runCount != 2 {
t.Fatalf("expected exactly 2 docker run attempts, got %d: %s", runCount, string(logData))
}
}
// TestDockerInitWhisperContainerExists verifies handling of existing containers
func TestDockerInitWhisperContainerExists(t *testing.T) {
tmp := t.TempDir()
dockerLog := filepath.Join(tmp, "docker.log")
envFile := filepath.Join(tmp, "env.txt")
// stub docker binary that shows existing running container
dockerBin := filepath.Join(tmp, "docker")
dockerScript := `#!/bin/sh
echo "$@" >>$DOCKER_LOG
if [ "$1" = "ps" ] && [ "$3" = "{{.Names}}" ]; then
if echo "$@" | grep -q -- "--format"; then
echo "test-whisper-existing" # container is running
fi
exit 0
else
exit 0
fi`
if err := os.WriteFile(dockerBin, []byte(dockerScript), 0755); err != nil {
t.Fatalf("write docker stub: %v", err)
}
// install subtitle-manager stub
smBin := "/usr/local/bin/subtitle-manager"
if err := os.WriteFile(smBin, []byte("#!/bin/sh\nenv >$ENV_FILE\n"), 0755); err != nil {
t.Fatalf("install subtitle-manager stub: %v", err)
}
t.Cleanup(func() { os.Remove(smBin) })
env := append(os.Environ(),
"PATH="+tmp+":"+os.Getenv("PATH"),
"DOCKER_LOG="+dockerLog,
"ENV_FILE="+envFile,
"ENABLE_WHISPER=1",
"WHISPER_CONTAINER_NAME=test-whisper-existing",
"WHISPER_PORT=12347",
"WHISPER_DEVICE=cpu",
"WHISPER_HEALTH_TIMEOUT=0", // Skip health check in tests
)
cmd := exec.Command("sh", "docker-init.sh")
cmd.Env = env
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("docker-init: %v\n%s", err, string(out))
}
logData, err := os.ReadFile(dockerLog)
if err != nil {
t.Fatalf("read docker log: %v", err)
}
// Should not have attempted to run a new container
runCount := strings.Count(string(logData), "run -d")
if runCount > 0 {
t.Fatalf("should not start new container when one exists, but got %d run attempts: %s", runCount, string(logData))
}
// Should still set environment variables
data, err := os.ReadFile(envFile)
if err != nil {
t.Fatalf("read env file: %v", err)
}
envContent := string(data)
if !strings.Contains(envContent, "SM_PROVIDERS_WHISPER_API_URL=http://localhost:12347") {
t.Fatalf("missing whisper url: %s", envContent)
}
}
// TestDockerInitWhisperDisabled verifies behavior when ENABLE_WHISPER is not set
func TestDockerInitWhisperDisabled(t *testing.T) {
tmp := t.TempDir()
dockerLog := filepath.Join(tmp, "docker.log")
envFile := filepath.Join(tmp, "env.txt")
// stub docker binary
dockerBin := filepath.Join(tmp, "docker")
if err := os.WriteFile(dockerBin, []byte("#!/bin/sh\necho \"$@\" >>$DOCKER_LOG\nexit 0\n"), 0755); err != nil {
t.Fatalf("write docker stub: %v", err)
}
// install subtitle-manager stub
smBin := "/usr/local/bin/subtitle-manager"
if err := os.WriteFile(smBin, []byte("#!/bin/sh\nenv >$ENV_FILE\n"), 0755); err != nil {
t.Fatalf("install subtitle-manager stub: %v", err)
}
t.Cleanup(func() { os.Remove(smBin) })
env := append(os.Environ(),
"PATH="+tmp+":"+os.Getenv("PATH"),
"DOCKER_LOG="+dockerLog,
"ENV_FILE="+envFile,
// ENABLE_WHISPER is not set
"WHISPER_CONTAINER_NAME=test-whisper-disabled",
)
cmd := exec.Command("sh", "docker-init.sh")
cmd.Env = env
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("docker-init: %v\n%s", err, string(out))
}
// Docker log should be empty - no docker commands executed
if _, err := os.Stat(dockerLog); err == nil {
logData, _ := os.ReadFile(dockerLog)
if len(logData) > 0 {
t.Fatalf("docker commands executed when ENABLE_WHISPER not set: %s", string(logData))
}
}
// Environment should not have Whisper URLs
data, err := os.ReadFile(envFile)
if err != nil {
t.Fatalf("read env file: %v", err)
}
envContent := string(data)
if strings.Contains(envContent, "SM_PROVIDERS_WHISPER_API_URL") {
t.Fatalf("whisper url set when disabled: %s", envContent)
}
}