Merge pull request #401 from tonistiigi/relative-wd

llb: fix relative working directory in client
docker-18.09
Akihiro Suda 2018-05-22 16:04:24 +09:00 committed by GitHub
commit 06d81be140
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 1 deletions

View File

@ -33,6 +33,7 @@ import (
func TestClientIntegration(t *testing.T) {
integration.Run(t, []integration.Test{
testRelativeWorkDir,
testCallDiskUsage,
testBuildMultiMount,
testBuildHTTPSource,
@ -52,6 +53,37 @@ func TestClientIntegration(t *testing.T) {
})
}
func testRelativeWorkDir(t *testing.T, sb integration.Sandbox) {
t.Parallel()
requiresLinux(t)
c, err := New(sb.Address())
require.NoError(t, err)
defer c.Close()
pwd := llb.Image("docker.io/library/busybox:latest").
Dir("test1").
Dir("test2").
Run(llb.Shlex(`sh -c "pwd > /out/pwd"`)).
AddMount("/out", llb.Scratch())
def, err := pwd.Marshal()
require.NoError(t, err)
destDir, err := ioutil.TempDir("", "buildkit")
require.NoError(t, err)
defer os.RemoveAll(destDir)
_, err = c.Solve(context.TODO(), def, SolveOpt{
Exporter: ExporterLocal,
ExporterOutputDir: destDir,
}, nil)
require.NoError(t, err)
dt, err := ioutil.ReadFile(filepath.Join(destDir, "pwd"))
require.NoError(t, err)
require.Equal(t, []byte("/test1/test2\n"), dt)
}
func testCallDiskUsage(t *testing.T, sb integration.Sandbox) {
t.Parallel()
c, err := New(sb.Address())

View File

@ -2,6 +2,7 @@ package llb
import (
"fmt"
"path"
"github.com/google/shlex"
)
@ -31,7 +32,15 @@ func dir(str string) StateOption {
func dirf(str string, v ...interface{}) StateOption {
return func(s State) State {
return s.WithValue(keyDir, fmt.Sprintf(str, v...))
value := fmt.Sprintf(str, v...)
if !path.IsAbs(value) {
prev := getDir(s)
if prev == "" {
prev = "/"
}
value = path.Join(prev, value)
}
return s.WithValue(keyDir, value)
}
}

24
client/llb/meta_test.go Normal file
View File

@ -0,0 +1,24 @@
package llb
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestRelativeWd(t *testing.T) {
st := Scratch().Dir("foo")
require.Equal(t, st.GetDir(), "/foo")
st = st.Dir("bar")
require.Equal(t, st.GetDir(), "/foo/bar")
st = st.Dir("..")
require.Equal(t, st.GetDir(), "/foo")
st = st.Dir("/baz")
require.Equal(t, st.GetDir(), "/baz")
st = st.Dir("../../..")
require.Equal(t, st.GetDir(), "/")
}