mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
integ-cli: use httpserver container for fakeGIT
This change enables `fakeGIT()` to use the new `fakeStorage` server which is automatically starting a container on the remote test daemon machine using the git repo directory (when requested). Fixes the following tests: - `TestBuildApiLowerDockerfile` - `TestBuildApiBuildGitWithF` - `TestBuildApiDoubleDockerfile` (skipped on windows: NTFS case-insensitive) - `TestBuildFromGIT` (still needs local server) Signed-off-by: Ahmet Alp Balkan <ahmetalpbalkan@gmail.com>
This commit is contained in:
parent
7c4951d552
commit
44ffb199d0
3 changed files with 90 additions and 46 deletions
|
@ -382,7 +382,7 @@ func TestBuildApiLowerDockerfile(t *testing.T) {
|
||||||
git, err := fakeGIT("repo", map[string]string{
|
git, err := fakeGIT("repo", map[string]string{
|
||||||
"dockerfile": `FROM busybox
|
"dockerfile": `FROM busybox
|
||||||
RUN echo from dockerfile`,
|
RUN echo from dockerfile`,
|
||||||
})
|
}, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -407,7 +407,7 @@ func TestBuildApiBuildGitWithF(t *testing.T) {
|
||||||
RUN echo from baz`,
|
RUN echo from baz`,
|
||||||
"Dockerfile": `FROM busybox
|
"Dockerfile": `FROM busybox
|
||||||
RUN echo from Dockerfile`,
|
RUN echo from Dockerfile`,
|
||||||
})
|
}, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -428,12 +428,13 @@ RUN echo from Dockerfile`,
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildApiDoubleDockerfile(t *testing.T) {
|
func TestBuildApiDoubleDockerfile(t *testing.T) {
|
||||||
|
testRequires(t, UnixCli) // dockerfile overwrites Dockerfile on Windows
|
||||||
git, err := fakeGIT("repo", map[string]string{
|
git, err := fakeGIT("repo", map[string]string{
|
||||||
"Dockerfile": `FROM busybox
|
"Dockerfile": `FROM busybox
|
||||||
RUN echo from Dockerfile`,
|
RUN echo from Dockerfile`,
|
||||||
"dockerfile": `FROM busybox
|
"dockerfile": `FROM busybox
|
||||||
RUN echo from dockerfile`,
|
RUN echo from dockerfile`,
|
||||||
})
|
}, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3940,7 +3940,7 @@ RUN cat /existing-directory-trailing-slash/test/foo | grep Hi`
|
||||||
if err := ioutil.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte(dockerfile), 0644); err != nil {
|
if err := ioutil.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte(dockerfile), 0644); err != nil {
|
||||||
t.Fatalf("failed to open destination dockerfile: %v", err)
|
t.Fatalf("failed to open destination dockerfile: %v", err)
|
||||||
}
|
}
|
||||||
return &FakeContext{Dir: tmpDir}
|
return fakeContextFromDir(tmpDir)
|
||||||
}()
|
}()
|
||||||
defer ctx.Close()
|
defer ctx.Close()
|
||||||
|
|
||||||
|
@ -3991,7 +3991,7 @@ func TestBuildAddTarXz(t *testing.T) {
|
||||||
if err := ioutil.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte(dockerfile), 0644); err != nil {
|
if err := ioutil.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte(dockerfile), 0644); err != nil {
|
||||||
t.Fatalf("failed to open destination dockerfile: %v", err)
|
t.Fatalf("failed to open destination dockerfile: %v", err)
|
||||||
}
|
}
|
||||||
return &FakeContext{Dir: tmpDir}
|
return fakeContextFromDir(tmpDir)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
defer ctx.Close()
|
defer ctx.Close()
|
||||||
|
@ -4051,7 +4051,7 @@ func TestBuildAddTarXzGz(t *testing.T) {
|
||||||
if err := ioutil.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte(dockerfile), 0644); err != nil {
|
if err := ioutil.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte(dockerfile), 0644); err != nil {
|
||||||
t.Fatalf("failed to open destination dockerfile: %v", err)
|
t.Fatalf("failed to open destination dockerfile: %v", err)
|
||||||
}
|
}
|
||||||
return &FakeContext{Dir: tmpDir}
|
return fakeContextFromDir(tmpDir)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
defer ctx.Close()
|
defer ctx.Close()
|
||||||
|
@ -4072,7 +4072,7 @@ func TestBuildFromGIT(t *testing.T) {
|
||||||
RUN [ -f /first ]
|
RUN [ -f /first ]
|
||||||
MAINTAINER docker`,
|
MAINTAINER docker`,
|
||||||
"first": "test git data",
|
"first": "test git data",
|
||||||
})
|
}, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -4563,7 +4563,7 @@ func TestBuildSymlinkBreakout(t *testing.T) {
|
||||||
})
|
})
|
||||||
w.Close()
|
w.Close()
|
||||||
f.Close()
|
f.Close()
|
||||||
if _, err := buildImageFromContext(name, &FakeContext{Dir: ctx}, false); err != nil {
|
if _, err := buildImageFromContext(name, fakeContextFromDir(ctx), false); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if _, err := os.Lstat(filepath.Join(tmpdir, "inject")); err == nil {
|
if _, err := os.Lstat(filepath.Join(tmpdir, "inject")); err == nil {
|
||||||
|
|
|
@ -559,7 +559,11 @@ func (f *FakeContext) Close() error {
|
||||||
return os.RemoveAll(f.Dir)
|
return os.RemoveAll(f.Dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fakeContext(dockerfile string, files map[string]string) (*FakeContext, error) {
|
func fakeContextFromDir(dir string) *FakeContext {
|
||||||
|
return &FakeContext{dir}
|
||||||
|
}
|
||||||
|
|
||||||
|
func fakeContextWithFiles(files map[string]string) (*FakeContext, error) {
|
||||||
tmp, err := ioutil.TempDir("", "fake-context")
|
tmp, err := ioutil.TempDir("", "fake-context")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -567,15 +571,32 @@ func fakeContext(dockerfile string, files map[string]string) (*FakeContext, erro
|
||||||
if err := os.Chmod(tmp, 0755); err != nil {
|
if err := os.Chmod(tmp, 0755); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
ctx := &FakeContext{tmp}
|
|
||||||
|
ctx := fakeContextFromDir(tmp)
|
||||||
for file, content := range files {
|
for file, content := range files {
|
||||||
if err := ctx.Add(file, content); err != nil {
|
if err := ctx.Add(file, content); err != nil {
|
||||||
ctx.Close()
|
ctx.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return ctx, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func fakeContextAddDockerfile(ctx *FakeContext, dockerfile string) error {
|
||||||
if err := ctx.Add("Dockerfile", dockerfile); err != nil {
|
if err := ctx.Add("Dockerfile", dockerfile); err != nil {
|
||||||
ctx.Close()
|
ctx.Close()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func fakeContext(dockerfile string, files map[string]string) (*FakeContext, error) {
|
||||||
|
ctx, err := fakeContextWithFiles(files)
|
||||||
|
if err != nil {
|
||||||
|
ctx.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := fakeContextAddDockerfile(ctx, dockerfile); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return ctx, nil
|
return ctx, nil
|
||||||
|
@ -591,10 +612,19 @@ type FakeStorage interface {
|
||||||
|
|
||||||
// fakeStorage returns either a local or remote (at daemon machine) file server
|
// fakeStorage returns either a local or remote (at daemon machine) file server
|
||||||
func fakeStorage(files map[string]string) (FakeStorage, error) {
|
func fakeStorage(files map[string]string) (FakeStorage, error) {
|
||||||
if isLocalDaemon {
|
ctx, err := fakeContextWithFiles(files)
|
||||||
return newLocalFakeStorage(files)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
return newRemoteFileServer(files)
|
return fakeStorageWithContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// fakeStorageWithContext returns either a local or remote (at daemon machine) file server
|
||||||
|
func fakeStorageWithContext(ctx *FakeContext) (FakeStorage, error) {
|
||||||
|
if isLocalDaemon {
|
||||||
|
return newLocalFakeStorage(ctx)
|
||||||
|
}
|
||||||
|
return newRemoteFileServer(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
// localFileStorage is a file storage on the running machine
|
// localFileStorage is a file storage on the running machine
|
||||||
|
@ -616,18 +646,7 @@ func (s *localFileStorage) Close() error {
|
||||||
return s.FakeContext.Close()
|
return s.FakeContext.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func newLocalFakeStorage(files map[string]string) (*localFileStorage, error) {
|
func newLocalFakeStorage(ctx *FakeContext) (*localFileStorage, error) {
|
||||||
tmp, err := ioutil.TempDir("", "fake-storage")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
ctx := &FakeContext{tmp}
|
|
||||||
for file, content := range files {
|
|
||||||
if err := ctx.Add(file, content); err != nil {
|
|
||||||
ctx.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
handler := http.FileServer(http.Dir(ctx.Dir))
|
handler := http.FileServer(http.Dir(ctx.Dir))
|
||||||
server := httptest.NewServer(handler)
|
server := httptest.NewServer(handler)
|
||||||
return &localFileStorage{
|
return &localFileStorage{
|
||||||
|
@ -671,15 +690,17 @@ func (f *remoteFileServer) Close() error {
|
||||||
return deleteContainer(f.container)
|
return deleteContainer(f.container)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRemoteFileServer(files map[string]string) (*remoteFileServer, error) {
|
func newRemoteFileServer(ctx *FakeContext) (*remoteFileServer, error) {
|
||||||
var (
|
var (
|
||||||
image = fmt.Sprintf("fileserver-img-%s", strings.ToLower(makeRandomString(10)))
|
image = fmt.Sprintf("fileserver-img-%s", strings.ToLower(makeRandomString(10)))
|
||||||
container = fmt.Sprintf("fileserver-cnt-%s", strings.ToLower(makeRandomString(10)))
|
container = fmt.Sprintf("fileserver-cnt-%s", strings.ToLower(makeRandomString(10)))
|
||||||
)
|
)
|
||||||
|
|
||||||
// Build the image
|
// Build the image
|
||||||
ctx, err := fakeContext(`FROM httpserver
|
if err := fakeContextAddDockerfile(ctx, `FROM httpserver
|
||||||
COPY . /static`, files)
|
COPY . /static`); err != nil {
|
||||||
|
return nil, fmt.Errorf("Cannot add Dockerfile to context: %v", err)
|
||||||
|
}
|
||||||
if _, err := buildImageFromContext(image, ctx, false); err != nil {
|
if _, err := buildImageFromContext(image, ctx, false); err != nil {
|
||||||
return nil, fmt.Errorf("failed building file storage container image: %v", err)
|
return nil, fmt.Errorf("failed building file storage container image: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -831,29 +852,40 @@ func buildImageFromPath(name, path string, useCache bool) (string, error) {
|
||||||
return getIDByName(name)
|
return getIDByName(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
type FakeGIT struct {
|
type GitServer interface {
|
||||||
|
URL() string
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
|
|
||||||
|
type localGitServer struct {
|
||||||
*httptest.Server
|
*httptest.Server
|
||||||
Root string
|
}
|
||||||
|
|
||||||
|
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
|
RepoURL string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *FakeGIT) Close() {
|
func (g *FakeGIT) Close() {
|
||||||
g.Server.Close()
|
g.server.Close()
|
||||||
os.RemoveAll(g.Root)
|
os.RemoveAll(g.root)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fakeGIT(name string, files map[string]string) (*FakeGIT, error) {
|
func fakeGIT(name string, files map[string]string, enforceLocalServer bool) (*FakeGIT, error) {
|
||||||
tmp, err := ioutil.TempDir("", "fake-git-repo")
|
ctx, err := fakeContextWithFiles(files)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
ctx := &FakeContext{tmp}
|
|
||||||
for file, content := range files {
|
|
||||||
if err := ctx.Add(file, content); err != nil {
|
|
||||||
ctx.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
defer ctx.Close()
|
defer ctx.Close()
|
||||||
curdir, err := os.Getwd()
|
curdir, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -904,12 +936,23 @@ func fakeGIT(name string, files map[string]string) (*FakeGIT, error) {
|
||||||
os.RemoveAll(root)
|
os.RemoveAll(root)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
handler := http.FileServer(http.Dir(root))
|
|
||||||
server := httptest.NewServer(handler)
|
var server GitServer
|
||||||
|
if !enforceLocalServer {
|
||||||
|
// use fakeStorage server, which might be local or remote (at test daemon)
|
||||||
|
server, err = fakeStorageWithContext(fakeContextFromDir(root))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("cannot start fake storage: %v", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// always start a local http server on CLI test machin
|
||||||
|
httpServer := httptest.NewServer(http.FileServer(http.Dir(root)))
|
||||||
|
server = &localGitServer{httpServer}
|
||||||
|
}
|
||||||
return &FakeGIT{
|
return &FakeGIT{
|
||||||
Server: server,
|
root: root,
|
||||||
Root: root,
|
server: server,
|
||||||
RepoURL: fmt.Sprintf("%s/%s.git", server.URL, name),
|
RepoURL: fmt.Sprintf("%s/%s.git", server.URL(), name),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue