From 1986a33db2778a6730c700aa8119b9d3a20b7154 Mon Sep 17 00:00:00 2001 From: Talon Bowler Date: Wed, 10 Jul 2024 00:05:52 -0700 Subject: [PATCH] First tests for CopyIgnoredFile check Signed-off-by: Talon Bowler --- frontend/dockerfile/dockerfile_lint_test.go | 45 +++++++++++++++++++++ frontend/dockerui/config.go | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/frontend/dockerfile/dockerfile_lint_test.go b/frontend/dockerfile/dockerfile_lint_test.go index 8c44f1c228b6c..3f46a09f0fc87 100644 --- a/frontend/dockerfile/dockerfile_lint_test.go +++ b/frontend/dockerfile/dockerfile_lint_test.go @@ -44,8 +44,52 @@ var lintTests = integration.TestFuncs( testRedundantTargetPlatform, testSecretsUsedInArgOrEnv, testInvalidDefaultArgInFrom, + testCopyIgnoredFiles, ) +func testCopyIgnoredFiles(t *testing.T, sb integration.Sandbox) { + dockerignore := []byte(` +Dockerfile +`) + dockerfile := []byte(` +FROM scratch +COPY Dockerfile . +`) + + checkLinterWarnings(t, sb, &lintTestParams{ + Dockerfile: dockerfile, + DockerIgnore: dockerignore, + BuildErrLocation: 3, + StreamBuildErrRegexp: regexp.MustCompile(`failed to solve: failed to compute cache key: failed to calculate checksum of ref [^\s]+ "/Dockerfile": not found`), + UnmarshalBuildErrRegexp: regexp.MustCompile(`failed to compute cache key: failed to calculate checksum of ref [^\s]+ "/Dockerfile": not found`), + Warnings: []expectedLintWarning{ + { + RuleName: "CopyIgnoredFile", + Description: "Attempting to Copy file that is excluded by .dockerignore", + Detail: `Attempting to Copy file "Dockerfile" that is excluded by .dockerignore`, + URL: "https://docs.docker.com/go/dockerfile/rule/copy-ignored-file/", + Level: 1, + Line: 3, + }, + }, + }) + + dockerignore = []byte(` +foobar +`) + dockerfile = []byte(` +FROM scratch AS base +COPY Dockerfile /foobar + +FROM base +COPY --from=base /foobar /Dockerfile +`) + checkLinterWarnings(t, sb, &lintTestParams{ + Dockerfile: dockerfile, + DockerIgnore: dockerignore, + }) +} + func testSecretsUsedInArgOrEnv(t *testing.T, sb integration.Sandbox) { dockerfile := []byte(` FROM scratch @@ -1205,6 +1249,7 @@ func checkUnmarshal(t *testing.T, sb integration.Sandbox, lintTest *lintTestPara _, err = lintTest.Client.Build(sb.Context(), client.SolveOpt{ LocalMounts: map[string]fsutil.FS{ dockerui.DefaultLocalNameDockerfile: lintTest.TmpDir, + dockerui.DefaultLocalNameContext: lintTest.TmpDir, }, }, "", frontend, nil) require.NoError(t, err) diff --git a/frontend/dockerui/config.go b/frontend/dockerui/config.go index a80a0b381032e..e100e0967fe61 100644 --- a/frontend/dockerui/config.go +++ b/frontend/dockerui/config.go @@ -466,7 +466,7 @@ func (bc *Client) DockerIgnorePatterns(ctx context.Context) ([]string, error) { if err != nil { return nil, err } - if bctx.context == nil { + if bctx.context != nil { return nil, nil }