2019-08-29 16:52:40 -04:00
|
|
|
package fakecontext // import "github.com/docker/docker/testutil/fakecontext"
|
2017-04-10 08:42:21 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-06-02 16:06:14 -04:00
|
|
|
"io"
|
2017-04-10 08:42:21 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-09-23 07:54:51 -04:00
|
|
|
"testing"
|
2017-06-02 16:06:14 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
2017-04-10 08:42:21 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// New creates a fake build context
|
2019-09-23 07:54:51 -04:00
|
|
|
func New(t testing.TB, dir string, modifiers ...func(*Fake) error) *Fake {
|
2019-09-23 08:06:27 -04:00
|
|
|
t.Helper()
|
2017-04-10 08:42:21 -04:00
|
|
|
fakeContext := &Fake{Dir: dir}
|
|
|
|
if dir == "" {
|
|
|
|
if err := newDir(fakeContext); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, modifier := range modifiers {
|
|
|
|
if err := modifier(fakeContext); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fakeContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDir(fake *Fake) error {
|
|
|
|
tmp, err := ioutil.TempDir("", "fake-context")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Chmod(tmp, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fake.Dir = tmp
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithFile adds the specified file (with content) in the build context
|
|
|
|
func WithFile(name, content string) func(*Fake) error {
|
|
|
|
return func(ctx *Fake) error {
|
|
|
|
return ctx.Add(name, content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDockerfile adds the specified content as Dockerfile in the build context
|
|
|
|
func WithDockerfile(content string) func(*Fake) error {
|
|
|
|
return WithFile("Dockerfile", content)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithFiles adds the specified files in the build context, content is a string
|
|
|
|
func WithFiles(files map[string]string) func(*Fake) error {
|
|
|
|
return func(fakeContext *Fake) error {
|
|
|
|
for file, content := range files {
|
|
|
|
if err := fakeContext.Add(file, content); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithBinaryFiles adds the specified files in the build context, content is binary
|
|
|
|
func WithBinaryFiles(files map[string]*bytes.Buffer) func(*Fake) error {
|
|
|
|
return func(fakeContext *Fake) error {
|
|
|
|
for file, content := range files {
|
2018-04-16 08:39:13 -04:00
|
|
|
if err := fakeContext.Add(file, content.String()); err != nil {
|
2017-04-10 08:42:21 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fake creates directories that can be used as a build context
|
|
|
|
type Fake struct {
|
|
|
|
Dir string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a file at a path, creating directories where necessary
|
|
|
|
func (f *Fake) Add(file, content string) error {
|
|
|
|
return f.addFile(file, []byte(content))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Fake) addFile(file string, content []byte) error {
|
|
|
|
fp := filepath.Join(f.Dir, filepath.FromSlash(file))
|
|
|
|
dirpath := filepath.Dir(fp)
|
|
|
|
if dirpath != "." {
|
|
|
|
if err := os.MkdirAll(dirpath, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ioutil.WriteFile(fp, content, 0644)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete a file at a path
|
|
|
|
func (f *Fake) Delete(file string) error {
|
|
|
|
fp := filepath.Join(f.Dir, filepath.FromSlash(file))
|
|
|
|
return os.RemoveAll(fp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close deletes the context
|
|
|
|
func (f *Fake) Close() error {
|
|
|
|
return os.RemoveAll(f.Dir)
|
|
|
|
}
|
2017-06-02 16:06:14 -04:00
|
|
|
|
|
|
|
// AsTarReader returns a ReadCloser with the contents of Dir as a tar archive.
|
2019-09-23 07:54:51 -04:00
|
|
|
func (f *Fake) AsTarReader(t testing.TB) io.ReadCloser {
|
2019-09-23 08:06:27 -04:00
|
|
|
t.Helper()
|
2017-06-02 16:06:14 -04:00
|
|
|
reader, err := archive.TarWithOptions(f.Dir, &archive.TarOptions{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create tar from %s: %s", f.Dir, err)
|
|
|
|
}
|
|
|
|
return reader
|
|
|
|
}
|