Skip to content

Commit

Permalink
wip: enabling integration tests on windows
Browse files Browse the repository at this point in the history
Depends on moby#4994
Partial fix for moby#4485

Starting off with this one as a POC on the approach, before we
proceed to complete the rest under `/frontend/dockerfile`.

Tests covered so far:

- [x] `/frontend/dockerfile: testEnvEmptyFormatting`
- [x] `/frontend/dockerfile: testDockerignoreOverride`
- [x] `/frontend/dockerfile: caseEmptyDestDir`

Signed-off-by: Anthony Nandaa <profnandaa@gmail.com>
  • Loading branch information
profnandaa committed Jun 13, 2024
1 parent eed17a4 commit ee99673
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 44 deletions.
67 changes: 40 additions & 27 deletions frontend/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,27 +236,14 @@ func init() {
func TestIntegration(t *testing.T) {
integration.Run(t, allTests, opts...)

integration.Run(t, securityTests, append(append(opts, securityOpts...),
integration.WithMatrix("security.insecure", map[string]interface{}{
"granted": securityInsecureGranted,
"denied": securityInsecureDenied,
}))...)
integration.Run(t, networkTests, append(opts,
integration.WithMatrix("network.host", map[string]interface{}{
"granted": networkHostGranted,
"denied": networkHostDenied,
}))...)
integration.Run(t, lintTests, opts...)
integration.Run(t, heredocTests, opts...)
integration.Run(t, outlineTests, opts...)
integration.Run(t, targetsTests, opts...)

integration.Run(t, reproTests, append(opts,
// Only use the amd64 digest, regardless to the host platform
integration.WithMirroredImages(map[string]string{
"amd64/bullseye-20230109-slim:latest": "docker.io/amd64/debian:bullseye-20230109-slim@sha256:1acb06a0c31fb467eb8327ad361f1091ab265e0bf26d452dea45dcb0c0ea5e75",
}),
)...)
runDockerfileSecurityTests(t)
runDockerfileNetworkTests(t)
runDockerfileReproTests(t)
}

func testDefaultEnvWithArgs(t *testing.T, sb integration.Sandbox) {
Expand Down Expand Up @@ -325,14 +312,20 @@ echo -n $my_arg $* > /out
}

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

dockerfile := []byte(`
dockerfile := []byte(integration.SelectTestCase(t, []string{
`
FROM busybox AS build
ENV myenv foo%sbar
RUN [ "$myenv" = 'foo%sbar' ]
`)
`,
`
FROM mcr.microsoft.com/windows/nanoserver:ltsc2022 AS build
ENV myenv foo%sbar
RUN if %myenv% NEQ foo%sbar (exit 1)
`,
}))

dir := integration.Tmpdir(
t,
Expand Down Expand Up @@ -361,23 +354,36 @@ RUN [ "$myenv" = 'foo%sbar' ]
}

func testDockerignoreOverride(t *testing.T, sb integration.Sandbox) {
integration.SkipOnPlatform(t, "windows")
f := getFrontend(t, sb)
dockerfile := []byte(`
dockerfile := []byte(integration.SelectTestCase(t, []string{
`
FROM busybox
COPY . .
RUN [ -f foo ] && [ ! -f bar ]
`)
`,
`
FROM mcr.microsoft.com/windows/nanoserver:ltsc2022
COPY . .
RUN if exist foo (if not exist bar (exit 0) else (exit 1))
`,
}))

ignore := []byte(`
bar
`)

dockerfile2 := []byte(`
dockerfile2 := []byte(integration.SelectTestCase(t, []string{
`
FROM busybox
COPY . .
RUN [ ! -f foo ] && [ -f bar ]
`)
`,
`
FROM mcr.microsoft.com/windows/nanoserver:ltsc2022
COPY . .
RUN if not exist foo (if exist bar (exit 0) else (exit 1))
`,
}))

ignore2 := []byte(`
foo
Expand Down Expand Up @@ -418,15 +424,22 @@ foo
}

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

dockerfile := []byte(`
dockerfile := []byte(integration.SelectTestCase(t, []string{
`
FROM busybox
ENV empty=""
COPY testfile $empty
RUN [ "$(cat testfile)" == "contents0" ]
`)
`,
`
FROM mcr.microsoft.com/windows/nanoserver:ltsc2022
COPY testfile ""
RUN cmd /V:on /C "set /p tfcontent=<testfile \
& if !tfcontent! == contents0 (exit 0) else (exit 1)"
`,
}))

dir := integration.Tmpdir(
t,
Expand Down
35 changes: 35 additions & 0 deletions frontend/dockerfile/dockerfile_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//go:build !windows
// +build !windows

package dockerfile

import (
"testing"

"github.com/moby/buildkit/util/testutil/integration"
)

func runDockerfileReproTests(t *testing.T) {
integration.Run(t, reproTests, append(opts,
// Only use the amd64 digest, regardless to the host platform
integration.WithMirroredImages(map[string]string{
"amd64/bullseye-20230109-slim:latest": "docker.io/amd64/debian:bullseye-20230109-slim@sha256:1acb06a0c31fb467eb8327ad361f1091ab265e0bf26d452dea45dcb0c0ea5e75",
}),
)...)
}

func runDockerfileSecurityTests(t *testing.T) {
integration.Run(t, securityTests, append(append(opts, securityOpts...),
integration.WithMatrix("security.insecure", map[string]interface{}{
"granted": securityInsecureGranted,
"denied": securityInsecureDenied,
}))...)
}

func runDockerfileNetworkTests(t *testing.T) {
integration.Run(t, networkTests, append(opts,
integration.WithMatrix("network.host", map[string]interface{}{
"granted": networkHostGranted,
"denied": networkHostDenied,
}))...)
}
18 changes: 18 additions & 0 deletions frontend/dockerfile/dockerfile_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build windows
// +build windows

package dockerfile

import "testing"

func runDockerfileReproTests(t *testing.T) {
t.Skip("ReproTests: skipped on Windows.")
}

func runDockerfileSecurityTests(t *testing.T) {
t.Skip("SecurityTests: skipped on Windows")
}

func runDockerfileNetworkTests(t *testing.T) {
t.Skip("SecurityTestsWithMatrix: skipped on Windows")
}
32 changes: 15 additions & 17 deletions util/testutil/integration/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,23 +277,7 @@ func copyImagesLocal(t *testing.T, host string, images map[string]string) error
}

func OfficialImages(names ...string) map[string]string {
ns := runtime.GOARCH
if ns == "arm64" {
ns = "arm64v8"
} else if ns != "amd64" {
ns = "library"
}
m := map[string]string{}
for _, name := range names {
ref := "docker.io/" + ns + "/" + name
if pns, ok := pins[name]; ok {
if dgst, ok := pns[ns]; ok {
ref += "@" + dgst
}
}
m["library/"+name] = ref
}
return m
return officialImages(names...)
}

func withMirrorConfig(mirror string) ConfigUpdater {
Expand Down Expand Up @@ -437,3 +421,17 @@ func SkipOnPlatform(t *testing.T, goos string) {
t.Skipf("Skipped on %s", goos)
}
}

// Selects a test case from a slice. Works with only two cases:
// slice[0] for Unix or slice[1] for Windows.
// You can provide a slice of two items of any type.
// Choosing this approach to simplify the inlined calls.
func SelectTestCase[T any](t *testing.T, tc []T) T {
if len(tc) != 2 {
t.Error("SelectTestCase: only supports two options")
}
if runtime.GOOS == "windows" {
return tc[1]
}
return tc[0]
}
28 changes: 28 additions & 0 deletions util/testutil/integration/run_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build !windows
// +build !windows

package integration

import "runtime"

var BusyboxImage = "busybox:latest"

func officialImages(names ...string) map[string]string {
ns := runtime.GOARCH
if ns == "arm64" {
ns = "arm64v8"
} else if ns != "amd64" {
ns = "library"
}
m := map[string]string{}
for _, name := range names {
ref := "docker.io/" + ns + "/" + name
if pns, ok := pins[name]; ok {
if dgst, ok := pns[ns]; ok {
ref += "@" + dgst
}
}
m["library/"+name] = ref
}
return m
}
16 changes: 16 additions & 0 deletions util/testutil/integration/run_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package integration

var BusyboxImage = "registry.k8s.io/e2e-test-images/busybox:1.29-2"

func officialImages(names ...string) map[string]string {
// basic mapping for now since there are
// very few official Windows-based images
// TODO: still don't understand why I can't use busybox:latest
// in the tests and I have to provide the full URI.
m := map[string]string{
// "busybox:latest": "registry.k8s.io/e2e-test-images/busybox:1.29-2",
// "busybox:wintools": "docker.io/wintools/busybox:latest",
// "nanoserver": "mcr.microsoft.com/windows/nanoserver:ltsc2022",
}
return m
}

0 comments on commit ee99673

Please sign in to comment.