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