context.go 2.8 KB

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