Skip to content

Commit

Permalink
tests: frontend/dockerfile: update integration tests for windows/wcow
Browse files Browse the repository at this point in the history
This is a continuation for the integration tests work #4485
We had some tests that we marked as Revit during our first pass
through the tests.

This commit addresses the following tests in `frontedn/dockerfile`:

- [x] `testNamedOCILayoutContext`
- [x] `testNamedImageContext`

> WIP: more being added.

Signed-off-by: Anthony Nandaa <[email protected]>
  • Loading branch information
profnandaa committed Dec 17, 2024
1 parent 4ea0679 commit 9c07846
Showing 1 changed file with 85 additions and 17 deletions.
102 changes: 85 additions & 17 deletions frontend/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7120,19 +7120,28 @@ COPY --from=base --chmod=0644 /out /out
}

func testNamedImageContext(t *testing.T, sb integration.Sandbox) {
integration.SkipOnPlatform(t, "windows")
ctx := sb.Context()

c, err := client.New(ctx, sb.Address())
require.NoError(t, err)
defer c.Close()

dockerfile := []byte(`
dockerfile := []byte(integration.UnixOrWindows(
`
FROM busybox AS base
RUN cat /etc/alpine-release > /out
FROM scratch
COPY --from=base /out /
`)
`,
`
FROM nanoserver AS base
USER ContainerAdministrator
RUN type \License.txt > \out
FROM nanoserver
COPY --from=base /out /
`,
))

dir := integration.Tmpdir(
t,
Expand Down Expand Up @@ -7168,11 +7177,18 @@ COPY --from=base /out /
workers.CheckFeatureCompat(t, sb, workers.FeatureDirectPush)

// Now test with an image with custom envs
dockerfile = []byte(`
dockerfile = []byte(integration.UnixOrWindows(
`
FROM alpine:latest
ENV PATH=/foobar:$PATH
ENV FOOBAR=foobar
`)
`,
`
FROM nanoserver:latest
ENV PATH=/foobar:$PATH
ENV FOOBAR=foobar
`,
))

dir = integration.Tmpdir(
t,
Expand Down Expand Up @@ -7203,7 +7219,8 @@ ENV FOOBAR=foobar
}, nil)
require.NoError(t, err)

dockerfile = []byte(`
dockerfile = []byte(integration.UnixOrWindows(
`
FROM busybox AS base
RUN cat /etc/alpine-release > /out
RUN env | grep PATH > /env_path
Expand All @@ -7212,7 +7229,23 @@ FROM scratch
COPY --from=base /out /
COPY --from=base /env_path /
COPY --from=base /env_foobar /
`)
`,
// NOTE for windows:
// `set` does not work here, in place of Unix `env`
// since `set` lists environment variables from registry.
// It's `setx` counterpart. See #5445 for details.
`
FROM nanoserver AS base
USER ContainerAdministrator
RUN type \License.txt > \out
RUN echo %PATH% > \env_path
RUN echo %FOOBAR% > \env_foobar
FROM nanoserver
COPY --from=base /out /
COPY --from=base /env_path /
COPY --from=base /env_foobar /
`,
))

dir = integration.Tmpdir(
t,
Expand Down Expand Up @@ -7255,14 +7288,24 @@ COPY --from=base /env_foobar /
// this case checks replacing stage that is based on another stage.
// moby/buildkit#5578-2539397486

dockerfile = []byte(`
dockerfile = []byte(integration.UnixOrWindows(
`
FROM busybox AS parent
FROM parent AS base
RUN echo base > /out
FROM base
RUN [ -f /etc/alpine-release ]
RUN [ ! -f /out ]
`)
`,
`
FROM nanoserver AS parent
FROM parent AS base
RUN echo base > /out
FROM base
RUN type \License.txt
RUN if not exist \out (exit 0) else (exit 1)
`,
))

dir = integration.Tmpdir(
t,
Expand Down Expand Up @@ -7664,7 +7707,6 @@ COPY --from=base /another /out2
}

func testNamedOCILayoutContext(t *testing.T, sb integration.Sandbox) {
integration.SkipOnPlatform(t, "windows")
workers.CheckFeatureCompat(t, sb, workers.FeatureOCIExporter, workers.FeatureOCILayout)
// how this test works:
// 1- we use a regular builder with a dockerfile to create an image two files: "out" with content "first", "out2" with content "second"
Expand All @@ -7680,13 +7722,22 @@ func testNamedOCILayoutContext(t *testing.T, sb integration.Sandbox) {
// create a tempdir where we will store the OCI layout
ocidir := t.TempDir()

ociDockerfile := []byte(`
ociDockerfile := []byte(integration.UnixOrWindows(
`
FROM busybox:latest
WORKDIR /test
RUN sh -c "echo -n first > out"
RUN sh -c "echo -n second > out2"
ENV foo=bar
`)
`,
`
FROM nanoserver
WORKDIR /test
RUN echo first> out"
RUN echo second> out2"
ENV foo=bar
`,
))
inDir := integration.Tmpdir(
t,
fstest.CreateFile("Dockerfile", ociDockerfile, 0600),
Expand Down Expand Up @@ -7744,7 +7795,8 @@ func testNamedOCILayoutContext(t *testing.T, sb integration.Sandbox) {
// 2. we override the context for `foo` to be our local OCI store, which has an `ENV foo=bar` override.
// As such, the `RUN echo $foo` step should have `$foo` set to `"bar"`, and so
// when we `COPY --from=imported`, it should have the content of `/outfoo` as `"bar"`
dockerfile := []byte(`
dockerfile := []byte(integration.UnixOrWindows(
`
FROM busybox AS base
RUN cat /etc/alpine-release > out
Expand All @@ -7754,7 +7806,20 @@ RUN echo -n $foo > outfoo
FROM scratch
COPY --from=base /test/o* /
COPY --from=imported /test/outfoo /
`)
`,
`
FROM nanoserver AS base
USER ContainerAdministrator
RUN ver > out
FROM foo AS imported
RUN echo %foo%> outfoo
FROM nanoserver
COPY --from=base /test/o* /
COPY --from=imported /test/outfoo /
`,
))

dir := integration.Tmpdir(
t,
Expand Down Expand Up @@ -7784,20 +7849,23 @@ COPY --from=imported /test/outfoo /
}, nil)
require.NoError(t, err)

// echo for Windows adds a \n
newLine := integration.UnixOrWindows("", "\r\n")

dt, err := os.ReadFile(filepath.Join(destDir, "out"))
require.NoError(t, err)
require.Greater(t, len(dt), 0)
require.Equal(t, []byte("first"), dt)
require.Equal(t, []byte("first"+newLine), dt)

dt, err = os.ReadFile(filepath.Join(destDir, "out2"))
require.NoError(t, err)
require.Greater(t, len(dt), 0)
require.Equal(t, []byte("second"), dt)
require.Equal(t, []byte("second"+newLine), dt)

dt, err = os.ReadFile(filepath.Join(destDir, "outfoo"))
require.NoError(t, err)
require.Greater(t, len(dt), 0)
require.Equal(t, []byte("bar"), dt)
require.Equal(t, []byte("bar"+newLine), dt)
}

func testNamedOCILayoutContextExport(t *testing.T, sb integration.Sandbox) {
Expand Down

0 comments on commit 9c07846

Please sign in to comment.