github.com/rish1988/moby@v25.0.2+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  )
    17  
    18  // MockBackend implements the builder.Backend interface for unit testing
    19  type MockBackend struct {
    20  	containerCreateFunc func(config backend.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 backend.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 *backend.ContainerRmConfig) error {
    38  	return nil
    39  }
    40  
    41  func (m *MockBackend) CommitBuildStep(ctx context.Context, 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) ContainerStart(ctx context.Context, containerID string, hostConfig *container.HostConfig, checkpoint string, checkpointDir string) error {
    49  	return nil
    50  }
    51  
    52  func (m *MockBackend) ContainerWait(ctx context.Context, containerID string, condition containerpkg.WaitCondition) (<-chan containerpkg.StateStatus, error) {
    53  	return nil, nil
    54  }
    55  
    56  func (m *MockBackend) ContainerCreateWorkdir(containerID string) error {
    57  	return nil
    58  }
    59  
    60  func (m *MockBackend) CopyOnBuild(containerID string, destPath string, srcRoot string, srcPath string, decompress bool) error {
    61  	return nil
    62  }
    63  
    64  func (m *MockBackend) GetImageAndReleasableLayer(ctx context.Context, refOrID string, opts backend.GetImageAndLayerOptions) (builder.Image, builder.ROLayer, error) {
    65  	if m.getImageFunc != nil {
    66  		return m.getImageFunc(refOrID)
    67  	}
    68  
    69  	return &mockImage{id: "theid"}, &mockLayer{}, nil
    70  }
    71  
    72  func (m *MockBackend) MakeImageCache(ctx context.Context, cacheFrom []string) (builder.ImageCache, error) {
    73  	if m.makeImageCacheFunc != nil {
    74  		return m.makeImageCacheFunc(cacheFrom), nil
    75  	}
    76  	return nil, nil
    77  }
    78  
    79  func (m *MockBackend) CreateImage(ctx context.Context, config []byte, parent string, layerDigest digest.Digest) (builder.Image, error) {
    80  	return &mockImage{id: "test"}, nil
    81  }
    82  
    83  type mockImage struct {
    84  	id     string
    85  	config *container.Config
    86  }
    87  
    88  func (i *mockImage) ImageID() string {
    89  	return i.id
    90  }
    91  
    92  func (i *mockImage) RunConfig() *container.Config {
    93  	return i.config
    94  }
    95  
    96  func (i *mockImage) OperatingSystem() string {
    97  	return runtime.GOOS
    98  }
    99  
   100  func (i *mockImage) MarshalJSON() ([]byte, error) {
   101  	type rawImage mockImage
   102  	return json.Marshal(rawImage(*i)) //nolint:staticcheck
   103  }
   104  
   105  type mockImageCache struct {
   106  	getCacheFunc func(parentID string, cfg *container.Config) (string, error)
   107  }
   108  
   109  func (mic *mockImageCache) GetCache(parentID string, cfg *container.Config) (string, error) {
   110  	if mic.getCacheFunc != nil {
   111  		return mic.getCacheFunc(parentID, cfg)
   112  	}
   113  	return "", nil
   114  }
   115  
   116  type mockLayer struct{}
   117  
   118  func (l *mockLayer) ContentStoreDigest() digest.Digest {
   119  	return ""
   120  }
   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  func (l *mockRWLayer) Release() error {
   137  	return nil
   138  }
   139  
   140  func (l *mockRWLayer) Commit() (builder.ROLayer, error) {
   141  	return nil, nil
   142  }
   143  
   144  func (l *mockRWLayer) Root() string {
   145  	return ""
   146  }