gitee.com/leisunstar/runtime@v0.0.0-20200521203717-5cef3e7b53f9/virtcontainers/types/asset_test.go (about) 1 // Copyright (c) 2017 Intel Corporation 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 // 5 6 package types 7 8 import ( 9 "io/ioutil" 10 "os" 11 "testing" 12 13 "github.com/kata-containers/runtime/virtcontainers/pkg/annotations" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 var assetContent = []byte("FakeAsset fake asset FAKE ASSET") 18 var assetContentHash = "92549f8d2018a95a294d28a65e795ed7d1a9d150009a28cea108ae10101178676f04ab82a6950d0099e4924f9c5e41dcba8ece56b75fc8b4e0a7492cb2a8c880" 19 var assetContentWrongHash = "92549f8d2018a95a294d28a65e795ed7d1a9d150009a28cea108ae10101178676f04ab82a6950d0099e4924f9c5e41dcba8ece56b75fc8b4e0a7492cb2a8c881" 20 21 func TestAssetWrongHashType(t *testing.T) { 22 assert := assert.New(t) 23 24 tmpfile, err := ioutil.TempFile("", "virtcontainers-test-") 25 assert.Nil(err) 26 27 defer func() { 28 tmpfile.Close() 29 os.Remove(tmpfile.Name()) // clean up 30 }() 31 32 _, err = tmpfile.Write(assetContent) 33 assert.Nil(err) 34 35 a := &Asset{ 36 path: tmpfile.Name(), 37 } 38 39 h, err := a.Hash("shafoo") 40 assert.Equal(h, "") 41 assert.NotNil(err) 42 } 43 44 func TestAssetHash(t *testing.T) { 45 assert := assert.New(t) 46 47 tmpfile, err := ioutil.TempFile("", "virtcontainers-test-") 48 assert.Nil(err) 49 50 defer func() { 51 tmpfile.Close() 52 os.Remove(tmpfile.Name()) // clean up 53 }() 54 55 _, err = tmpfile.Write(assetContent) 56 assert.Nil(err) 57 58 a := &Asset{ 59 path: tmpfile.Name(), 60 } 61 62 hash, err := a.Hash(annotations.SHA512) 63 assert.Nil(err) 64 assert.Equal(assetContentHash, hash) 65 assert.Equal(assetContentHash, a.computedHash) 66 } 67 68 func TestAssetNew(t *testing.T) { 69 assert := assert.New(t) 70 71 tmpfile, err := ioutil.TempFile("", "virtcontainers-test-") 72 assert.Nil(err) 73 74 defer func() { 75 tmpfile.Close() 76 os.Remove(tmpfile.Name()) // clean up 77 }() 78 79 _, err = tmpfile.Write(assetContent) 80 assert.Nil(err) 81 82 anno := map[string]string{ 83 annotations.KernelPath: tmpfile.Name(), 84 annotations.KernelHash: assetContentHash, 85 } 86 a, err := NewAsset(anno, ImageAsset) 87 assert.Nil(err) 88 assert.Nil(a) 89 90 a, err = NewAsset(anno, KernelAsset) 91 assert.Nil(err) 92 assert.Equal(assetContentHash, a.computedHash) 93 94 anno = map[string]string{ 95 annotations.KernelPath: tmpfile.Name(), 96 annotations.KernelHash: assetContentWrongHash, 97 } 98 99 _, err = NewAsset(anno, KernelAsset) 100 assert.NotNil(err) 101 }