github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/integration-cli/cli/build/fakegit/fakegit.go (about)

     1  package fakegit
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"os"
     9  	"os/exec"
    10  	"path/filepath"
    11  
    12  	"github.com/docker/docker/integration-cli/cli/build/fakecontext"
    13  	"github.com/docker/docker/integration-cli/cli/build/fakestorage"
    14  )
    15  
    16  type testingT interface {
    17  	logT
    18  	Fatal(args ...interface{})
    19  	Fatalf(string, ...interface{})
    20  }
    21  
    22  type logT interface {
    23  	Logf(string, ...interface{})
    24  }
    25  
    26  type gitServer interface {
    27  	URL() string
    28  	Close() error
    29  }
    30  
    31  type localGitServer struct {
    32  	*httptest.Server
    33  }
    34  
    35  func (r *localGitServer) Close() error {
    36  	r.Server.Close()
    37  	return nil
    38  }
    39  
    40  func (r *localGitServer) URL() string {
    41  	return r.Server.URL
    42  }
    43  
    44  // FakeGit is a fake git server
    45  type FakeGit struct {
    46  	root    string
    47  	server  gitServer
    48  	RepoURL string
    49  }
    50  
    51  // Close closes the server, implements Closer interface
    52  func (g *FakeGit) Close() {
    53  	g.server.Close()
    54  	os.RemoveAll(g.root)
    55  }
    56  
    57  // New create a fake git server that can be used for git related tests
    58  func New(c testingT, name string, files map[string]string, enforceLocalServer bool) *FakeGit {
    59  	ctx := fakecontext.New(c, "", fakecontext.WithFiles(files))
    60  	defer ctx.Close()
    61  	curdir, err := os.Getwd()
    62  	if err != nil {
    63  		c.Fatal(err)
    64  	}
    65  	defer os.Chdir(curdir)
    66  
    67  	if output, err := exec.Command("git", "init", ctx.Dir).CombinedOutput(); err != nil {
    68  		c.Fatalf("error trying to init repo: %s (%s)", err, output)
    69  	}
    70  	err = os.Chdir(ctx.Dir)
    71  	if err != nil {
    72  		c.Fatal(err)
    73  	}
    74  	if output, err := exec.Command("git", "config", "user.name", "Fake User").CombinedOutput(); err != nil {
    75  		c.Fatalf("error trying to set 'user.name': %s (%s)", err, output)
    76  	}
    77  	if output, err := exec.Command("git", "config", "user.email", "fake.user@example.com").CombinedOutput(); err != nil {
    78  		c.Fatalf("error trying to set 'user.email': %s (%s)", err, output)
    79  	}
    80  	if output, err := exec.Command("git", "add", "*").CombinedOutput(); err != nil {
    81  		c.Fatalf("error trying to add files to repo: %s (%s)", err, output)
    82  	}
    83  	if output, err := exec.Command("git", "commit", "-a", "-m", "Initial commit").CombinedOutput(); err != nil {
    84  		c.Fatalf("error trying to commit to repo: %s (%s)", err, output)
    85  	}
    86  
    87  	root, err := ioutil.TempDir("", "docker-test-git-repo")
    88  	if err != nil {
    89  		c.Fatal(err)
    90  	}
    91  	repoPath := filepath.Join(root, name+".git")
    92  	if output, err := exec.Command("git", "clone", "--bare", ctx.Dir, repoPath).CombinedOutput(); err != nil {
    93  		os.RemoveAll(root)
    94  		c.Fatalf("error trying to clone --bare: %s (%s)", err, output)
    95  	}
    96  	err = os.Chdir(repoPath)
    97  	if err != nil {
    98  		os.RemoveAll(root)
    99  		c.Fatal(err)
   100  	}
   101  	if output, err := exec.Command("git", "update-server-info").CombinedOutput(); err != nil {
   102  		os.RemoveAll(root)
   103  		c.Fatalf("error trying to git update-server-info: %s (%s)", err, output)
   104  	}
   105  	err = os.Chdir(curdir)
   106  	if err != nil {
   107  		os.RemoveAll(root)
   108  		c.Fatal(err)
   109  	}
   110  
   111  	var server gitServer
   112  	if !enforceLocalServer {
   113  		// use fakeStorage server, which might be local or remote (at test daemon)
   114  		server = fakestorage.New(c, root)
   115  	} else {
   116  		// always start a local http server on CLI test machine
   117  		httpServer := httptest.NewServer(http.FileServer(http.Dir(root)))
   118  		server = &localGitServer{httpServer}
   119  	}
   120  	return &FakeGit{
   121  		root:    root,
   122  		server:  server,
   123  		RepoURL: fmt.Sprintf("%s/%s.git", server.URL(), name),
   124  	}
   125  }