mockbackend_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package dockerfile // import "github.com/docker/docker/builder/dockerfile"
  2. import (
  3. "context"
  4. "encoding/json"
  5. "io"
  6. "runtime"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/api/types/backend"
  9. "github.com/docker/docker/api/types/container"
  10. "github.com/docker/docker/builder"
  11. containerpkg "github.com/docker/docker/container"
  12. "github.com/docker/docker/image"
  13. "github.com/docker/docker/layer"
  14. "github.com/docker/docker/pkg/containerfs"
  15. ocispec "github.com/opencontainers/image-spec/specs-go/v1"
  16. )
  17. // MockBackend implements the builder.Backend interface for unit testing
  18. type MockBackend struct {
  19. containerCreateFunc func(config types.ContainerCreateConfig) (container.CreateResponse, error)
  20. commitFunc func(backend.CommitConfig) (image.ID, error)
  21. getImageFunc func(string) (builder.Image, builder.ROLayer, error)
  22. makeImageCacheFunc func(cacheFrom []string) builder.ImageCache
  23. }
  24. func (m *MockBackend) ContainerAttachRaw(cID string, stdin io.ReadCloser, stdout, stderr io.Writer, stream bool, attached chan struct{}) error {
  25. return nil
  26. }
  27. func (m *MockBackend) ContainerCreateIgnoreImagesArgsEscaped(config types.ContainerCreateConfig) (container.CreateResponse, error) {
  28. if m.containerCreateFunc != nil {
  29. return m.containerCreateFunc(config)
  30. }
  31. return container.CreateResponse{}, nil
  32. }
  33. func (m *MockBackend) ContainerRm(name string, config *types.ContainerRmConfig) error {
  34. return nil
  35. }
  36. func (m *MockBackend) CommitBuildStep(c backend.CommitConfig) (image.ID, error) {
  37. if m.commitFunc != nil {
  38. return m.commitFunc(c)
  39. }
  40. return "", nil
  41. }
  42. func (m *MockBackend) ContainerKill(containerID string, sig string) error {
  43. return nil
  44. }
  45. func (m *MockBackend) ContainerStart(containerID string, hostConfig *container.HostConfig, checkpoint string, checkpointDir string) error {
  46. return nil
  47. }
  48. func (m *MockBackend) ContainerWait(ctx context.Context, containerID string, condition containerpkg.WaitCondition) (<-chan containerpkg.StateStatus, error) {
  49. return nil, nil
  50. }
  51. func (m *MockBackend) ContainerCreateWorkdir(containerID string) error {
  52. return nil
  53. }
  54. func (m *MockBackend) CopyOnBuild(containerID string, destPath string, srcRoot string, srcPath string, decompress bool) error {
  55. return nil
  56. }
  57. func (m *MockBackend) GetImageAndReleasableLayer(ctx context.Context, refOrID string, opts backend.GetImageAndLayerOptions) (builder.Image, builder.ROLayer, error) {
  58. if m.getImageFunc != nil {
  59. return m.getImageFunc(refOrID)
  60. }
  61. return &mockImage{id: "theid"}, &mockLayer{}, nil
  62. }
  63. func (m *MockBackend) MakeImageCache(cacheFrom []string) builder.ImageCache {
  64. if m.makeImageCacheFunc != nil {
  65. return m.makeImageCacheFunc(cacheFrom)
  66. }
  67. return nil
  68. }
  69. func (m *MockBackend) CreateImage(config []byte, parent string) (builder.Image, error) {
  70. return &mockImage{id: "test"}, nil
  71. }
  72. type mockImage struct {
  73. id string
  74. config *container.Config
  75. }
  76. func (i *mockImage) ImageID() string {
  77. return i.id
  78. }
  79. func (i *mockImage) RunConfig() *container.Config {
  80. return i.config
  81. }
  82. func (i *mockImage) OperatingSystem() string {
  83. return runtime.GOOS
  84. }
  85. func (i *mockImage) MarshalJSON() ([]byte, error) {
  86. type rawImage mockImage
  87. return json.Marshal(rawImage(*i)) //nolint:staticcheck
  88. }
  89. type mockImageCache struct {
  90. getCacheFunc func(parentID string, cfg *container.Config) (string, error)
  91. }
  92. func (mic *mockImageCache) GetCache(parentID string, cfg *container.Config, _ ocispec.Platform) (string, error) {
  93. if mic.getCacheFunc != nil {
  94. return mic.getCacheFunc(parentID, cfg)
  95. }
  96. return "", nil
  97. }
  98. type mockLayer struct{}
  99. func (l *mockLayer) Release() error {
  100. return nil
  101. }
  102. func (l *mockLayer) NewRWLayer() (builder.RWLayer, error) {
  103. return &mockRWLayer{}, nil
  104. }
  105. func (l *mockLayer) DiffID() layer.DiffID {
  106. return "abcdef"
  107. }
  108. type mockRWLayer struct {
  109. }
  110. func (l *mockRWLayer) Release() error {
  111. return nil
  112. }
  113. func (l *mockRWLayer) Commit() (builder.ROLayer, error) {
  114. return nil, nil
  115. }
  116. func (l *mockRWLayer) Root() containerfs.ContainerFS {
  117. return nil
  118. }