imagecontext_test.go 2.9 KB

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