Skip to content
This repository was archived by the owner on May 3, 2022. It is now read-only.

Commit 89c368f

Browse files
Swapnil Bawaskarglyn
authored andcommitted
keep bundle and manifest in sync (#794)
* keep bundle and manifest in sync copy all fields from manifest to bundle to ensure that bundle is not missing any configuration that the bundle author intended. Added unit tests to keep the two structure in sync Co-Authored-By: Glyn Normington <gnormington@pivotal.io>
1 parent 465d589 commit 89c368f

File tree

3 files changed

+121
-15
lines changed

3 files changed

+121
-15
lines changed

pkg/builder/builder.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,19 @@ func (b *Builder) PrepareBuild(bldr *Builder, mfst *manifest.Manifest, appDir st
6767
}
6868

6969
bf := &bundle.Bundle{
70-
Name: ctx.Manifest.Name,
70+
Actions: ctx.Manifest.Actions,
71+
Credentials: ctx.Manifest.Credentials,
72+
Custom: ctx.Manifest.Custom,
73+
Definitions: ctx.Manifest.Definitions,
7174
Description: ctx.Manifest.Description,
7275
Images: ctx.Manifest.Images,
7376
Keywords: ctx.Manifest.Keywords,
7477
Maintainers: ctx.Manifest.Maintainers,
75-
Actions: ctx.Manifest.Actions,
78+
Name: ctx.Manifest.Name,
79+
Outputs: ctx.Manifest.Outputs,
7680
Parameters: ctx.Manifest.Parameters,
77-
Credentials: ctx.Manifest.Credentials,
78-
Version: ctx.Manifest.Version,
7981
SchemaVersion: ctx.Manifest.SchemaVersion,
82+
Version: ctx.Manifest.Version,
8083
}
8184

8285
for _, imb := range imageBuilders {

pkg/builder/builder_test.go

Lines changed: 112 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
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

5354
func 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.\nmanifest: %+v\nbundle: %+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
}

pkg/duffle/manifest/manifest.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ type Manifest struct {
2424
Parameters *bundle.ParametersDefinition `json:"parameters,omitempty" mapstructure:"parameters"`
2525
Credentials map[string]bundle.Credential `json:"credentials,omitempty" mapstructure:"credentials"`
2626
Definitions definition.Definitions `json:"definitions,omitempty" mapstructure:"definitions"`
27+
Outputs *bundle.OutputsDefinition `json:"outputs,omitempty" mapstructure:"outputs"`
28+
Custom map[string]interface{} `json:"custom,omitempty" mapstructure:"custom"`
2729
}
2830

2931
// InvocationImage represents an invocation image component of a CNAB bundle

0 commit comments

Comments
 (0)