-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request moby#32713 from vdemeester/integration-cli-move-fa…
…kegit [integration] Move fakegit to its own package in cli
- Loading branch information
Showing
4 changed files
with
134 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package fakegit | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
"github.com/docker/docker/integration-cli/cli/build/fakecontext" | ||
"github.com/docker/docker/integration-cli/cli/build/fakestorage" | ||
) | ||
|
||
type testingT interface { | ||
logT | ||
Fatal(args ...interface{}) | ||
Fatalf(string, ...interface{}) | ||
} | ||
|
||
type logT interface { | ||
Logf(string, ...interface{}) | ||
} | ||
|
||
type gitServer interface { | ||
URL() string | ||
Close() error | ||
} | ||
|
||
type localGitServer struct { | ||
*httptest.Server | ||
} | ||
|
||
func (r *localGitServer) Close() error { | ||
r.Server.Close() | ||
return nil | ||
} | ||
|
||
func (r *localGitServer) URL() string { | ||
return r.Server.URL | ||
} | ||
|
||
// FakeGit is a fake git server | ||
type FakeGit struct { | ||
root string | ||
server gitServer | ||
RepoURL string | ||
} | ||
|
||
// Close closes the server, implements Closer interface | ||
func (g *FakeGit) Close() { | ||
g.server.Close() | ||
os.RemoveAll(g.root) | ||
} | ||
|
||
// New create a fake git server that can be used for git related tests | ||
func New(c testingT, name string, files map[string]string, enforceLocalServer bool) *FakeGit { | ||
ctx := fakecontext.New(c, "", fakecontext.WithFiles(files)) | ||
defer ctx.Close() | ||
curdir, err := os.Getwd() | ||
if err != nil { | ||
c.Fatal(err) | ||
} | ||
defer os.Chdir(curdir) | ||
|
||
if output, err := exec.Command("git", "init", ctx.Dir).CombinedOutput(); err != nil { | ||
c.Fatalf("error trying to init repo: %s (%s)", err, output) | ||
} | ||
err = os.Chdir(ctx.Dir) | ||
if err != nil { | ||
c.Fatal(err) | ||
} | ||
if output, err := exec.Command("git", "config", "user.name", "Fake User").CombinedOutput(); err != nil { | ||
c.Fatalf("error trying to set 'user.name': %s (%s)", err, output) | ||
} | ||
if output, err := exec.Command("git", "config", "user.email", "[email protected]").CombinedOutput(); err != nil { | ||
c.Fatalf("error trying to set 'user.email': %s (%s)", err, output) | ||
} | ||
if output, err := exec.Command("git", "add", "*").CombinedOutput(); err != nil { | ||
c.Fatalf("error trying to add files to repo: %s (%s)", err, output) | ||
} | ||
if output, err := exec.Command("git", "commit", "-a", "-m", "Initial commit").CombinedOutput(); err != nil { | ||
c.Fatalf("error trying to commit to repo: %s (%s)", err, output) | ||
} | ||
|
||
root, err := ioutil.TempDir("", "docker-test-git-repo") | ||
if err != nil { | ||
c.Fatal(err) | ||
} | ||
repoPath := filepath.Join(root, name+".git") | ||
if output, err := exec.Command("git", "clone", "--bare", ctx.Dir, repoPath).CombinedOutput(); err != nil { | ||
os.RemoveAll(root) | ||
c.Fatalf("error trying to clone --bare: %s (%s)", err, output) | ||
} | ||
err = os.Chdir(repoPath) | ||
if err != nil { | ||
os.RemoveAll(root) | ||
c.Fatal(err) | ||
} | ||
if output, err := exec.Command("git", "update-server-info").CombinedOutput(); err != nil { | ||
os.RemoveAll(root) | ||
c.Fatalf("error trying to git update-server-info: %s (%s)", err, output) | ||
} | ||
err = os.Chdir(curdir) | ||
if err != nil { | ||
os.RemoveAll(root) | ||
c.Fatal(err) | ||
} | ||
|
||
var server gitServer | ||
if !enforceLocalServer { | ||
// use fakeStorage server, which might be local or remote (at test daemon) | ||
server = fakestorage.New(c, root) | ||
} else { | ||
// always start a local http server on CLI test machine | ||
httpServer := httptest.NewServer(http.FileServer(http.Dir(root))) | ||
server = &localGitServer{httpServer} | ||
} | ||
return &FakeGit{ | ||
root: root, | ||
server: server, | ||
RepoURL: fmt.Sprintf("%s/%s.git", server.URL(), name), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,7 @@ import ( | |
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"os/exec" | ||
"path" | ||
"path/filepath" | ||
"strconv" | ||
|
@@ -19,8 +17,6 @@ import ( | |
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/integration-cli/checker" | ||
"github.com/docker/docker/integration-cli/cli" | ||
"github.com/docker/docker/integration-cli/cli/build/fakecontext" | ||
"github.com/docker/docker/integration-cli/cli/build/fakestorage" | ||
"github.com/docker/docker/integration-cli/daemon" | ||
"github.com/docker/docker/integration-cli/registry" | ||
"github.com/docker/docker/integration-cli/request" | ||
|
@@ -211,104 +207,6 @@ func trustedBuild(cmd *icmd.Cmd) func() { | |
return nil | ||
} | ||
|
||
type gitServer interface { | ||
URL() string | ||
Close() error | ||
} | ||
|
||
type localGitServer struct { | ||
*httptest.Server | ||
} | ||
|
||
func (r *localGitServer) Close() error { | ||
r.Server.Close() | ||
return nil | ||
} | ||
|
||
func (r *localGitServer) URL() string { | ||
return r.Server.URL | ||
} | ||
|
||
type fakeGit struct { | ||
root string | ||
server gitServer | ||
RepoURL string | ||
} | ||
|
||
func (g *fakeGit) Close() { | ||
g.server.Close() | ||
os.RemoveAll(g.root) | ||
} | ||
|
||
func newFakeGit(c *check.C, name string, files map[string]string, enforceLocalServer bool) *fakeGit { | ||
ctx := fakecontext.New(c, "", fakecontext.WithFiles(files)) | ||
defer ctx.Close() | ||
curdir, err := os.Getwd() | ||
if err != nil { | ||
c.Fatal(err) | ||
} | ||
defer os.Chdir(curdir) | ||
|
||
if output, err := exec.Command("git", "init", ctx.Dir).CombinedOutput(); err != nil { | ||
c.Fatalf("error trying to init repo: %s (%s)", err, output) | ||
} | ||
err = os.Chdir(ctx.Dir) | ||
if err != nil { | ||
c.Fatal(err) | ||
} | ||
if output, err := exec.Command("git", "config", "user.name", "Fake User").CombinedOutput(); err != nil { | ||
c.Fatalf("error trying to set 'user.name': %s (%s)", err, output) | ||
} | ||
if output, err := exec.Command("git", "config", "user.email", "[email protected]").CombinedOutput(); err != nil { | ||
c.Fatalf("error trying to set 'user.email': %s (%s)", err, output) | ||
} | ||
if output, err := exec.Command("git", "add", "*").CombinedOutput(); err != nil { | ||
c.Fatalf("error trying to add files to repo: %s (%s)", err, output) | ||
} | ||
if output, err := exec.Command("git", "commit", "-a", "-m", "Initial commit").CombinedOutput(); err != nil { | ||
c.Fatalf("error trying to commit to repo: %s (%s)", err, output) | ||
} | ||
|
||
root, err := ioutil.TempDir("", "docker-test-git-repo") | ||
if err != nil { | ||
c.Fatal(err) | ||
} | ||
repoPath := filepath.Join(root, name+".git") | ||
if output, err := exec.Command("git", "clone", "--bare", ctx.Dir, repoPath).CombinedOutput(); err != nil { | ||
os.RemoveAll(root) | ||
c.Fatalf("error trying to clone --bare: %s (%s)", err, output) | ||
} | ||
err = os.Chdir(repoPath) | ||
if err != nil { | ||
os.RemoveAll(root) | ||
c.Fatal(err) | ||
} | ||
if output, err := exec.Command("git", "update-server-info").CombinedOutput(); err != nil { | ||
os.RemoveAll(root) | ||
c.Fatalf("error trying to git update-server-info: %s (%s)", err, output) | ||
} | ||
err = os.Chdir(curdir) | ||
if err != nil { | ||
os.RemoveAll(root) | ||
c.Fatal(err) | ||
} | ||
|
||
var server gitServer | ||
if !enforceLocalServer { | ||
// use fakeStorage server, which might be local or remote (at test daemon) | ||
server = fakestorage.New(c, root) | ||
} else { | ||
// always start a local http server on CLI test machine | ||
httpServer := httptest.NewServer(http.FileServer(http.Dir(root))) | ||
server = &localGitServer{httpServer} | ||
} | ||
return &fakeGit{ | ||
root: root, | ||
server: server, | ||
RepoURL: fmt.Sprintf("%s/%s.git", server.URL(), name), | ||
} | ||
} | ||
|
||
// Write `content` to the file at path `dst`, creating it if necessary, | ||
// as well as any missing directories. | ||
// The file is truncated if it already exists. | ||
|