testutil: add logs printing on error

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
docker-19.03
Tonis Tiigi 2019-07-12 10:22:25 -07:00
parent 653c91e114
commit e38512c056
2 changed files with 15 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -154,6 +155,7 @@ disabled_plugins = ["cri"]
buildkitdSock, stop, err := runBuildkitd(buildkitdArgs, logs, 0, 0) buildkitdSock, stop, err := runBuildkitd(buildkitdArgs, logs, 0, 0)
if err != nil { if err != nil {
printLogs(logs, log.Println)
return nil, nil, err return nil, nil, err
} }
deferF.append(stop) deferF.append(stop)

View File

@ -5,6 +5,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -98,6 +99,7 @@ func (s *oci) New(opt ...SandboxOpt) (Sandbox, func() error, error) {
buildkitdSock, stop, err := runBuildkitd(buildkitdArgs, logs, s.uid, s.gid) buildkitdSock, stop, err := runBuildkitd(buildkitdArgs, logs, s.uid, s.gid)
if err != nil { if err != nil {
deferF.F()() deferF.F()()
printLogs(logs, log.Println)
return nil, nil, err return nil, nil, err
} }
@ -106,6 +108,16 @@ func (s *oci) New(opt ...SandboxOpt) (Sandbox, func() error, error) {
return &sandbox{address: buildkitdSock, mv: c.mv, logs: logs, cleanup: deferF, rootless: s.uid != 0}, deferF.F(), nil return &sandbox{address: buildkitdSock, mv: c.mv, logs: logs, cleanup: deferF, rootless: s.uid != 0}, deferF.F(), nil
} }
func printLogs(logs map[string]*bytes.Buffer, f func(args ...interface{})) {
for name, l := range logs {
f(name)
s := bufio.NewScanner(l)
for s.Scan() {
f(s.Text())
}
}
}
type sandbox struct { type sandbox struct {
address string address string
logs map[string]*bytes.Buffer logs map[string]*bytes.Buffer
@ -119,13 +131,7 @@ func (sb *sandbox) Address() string {
} }
func (sb *sandbox) PrintLogs(t *testing.T) { func (sb *sandbox) PrintLogs(t *testing.T) {
for name, l := range sb.logs { printLogs(sb.logs, t.Log)
t.Log(name)
s := bufio.NewScanner(l)
for s.Scan() {
t.Log(s.Text())
}
}
} }
func (sb *sandbox) NewRegistry() (string, error) { func (sb *sandbox) NewRegistry() (string, error) {