image_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package image // import "github.com/docker/docker/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/google/go-cmp/cmp"
  11. "gotest.tools/v3/assert"
  12. is "gotest.tools/v3/assert/cmp"
  13. )
  14. const sampleImageJSON = `{
  15. "architecture": "amd64",
  16. "os": "linux",
  17. "config": {},
  18. "rootfs": {
  19. "type": "layers",
  20. "diff_ids": []
  21. }
  22. }`
  23. func TestNewFromJSON(t *testing.T) {
  24. img, err := NewFromJSON([]byte(sampleImageJSON))
  25. assert.NilError(t, err)
  26. assert.Check(t, is.Equal(sampleImageJSON, string(img.RawJSON())))
  27. }
  28. func TestNewFromJSONWithInvalidJSON(t *testing.T) {
  29. _, err := NewFromJSON([]byte("{}"))
  30. assert.Check(t, is.Error(err, "invalid image JSON, no RootFS key"))
  31. }
  32. func TestMarshalKeyOrder(t *testing.T) {
  33. b, err := json.Marshal(&Image{
  34. V1Image: V1Image{
  35. Comment: "a",
  36. Author: "b",
  37. Architecture: "c",
  38. },
  39. })
  40. assert.Check(t, err)
  41. expectedOrder := []string{"architecture", "author", "comment"}
  42. var indexes []int
  43. for _, k := range expectedOrder {
  44. indexes = append(indexes, strings.Index(string(b), k))
  45. }
  46. if !sort.IntsAreSorted(indexes) {
  47. t.Fatal("invalid key order in JSON: ", string(b))
  48. }
  49. }
  50. const sampleHistoryJSON = `{
  51. "created": "2021-01-13T09:35:56Z",
  52. "created_by": "image_test.go"
  53. }`
  54. func TestHistoryEqual(t *testing.T) {
  55. h := historyFromJSON(t, sampleHistoryJSON)
  56. hCopy := h
  57. assert.Check(t, h.Equal(hCopy))
  58. hUTC := historyFromJSON(t, `{"created": "2021-01-13T14:00:00Z"}`)
  59. hOffset0 := historyFromJSON(t, `{"created": "2021-01-13T14:00:00+00:00"}`)
  60. assert.Check(t, hUTC.Created != hOffset0.Created)
  61. assert.Check(t, hUTC.Equal(hOffset0))
  62. }
  63. func historyFromJSON(t *testing.T, historyJSON string) History {
  64. var h History
  65. err := json.Unmarshal([]byte(historyJSON), &h)
  66. assert.Check(t, err)
  67. return h
  68. }
  69. func TestImage(t *testing.T) {
  70. cid := "50a16564e727"
  71. config := &container.Config{
  72. Hostname: "hostname",
  73. Domainname: "domain",
  74. User: "root",
  75. }
  76. os := runtime.GOOS
  77. img := &Image{
  78. V1Image: V1Image{
  79. Config: config,
  80. },
  81. computedID: ID(cid),
  82. }
  83. assert.Check(t, is.Equal(cid, img.ImageID()))
  84. assert.Check(t, is.Equal(cid, img.ID().String()))
  85. assert.Check(t, is.Equal(os, img.OperatingSystem()))
  86. assert.Check(t, is.DeepEqual(config, img.RunConfig()))
  87. }
  88. func TestImageOSNotEmpty(t *testing.T) {
  89. os := "os"
  90. img := &Image{
  91. V1Image: V1Image{
  92. OS: os,
  93. },
  94. OSVersion: "osversion",
  95. }
  96. assert.Check(t, is.Equal(os, img.OperatingSystem()))
  97. }
  98. func TestNewChildImageFromImageWithRootFS(t *testing.T) {
  99. rootFS := NewRootFS()
  100. rootFS.Append(layer.DiffID("ba5e"))
  101. parent := &Image{
  102. RootFS: rootFS,
  103. History: []History{
  104. NewHistory("a", "c", "r", false),
  105. },
  106. }
  107. childConfig := ChildConfig{
  108. DiffID: layer.DiffID("abcdef"),
  109. Author: "author",
  110. Comment: "comment",
  111. ContainerConfig: &container.Config{
  112. Cmd: []string{"echo", "foo"},
  113. },
  114. Config: &container.Config{},
  115. }
  116. newImage := NewChildImage(parent, childConfig, "platform")
  117. expectedDiffIDs := []layer.DiffID{layer.DiffID("ba5e"), layer.DiffID("abcdef")}
  118. assert.Check(t, is.DeepEqual(expectedDiffIDs, newImage.RootFS.DiffIDs))
  119. assert.Check(t, is.Equal(childConfig.Author, newImage.Author))
  120. assert.Check(t, is.DeepEqual(childConfig.Config, newImage.Config))
  121. assert.Check(t, is.DeepEqual(*childConfig.ContainerConfig, newImage.ContainerConfig))
  122. assert.Check(t, is.Equal("platform", newImage.OS))
  123. assert.Check(t, is.DeepEqual(childConfig.Config, newImage.Config))
  124. assert.Check(t, is.Len(newImage.History, 2))
  125. assert.Check(t, is.Equal(childConfig.Comment, newImage.History[1].Comment))
  126. assert.Check(t, !cmp.Equal(parent.RootFS.DiffIDs, newImage.RootFS.DiffIDs),
  127. "RootFS should be copied not mutated")
  128. }