github.com/toplink-cn/moby@v0.0.0-20240305205811-460b4aebdf81/image/image_test.go (about)

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