github.com/lazyboychen7/engine@v17.12.1-ce-rc2+incompatible/image/image_test.go (about)

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