Skip to content

Commit feaae22

Browse files
fix: detect silent wget failures in container DownloadFile
DownloadFile discarded wget's exit code, 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. Add ExecInspect to runCmdInContainer to capture the exit code. Check wget's exit code in DownloadFile and panic with stderr context on failure. Also adds mkdir -p for idempotent directory creation. Fixes #566
1 parent fb4625f commit feaae22

2 files changed

Lines changed: 33 additions & 14 deletions

File tree

internal/container/controller.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -250,23 +250,27 @@ 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+
if file == "" {
258+
panic("src URL has no filename: " + src)
259+
}
260+
dest := destFolder + "/" + file // not using filepath.Join here, this is in the *nix container. always /
261+
262+
cmd = []string{"wget", "-O", dest, src}
263+
_, stderr, exitCode := c.runCmdInContainer(id, cmd)
264+
if exitCode != 0 {
265+
msg := "download failed: " + src
266+
if len(stderr) > 0 {
267+
msg += "\nwget output: " + string(stderr)
268+
}
269+
panic(msg)
264270
}
265-
266-
c.runCmdInContainer(id, cmd)
267271
}
268272

269-
func (c Controller) runCmdInContainer(id string, cmd []string) ([]byte, []byte) {
273+
func (c Controller) runCmdInContainer(id string, cmd []string) ([]byte, []byte, int) {
270274
trace("Running command in container: " + strings.Join(cmd, " "))
271275

272276
response, err := c.cli.ExecCreate(
@@ -308,7 +312,14 @@ func (c Controller) runCmdInContainer(id string, cmd []string) ([]byte, []byte)
308312
trace("Stdout: " + string(stdout))
309313
trace("Stderr: " + string(stderr))
310314

311-
return stdout, stderr
315+
inspect, err := c.cli.ExecInspect(
316+
context.Background(),
317+
response.ID,
318+
client.ExecInspectOptions{},
319+
)
320+
checkErr(err)
321+
322+
return stdout, stderr, inspect.ExitCode
312323
}
313324

314325
// ContainerRunning returns true if the container with the given ID is running.

internal/container/controller_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ package container
55

66
import (
77
"fmt"
8-
"github.com/stretchr/testify/assert"
98
"net/http"
109
"net/http/httptest"
1110
"testing"
11+
12+
"github.com/stretchr/testify/assert"
1213
)
1314

1415
func TestController_ListTags(t *testing.T) {
@@ -54,7 +55,7 @@ func TestController_EnsureImage(t *testing.T) {
5455
}))
5556
defer ts.Close()
5657

57-
c.DownloadFile(id, ts.URL, "test.txt")
58+
c.DownloadFile(id, ts.URL+"/test.bak", "/tmp")
5859

5960
err = c.ContainerStop(id)
6061
checkErr(err)
@@ -187,3 +188,10 @@ func TestController_DownloadFileNeg3(t *testing.T) {
187188
c.DownloadFile("not_blank", "not_blank", "")
188189
})
189190
}
191+
192+
func TestController_DownloadFileNoFilename(t *testing.T) {
193+
c := NewController()
194+
assert.Panics(t, func() {
195+
c.DownloadFile("not_blank", "http://host:9999/", "/tmp")
196+
})
197+
}

0 commit comments

Comments
 (0)