| ... | ... |
@@ -1800,3 +1800,32 @@ func TestBuildAddTar(t *testing.T) {
|
| 1800 | 1800 |
} |
| 1801 | 1801 |
logDone("build - ADD tar")
|
| 1802 | 1802 |
} |
| 1803 |
+ |
|
| 1804 |
+func TestBuildFromGIT(t *testing.T) {
|
|
| 1805 |
+ name := "testbuildfromgit" |
|
| 1806 |
+ defer deleteImages(name) |
|
| 1807 |
+ git, err := fakeGIT("repo", map[string]string{
|
|
| 1808 |
+ "Dockerfile": `FROM busybox |
|
| 1809 |
+ ADD first /first |
|
| 1810 |
+ RUN [ -f /first ] |
|
| 1811 |
+ MAINTAINER docker`, |
|
| 1812 |
+ "first": "test git data", |
|
| 1813 |
+ }) |
|
| 1814 |
+ if err != nil {
|
|
| 1815 |
+ t.Fatal(err) |
|
| 1816 |
+ } |
|
| 1817 |
+ defer git.Close() |
|
| 1818 |
+ |
|
| 1819 |
+ _, err = buildImageFromPath(name, git.RepoURL, true) |
|
| 1820 |
+ if err != nil {
|
|
| 1821 |
+ t.Fatal(err) |
|
| 1822 |
+ } |
|
| 1823 |
+ res, err := inspectField(name, "Author") |
|
| 1824 |
+ if err != nil {
|
|
| 1825 |
+ t.Fatal(err) |
|
| 1826 |
+ } |
|
| 1827 |
+ if res != "docker" {
|
|
| 1828 |
+ t.Fatal("Maintainer should be docker, got %s", res)
|
|
| 1829 |
+ } |
|
| 1830 |
+ logDone("build - build from GIT")
|
|
| 1831 |
+} |
| ... | ... |
@@ -8,6 +8,7 @@ import ( |
| 8 | 8 |
"os" |
| 9 | 9 |
"os/exec" |
| 10 | 10 |
"path" |
| 11 |
+ "path/filepath" |
|
| 11 | 12 |
"strconv" |
| 12 | 13 |
"strings" |
| 13 | 14 |
"testing" |
| ... | ... |
@@ -254,3 +255,93 @@ func buildImageFromContext(name string, ctx *FakeContext, useCache bool) (string |
| 254 | 254 |
} |
| 255 | 255 |
return getIDByName(name) |
| 256 | 256 |
} |
| 257 |
+ |
|
| 258 |
+func buildImageFromPath(name, path string, useCache bool) (string, error) {
|
|
| 259 |
+ args := []string{"build", "-t", name}
|
|
| 260 |
+ if !useCache {
|
|
| 261 |
+ args = append(args, "--no-cache") |
|
| 262 |
+ } |
|
| 263 |
+ args = append(args, path) |
|
| 264 |
+ buildCmd := exec.Command(dockerBinary, args...) |
|
| 265 |
+ out, exitCode, err := runCommandWithOutput(buildCmd) |
|
| 266 |
+ if err != nil || exitCode != 0 {
|
|
| 267 |
+ return "", fmt.Errorf("failed to build the image: %s", out)
|
|
| 268 |
+ } |
|
| 269 |
+ return getIDByName(name) |
|
| 270 |
+} |
|
| 271 |
+ |
|
| 272 |
+type FakeGIT struct {
|
|
| 273 |
+ *httptest.Server |
|
| 274 |
+ Root string |
|
| 275 |
+ RepoURL string |
|
| 276 |
+} |
|
| 277 |
+ |
|
| 278 |
+func (g *FakeGIT) Close() {
|
|
| 279 |
+ g.Server.Close() |
|
| 280 |
+ os.RemoveAll(g.Root) |
|
| 281 |
+} |
|
| 282 |
+ |
|
| 283 |
+func fakeGIT(name string, files map[string]string) (*FakeGIT, error) {
|
|
| 284 |
+ tmp, err := ioutil.TempDir("", "fake-git-repo")
|
|
| 285 |
+ if err != nil {
|
|
| 286 |
+ return nil, err |
|
| 287 |
+ } |
|
| 288 |
+ ctx := &FakeContext{tmp}
|
|
| 289 |
+ for file, content := range files {
|
|
| 290 |
+ if err := ctx.Add(file, content); err != nil {
|
|
| 291 |
+ ctx.Close() |
|
| 292 |
+ return nil, err |
|
| 293 |
+ } |
|
| 294 |
+ } |
|
| 295 |
+ defer ctx.Close() |
|
| 296 |
+ curdir, err := os.Getwd() |
|
| 297 |
+ if err != nil {
|
|
| 298 |
+ return nil, err |
|
| 299 |
+ } |
|
| 300 |
+ defer os.Chdir(curdir) |
|
| 301 |
+ |
|
| 302 |
+ if output, err := exec.Command("git", "init", ctx.Dir).CombinedOutput(); err != nil {
|
|
| 303 |
+ return nil, fmt.Errorf("Error trying to init repo: %s (%s)", err, output)
|
|
| 304 |
+ } |
|
| 305 |
+ err = os.Chdir(ctx.Dir) |
|
| 306 |
+ if err != nil {
|
|
| 307 |
+ return nil, err |
|
| 308 |
+ } |
|
| 309 |
+ if output, err := exec.Command("git", "add", "*").CombinedOutput(); err != nil {
|
|
| 310 |
+ return nil, fmt.Errorf("Error trying to add files to repo: %s (%s)", err, output)
|
|
| 311 |
+ } |
|
| 312 |
+ if output, err := exec.Command("git", "commit", "-a", "-m", "Initial commit").CombinedOutput(); err != nil {
|
|
| 313 |
+ return nil, fmt.Errorf("Error trying to commit to repo: %s (%s)", err, output)
|
|
| 314 |
+ } |
|
| 315 |
+ |
|
| 316 |
+ root, err := ioutil.TempDir("", "docker-test-git-repo")
|
|
| 317 |
+ if err != nil {
|
|
| 318 |
+ return nil, err |
|
| 319 |
+ } |
|
| 320 |
+ repoPath := filepath.Join(root, name+".git") |
|
| 321 |
+ if output, err := exec.Command("git", "clone", "--bare", ctx.Dir, repoPath).CombinedOutput(); err != nil {
|
|
| 322 |
+ os.RemoveAll(root) |
|
| 323 |
+ return nil, fmt.Errorf("Error trying to clone --bare: %s (%s)", err, output)
|
|
| 324 |
+ } |
|
| 325 |
+ err = os.Chdir(repoPath) |
|
| 326 |
+ if err != nil {
|
|
| 327 |
+ os.RemoveAll(root) |
|
| 328 |
+ return nil, err |
|
| 329 |
+ } |
|
| 330 |
+ if output, err := exec.Command("git", "update-server-info").CombinedOutput(); err != nil {
|
|
| 331 |
+ os.RemoveAll(root) |
|
| 332 |
+ return nil, fmt.Errorf("Error trying to git update-server-info: %s (%s)", err, output)
|
|
| 333 |
+ } |
|
| 334 |
+ err = os.Chdir(curdir) |
|
| 335 |
+ if err != nil {
|
|
| 336 |
+ os.RemoveAll(root) |
|
| 337 |
+ return nil, err |
|
| 338 |
+ } |
|
| 339 |
+ handler := http.FileServer(http.Dir(root)) |
|
| 340 |
+ server := httptest.NewServer(handler) |
|
| 341 |
+ return &FakeGIT{
|
|
| 342 |
+ Server: server, |
|
| 343 |
+ Root: root, |
|
| 344 |
+ RepoURL: fmt.Sprintf("%s/%s.git", server.URL, name),
|
|
| 345 |
+ }, nil |
|
| 346 |
+} |