1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
//go:build dfexcludepatterns
package dockerfile
import (
"io/fs"
"net/http"
"net/http/httptest"
"os"
"path"
"path/filepath"
"testing"
"github.com/containerd/continuity/fs/fstest"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/frontend/dockerui"
"github.com/moby/buildkit/util/testutil/integration"
"github.com/stretchr/testify/require"
"github.com/tonistiigi/fsutil"
)
var excludedFilesTests = integration.TestFuncs(
testExcludedFilesOnCopy,
)
func init() {
allTests = append(allTests, excludedFilesTests...)
}
// See #4439
func testExcludedFilesOnCopy(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()
srcDir := integration.Tmpdir(
t,
fstest.CreateDir("dir1", fs.ModePerm),
fstest.CreateDir("dir2", fs.ModePerm),
fstest.CreateDir("dir3", fs.ModePerm),
fstest.CreateFile("dir1/file-101.png", []byte(`2`), 0600),
fstest.CreateFile("dir1/file-102.txt", []byte(`3`), 0600),
fstest.CreateFile("dir1/file-103.jpeg", []byte(`4`), 0600),
fstest.CreateFile("dir2/file-201.pdf", []byte(`6`), 0600),
fstest.CreateFile("dir2/file-202.jpeg", []byte(`7`), 0600),
fstest.CreateFile("dir2/file-203.png", []byte(`8`), 0600),
fstest.CreateFile("dir3/file-301.mp3", []byte(`9`), 0600),
fstest.CreateFile("dir3/file-302.mpeg", []byte(`10`), 0600),
)
runTest := func(dockerfile []byte, localMounts map[string]fsutil.FS) {
f := getFrontend(t, sb)
destDir := integration.Tmpdir(t)
dockerDir := integration.Tmpdir(
t,
fstest.CreateFile(dockerui.DefaultDockerfileName, dockerfile, 0600),
)
if localMounts == nil {
localMounts = map[string]fsutil.FS{}
}
localMounts[dockerui.DefaultLocalNameDockerfile] = dockerDir
_, err = f.Solve(ctx, c, client.SolveOpt{
LocalMounts: localMounts,
Exports: []client.ExportEntry{
{
Type: client.ExporterLocal,
OutputDir: destDir.Name,
},
},
}, nil)
require.NoError(t, err)
testCases := []struct {
filename string
excluded bool
expectedContent string
}{
// Files copied with COPY command
{filename: "builder1/1/dir1/file-101.png", excluded: true},
{filename: "builder1/1/dir1/file-102.txt", excluded: false, expectedContent: `3`},
{filename: "builder1/1/dir1/file-103.jpeg", excluded: true},
{filename: "builder1/1/dir2/file-201.pdf", excluded: false, expectedContent: `6`},
{filename: "builder1/1/dir2/file-202.jpeg", excluded: true},
{filename: "builder1/1/dir2/file-203.png", excluded: true},
{filename: "builder1/1/dir3/file-301.mp3", excluded: false, expectedContent: `9`},
{filename: "builder1/1/dir3/file-302.mpeg", excluded: false, expectedContent: `10`},
// files copied with COPY command
{filename: "builder2/2/dir1/file-101.png", excluded: false, expectedContent: `2`},
{filename: "builder2/2/dir1/file-102.txt", excluded: true},
{filename: "builder2/2/dir1/file-103.jpeg", excluded: false, expectedContent: `4`},
{filename: "builder2/2/dir2/file-201.pdf", excluded: true},
{filename: "builder2/2/dir2/file-202.jpeg", excluded: false, expectedContent: `7`},
{filename: "builder2/2/dir2/file-203.png", excluded: false, expectedContent: `8`},
{filename: "builder2/2/dir3/file-301.mp3", excluded: false, expectedContent: `9`},
{filename: "builder2/2/dir3/file-302.mpeg", excluded: false, expectedContent: `10`},
// Files copied with ADD command
{filename: "builder3/3/file-301.mp3", excluded: true},
{filename: "builder3/3/file-302.mpeg", excluded: false, expectedContent: `10`},
// Files copied with COPY wildcard
{filename: "builder4/4/file-101.png", excluded: true},
{filename: "builder4/4/file-102.txt", excluded: true},
{filename: "builder4/4/file-103.jpeg", excluded: true},
{filename: "builder4/4/file-201.pdf", excluded: true},
{filename: "builder4/4/file-202.jpeg", excluded: true},
{filename: "builder4/4/file-203.png", excluded: true},
{filename: "builder4/4/file-301.mp3", excluded: true},
{filename: "builder4/4/file-301.mp3", excluded: true},
{filename: "builder4/4/file-302.mpeg", excluded: false, expectedContent: `10`},
// Files copied with ADD wildcard
{filename: "builder5/5/file-101.png", excluded: true},
{filename: "builder5/5/file-102.txt", excluded: false, expectedContent: `3`},
{filename: "builder5/5/file-103.jpeg", excluded: true},
{filename: "builder5/5/file-201.pdf", excluded: true},
{filename: "builder5/5/file-202.jpeg", excluded: true},
{filename: "builder5/5/file-203.png", excluded: true},
{filename: "builder5/5/file-301.mp3", excluded: true},
{filename: "builder5/5/file-301.mp3", excluded: true},
{filename: "builder5/5/file-302.mpeg", excluded: true},
}
for _, tc := range testCases {
dt, err := os.ReadFile(path.Join(destDir.Name, tc.filename))
if tc.excluded {
require.Errorf(t, err, "File %s should not exist: %v", tc.filename, err)
continue
}
require.NoErrorf(t, err, "File %s should exist", tc.filename)
require.Equalf(t, tc.expectedContent, string(dt), "File %s does not have matched content", tc.filename)
}
// As all files are excluded, the directory must be empty
items, err := os.ReadDir(path.Join(destDir.Name, `builder6/6`))
require.NoError(t, err)
require.Empty(t, items)
}
// First, test with reading from a plain directory
runTest([]byte(`
# ignore all the image files
FROM scratch as builder1
COPY --exclude=*/*.png --exclude=*/*.jpeg . /1
# ignore all document files
FROM scratch as builder2
COPY --exclude=*/*.pdf --exclude=*/*.txt . /2
# ignore all mp3 files
FROM scratch as builder3
ADD --exclude=*.mp3 dir3/ /3/
# Ignore everything, keeping only .mpeg files, using src as a wildcard.
# it's a fake example, just to be sure that src as wildcards work as expected.
FROM scratch as builder4
COPY --exclude=*.mp3 --exclude=*.jpeg --exclude=*.png --exclude=*.pdf --exclude=*.txt dir* /4/
# Ignore everything, keeping only txt files.
# it's a fake example, just to be sure that src as wildcards work as expected.
FROM scratch as builder5
ADD --exclude=*.mp* --exclude=*.jpeg --exclude=*.png --exclude=*.pdf dir* /5
# Exclude all files. No idea how it could be useful, but it should not be forbidden.
FROM scratch as builder6
ADD --exclude=* dir* /6
FROM scratch
COPY --from=builder1 / /builder1
COPY --from=builder2 / /builder2
COPY --from=builder3 / /builder3
COPY --from=builder4 / /builder4
COPY --from=builder5 / /builder5
COPY --from=builder6 / /builder6
`), map[string]fsutil.FS{
dockerui.DefaultLocalNameContext: srcDir,
})
// Load context from a git repository
{
gitCommands := []string{
"git init",
"git config --local user.email test",
"git config --local user.name test",
"git add *",
"git commit -m 0.1",
"git tag 0.1",
"git update-server-info",
}
err = runShell(srcDir.Name, gitCommands...)
require.NoError(t, err)
server := httptest.NewServer(http.FileServer(http.Dir(filepath.Clean(srcDir.Name))))
defer server.Close()
serverURL := server.URL
// In this example, all the ADD commands were replaced by COPY, as ADD does not support
// the `--from=` flag, and we want to keep the assertions intact.
runTest([]byte(`
FROM scratch AS base
ARG REPO="`+serverURL+`/.git"
ARG TAG="0.1"
ADD --keep-git-dir=true ${REPO}#${TAG} /
# ignore all the image files
FROM scratch as builder1
COPY --from=base --exclude=*/*.png --exclude=*/*.jpeg . /1
# ignore all document files
FROM scratch as builder2
COPY --from=base --exclude=*/*.pdf --exclude=*/*.txt . /2
# ignore all mp3 files
FROM scratch as builder3
COPY --from=base --exclude=*.mp3 dir3/ /3/
# Ignore everything, keeping only .mpeg files, using src as a wildcard.
# it's a fake example, just to be sure that src as wildcards work as expected.
FROM scratch as builder4
COPY --from=base --exclude=*.mp3 --exclude=*.jpeg --exclude=*.png --exclude=*.pdf --exclude=*.txt dir* /4/
# Ignore everything, keeping only txt files.
# it's a fake example, just to be sure that src as wildcards work as expected.
FROM scratch as builder5
COPY --from=base --exclude=*.mp* --exclude=*.jpeg --exclude=*.png --exclude=*.pdf dir* /5
# Exclude all files. No idea how it could be useful, but it should not be forbidden.
FROM scratch as builder6
COPY --from=base --exclude=* dir* /6
FROM scratch
COPY --from=builder1 / /builder1
COPY --from=builder2 / /builder2
COPY --from=builder3 / /builder3
COPY --from=builder4 / /builder4
COPY --from=builder5 / /builder5
COPY --from=builder6 / /builder6
`), nil)
}
}
|