Forráskód Böngészése

Merge pull request #7209 from LK4D4/test_build_from_git

Test on building from GIT url
Tibor Vass 11 éve
szülő
commit
da40d9b7a5

+ 29 - 0
integration-cli/docker_cli_build_test.go

@@ -1800,3 +1800,32 @@ func TestBuildAddTar(t *testing.T) {
 	}
 	}
 	logDone("build - ADD tar")
 	logDone("build - ADD tar")
 }
 }
+
+func TestBuildFromGIT(t *testing.T) {
+	name := "testbuildfromgit"
+	defer deleteImages(name)
+	git, err := fakeGIT("repo", map[string]string{
+		"Dockerfile": `FROM busybox
+					ADD first /first
+					RUN [ -f /first ]
+					MAINTAINER docker`,
+		"first": "test git data",
+	})
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer git.Close()
+
+	_, err = buildImageFromPath(name, git.RepoURL, true)
+	if err != nil {
+		t.Fatal(err)
+	}
+	res, err := inspectField(name, "Author")
+	if err != nil {
+		t.Fatal(err)
+	}
+	if res != "docker" {
+		t.Fatal("Maintainer should be docker, got %s", res)
+	}
+	logDone("build - build from GIT")
+}

+ 91 - 0
integration-cli/docker_utils.go

@@ -8,6 +8,7 @@ import (
 	"os"
 	"os"
 	"os/exec"
 	"os/exec"
 	"path"
 	"path"
+	"path/filepath"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
 	"testing"
 	"testing"
@@ -254,3 +255,93 @@ func buildImageFromContext(name string, ctx *FakeContext, useCache bool) (string
 	}
 	}
 	return getIDByName(name)
 	return getIDByName(name)
 }
 }
+
+func buildImageFromPath(name, path string, useCache bool) (string, error) {
+	args := []string{"build", "-t", name}
+	if !useCache {
+		args = append(args, "--no-cache")
+	}
+	args = append(args, path)
+	buildCmd := exec.Command(dockerBinary, args...)
+	out, exitCode, err := runCommandWithOutput(buildCmd)
+	if err != nil || exitCode != 0 {
+		return "", fmt.Errorf("failed to build the image: %s", out)
+	}
+	return getIDByName(name)
+}
+
+type FakeGIT struct {
+	*httptest.Server
+	Root    string
+	RepoURL string
+}
+
+func (g *FakeGIT) Close() {
+	g.Server.Close()
+	os.RemoveAll(g.Root)
+}
+
+func fakeGIT(name string, files map[string]string) (*FakeGIT, error) {
+	tmp, err := ioutil.TempDir("", "fake-git-repo")
+	if err != nil {
+		return nil, err
+	}
+	ctx := &FakeContext{tmp}
+	for file, content := range files {
+		if err := ctx.Add(file, content); err != nil {
+			ctx.Close()
+			return nil, err
+		}
+	}
+	defer ctx.Close()
+	curdir, err := os.Getwd()
+	if err != nil {
+		return nil, err
+	}
+	defer os.Chdir(curdir)
+
+	if output, err := exec.Command("git", "init", ctx.Dir).CombinedOutput(); err != nil {
+		return nil, fmt.Errorf("Error trying to init repo: %s (%s)", err, output)
+	}
+	err = os.Chdir(ctx.Dir)
+	if err != nil {
+		return nil, err
+	}
+	if output, err := exec.Command("git", "add", "*").CombinedOutput(); err != nil {
+		return nil, fmt.Errorf("Error trying to add files to repo: %s (%s)", err, output)
+	}
+	if output, err := exec.Command("git", "commit", "-a", "-m", "Initial commit").CombinedOutput(); err != nil {
+		return nil, fmt.Errorf("Error trying to commit to repo: %s (%s)", err, output)
+	}
+
+	root, err := ioutil.TempDir("", "docker-test-git-repo")
+	if err != nil {
+		return nil, err
+	}
+	repoPath := filepath.Join(root, name+".git")
+	if output, err := exec.Command("git", "clone", "--bare", ctx.Dir, repoPath).CombinedOutput(); err != nil {
+		os.RemoveAll(root)
+		return nil, fmt.Errorf("Error trying to clone --bare: %s (%s)", err, output)
+	}
+	err = os.Chdir(repoPath)
+	if err != nil {
+		os.RemoveAll(root)
+		return nil, err
+	}
+	if output, err := exec.Command("git", "update-server-info").CombinedOutput(); err != nil {
+		os.RemoveAll(root)
+		return nil, fmt.Errorf("Error trying to git update-server-info: %s (%s)", err, output)
+	}
+	err = os.Chdir(curdir)
+	if err != nil {
+		os.RemoveAll(root)
+		return nil, err
+	}
+	handler := http.FileServer(http.Dir(root))
+	server := httptest.NewServer(handler)
+	return &FakeGIT{
+		Server:  server,
+		Root:    root,
+		RepoURL: fmt.Sprintf("%s/%s.git", server.URL, name),
+	}, nil
+}