@ -4,6 +4,7 @@ import (
"context"
"os"
"os/exec"
"sync"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/util/testutil/integration"
@ -18,6 +19,11 @@ func InitDockerContainerWorker() {
type containerWorker struct {
id string
docker integration . Backend
dockerClose func ( ) error
dockerErr error
dockerOnce sync . Once
}
func ( w * containerWorker ) Name ( ) string {
@ -29,9 +35,11 @@ func (w *containerWorker) Rootless() bool {
}
func ( w * containerWorker ) New ( ctx context . Context , cfg * integration . BackendConfig ) ( integration . Backend , func ( ) error , error ) {
bk , bkclose , err := dockerWorker { id : w . id } . New ( ctx , cfg )
if err != nil {
return bk , bkclose , err
w . dockerOnce . Do ( func ( ) {
w . docker , w . dockerClose , w . dockerErr = dockerWorker { id : w . id } . New ( ctx , cfg )
} )
if w . dockerErr != nil {
return w . docker , w . dockerClose , w . dockerErr
}
name := "integration-container-" + identity . NewID ( )
@ -42,29 +50,32 @@ func (w *containerWorker) New(ctx context.Context, cfg *integration.BackendConfi
"--driver=docker-container" ,
"--driver-opt=network=host" ,
)
cmd . Env = append ( os . Environ ( ) , "DOCKER_CONTEXT=" + bk . DockerAddress ( ) )
cmd . Env = append ( os . Environ ( ) , "DOCKER_CONTEXT=" + w. docker . DockerAddress ( ) )
if err := cmd . Run ( ) ; err != nil {
return nil , nil , errors . Wrapf ( err , "failed to create buildx instance %s" , name )
}
cl := func ( ) error {
var err error
if err1 := bkclose ( ) ; err == nil {
err = err1
}
cmd := exec . Command ( "buildx" , "rm" , "-f" , name )
if err1 := cmd . Run ( ) ; err == nil {
err = err1
}
return err
return cmd . Run ( )
}
return & backend {
context : bk . DockerAddress ( ) ,
context : w. docker . DockerAddress ( ) ,
builder : name ,
} , cl , nil
}
func ( w * containerWorker ) Close ( ) error {
if close := w . dockerClose ; close != nil {
return close ( )
}
// reset the worker to be ready to go again
w . docker = nil
w . dockerClose = nil
w . dockerErr = nil
w . dockerOnce = sync . Once { }
return nil
}