mockbackend_test.go 3.9 KB

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