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