context.go 2.8 KB

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