mockbackend_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package dockerfile
  2. import (
  3. "io"
  4. "time"
  5. "github.com/docker/distribution/reference"
  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. "github.com/docker/docker/image"
  11. "golang.org/x/net/context"
  12. )
  13. // MockBackend implements the builder.Backend interface for unit testing
  14. type MockBackend struct {
  15. containerCreateFunc func(config types.ContainerCreateConfig) (container.ContainerCreateCreatedBody, error)
  16. commitFunc func(string, *backend.ContainerCommitConfig) (string, error)
  17. getImageFunc func(string) (builder.Image, builder.ReleaseableLayer, error)
  18. }
  19. func (m *MockBackend) TagImageWithReference(image.ID, reference.Named) error {
  20. return nil
  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) ContainerCreate(config types.ContainerCreateConfig) (container.ContainerCreateCreatedBody, error) {
  26. if m.containerCreateFunc != nil {
  27. return m.containerCreateFunc(config)
  28. }
  29. return container.ContainerCreateCreatedBody{}, nil
  30. }
  31. func (m *MockBackend) ContainerRm(name string, config *types.ContainerRmConfig) error {
  32. return nil
  33. }
  34. func (m *MockBackend) Commit(cID string, cfg *backend.ContainerCommitConfig) (string, error) {
  35. if m.commitFunc != nil {
  36. return m.commitFunc(cID, cfg)
  37. }
  38. return "", nil
  39. }
  40. func (m *MockBackend) ContainerKill(containerID string, sig uint64) error {
  41. return nil
  42. }
  43. func (m *MockBackend) ContainerStart(containerID string, hostConfig *container.HostConfig, checkpoint string, checkpointDir string) error {
  44. return nil
  45. }
  46. func (m *MockBackend) ContainerWait(containerID string, timeout time.Duration) (int, error) {
  47. return 0, 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) HasExperimental() bool {
  56. return false
  57. }
  58. func (m *MockBackend) SquashImage(from string, to string) (string, error) {
  59. return "", nil
  60. }
  61. func (m *MockBackend) GetImageAndLayer(ctx context.Context, refOrID string, opts backend.GetImageAndLayerOptions) (builder.Image, builder.ReleaseableLayer, error) {
  62. if m.getImageFunc != nil {
  63. return m.getImageFunc(refOrID)
  64. }
  65. return &mockImage{id: "theid"}, &mockLayer{}, nil
  66. }
  67. type mockImage struct {
  68. id string
  69. config *container.Config
  70. }
  71. func (i *mockImage) ImageID() string {
  72. return i.id
  73. }
  74. func (i *mockImage) RunConfig() *container.Config {
  75. return i.config
  76. }
  77. type mockImageCache struct {
  78. getCacheFunc func(parentID string, cfg *container.Config) (string, error)
  79. }
  80. func (mic *mockImageCache) GetCache(parentID string, cfg *container.Config) (string, error) {
  81. if mic.getCacheFunc != nil {
  82. return mic.getCacheFunc(parentID, cfg)
  83. }
  84. return "", nil
  85. }
  86. type mockLayer struct{}
  87. func (l *mockLayer) Release() error {
  88. return nil
  89. }
  90. func (l *mockLayer) Mount() (string, error) {
  91. return "mountPath", nil
  92. }