github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/builder/dockerfile/imagecontext_test.go (about)

     1  package dockerfile // import "github.com/Prakhar-Agarwal-byte/moby/builder/dockerfile"
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"runtime"
     7  	"testing"
     8  
     9  	"github.com/containerd/containerd/platforms"
    10  	"github.com/Prakhar-Agarwal-byte/moby/builder"
    11  	"github.com/Prakhar-Agarwal-byte/moby/image"
    12  	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
    13  	"gotest.tools/v3/assert"
    14  )
    15  
    16  func getMockImageSource(getImageImage builder.Image, getImageLayer builder.ROLayer, getImageError error) *imageSources {
    17  	return &imageSources{
    18  		byImageID: make(map[string]*imageMount),
    19  		mounts:    []*imageMount{},
    20  		getImage: func(_ context.Context, name string, localOnly bool, platform *ocispec.Platform) (builder.Image, builder.ROLayer, error) {
    21  			return getImageImage, getImageLayer, getImageError
    22  		},
    23  	}
    24  }
    25  
    26  func getMockImageMount() *imageMount {
    27  	return &imageMount{
    28  		image: nil,
    29  		layer: nil,
    30  	}
    31  }
    32  
    33  func TestAddScratchImageAddsToMounts(t *testing.T) {
    34  	is := getMockImageSource(nil, nil, fmt.Errorf("getImage is not implemented"))
    35  	im := getMockImageMount()
    36  
    37  	// We are testing whether the imageMount is added to is.mounts
    38  	assert.Equal(t, len(is.mounts), 0)
    39  	is.Add(im, nil)
    40  	assert.Equal(t, len(is.mounts), 1)
    41  }
    42  
    43  func TestAddFromScratchPopulatesPlatform(t *testing.T) {
    44  	is := getMockImageSource(nil, nil, fmt.Errorf("getImage is not implemented"))
    45  
    46  	platforms := []*ocispec.Platform{
    47  		{
    48  			OS:           "linux",
    49  			Architecture: "amd64",
    50  		},
    51  		{
    52  			OS:           "linux",
    53  			Architecture: "arm64",
    54  			Variant:      "v8",
    55  		},
    56  	}
    57  
    58  	for i, platform := range platforms {
    59  		im := getMockImageMount()
    60  		assert.Equal(t, len(is.mounts), i)
    61  		is.Add(im, platform)
    62  		image, ok := im.image.(*image.Image)
    63  		assert.Assert(t, ok)
    64  		assert.Equal(t, image.OS, platform.OS)
    65  		assert.Equal(t, image.Architecture, platform.Architecture)
    66  		assert.Equal(t, image.Variant, platform.Variant)
    67  	}
    68  }
    69  
    70  func TestAddFromScratchDoesNotModifyArgPlatform(t *testing.T) {
    71  	is := getMockImageSource(nil, nil, fmt.Errorf("getImage is not implemented"))
    72  	im := getMockImageMount()
    73  
    74  	platform := &ocispec.Platform{
    75  		OS:           "windows",
    76  		Architecture: "amd64",
    77  	}
    78  	argPlatform := *platform
    79  
    80  	is.Add(im, &argPlatform)
    81  	// The way the code is written right now, this test
    82  	// really doesn't do much except on Windows.
    83  	assert.DeepEqual(t, *platform, argPlatform)
    84  }
    85  
    86  func TestAddFromScratchPopulatesPlatformIfNil(t *testing.T) {
    87  	is := getMockImageSource(nil, nil, fmt.Errorf("getImage is not implemented"))
    88  	im := getMockImageMount()
    89  	is.Add(im, nil)
    90  	image, ok := im.image.(*image.Image)
    91  	assert.Assert(t, ok)
    92  
    93  	expectedPlatform := platforms.DefaultSpec()
    94  	if runtime.GOOS == "windows" {
    95  		expectedPlatform.OS = "linux"
    96  	}
    97  	assert.Equal(t, expectedPlatform.OS, image.OS)
    98  	assert.Equal(t, expectedPlatform.Architecture, image.Architecture)
    99  	assert.Equal(t, expectedPlatform.Variant, image.Variant)
   100  }
   101  
   102  func TestImageSourceGetAddsToMounts(t *testing.T) {
   103  	is := getMockImageSource(nil, nil, nil)
   104  	ctx := context.Background()
   105  	_, err := is.Get(ctx, "test", false, nil)
   106  	assert.NilError(t, err)
   107  	assert.Equal(t, len(is.mounts), 1)
   108  }