mockbackend_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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) GetImageAndReleasableLayer(ctx context.Context, refOrID string, opts backend.GetImageAndLayerOptions) (builder.Image, builder.ReleaseableLayer, error) {
  56. if m.getImageFunc != nil {
  57. return m.getImageFunc(refOrID)
  58. }
  59. return &mockImage{id: "theid"}, &mockLayer{}, nil
  60. }
  61. type mockImage struct {
  62. id string
  63. config *container.Config
  64. }
  65. func (i *mockImage) ImageID() string {
  66. return i.id
  67. }
  68. func (i *mockImage) RunConfig() *container.Config {
  69. return i.config
  70. }
  71. type mockImageCache struct {
  72. getCacheFunc func(parentID string, cfg *container.Config) (string, error)
  73. }
  74. func (mic *mockImageCache) GetCache(parentID string, cfg *container.Config) (string, error) {
  75. if mic.getCacheFunc != nil {
  76. return mic.getCacheFunc(parentID, cfg)
  77. }
  78. return "", nil
  79. }
  80. type mockLayer struct{}
  81. func (l *mockLayer) Release() error {
  82. return nil
  83. }
  84. func (l *mockLayer) Mount() (string, error) {
  85. return "mountPath", nil
  86. }