66package types
77
88import (
9+ "fmt"
910 "io/ioutil"
1011 "os"
1112 "testing"
@@ -65,6 +66,27 @@ func TestAssetHash(t *testing.T) {
6566 assert .Equal (assetContentHash , a .computedHash )
6667}
6768
69+ func testPath (t * testing.T , a * Asset , correctPath string , msg string ) {
70+ assert := assert .New (t )
71+
72+ returnedPath := a .Path ()
73+ assert .Equal (returnedPath , correctPath , msg )
74+ }
75+
76+ func testType (t * testing.T , a * Asset , correctType AssetType , msg string ) {
77+ assert := assert .New (t )
78+
79+ returnedType := a .Type ()
80+ assert .Equal (returnedType , correctType , msg )
81+ }
82+
83+ func testValid (t * testing.T , a * Asset , msg string ) {
84+ assert := assert .New (t )
85+
86+ v := a .Valid ()
87+ assert .True (v , msg )
88+ }
89+
6890func TestAssetNew (t * testing.T ) {
6991 assert := assert .New (t )
7092
@@ -79,23 +101,59 @@ func TestAssetNew(t *testing.T) {
79101 _ , err = tmpfile .Write (assetContent )
80102 assert .Nil (err )
81103
82- anno := map [string ]string {
83- annotations .KernelPath : tmpfile .Name (),
84- annotations .KernelHash : assetContentHash ,
104+ type testData struct {
105+ inputPathVar string
106+ inputHashVar string
107+ inputAssetType AssetType
108+ inputHash string
109+ expectError bool
110+ expectNilAsset bool
85111 }
86- a , err := NewAsset (anno , ImageAsset )
87- assert .Nil (err )
88- assert .Nil (a )
89112
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 ,
113+ data := []testData {
114+ // Successful with correct hash
115+ {annotations .KernelPath , annotations .KernelHash , KernelAsset , assetContentHash , false , false },
116+ {annotations .ImagePath , annotations .ImageHash , ImageAsset , assetContentHash , false , false },
117+ {annotations .InitrdPath , annotations .InitrdHash , InitrdAsset , assetContentHash , false , false },
118+ {annotations .HypervisorPath , annotations .HypervisorHash , HypervisorAsset , assetContentHash , false , false },
119+ {annotations .JailerPath , annotations .JailerHash , JailerAsset , assetContentHash , false , false },
120+ {annotations .FirmwarePath , annotations .FirmwareHash , FirmwareAsset , assetContentHash , false , false },
121+
122+ // Failure with incorrect hash
123+ {annotations .KernelPath , annotations .KernelHash , KernelAsset , assetContentWrongHash , true , false },
124+ {annotations .ImagePath , annotations .ImageHash , ImageAsset , assetContentWrongHash , true , false },
125+ {annotations .InitrdPath , annotations .InitrdHash , InitrdAsset , assetContentWrongHash , true , false },
126+ {annotations .HypervisorPath , annotations .HypervisorHash , HypervisorAsset , assetContentWrongHash , true , false },
127+ {annotations .JailerPath , annotations .JailerHash , JailerAsset , assetContentWrongHash , true , false },
128+ {annotations .FirmwarePath , annotations .FirmwareHash , FirmwareAsset , assetContentWrongHash , true , false },
129+
130+ // Other failures
131+ {annotations .KernelPath , annotations .KernelHash , ImageAsset , assetContentHash , false , true },
97132 }
98133
99- _ , err = NewAsset (anno , KernelAsset )
100- assert .NotNil (err )
134+ for i , d := range data {
135+ msg := fmt .Sprintf ("test[%d]: %+v" , i , d )
136+
137+ anno := map [string ]string {
138+ d .inputPathVar : tmpfile .Name (),
139+ d .inputHashVar : d .inputHash ,
140+ }
141+
142+ if d .expectNilAsset {
143+ a , err := NewAsset (anno , d .inputAssetType )
144+ assert .NoError (err , msg )
145+ assert .Nil (a , msg )
146+ } else if d .expectError {
147+ _ , err := NewAsset (anno , d .inputAssetType )
148+ assert .NotNil (err , msg )
149+ } else {
150+ a , err := NewAsset (anno , d .inputAssetType )
151+ assert .Nil (err , msg )
152+ assert .Equal (assetContentHash , a .computedHash , msg )
153+
154+ testPath (t , a , tmpfile .Name (), msg )
155+ testType (t , a , d .inputAssetType , msg )
156+ testValid (t , a , msg )
157+ }
158+ }
101159}
0 commit comments