github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/integration-cli/docker_api_build_test.go (about)

     1  package main
     2  
     3  import (
     4  	"archive/tar"
     5  	"bytes"
     6  	"net/http"
     7  	"regexp"
     8  	"strings"
     9  
    10  	"github.com/docker/docker/pkg/integration/checker"
    11  	"github.com/go-check/check"
    12  )
    13  
    14  func (s *DockerSuite) TestBuildApiDockerFileRemote(c *check.C) {
    15  	testRequires(c, NotUserNamespace)
    16  	testRequires(c, DaemonIsLinux)
    17  	server, err := fakeStorage(map[string]string{
    18  		"testD": `FROM busybox
    19  COPY * /tmp/
    20  RUN find / -name ba*
    21  RUN find /tmp/`,
    22  	})
    23  	c.Assert(err, checker.IsNil)
    24  	defer server.Close()
    25  
    26  	res, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+server.URL()+"/testD", nil, "application/json")
    27  	c.Assert(err, checker.IsNil)
    28  	c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
    29  
    30  	buf, err := readBody(body)
    31  	c.Assert(err, checker.IsNil)
    32  
    33  	// Make sure Dockerfile exists.
    34  	// Make sure 'baz' doesn't exist ANYWHERE despite being mentioned in the URL
    35  	out := string(buf)
    36  	c.Assert(out, checker.Contains, "/tmp/Dockerfile")
    37  	c.Assert(out, checker.Not(checker.Contains), "baz")
    38  }
    39  
    40  func (s *DockerSuite) TestBuildApiRemoteTarballContext(c *check.C) {
    41  	testRequires(c, DaemonIsLinux)
    42  	buffer := new(bytes.Buffer)
    43  	tw := tar.NewWriter(buffer)
    44  	defer tw.Close()
    45  
    46  	dockerfile := []byte("FROM busybox")
    47  	err := tw.WriteHeader(&tar.Header{
    48  		Name: "Dockerfile",
    49  		Size: int64(len(dockerfile)),
    50  	})
    51  	// failed to write tar file header
    52  	c.Assert(err, checker.IsNil)
    53  
    54  	_, err = tw.Write(dockerfile)
    55  	// failed to write tar file content
    56  	c.Assert(err, checker.IsNil)
    57  
    58  	// failed to close tar archive
    59  	c.Assert(tw.Close(), checker.IsNil)
    60  
    61  	server, err := fakeBinaryStorage(map[string]*bytes.Buffer{
    62  		"testT.tar": buffer,
    63  	})
    64  	c.Assert(err, checker.IsNil)
    65  
    66  	defer server.Close()
    67  
    68  	res, b, err := sockRequestRaw("POST", "/build?remote="+server.URL()+"/testT.tar", nil, "application/tar")
    69  	c.Assert(err, checker.IsNil)
    70  	c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
    71  	b.Close()
    72  }
    73  
    74  func (s *DockerSuite) TestBuildApiRemoteTarballContextWithCustomDockerfile(c *check.C) {
    75  	testRequires(c, DaemonIsLinux)
    76  	buffer := new(bytes.Buffer)
    77  	tw := tar.NewWriter(buffer)
    78  	defer tw.Close()
    79  
    80  	dockerfile := []byte(`FROM busybox
    81  RUN echo 'wrong'`)
    82  	err := tw.WriteHeader(&tar.Header{
    83  		Name: "Dockerfile",
    84  		Size: int64(len(dockerfile)),
    85  	})
    86  	// failed to write tar file header
    87  	c.Assert(err, checker.IsNil)
    88  
    89  	_, err = tw.Write(dockerfile)
    90  	// failed to write tar file content
    91  	c.Assert(err, checker.IsNil)
    92  
    93  	custom := []byte(`FROM busybox
    94  RUN echo 'right'
    95  `)
    96  	err = tw.WriteHeader(&tar.Header{
    97  		Name: "custom",
    98  		Size: int64(len(custom)),
    99  	})
   100  
   101  	// failed to write tar file header
   102  	c.Assert(err, checker.IsNil)
   103  
   104  	_, err = tw.Write(custom)
   105  	// failed to write tar file content
   106  	c.Assert(err, checker.IsNil)
   107  
   108  	// failed to close tar archive
   109  	c.Assert(tw.Close(), checker.IsNil)
   110  
   111  	server, err := fakeBinaryStorage(map[string]*bytes.Buffer{
   112  		"testT.tar": buffer,
   113  	})
   114  	c.Assert(err, checker.IsNil)
   115  
   116  	defer server.Close()
   117  	url := "/build?dockerfile=custom&remote=" + server.URL() + "/testT.tar"
   118  	res, body, err := sockRequestRaw("POST", url, nil, "application/tar")
   119  	c.Assert(err, checker.IsNil)
   120  	c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
   121  
   122  	defer body.Close()
   123  	content, err := readBody(body)
   124  	c.Assert(err, checker.IsNil)
   125  
   126  	// Build used the wrong dockerfile.
   127  	c.Assert(string(content), checker.Not(checker.Contains), "wrong")
   128  }
   129  
   130  func (s *DockerSuite) TestBuildApiLowerDockerfile(c *check.C) {
   131  	testRequires(c, DaemonIsLinux)
   132  	git, err := newFakeGit("repo", map[string]string{
   133  		"dockerfile": `FROM busybox
   134  RUN echo from dockerfile`,
   135  	}, false)
   136  	c.Assert(err, checker.IsNil)
   137  	defer git.Close()
   138  
   139  	res, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
   140  	c.Assert(err, checker.IsNil)
   141  	c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
   142  
   143  	buf, err := readBody(body)
   144  	c.Assert(err, checker.IsNil)
   145  
   146  	out := string(buf)
   147  	c.Assert(out, checker.Contains, "from dockerfile")
   148  }
   149  
   150  func (s *DockerSuite) TestBuildApiBuildGitWithF(c *check.C) {
   151  	testRequires(c, DaemonIsLinux)
   152  	git, err := newFakeGit("repo", map[string]string{
   153  		"baz": `FROM busybox
   154  RUN echo from baz`,
   155  		"Dockerfile": `FROM busybox
   156  RUN echo from Dockerfile`,
   157  	}, false)
   158  	c.Assert(err, checker.IsNil)
   159  	defer git.Close()
   160  
   161  	// Make sure it tries to 'dockerfile' query param value
   162  	res, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+git.RepoURL, nil, "application/json")
   163  	c.Assert(err, checker.IsNil)
   164  	c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
   165  
   166  	buf, err := readBody(body)
   167  	c.Assert(err, checker.IsNil)
   168  
   169  	out := string(buf)
   170  	c.Assert(out, checker.Contains, "from baz")
   171  }
   172  
   173  func (s *DockerSuite) TestBuildApiDoubleDockerfile(c *check.C) {
   174  	testRequires(c, UnixCli) // dockerfile overwrites Dockerfile on Windows
   175  	git, err := newFakeGit("repo", map[string]string{
   176  		"Dockerfile": `FROM busybox
   177  RUN echo from Dockerfile`,
   178  		"dockerfile": `FROM busybox
   179  RUN echo from dockerfile`,
   180  	}, false)
   181  	c.Assert(err, checker.IsNil)
   182  	defer git.Close()
   183  
   184  	// Make sure it tries to 'dockerfile' query param value
   185  	res, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
   186  	c.Assert(err, checker.IsNil)
   187  	c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
   188  
   189  	buf, err := readBody(body)
   190  	c.Assert(err, checker.IsNil)
   191  
   192  	out := string(buf)
   193  	c.Assert(out, checker.Contains, "from Dockerfile")
   194  }
   195  
   196  func (s *DockerSuite) TestBuildApiUnnormalizedTarPaths(c *check.C) {
   197  	// Make sure that build context tars with entries of the form
   198  	// x/./y don't cause caching false positives.
   199  
   200  	buildFromTarContext := func(fileContents []byte) string {
   201  		buffer := new(bytes.Buffer)
   202  		tw := tar.NewWriter(buffer)
   203  		defer tw.Close()
   204  
   205  		dockerfile := []byte(`FROM busybox
   206  	COPY dir /dir/`)
   207  		err := tw.WriteHeader(&tar.Header{
   208  			Name: "Dockerfile",
   209  			Size: int64(len(dockerfile)),
   210  		})
   211  		//failed to write tar file header
   212  		c.Assert(err, checker.IsNil)
   213  
   214  		_, err = tw.Write(dockerfile)
   215  		// failed to write Dockerfile in tar file content
   216  		c.Assert(err, checker.IsNil)
   217  
   218  		err = tw.WriteHeader(&tar.Header{
   219  			Name: "dir/./file",
   220  			Size: int64(len(fileContents)),
   221  		})
   222  		//failed to write tar file header
   223  		c.Assert(err, checker.IsNil)
   224  
   225  		_, err = tw.Write(fileContents)
   226  		// failed to write file contents in tar file content
   227  		c.Assert(err, checker.IsNil)
   228  
   229  		// failed to close tar archive
   230  		c.Assert(tw.Close(), checker.IsNil)
   231  
   232  		res, body, err := sockRequestRaw("POST", "/build", buffer, "application/x-tar")
   233  		c.Assert(err, checker.IsNil)
   234  		c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
   235  
   236  		out, err := readBody(body)
   237  		c.Assert(err, checker.IsNil)
   238  		lines := strings.Split(string(out), "\n")
   239  		c.Assert(len(lines), checker.GreaterThan, 1)
   240  		c.Assert(lines[len(lines)-2], checker.Matches, ".*Successfully built [0-9a-f]{12}.*")
   241  
   242  		re := regexp.MustCompile("Successfully built ([0-9a-f]{12})")
   243  		matches := re.FindStringSubmatch(lines[len(lines)-2])
   244  		return matches[1]
   245  	}
   246  
   247  	imageA := buildFromTarContext([]byte("abc"))
   248  	imageB := buildFromTarContext([]byte("def"))
   249  
   250  	c.Assert(imageA, checker.Not(checker.Equals), imageB)
   251  }