image_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package image
  2. import (
  3. "encoding/json"
  4. "runtime"
  5. "sort"
  6. "strings"
  7. "testing"
  8. "github.com/docker/docker/api/types/container"
  9. "github.com/docker/docker/layer"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. )
  13. const sampleImageJSON = `{
  14. "architecture": "amd64",
  15. "os": "linux",
  16. "config": {},
  17. "rootfs": {
  18. "type": "layers",
  19. "diff_ids": []
  20. }
  21. }`
  22. func TestNewFromJSON(t *testing.T) {
  23. img, err := NewFromJSON([]byte(sampleImageJSON))
  24. require.NoError(t, err)
  25. assert.Equal(t, sampleImageJSON, string(img.RawJSON()))
  26. }
  27. func TestNewFromJSONWithInvalidJSON(t *testing.T) {
  28. _, err := NewFromJSON([]byte("{}"))
  29. assert.EqualError(t, err, "invalid image JSON, no RootFS key")
  30. }
  31. func TestMarshalKeyOrder(t *testing.T) {
  32. b, err := json.Marshal(&Image{
  33. V1Image: V1Image{
  34. Comment: "a",
  35. Author: "b",
  36. Architecture: "c",
  37. },
  38. })
  39. assert.NoError(t, err)
  40. expectedOrder := []string{"architecture", "author", "comment"}
  41. var indexes []int
  42. for _, k := range expectedOrder {
  43. indexes = append(indexes, strings.Index(string(b), k))
  44. }
  45. if !sort.IntsAreSorted(indexes) {
  46. t.Fatal("invalid key order in JSON: ", string(b))
  47. }
  48. }
  49. func TestImage(t *testing.T) {
  50. cid := "50a16564e727"
  51. config := &container.Config{
  52. Hostname: "hostname",
  53. Domainname: "domain",
  54. User: "root",
  55. }
  56. platform := runtime.GOOS
  57. img := &Image{
  58. V1Image: V1Image{
  59. Config: config,
  60. },
  61. computedID: ID(cid),
  62. }
  63. assert.Equal(t, cid, img.ImageID())
  64. assert.Equal(t, cid, img.ID().String())
  65. assert.Equal(t, platform, img.Platform())
  66. assert.Equal(t, config, img.RunConfig())
  67. }
  68. func TestImagePlatformNotEmpty(t *testing.T) {
  69. platform := "platform"
  70. img := &Image{
  71. V1Image: V1Image{
  72. OS: platform,
  73. },
  74. OSVersion: "osversion",
  75. }
  76. assert.Equal(t, platform, img.Platform())
  77. }
  78. func TestNewChildImageFromImageWithRootFS(t *testing.T) {
  79. rootFS := NewRootFS()
  80. rootFS.Append(layer.DiffID("ba5e"))
  81. parent := &Image{
  82. RootFS: rootFS,
  83. History: []History{
  84. NewHistory("a", "c", "r", false),
  85. },
  86. }
  87. childConfig := ChildConfig{
  88. DiffID: layer.DiffID("abcdef"),
  89. Author: "author",
  90. Comment: "comment",
  91. ContainerConfig: &container.Config{
  92. Cmd: []string{"echo", "foo"},
  93. },
  94. Config: &container.Config{},
  95. }
  96. newImage := NewChildImage(parent, childConfig, "platform")
  97. expectedDiffIDs := []layer.DiffID{layer.DiffID("ba5e"), layer.DiffID("abcdef")}
  98. assert.Equal(t, expectedDiffIDs, newImage.RootFS.DiffIDs)
  99. assert.Equal(t, childConfig.Author, newImage.Author)
  100. assert.Equal(t, childConfig.Config, newImage.Config)
  101. assert.Equal(t, *childConfig.ContainerConfig, newImage.ContainerConfig)
  102. assert.Equal(t, "platform", newImage.OS)
  103. assert.Equal(t, childConfig.Config, newImage.Config)
  104. assert.Len(t, newImage.History, 2)
  105. assert.Equal(t, childConfig.Comment, newImage.History[1].Comment)
  106. // RootFS should be copied not mutated
  107. assert.NotEqual(t, parent.RootFS.DiffIDs, newImage.RootFS.DiffIDs)
  108. }