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