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