2015-12-14 05:17:51 -05:00
|
|
|
package gitutils
|
2015-04-21 17:24:29 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-04-24 18:12:45 -04:00
|
|
|
"io/ioutil"
|
2015-04-21 17:24:29 -04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2015-04-24 18:12:45 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-04-21 17:24:29 -04:00
|
|
|
"reflect"
|
2016-02-11 21:19:17 -05:00
|
|
|
"runtime"
|
2015-04-21 17:24:29 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCloneArgsSmartHttp(t *testing.T) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
serverURL, _ := url.Parse(server.URL)
|
|
|
|
|
|
|
|
serverURL.Path = "/repo.git"
|
|
|
|
gitURL := serverURL.String()
|
|
|
|
|
|
|
|
mux.HandleFunc("/repo.git/info/refs", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
q := r.URL.Query().Get("service")
|
|
|
|
w.Header().Set("Content-Type", fmt.Sprintf("application/x-%s-advertisement", q))
|
|
|
|
})
|
|
|
|
|
2015-04-24 18:12:45 -04:00
|
|
|
args := cloneArgs(serverURL, "/tmp")
|
2015-04-21 17:24:29 -04:00
|
|
|
exp := []string{"clone", "--recursive", "--depth", "1", gitURL, "/tmp"}
|
|
|
|
if !reflect.DeepEqual(args, exp) {
|
|
|
|
t.Fatalf("Expected %v, got %v", exp, args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloneArgsDumbHttp(t *testing.T) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
serverURL, _ := url.Parse(server.URL)
|
|
|
|
|
|
|
|
serverURL.Path = "/repo.git"
|
|
|
|
gitURL := serverURL.String()
|
|
|
|
|
|
|
|
mux.HandleFunc("/repo.git/info/refs", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
})
|
|
|
|
|
2015-04-24 18:12:45 -04:00
|
|
|
args := cloneArgs(serverURL, "/tmp")
|
2015-04-21 17:24:29 -04:00
|
|
|
exp := []string{"clone", "--recursive", gitURL, "/tmp"}
|
|
|
|
if !reflect.DeepEqual(args, exp) {
|
|
|
|
t.Fatalf("Expected %v, got %v", exp, args)
|
|
|
|
}
|
|
|
|
}
|
2015-04-24 18:12:45 -04:00
|
|
|
|
2015-04-21 17:24:29 -04:00
|
|
|
func TestCloneArgsGit(t *testing.T) {
|
2015-04-24 18:12:45 -04:00
|
|
|
u, _ := url.Parse("git://github.com/docker/docker")
|
|
|
|
args := cloneArgs(u, "/tmp")
|
2015-04-21 17:24:29 -04:00
|
|
|
exp := []string{"clone", "--recursive", "--depth", "1", "git://github.com/docker/docker", "/tmp"}
|
|
|
|
if !reflect.DeepEqual(args, exp) {
|
|
|
|
t.Fatalf("Expected %v, got %v", exp, args)
|
|
|
|
}
|
|
|
|
}
|
2015-04-24 18:12:45 -04:00
|
|
|
|
|
|
|
func TestCloneArgsStripFragment(t *testing.T) {
|
|
|
|
u, _ := url.Parse("git://github.com/docker/docker#test")
|
|
|
|
args := cloneArgs(u, "/tmp")
|
|
|
|
exp := []string{"clone", "--recursive", "git://github.com/docker/docker", "/tmp"}
|
|
|
|
if !reflect.DeepEqual(args, exp) {
|
|
|
|
t.Fatalf("Expected %v, got %v", exp, args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckoutGit(t *testing.T) {
|
|
|
|
root, err := ioutil.TempDir("", "docker-build-git-checkout")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(root)
|
|
|
|
|
2016-02-11 21:19:17 -05:00
|
|
|
eol := "\n"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
eol = "\r\n"
|
|
|
|
}
|
|
|
|
|
2015-04-24 18:12:45 -04:00
|
|
|
gitDir := filepath.Join(root, "repo")
|
|
|
|
_, err = git("init", gitDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = gitWithinDir(gitDir, "config", "user.email", "test@docker.com"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = gitWithinDir(gitDir, "config", "user.name", "Docker test"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = ioutil.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch"), 0644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
subDir := filepath.Join(gitDir, "subdir")
|
|
|
|
if err = os.Mkdir(subDir, 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = ioutil.WriteFile(filepath.Join(subDir, "Dockerfile"), []byte("FROM scratch\nEXPOSE 5000"), 0644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-02-11 21:19:17 -05:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
if err = os.Symlink("../subdir", filepath.Join(gitDir, "parentlink")); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-01 14:41:45 -04:00
|
|
|
|
2016-02-11 21:19:17 -05:00
|
|
|
if err = os.Symlink("/subdir", filepath.Join(gitDir, "absolutelink")); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-01 14:41:45 -04:00
|
|
|
}
|
|
|
|
|
2015-04-24 18:12:45 -04:00
|
|
|
if _, err = gitWithinDir(gitDir, "add", "-A"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = gitWithinDir(gitDir, "commit", "-am", "First commit"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = gitWithinDir(gitDir, "checkout", "-b", "test"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = ioutil.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch\nEXPOSE 3000"), 0644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = ioutil.WriteFile(filepath.Join(subDir, "Dockerfile"), []byte("FROM busybox\nEXPOSE 5000"), 0644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = gitWithinDir(gitDir, "add", "-A"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = gitWithinDir(gitDir, "commit", "-am", "Branch commit"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = gitWithinDir(gitDir, "checkout", "master"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-02-11 21:19:17 -05:00
|
|
|
type singleCase struct {
|
2015-04-24 18:12:45 -04:00
|
|
|
frag string
|
|
|
|
exp string
|
|
|
|
fail bool
|
2016-02-11 21:19:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
cases := []singleCase{
|
2015-04-24 18:12:45 -04:00
|
|
|
{"", "FROM scratch", false},
|
|
|
|
{"master", "FROM scratch", false},
|
2016-02-11 21:19:17 -05:00
|
|
|
{":subdir", "FROM scratch" + eol + "EXPOSE 5000", false},
|
2015-04-24 18:12:45 -04:00
|
|
|
{":nosubdir", "", true}, // missing directory error
|
|
|
|
{":Dockerfile", "", true}, // not a directory error
|
|
|
|
{"master:nosubdir", "", true},
|
2016-02-11 21:19:17 -05:00
|
|
|
{"master:subdir", "FROM scratch" + eol + "EXPOSE 5000", false},
|
2015-06-01 14:41:45 -04:00
|
|
|
{"master:../subdir", "", true},
|
2016-02-11 21:19:17 -05:00
|
|
|
{"test", "FROM scratch" + eol + "EXPOSE 3000", false},
|
|
|
|
{"test:", "FROM scratch" + eol + "EXPOSE 3000", false},
|
|
|
|
{"test:subdir", "FROM busybox" + eol + "EXPOSE 5000", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
// Windows GIT (2.7.1 x64) does not support parentlink/absolutelink. Sample output below
|
|
|
|
// git --work-tree .\repo --git-dir .\repo\.git add -A
|
|
|
|
// error: readlink("absolutelink"): Function not implemented
|
|
|
|
// error: unable to index file absolutelink
|
|
|
|
// fatal: adding files failed
|
|
|
|
cases = append(cases, singleCase{frag: "master:absolutelink", exp: "FROM scratch" + eol + "EXPOSE 5000", fail: false})
|
|
|
|
cases = append(cases, singleCase{frag: "master:parentlink", exp: "FROM scratch" + eol + "EXPOSE 5000", fail: false})
|
2015-04-24 18:12:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
r, err := checkoutGit(c.frag, gitDir)
|
|
|
|
|
|
|
|
fail := err != nil
|
|
|
|
if fail != c.fail {
|
|
|
|
t.Fatalf("Expected %v failure, error was %v\n", c.fail, err)
|
|
|
|
}
|
|
|
|
if c.fail {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := ioutil.ReadFile(filepath.Join(r, "Dockerfile"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(b) != c.exp {
|
|
|
|
t.Fatalf("Expected %v, was %v\n", c.exp, string(b))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|