summaryrefslogtreecommitdiff
path: root/builder/dockerfile/imagecontext_test.go
blob: 14d347899ac665d47fa80800f9d3a6d12d5422a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package dockerfile // import "github.com/docker/docker/builder/dockerfile"

import (
	"context"
	"fmt"
	"runtime"
	"testing"

	"github.com/containerd/containerd/platforms"
	"github.com/docker/docker/builder"
	"github.com/docker/docker/image"
	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
	"gotest.tools/v3/assert"
)

func getMockImageSource(getImageImage builder.Image, getImageLayer builder.ROLayer, getImageError error) *imageSources {
	return &imageSources{
		byImageID: make(map[string]*imageMount),
		mounts:    []*imageMount{},
		getImage: func(_ context.Context, name string, localOnly bool, platform *ocispec.Platform) (builder.Image, builder.ROLayer, error) {
			return getImageImage, getImageLayer, getImageError
		},
	}
}

func getMockImageMount() *imageMount {
	return &imageMount{
		image: nil,
		layer: nil,
	}
}

func TestAddScratchImageAddsToMounts(t *testing.T) {
	is := getMockImageSource(nil, nil, fmt.Errorf("getImage is not implemented"))
	im := getMockImageMount()

	// We are testing whether the imageMount is added to is.mounts
	assert.Equal(t, len(is.mounts), 0)
	is.Add(im, nil)
	assert.Equal(t, len(is.mounts), 1)
}

func TestAddFromScratchPopulatesPlatform(t *testing.T) {
	is := getMockImageSource(nil, nil, fmt.Errorf("getImage is not implemented"))

	platforms := []*ocispec.Platform{
		{
			OS:           "linux",
			Architecture: "amd64",
		},
		{
			OS:           "linux",
			Architecture: "arm64",
			Variant:      "v8",
		},
	}

	for i, platform := range platforms {
		im := getMockImageMount()
		assert.Equal(t, len(is.mounts), i)
		is.Add(im, platform)
		image, ok := im.image.(*image.Image)
		assert.Assert(t, ok)
		assert.Equal(t, image.OS, platform.OS)
		assert.Equal(t, image.Architecture, platform.Architecture)
		assert.Equal(t, image.Variant, platform.Variant)
	}
}

func TestAddFromScratchDoesNotModifyArgPlatform(t *testing.T) {
	is := getMockImageSource(nil, nil, fmt.Errorf("getImage is not implemented"))
	im := getMockImageMount()

	platform := &ocispec.Platform{
		OS:           "windows",
		Architecture: "amd64",
	}
	argPlatform := *platform

	is.Add(im, &argPlatform)
	// The way the code is written right now, this test
	// really doesn't do much except on Windows.
	assert.DeepEqual(t, *platform, argPlatform)
}

func TestAddFromScratchPopulatesPlatformIfNil(t *testing.T) {
	is := getMockImageSource(nil, nil, fmt.Errorf("getImage is not implemented"))
	im := getMockImageMount()
	is.Add(im, nil)
	image, ok := im.image.(*image.Image)
	assert.Assert(t, ok)

	expectedPlatform := platforms.DefaultSpec()
	if runtime.GOOS == "windows" {
		expectedPlatform.OS = "linux"
	}
	assert.Equal(t, expectedPlatform.OS, image.OS)
	assert.Equal(t, expectedPlatform.Architecture, image.Architecture)
	assert.Equal(t, expectedPlatform.Variant, image.Variant)
}

func TestImageSourceGetAddsToMounts(t *testing.T) {
	is := getMockImageSource(nil, nil, nil)
	ctx := context.Background()
	_, err := is.Get(ctx, "test", false, nil)
	assert.NilError(t, err)
	assert.Equal(t, len(is.mounts), 1)
}