github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/builder/dockerfile/mockbackend_test.go (about)

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