Browse code

Test on building from GIT url

Also I added fake git server to utils
Docker-DCO-1.1-Signed-off-by: Alexandr Morozov <lk4d4math@gmail.com> (github: LK4D4)

Alexandr Morozov authored on 2014/07/24 16:19:16
Showing 2 changed files
... ...
@@ -1777,3 +1777,32 @@ func TestBuildAddTar(t *testing.T) {
1777 1777
 	}
1778 1778
 	logDone("build - ADD tar")
1779 1779
 }
1780
+
1781
+func TestBuildFromGIT(t *testing.T) {
1782
+	name := "testbuildfromgit"
1783
+	defer deleteImages(name)
1784
+	git, err := fakeGIT("repo", map[string]string{
1785
+		"Dockerfile": `FROM busybox
1786
+					ADD first /first
1787
+					RUN [ -f /first ]
1788
+					MAINTAINER docker`,
1789
+		"first": "test git data",
1790
+	})
1791
+	if err != nil {
1792
+		t.Fatal(err)
1793
+	}
1794
+	defer git.Close()
1795
+
1796
+	_, err = buildImageFromPath(name, git.RepoURL, true)
1797
+	if err != nil {
1798
+		t.Fatal(err)
1799
+	}
1800
+	res, err := inspectField(name, "Author")
1801
+	if err != nil {
1802
+		t.Fatal(err)
1803
+	}
1804
+	if res != "docker" {
1805
+		t.Fatal("Maintainer should be docker, got %s", res)
1806
+	}
1807
+	logDone("build - build from GIT")
1808
+}
... ...
@@ -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
+}