github.com/reds/docker@v1.11.2-rc1/pkg/gitutils/gitutils_test.go (about)

     1  package gitutils
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"net/url"
     9  	"os"
    10  	"path/filepath"
    11  	"reflect"
    12  	"runtime"
    13  	"testing"
    14  )
    15  
    16  func TestCloneArgsSmartHttp(t *testing.T) {
    17  	mux := http.NewServeMux()
    18  	server := httptest.NewServer(mux)
    19  	serverURL, _ := url.Parse(server.URL)
    20  
    21  	serverURL.Path = "/repo.git"
    22  	gitURL := serverURL.String()
    23  
    24  	mux.HandleFunc("/repo.git/info/refs", func(w http.ResponseWriter, r *http.Request) {
    25  		q := r.URL.Query().Get("service")
    26  		w.Header().Set("Content-Type", fmt.Sprintf("application/x-%s-advertisement", q))
    27  	})
    28  
    29  	args := cloneArgs(serverURL, "/tmp")
    30  	exp := []string{"clone", "--recursive", "--depth", "1", gitURL, "/tmp"}
    31  	if !reflect.DeepEqual(args, exp) {
    32  		t.Fatalf("Expected %v, got %v", exp, args)
    33  	}
    34  }
    35  
    36  func TestCloneArgsDumbHttp(t *testing.T) {
    37  	mux := http.NewServeMux()
    38  	server := httptest.NewServer(mux)
    39  	serverURL, _ := url.Parse(server.URL)
    40  
    41  	serverURL.Path = "/repo.git"
    42  	gitURL := serverURL.String()
    43  
    44  	mux.HandleFunc("/repo.git/info/refs", func(w http.ResponseWriter, r *http.Request) {
    45  		w.Header().Set("Content-Type", "text/plain")
    46  	})
    47  
    48  	args := cloneArgs(serverURL, "/tmp")
    49  	exp := []string{"clone", "--recursive", gitURL, "/tmp"}
    50  	if !reflect.DeepEqual(args, exp) {
    51  		t.Fatalf("Expected %v, got %v", exp, args)
    52  	}
    53  }
    54  
    55  func TestCloneArgsGit(t *testing.T) {
    56  	u, _ := url.Parse("git://github.com/docker/docker")
    57  	args := cloneArgs(u, "/tmp")
    58  	exp := []string{"clone", "--recursive", "--depth", "1", "git://github.com/docker/docker", "/tmp"}
    59  	if !reflect.DeepEqual(args, exp) {
    60  		t.Fatalf("Expected %v, got %v", exp, args)
    61  	}
    62  }
    63  
    64  func TestCloneArgsStripFragment(t *testing.T) {
    65  	u, _ := url.Parse("git://github.com/docker/docker#test")
    66  	args := cloneArgs(u, "/tmp")
    67  	exp := []string{"clone", "--recursive", "git://github.com/docker/docker", "/tmp"}
    68  	if !reflect.DeepEqual(args, exp) {
    69  		t.Fatalf("Expected %v, got %v", exp, args)
    70  	}
    71  }
    72  
    73  func TestCheckoutGit(t *testing.T) {
    74  	root, err := ioutil.TempDir("", "docker-build-git-checkout")
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  	defer os.RemoveAll(root)
    79  
    80  	eol := "\n"
    81  	if runtime.GOOS == "windows" {
    82  		eol = "\r\n"
    83  	}
    84  
    85  	gitDir := filepath.Join(root, "repo")
    86  	_, err = git("init", gitDir)
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  
    91  	if _, err = gitWithinDir(gitDir, "config", "user.email", "test@docker.com"); err != nil {
    92  		t.Fatal(err)
    93  	}
    94  
    95  	if _, err = gitWithinDir(gitDir, "config", "user.name", "Docker test"); err != nil {
    96  		t.Fatal(err)
    97  	}
    98  
    99  	if err = ioutil.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch"), 0644); err != nil {
   100  		t.Fatal(err)
   101  	}
   102  
   103  	subDir := filepath.Join(gitDir, "subdir")
   104  	if err = os.Mkdir(subDir, 0755); err != nil {
   105  		t.Fatal(err)
   106  	}
   107  
   108  	if err = ioutil.WriteFile(filepath.Join(subDir, "Dockerfile"), []byte("FROM scratch\nEXPOSE 5000"), 0644); err != nil {
   109  		t.Fatal(err)
   110  	}
   111  
   112  	if runtime.GOOS != "windows" {
   113  		if err = os.Symlink("../subdir", filepath.Join(gitDir, "parentlink")); err != nil {
   114  			t.Fatal(err)
   115  		}
   116  
   117  		if err = os.Symlink("/subdir", filepath.Join(gitDir, "absolutelink")); err != nil {
   118  			t.Fatal(err)
   119  		}
   120  	}
   121  
   122  	if _, err = gitWithinDir(gitDir, "add", "-A"); err != nil {
   123  		t.Fatal(err)
   124  	}
   125  
   126  	if _, err = gitWithinDir(gitDir, "commit", "-am", "First commit"); err != nil {
   127  		t.Fatal(err)
   128  	}
   129  
   130  	if _, err = gitWithinDir(gitDir, "checkout", "-b", "test"); err != nil {
   131  		t.Fatal(err)
   132  	}
   133  
   134  	if err = ioutil.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch\nEXPOSE 3000"), 0644); err != nil {
   135  		t.Fatal(err)
   136  	}
   137  
   138  	if err = ioutil.WriteFile(filepath.Join(subDir, "Dockerfile"), []byte("FROM busybox\nEXPOSE 5000"), 0644); err != nil {
   139  		t.Fatal(err)
   140  	}
   141  
   142  	if _, err = gitWithinDir(gitDir, "add", "-A"); err != nil {
   143  		t.Fatal(err)
   144  	}
   145  
   146  	if _, err = gitWithinDir(gitDir, "commit", "-am", "Branch commit"); err != nil {
   147  		t.Fatal(err)
   148  	}
   149  
   150  	if _, err = gitWithinDir(gitDir, "checkout", "master"); err != nil {
   151  		t.Fatal(err)
   152  	}
   153  
   154  	type singleCase struct {
   155  		frag string
   156  		exp  string
   157  		fail bool
   158  	}
   159  
   160  	cases := []singleCase{
   161  		{"", "FROM scratch", false},
   162  		{"master", "FROM scratch", false},
   163  		{":subdir", "FROM scratch" + eol + "EXPOSE 5000", false},
   164  		{":nosubdir", "", true},   // missing directory error
   165  		{":Dockerfile", "", true}, // not a directory error
   166  		{"master:nosubdir", "", true},
   167  		{"master:subdir", "FROM scratch" + eol + "EXPOSE 5000", false},
   168  		{"master:../subdir", "", true},
   169  		{"test", "FROM scratch" + eol + "EXPOSE 3000", false},
   170  		{"test:", "FROM scratch" + eol + "EXPOSE 3000", false},
   171  		{"test:subdir", "FROM busybox" + eol + "EXPOSE 5000", false},
   172  	}
   173  
   174  	if runtime.GOOS != "windows" {
   175  		// Windows GIT (2.7.1 x64) does not support parentlink/absolutelink. Sample output below
   176  		// 	git --work-tree .\repo --git-dir .\repo\.git add -A
   177  		//	error: readlink("absolutelink"): Function not implemented
   178  		// 	error: unable to index file absolutelink
   179  		// 	fatal: adding files failed
   180  		cases = append(cases, singleCase{frag: "master:absolutelink", exp: "FROM scratch" + eol + "EXPOSE 5000", fail: false})
   181  		cases = append(cases, singleCase{frag: "master:parentlink", exp: "FROM scratch" + eol + "EXPOSE 5000", fail: false})
   182  	}
   183  
   184  	for _, c := range cases {
   185  		r, err := checkoutGit(c.frag, gitDir)
   186  
   187  		fail := err != nil
   188  		if fail != c.fail {
   189  			t.Fatalf("Expected %v failure, error was %v\n", c.fail, err)
   190  		}
   191  		if c.fail {
   192  			continue
   193  		}
   194  
   195  		b, err := ioutil.ReadFile(filepath.Join(r, "Dockerfile"))
   196  		if err != nil {
   197  			t.Fatal(err)
   198  		}
   199  
   200  		if string(b) != c.exp {
   201  			t.Fatalf("Expected %v, was %v\n", c.exp, string(b))
   202  		}
   203  	}
   204  }