Test on building from GIT url

Also I added fake git server to utils
Docker-DCO-1.1-Signed-off-by: Alexandr Morozov <lk4d4math@gmail.com> (github: LK4D4)
This commit is contained in:
Alexandr Morozov 2014-07-24 11:19:16 +04:00
parent 9380e8cbe7
commit 5b0d4cf296
2 changed files with 120 additions and 0 deletions

View File

@ -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")
}

View File

@ -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
}