77 "testing"
88
99 "github.com/deislabs/cnab-go/bundle"
10+ "github.com/deislabs/cnab-go/bundle/definition"
1011
1112 "github.com/deislabs/duffle/pkg/duffle/manifest"
1213 "github.com/deislabs/duffle/pkg/imagebuilder"
@@ -51,25 +52,38 @@ func (tc testImage) Build(ctx context.Context, log io.WriteCloser) error {
5152}
5253
5354func TestPrepareBuild (t * testing.T ) {
55+ outputs := & bundle.OutputsDefinition {
56+ Fields : map [string ]bundle.OutputDefinition {"output1" : {}},
57+ }
58+ params := & bundle.ParametersDefinition {
59+ Fields : map [string ]bundle.ParameterDefinition {"param1" : {}},
60+ }
5461 mfst := & manifest.Manifest {
55- Name : "foo" ,
56- Version : "0.1.0" ,
57- SchemaVersion : "v1.0.0" ,
58- Description : "description" ,
59- Keywords : []string {"test" },
62+ Actions : map [string ]bundle.Action {"act1" : {}},
63+ Credentials : map [string ]bundle.Credential {"cred1" : {}},
64+ Custom : map [string ]interface {}{"cus1" : nil },
65+ Definitions : map [string ]* definition.Schema {"def1" : {}},
66+ Description : "description" ,
67+ Images : map [string ]bundle.Image {"img1" : {}},
6068 InvocationImages : map [string ]* manifest.InvocationImage {
6169 "cnab" : {
6270 Name : "cnab" ,
6371 Configuration : map [string ]string {"registry" : "registry" },
6472 },
6573 },
74+ Keywords : []string {"test" },
6675 Maintainers : []bundle.Maintainer {
6776 {
6877 Name : "test" ,
6978 Email : "test@test.com" ,
7079 URL : "https://test.com" ,
7180 },
7281 },
82+ Name : "foo" ,
83+ Outputs : outputs ,
84+ Parameters : params ,
85+ SchemaVersion : "v1.0.0" ,
86+ Version : "0.1.0" ,
7387 }
7488
7589 components := []imagebuilder.ImageBuilder {
@@ -86,21 +100,108 @@ func TestPrepareBuild(t *testing.T) {
86100 if err != nil {
87101 t .Error (err )
88102 }
103+ checksPerformed := 0
89104
90- if len (b .InvocationImages ) != 1 {
91- t .Fatalf ("expected there to be 1 image, got %d. Full output: % v" , len ( b . Images ) , b )
105+ if ! reflect . DeepEqual (b .Actions , mfst . Actions ) {
106+ t .Errorf ("expected actions to be %+v but was %+ v" , mfst . Actions , b . Actions )
92107 }
108+ checksPerformed ++
93109
94- if b . Version != mfst .Version {
95- t .Errorf ("expected version %v, got % v" , mfst .Version , b .Version )
110+ if ! reflect . DeepEqual ( b . Credentials , mfst .Credentials ) {
111+ t .Errorf ("expected credentials to be %+v but was %+ v" , mfst .Credentials , b .Credentials )
96112 }
97- if b .SchemaVersion != mfst .SchemaVersion {
98- t .Errorf ("expected schemaVersion %v, got %v" , mfst .SchemaVersion , b .SchemaVersion )
113+ checksPerformed ++
114+
115+ if ! reflect .DeepEqual (b .Custom , mfst .Custom ) {
116+ t .Errorf ("expected custom to be %+v but was %+v" , mfst .Custom , b .Custom )
117+ }
118+ checksPerformed ++
119+
120+ if ! reflect .DeepEqual (b .Definitions , mfst .Definitions ) {
121+ t .Errorf ("expected definitions to be %+v but was %+v" , mfst .Definitions , b .Definitions )
99122 }
123+ checksPerformed ++
124+
125+ if b .Description != mfst .Description {
126+ t .Errorf ("expected description to be %+v but was %+v" , mfst .Description , b .Description )
127+ }
128+ checksPerformed ++
129+
130+ if len (b .InvocationImages ) != 1 {
131+ t .Fatalf ("expected there to be 1 image, got %d. Full output: %v" , len (b .Images ), b )
132+ }
133+ checksPerformed ++
134+
100135 expected := bundle.InvocationImage {}
101136 expected .Image = "cnab:0.1.0"
102137 expected .ImageType = "docker"
103138 if ! reflect .DeepEqual (b .InvocationImages [0 ], expected ) {
104139 t .Errorf ("expected %v, got %v" , expected , b .InvocationImages [0 ])
105140 }
141+ checksPerformed ++
142+
143+ if ! reflect .DeepEqual (b .Keywords , mfst .Keywords ) {
144+ t .Errorf ("expected keywords to be %+v but was %+v" , mfst .Keywords , b .Keywords )
145+ }
146+ checksPerformed ++
147+
148+ if ! reflect .DeepEqual (b .Maintainers , mfst .Maintainers ) {
149+ t .Errorf ("expected maintainers to be %+v but was %+v" , mfst .Maintainers , b .Maintainers )
150+ }
151+ checksPerformed ++
152+
153+ if b .Name != mfst .Name {
154+ t .Errorf ("expected name to be %+v but was %+v" , mfst .Name , b .Name )
155+ }
156+ checksPerformed ++
157+
158+ if ! reflect .DeepEqual (b .Outputs , mfst .Outputs ) {
159+ t .Errorf ("expected outputs to be %+v but was %+v" , mfst .Outputs , b .Outputs )
160+ }
161+ checksPerformed ++
162+
163+ if ! reflect .DeepEqual (b .Parameters , mfst .Parameters ) {
164+ t .Errorf ("expected parameters to be %+v but was %+v" , mfst .Parameters , b .Parameters )
165+ }
166+ checksPerformed ++
167+
168+ if b .SchemaVersion != mfst .SchemaVersion {
169+ t .Errorf ("expected schemaVersion %v, got %v" , mfst .SchemaVersion , b .SchemaVersion )
170+ }
171+ checksPerformed ++
172+
173+ if b .Version != mfst .Version {
174+ t .Errorf ("expected version %v, got %v" , mfst .Version , b .Version )
175+ }
176+ checksPerformed ++
177+
178+ // Ensure that all the fields have been checked. If the structures need to diverge in the future, this test should be modified.
179+ mfstFields := getFields (manifest.Manifest {})
180+ if len (mfstFields ) != checksPerformed {
181+ t .Errorf ("expected to check %v fields for equality, but checked only %v fields" , len (mfstFields ), checksPerformed )
182+ }
183+ }
184+
185+ func TestBundleAndManifestHaveSameFields (t * testing.T ) {
186+ mfst := manifest.Manifest {}
187+ mfstFields := getFields (mfst )
188+
189+ b := bundle.Bundle {}
190+ bundleFields := getFields (b )
191+
192+ if ! reflect .DeepEqual (bundleFields , mfstFields ) {
193+ t .Errorf ("manifest and bundle have different fields.\n manifest: %+v\n bundle: %+v\n " , mfstFields , bundleFields )
194+ }
195+ }
196+
197+ func getFields (i interface {}) map [string ]struct {} {
198+ fields := make (map [string ]struct {}, 15 )
199+
200+ v := reflect .ValueOf (i )
201+ typeOf := reflect .TypeOf (i )
202+
203+ for i := 0 ; i < v .NumField (); i ++ {
204+ fields [typeOf .Field (i ).Name ] = struct {}{}
205+ }
206+ return fields
106207}
0 commit comments