github.com/docker/Engine@v17.12.1-ce-rc2+incompatible/integration-cli/cli/build/fakecontext/context.go (about)

     1  package fakecontext
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"github.com/docker/docker/pkg/archive"
    11  )
    12  
    13  type testingT interface {
    14  	Fatal(args ...interface{})
    15  	Fatalf(string, ...interface{})
    16  }
    17  
    18  // New creates a fake build context
    19  func New(t testingT, dir string, modifiers ...func(*Fake) error) *Fake {
    20  	fakeContext := &Fake{Dir: dir}
    21  	if dir == "" {
    22  		if err := newDir(fakeContext); err != nil {
    23  			t.Fatal(err)
    24  		}
    25  	}
    26  
    27  	for _, modifier := range modifiers {
    28  		if err := modifier(fakeContext); err != nil {
    29  			t.Fatal(err)
    30  		}
    31  	}
    32  
    33  	return fakeContext
    34  }
    35  
    36  func newDir(fake *Fake) error {
    37  	tmp, err := ioutil.TempDir("", "fake-context")
    38  	if err != nil {
    39  		return err
    40  	}
    41  	if err := os.Chmod(tmp, 0755); err != nil {
    42  		return err
    43  	}
    44  	fake.Dir = tmp
    45  	return nil
    46  }
    47  
    48  // WithFile adds the specified file (with content) in the build context
    49  func WithFile(name, content string) func(*Fake) error {
    50  	return func(ctx *Fake) error {
    51  		return ctx.Add(name, content)
    52  	}
    53  }
    54  
    55  // WithDockerfile adds the specified content as Dockerfile in the build context
    56  func WithDockerfile(content string) func(*Fake) error {
    57  	return WithFile("Dockerfile", content)
    58  }
    59  
    60  // WithFiles adds the specified files in the build context, content is a string
    61  func WithFiles(files map[string]string) func(*Fake) error {
    62  	return func(fakeContext *Fake) error {
    63  		for file, content := range files {
    64  			if err := fakeContext.Add(file, content); err != nil {
    65  				return err
    66  			}
    67  		}
    68  		return nil
    69  	}
    70  }
    71  
    72  // WithBinaryFiles adds the specified files in the build context, content is binary
    73  func WithBinaryFiles(files map[string]*bytes.Buffer) func(*Fake) error {
    74  	return func(fakeContext *Fake) error {
    75  		for file, content := range files {
    76  			if err := fakeContext.Add(file, string(content.Bytes())); err != nil {
    77  				return err
    78  			}
    79  		}
    80  		return nil
    81  	}
    82  }
    83  
    84  // Fake creates directories that can be used as a build context
    85  type Fake struct {
    86  	Dir string
    87  }
    88  
    89  // Add a file at a path, creating directories where necessary
    90  func (f *Fake) Add(file, content string) error {
    91  	return f.addFile(file, []byte(content))
    92  }
    93  
    94  func (f *Fake) addFile(file string, content []byte) error {
    95  	fp := filepath.Join(f.Dir, filepath.FromSlash(file))
    96  	dirpath := filepath.Dir(fp)
    97  	if dirpath != "." {
    98  		if err := os.MkdirAll(dirpath, 0755); err != nil {
    99  			return err
   100  		}
   101  	}
   102  	return ioutil.WriteFile(fp, content, 0644)
   103  
   104  }
   105  
   106  // Delete a file at a path
   107  func (f *Fake) Delete(file string) error {
   108  	fp := filepath.Join(f.Dir, filepath.FromSlash(file))
   109  	return os.RemoveAll(fp)
   110  }
   111  
   112  // Close deletes the context
   113  func (f *Fake) Close() error {
   114  	return os.RemoveAll(f.Dir)
   115  }
   116  
   117  // AsTarReader returns a ReadCloser with the contents of Dir as a tar archive.
   118  func (f *Fake) AsTarReader(t testingT) io.ReadCloser {
   119  	reader, err := archive.TarWithOptions(f.Dir, &archive.TarOptions{})
   120  	if err != nil {
   121  		t.Fatalf("Failed to create tar from %s: %s", f.Dir, err)
   122  	}
   123  	return reader
   124  }