mockbackend_test.go 3.7 KB

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