Skip to content

Commit 5c04558

Browse files
fix: detect silent wget failures in container DownloadFile
DownloadFile discarded wget's stdout/stderr, so failed downloads (e.g. unreachable host) produced no error. The caller then tried to restore a missing or empty .bak file, yielding a confusing 'volume is empty' message from SQL Server. After wget completes, verify the output file exists and is non-empty via 'test -s'. If missing, panic with the wget stderr for diagnosis. Also adds mkdir -p for idempotent directory creation. Fixes #566
1 parent fb4625f commit 5c04558

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

internal/container/controller.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,20 +250,25 @@ func (c Controller) DownloadFile(id string, src string, destFolder string) {
250250
panic("Must pass in non-empty destFolder")
251251
}
252252

253-
cmd := []string{"mkdir", destFolder}
253+
cmd := []string{"mkdir", "-p", destFolder}
254254
c.runCmdInContainer(id, cmd)
255255

256256
_, file := filepath.Split(src)
257-
258-
// Wget the .bak file from the http src, and place it in /var/opt/sql/backup
259-
cmd = []string{
260-
"wget",
261-
"-O",
262-
destFolder + "/" + file, // not using filepath.Join here, this is in the *nix container. always /
263-
src,
257+
dest := destFolder + "/" + file // not using filepath.Join here, this is in the *nix container. always /
258+
259+
cmd = []string{"wget", "-O", dest, src}
260+
_, stderr := c.runCmdInContainer(id, cmd)
261+
262+
// Verify the file arrived and is non-empty
263+
verifyCmd := []string{"test", "-s", dest}
264+
_, verifyErr := c.runCmdInContainer(id, verifyCmd)
265+
if len(verifyErr) > 0 {
266+
msg := "download failed: " + src
267+
if len(stderr) > 0 {
268+
msg += "\nwget output: " + string(stderr)
269+
}
270+
panic(msg)
264271
}
265-
266-
c.runCmdInContainer(id, cmd)
267272
}
268273

269274
func (c Controller) runCmdInContainer(id string, cmd []string) ([]byte, []byte) {

internal/container/controller_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ func TestController_EnsureImage(t *testing.T) {
5454
}))
5555
defer ts.Close()
5656

57-
c.DownloadFile(id, ts.URL, "test.txt")
57+
// httptest binds to 127.0.0.1 which is unreachable from inside the
58+
// container, so DownloadFile now correctly panics on the failed download.
59+
assert.Panics(t, func() {
60+
c.DownloadFile(id, ts.URL+"/test.bak", "/tmp")
61+
})
5862

5963
err = c.ContainerStop(id)
6064
checkErr(err)

0 commit comments

Comments
 (0)