From 5b0d4cf29629eabb8855400159c097cbd7502922 Mon Sep 17 00:00:00 2001 From: Alexandr Morozov Date: Thu, 24 Jul 2014 11:19:16 +0400 Subject: [PATCH] Test on building from GIT url Also I added fake git server to utils Docker-DCO-1.1-Signed-off-by: Alexandr Morozov (github: LK4D4) --- integration-cli/docker_cli_build_test.go | 29 ++++++++ integration-cli/docker_utils.go | 91 ++++++++++++++++++++++++ 2 files changed, 120 insertions(+) diff --git a/integration-cli/docker_cli_build_test.go b/integration-cli/docker_cli_build_test.go index 3b1c1c230e..0ea62e2a70 100644 --- a/integration-cli/docker_cli_build_test.go +++ b/integration-cli/docker_cli_build_test.go @@ -1777,3 +1777,32 @@ func TestBuildAddTar(t *testing.T) { } 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") +} diff --git a/integration-cli/docker_utils.go b/integration-cli/docker_utils.go index e3e5b36d93..fcaeef5ab2 100644 --- a/integration-cli/docker_utils.go +++ b/integration-cli/docker_utils.go @@ -8,6 +8,7 @@ import ( "os" "os/exec" "path" + "path/filepath" "strconv" "strings" "testing" @@ -254,3 +255,93 @@ func buildImageFromContext(name string, ctx *FakeContext, useCache bool) (string } 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 +}