imagecontext_test.go 3.0 KB

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