Skip to content

Commit

Permalink
Updated tests in frontend/dockerfile/dockerfile_test.go to run on Win…
Browse files Browse the repository at this point in the history
…dows.

Partially addressing #4485

Signed-off-by: Billy Owire <billyowire95@gmail.com>
  • Loading branch information
tonistiigi authored and billywr committed Sep 23, 2024
2 parents 7e4a085 + 855e120 commit fc55fad
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 27 deletions.
9 changes: 8 additions & 1 deletion cache/contenthash/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"crypto/sha256"
"github.com/Microsoft/go-winio"
"io"
"os"
"path"
Expand Down Expand Up @@ -1034,7 +1035,7 @@ func (cc *cacheContext) scanPath(ctx context.Context, m *mount, p string, follow
scanPath = resolvedPath
}

err = filepath.Walk(scanPath, func(itemPath string, fi os.FileInfo, err error) error {
walkFn := func(itemPath string, fi os.FileInfo, err error) error {
if scanCounterEnable {
scanCounter.Add(1)
}
Expand Down Expand Up @@ -1073,7 +1074,13 @@ func (cc *cacheContext) scanPath(ctx context.Context, m *mount, p string, follow
txn.Insert(k, cr)
}
return nil
}

privileges := []string{winio.SeBackupPrivilege}
err = winio.RunWithPrivileges(privileges, func() error {
return filepath.Walk(scanPath, walkFn)
})

if err != nil {
return err
}
Expand Down
83 changes: 57 additions & 26 deletions frontend/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4341,13 +4341,20 @@ COPY foo bar
}

func testMultiStageImplicitFrom(t *testing.T, sb integration.Sandbox) {
integration.SkipOnPlatform(t, "windows")
f := getFrontend(t, sb)

dockerfile := []byte(`
dockerfile := []byte(integration.UnixOrWindows(
`
FROM scratch
COPY --from=busybox /etc/passwd test
`)
`, `
FROM nanoserver AS build
RUN echo test> test
FROM nanoserver
COPY --from=build /test /test
`,
))

dir := integration.Tmpdir(
t,
Expand Down Expand Up @@ -4376,17 +4383,24 @@ COPY --from=busybox /etc/passwd test

dt, err := os.ReadFile(filepath.Join(destDir, "test"))
require.NoError(t, err)
require.Contains(t, string(dt), "root")
require.Contains(t, string(dt), integration.UnixOrWindows("root", "test"))

// testing masked image will load actual stage

dockerfile = []byte(`
dockerfile = []byte(integration.UnixOrWindows(
`
FROM busybox AS golang
RUN mkdir -p /usr/bin && echo -n foo > /usr/bin/go
FROM scratch
COPY --from=golang /usr/bin/go go
`)
`, `
FROM nanoserver AS golang
RUN echo foo> go
FROM nanoserver
COPY --from=golang /go /go
`))

dir = integration.Tmpdir(
t,
Expand Down Expand Up @@ -4414,17 +4428,18 @@ COPY --from=golang /usr/bin/go go
}

func testMultiStageCaseInsensitive(t *testing.T, sb integration.Sandbox) {
integration.SkipOnPlatform(t, "windows")
f := getFrontend(t, sb)

dockerfile := []byte(`
FROM scratch AS STAge0
dockerfileStr := `
FROM %s AS STAge0
COPY foo bar
FROM scratch AS staGE1
FROM %s AS staGE1
COPY --from=staGE0 bar baz
FROM scratch
FROM %s
COPY --from=stage1 baz bax
`)
`
baseImage := integration.UnixOrWindows("scratch", "nanoserver")
dockerfile := []byte(fmt.Sprintf(dockerfileStr, baseImage, baseImage, baseImage))
dir := integration.Tmpdir(
t,
fstest.CreateFile("Dockerfile", dockerfile, 0600),
Expand Down Expand Up @@ -4572,7 +4587,6 @@ RUN ls /files/file1
}

func testOnBuildCleared(t *testing.T, sb integration.Sandbox) {
integration.SkipOnPlatform(t, "windows")
workers.CheckFeatureCompat(t, sb, workers.FeatureDirectPush)
f := getFrontend(t, sb)

Expand All @@ -4582,10 +4596,15 @@ func testOnBuildCleared(t *testing.T, sb integration.Sandbox) {
}
require.NoError(t, err)

dockerfile := []byte(`
dockerfile := []byte(integration.UnixOrWindows(
`
FROM busybox
ONBUILD RUN mkdir -p /out && echo -n 11 >> /out/foo
`)
`, `
FROM nanoserver
ONBUILD RUN mkdir /out && echo 11 >> /out/foo
`,
))

dir := integration.Tmpdir(
t,
Expand Down Expand Up @@ -5404,19 +5423,27 @@ COPY --from=build out .
}

func testBuiltinArgs(t *testing.T, sb integration.Sandbox) {
integration.SkipOnPlatform(t, "windows")
f := getFrontend(t, sb)

dockerfile := []byte(`
dockerfile := []byte(integration.UnixOrWindows(
`
FROM busybox AS build
ARG FOO
ARG BAR
ARG BAZ=bazcontent
RUN echo -n $HTTP_PROXY::$NO_PROXY::$FOO::$BAR::$BAZ > /out
FROM scratch
COPY --from=build /out /
`)
`, `
FROM nanoserver AS build
ARG FOO
ARG BAZ=bazcontent
RUN echo %HTTP_PROXY%::%NO_PROXY%::%FOO%::%BAR%::%BAZ%> out
FROM nanoserver
COPY --from=build out /
`,
))

dir := integration.Tmpdir(
t,
fstest.CreateFile("Dockerfile", dockerfile, 0600),
Expand Down Expand Up @@ -5451,7 +5478,9 @@ COPY --from=build /out /

dt, err := os.ReadFile(filepath.Join(destDir, "out"))
require.NoError(t, err)
require.Equal(t, "hpvalue::npvalue::foocontents::::bazcontent", string(dt))
// Windows can't interpret empty env variables, %BAR% handles empty values.
expectedStr := integration.UnixOrWindows(`hpvalue::npvalue::foocontents::::bazcontent`, "hpvalue::npvalue::foocontents::%BAR%::bazcontent\r\n")
require.Equal(t, expectedStr, string(dt))

// repeat with changed default args should match the old cache
destDir = t.TempDir()
Expand All @@ -5478,7 +5507,8 @@ COPY --from=build /out /

dt, err = os.ReadFile(filepath.Join(destDir, "out"))
require.NoError(t, err)
require.Equal(t, "hpvalue::npvalue::foocontents::::bazcontent", string(dt))
expectedStr = integration.UnixOrWindows("hpvalue::npvalue::foocontents::::b", "hpvalue::npvalue::foocontents::%BAR%::bazcontent\r\n")
require.Equal(t, expectedStr, string(dt))

// changing actual value invalidates cache
destDir = t.TempDir()
Expand All @@ -5505,7 +5535,8 @@ COPY --from=build /out /

dt, err = os.ReadFile(filepath.Join(destDir, "out"))
require.NoError(t, err)
require.Equal(t, "hpvalue2::::foocontents2::::bazcontent", string(dt))
expectedStr = integration.UnixOrWindows("hpvalue2::::foocontents2::::bazcon", "hpvalue2::%NO_PROXY%::foocontents2::%BAR%::bazcontent\r\n")
require.Equal(t, expectedStr, string(dt))
}

func testTarContext(t *testing.T, sb integration.Sandbox) {
Expand Down Expand Up @@ -5622,15 +5653,15 @@ COPY foo bar
}

func testFrontendUseForwardedSolveResults(t *testing.T, sb integration.Sandbox) {
integration.SkipOnPlatform(t, "windows")
c, err := client.New(sb.Context(), sb.Address())
require.NoError(t, err)
defer c.Close()

dockerfile := []byte(`
FROM scratch
dockerfileStr := `
FROM %s
COPY foo foo2
`)
`
dockerfile := []byte(fmt.Sprintf(dockerfileStr, integration.UnixOrWindows("scratch", "nanoserver")))
dir := integration.Tmpdir(
t,
fstest.CreateFile("Dockerfile", dockerfile, 0600),
Expand Down

0 comments on commit fc55fad

Please sign in to comment.