|
4 | 4 | "archive/tar"
|
5 | 5 | "bytes"
|
6 | 6 | "net/http"
|
| 7 | + "regexp" |
| 8 | + "strings" |
7 | 9 |
|
8 | 10 | "github.com/docker/docker/pkg/integration/checker"
|
9 | 11 | "github.com/go-check/check"
|
@@ -255,3 +257,60 @@ func (s *DockerSuite) TestBuildApiDockerfileSymlink(c *check.C) {
|
255 | 257 | // a nonexistent file.
|
256 | 258 | c.Assert(string(out), checker.Contains, "Cannot locate specified Dockerfile: Dockerfile", check.Commentf("Didn't complain about leaving build context"))
|
257 | 259 | }
|
| 260 | + |
| 261 | +func (s *DockerSuite) TestBuildApiUnnormalizedTarPaths(c *check.C) { |
| 262 | + // Make sure that build context tars with entries of the form |
| 263 | + // x/./y don't cause caching false positives. |
| 264 | + |
| 265 | + buildFromTarContext := func(fileContents []byte) string { |
| 266 | + buffer := new(bytes.Buffer) |
| 267 | + tw := tar.NewWriter(buffer) |
| 268 | + defer tw.Close() |
| 269 | + |
| 270 | + dockerfile := []byte(`FROM busybox |
| 271 | + COPY dir /dir/`) |
| 272 | + err := tw.WriteHeader(&tar.Header{ |
| 273 | + Name: "Dockerfile", |
| 274 | + Size: int64(len(dockerfile)), |
| 275 | + }) |
| 276 | + //failed to write tar file header |
| 277 | + c.Assert(err, checker.IsNil) |
| 278 | + |
| 279 | + _, err = tw.Write(dockerfile) |
| 280 | + // failed to write Dockerfile in tar file content |
| 281 | + c.Assert(err, checker.IsNil) |
| 282 | + |
| 283 | + err = tw.WriteHeader(&tar.Header{ |
| 284 | + Name: "dir/./file", |
| 285 | + Size: int64(len(fileContents)), |
| 286 | + }) |
| 287 | + //failed to write tar file header |
| 288 | + c.Assert(err, checker.IsNil) |
| 289 | + |
| 290 | + _, err = tw.Write(fileContents) |
| 291 | + // failed to write file contents in tar file content |
| 292 | + c.Assert(err, checker.IsNil) |
| 293 | + |
| 294 | + // failed to close tar archive |
| 295 | + c.Assert(tw.Close(), checker.IsNil) |
| 296 | + |
| 297 | + res, body, err := sockRequestRaw("POST", "/build", buffer, "application/x-tar") |
| 298 | + c.Assert(err, checker.IsNil) |
| 299 | + c.Assert(res.StatusCode, checker.Equals, http.StatusOK) |
| 300 | + |
| 301 | + out, err := readBody(body) |
| 302 | + c.Assert(err, checker.IsNil) |
| 303 | + lines := strings.Split(string(out), "\n") |
| 304 | + c.Assert(len(lines), checker.GreaterThan, 1) |
| 305 | + c.Assert(lines[len(lines)-2], checker.Matches, ".*Successfully built [0-9a-f]{12}.*") |
| 306 | + |
| 307 | + re := regexp.MustCompile("Successfully built ([0-9a-f]{12})") |
| 308 | + matches := re.FindStringSubmatch(lines[len(lines)-2]) |
| 309 | + return matches[1] |
| 310 | + } |
| 311 | + |
| 312 | + imageA := buildFromTarContext([]byte("abc")) |
| 313 | + imageB := buildFromTarContext([]byte("def")) |
| 314 | + |
| 315 | + c.Assert(imageA, checker.Not(checker.Equals), imageB) |
| 316 | +} |
0 commit comments