Merge pull request #32713 from vdemeester/integration-cli-move-fakegit

[integration] Move fakegit to its own package in cli
This commit is contained in:
Sebastiaan van Stijn 2017-04-19 20:30:47 -05:00 committed by GitHub
commit 7936a962dc
4 changed files with 134 additions and 109 deletions

View file

@ -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", "fake.user@example.com").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),
}
}

View file

@ -10,6 +10,7 @@ import (
"github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli/build/fakecontext"
"github.com/docker/docker/integration-cli/cli/build/fakegit"
"github.com/docker/docker/integration-cli/cli/build/fakestorage"
"github.com/docker/docker/integration-cli/request"
"github.com/docker/docker/pkg/testutil"
@ -134,7 +135,7 @@ RUN echo 'right'
}
func (s *DockerSuite) TestBuildAPILowerDockerfile(c *check.C) {
git := newFakeGit(c, "repo", map[string]string{
git := fakegit.New(c, "repo", map[string]string{
"dockerfile": `FROM busybox
RUN echo from dockerfile`,
}, false)
@ -152,7 +153,7 @@ RUN echo from dockerfile`,
}
func (s *DockerSuite) TestBuildAPIBuildGitWithF(c *check.C) {
git := newFakeGit(c, "repo", map[string]string{
git := fakegit.New(c, "repo", map[string]string{
"baz": `FROM busybox
RUN echo from baz`,
"Dockerfile": `FROM busybox
@ -174,7 +175,7 @@ RUN echo from Dockerfile`,
func (s *DockerSuite) TestBuildAPIDoubleDockerfile(c *check.C) {
testRequires(c, UnixCli) // dockerfile overwrites Dockerfile on Windows
git := newFakeGit(c, "repo", map[string]string{
git := fakegit.New(c, "repo", map[string]string{
"Dockerfile": `FROM busybox
RUN echo from Dockerfile`,
"dockerfile": `FROM busybox

View file

@ -21,6 +21,7 @@ import (
"github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/integration-cli/cli/build"
"github.com/docker/docker/integration-cli/cli/build/fakecontext"
"github.com/docker/docker/integration-cli/cli/build/fakegit"
"github.com/docker/docker/integration-cli/cli/build/fakestorage"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/stringutils"
@ -3022,7 +3023,7 @@ func (s *DockerSuite) TestBuildAddTarXzGz(c *check.C) {
func (s *DockerSuite) TestBuildFromGit(c *check.C) {
name := "testbuildfromgit"
git := newFakeGit(c, "repo", map[string]string{
git := fakegit.New(c, "repo", map[string]string{
"Dockerfile": `FROM busybox
ADD first /first
RUN [ -f /first ]
@ -3041,7 +3042,7 @@ func (s *DockerSuite) TestBuildFromGit(c *check.C) {
func (s *DockerSuite) TestBuildFromGitWithContext(c *check.C) {
name := "testbuildfromgit"
git := newFakeGit(c, "repo", map[string]string{
git := fakegit.New(c, "repo", map[string]string{
"docker/Dockerfile": `FROM busybox
ADD first /first
RUN [ -f /first ]
@ -3060,7 +3061,7 @@ func (s *DockerSuite) TestBuildFromGitWithContext(c *check.C) {
func (s *DockerSuite) TestBuildFromGitwithF(c *check.C) {
name := "testbuildfromgitwithf"
git := newFakeGit(c, "repo", map[string]string{
git := fakegit.New(c, "repo", map[string]string{
"myApp/myDockerfile": `FROM busybox
RUN echo hi from Dockerfile`,
}, true)
@ -3425,7 +3426,7 @@ func (s *DockerSuite) TestBuildNotVerboseSuccess(c *check.C) {
{
Name: "quiet_build_git_success",
BuildFunc: func(name string) *icmd.Result {
git := newFakeGit(c, "repo", map[string]string{
git := fakegit.New(c, "repo", map[string]string{
"Dockerfile": "FROM busybox",
}, true)
return buildImage(name, buildFlags, build.WithContextPath(git.RepoURL))

View file

@ -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", "fake.user@example.com").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.