github.com/moby/docker@v26.1.3+incompatible/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/backend"
    10  	"github.com/docker/docker/api/types/container"
    11  	"github.com/docker/docker/builder"
    12  	containerpkg "github.com/docker/docker/container"
    13  	"github.com/docker/docker/image"
    14  	"github.com/docker/docker/layer"
    15  	"github.com/opencontainers/go-digest"
    16  	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
    17  )
    18  
    19  // MockBackend implements the builder.Backend interface for unit testing
    20  type MockBackend struct {
    21  	containerCreateFunc func(config backend.ContainerCreateConfig) (container.CreateResponse, 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(ctx context.Context, config backend.ContainerCreateConfig) (container.CreateResponse, error) {
    32  	if m.containerCreateFunc != nil {
    33  		return m.containerCreateFunc(config)
    34  	}
    35  	return container.CreateResponse{}, nil
    36  }
    37  
    38  func (m *MockBackend) ContainerRm(name string, config *backend.ContainerRmConfig) error {
    39  	return nil
    40  }
    41  
    42  func (m *MockBackend) CommitBuildStep(ctx context.Context, 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) ContainerStart(ctx context.Context, containerID string, checkpoint string, checkpointDir string) error {
    50  	return nil
    51  }
    52  
    53  func (m *MockBackend) ContainerWait(ctx context.Context, containerID string, condition containerpkg.WaitCondition) (<-chan containerpkg.StateStatus, error) {
    54  	return nil, nil
    55  }
    56  
    57  func (m *MockBackend) ContainerCreateWorkdir(containerID string) error {
    58  	return nil
    59  }
    60  
    61  func (m *MockBackend) CopyOnBuild(containerID string, destPath string, srcRoot string, srcPath string, decompress bool) error {
    62  	return nil
    63  }
    64  
    65  func (m *MockBackend) GetImageAndReleasableLayer(ctx context.Context, refOrID string, opts backend.GetImageAndLayerOptions) (builder.Image, builder.ROLayer, error) {
    66  	if m.getImageFunc != nil {
    67  		return m.getImageFunc(refOrID)
    68  	}
    69  
    70  	return &mockImage{id: "theid"}, &mockLayer{}, nil
    71  }
    72  
    73  func (m *MockBackend) MakeImageCache(ctx context.Context, cacheFrom []string) (builder.ImageCache, error) {
    74  	if m.makeImageCacheFunc != nil {
    75  		return m.makeImageCacheFunc(cacheFrom), nil
    76  	}
    77  	return nil, nil
    78  }
    79  
    80  func (m *MockBackend) CreateImage(ctx context.Context, config []byte, parent string, layerDigest digest.Digest) (builder.Image, error) {
    81  	return &mockImage{id: "test"}, nil
    82  }
    83  
    84  type mockImage struct {
    85  	id     string
    86  	config *container.Config
    87  }
    88  
    89  func (i *mockImage) ImageID() string {
    90  	return i.id
    91  }
    92  
    93  func (i *mockImage) RunConfig() *container.Config {
    94  	return i.config
    95  }
    96  
    97  func (i *mockImage) OperatingSystem() string {
    98  	return runtime.GOOS
    99  }
   100  
   101  func (i *mockImage) MarshalJSON() ([]byte, error) {
   102  	type rawImage mockImage
   103  	return json.Marshal(rawImage(*i)) //nolint:staticcheck
   104  }
   105  
   106  type mockImageCache struct {
   107  	getCacheFunc func(parentID string, cfg *container.Config) (string, error)
   108  }
   109  
   110  func (mic *mockImageCache) GetCache(parentID string, cfg *container.Config, _ ocispec.Platform) (string, error) {
   111  	if mic.getCacheFunc != nil {
   112  		return mic.getCacheFunc(parentID, cfg)
   113  	}
   114  	return "", nil
   115  }
   116  
   117  type mockLayer struct{}
   118  
   119  func (l *mockLayer) ContentStoreDigest() digest.Digest {
   120  	return ""
   121  }
   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 "abcdef"
   133  }
   134  
   135  type mockRWLayer struct{}
   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  }