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

View File

@ -5,6 +5,7 @@ import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"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)
if err != nil {
deferF.F()()
printLogs(logs, log.Println)
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
}
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 {
address string
logs map[string]*bytes.Buffer
@ -119,13 +131,7 @@ func (sb *sandbox) Address() string {
}
func (sb *sandbox) PrintLogs(t *testing.T) {
for name, l := range sb.logs {
t.Log(name)
s := bufio.NewScanner(l)
for s.Scan() {
t.Log(s.Text())
}
}
printLogs(sb.logs, t.Log)
}
func (sb *sandbox) NewRegistry() (string, error) {