Skip to content

Commit 8a5de5a

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 8a5de5a

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

internal/container/controller.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -250,23 +250,24 @@ 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, exitCode := c.runCmdInContainer(id, cmd)
261+
if exitCode != 0 {
262+
msg := "download failed: " + src
263+
if len(stderr) > 0 {
264+
msg += "\nwget output: " + string(stderr)
265+
}
266+
panic(msg)
264267
}
265-
266-
c.runCmdInContainer(id, cmd)
267268
}
268269

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

272273
response, err := c.cli.ExecCreate(
@@ -308,7 +309,14 @@ func (c Controller) runCmdInContainer(id string, cmd []string) ([]byte, []byte)
308309
trace("Stdout: " + string(stdout))
309310
trace("Stderr: " + string(stderr))
310311

311-
return stdout, stderr
312+
inspect, err := c.cli.ExecInspect(
313+
context.Background(),
314+
response.ID,
315+
client.ExecInspectOptions{},
316+
)
317+
checkErr(err)
318+
319+
return stdout, stderr, inspect.ExitCode
312320
}
313321

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

internal/container/controller_test.go

Lines changed: 3 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)

0 commit comments

Comments
 (0)