github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/builder/dockerfile/imagecontext_test.go (about)

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