This repository has been archived by the owner on May 3, 2022. It is now read-only.
/
bundle_test.go
202 lines (181 loc) · 4.69 KB
/
bundle_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package bundle
import (
"io/ioutil"
"testing"
"github.com/stretchr/testify/assert"
)
func TestReadTopLevelProperties(t *testing.T) {
json := `{
"name": "foo",
"version": "1.0",
"images": {},
"credentials": {}
}`
bundle, err := Unmarshal([]byte(json))
if err != nil {
t.Fatal(err)
}
if bundle.Name != "foo" {
t.Errorf("Expected name 'foo', got '%s'", bundle.Name)
}
if bundle.Version != "1.0" {
t.Errorf("Expected version '1.0', got '%s'", bundle.Version)
}
if len(bundle.Images) != 0 {
t.Errorf("Expected no images, got %d", len(bundle.Images))
}
if len(bundle.Credentials) != 0 {
t.Errorf("Expected no credentials, got %d", len(bundle.Credentials))
}
}
func TestReadImageProperties(t *testing.T) {
data, err := ioutil.ReadFile("../../tests/testdata/bundles/foo.json")
if err != nil {
t.Errorf("cannot read bundle file: %v", err)
}
bundle, err := Unmarshal(data)
if err != nil {
t.Fatal(err)
}
if len(bundle.Images) != 2 {
t.Errorf("Expected 2 images, got %d", len(bundle.Images))
}
image1 := bundle.Images["image1"]
if image1.Description != "image1" {
t.Errorf("Expected description 'image1', got '%s'", image1.Description)
}
if image1.Image != "urn:image1uri" {
t.Errorf("Expected Image 'urn:image1uri', got '%s'", image1.Image)
}
if len(image1.Refs) != 1 {
t.Errorf("Expected 1 ref, got %d", len(image1.Refs))
}
}
func TestReadCredentialProperties(t *testing.T) {
data, err := ioutil.ReadFile("../../tests/testdata/bundles/foo.json")
if err != nil {
t.Errorf("cannot read bundle file: %v", err)
}
bundle, err := Unmarshal(data)
if err != nil {
t.Fatal(err)
}
if len(bundle.Credentials) != 3 {
t.Errorf("Expected 3 credentials, got %d", len(bundle.Credentials))
}
f := bundle.Credentials["foo"]
if f.Path != "pfoo" {
t.Errorf("Expected path 'pfoo', got '%s'", f.Path)
}
if f.EnvironmentVariable != "" {
t.Errorf("Expected env '', got '%s'", f.EnvironmentVariable)
}
b := bundle.Credentials["bar"]
if b.Path != "" {
t.Errorf("Expected path '', got '%s'", b.Path)
}
if b.EnvironmentVariable != "ebar" {
t.Errorf("Expected env 'ebar', got '%s'", b.EnvironmentVariable)
}
q := bundle.Credentials["quux"]
if q.Path != "pquux" {
t.Errorf("Expected path 'pquux', got '%s'", q.Path)
}
if q.EnvironmentVariable != "equux" {
t.Errorf("Expected env 'equux', got '%s'", q.EnvironmentVariable)
}
}
func TestValuesOrDefaults(t *testing.T) {
is := assert.New(t)
vals := map[string]interface{}{
"port": 8080,
"host": "localhost",
"enabled": true,
}
b := &Bundle{
Parameters: map[string]ParameterDefinition{
"port": {
DataType: "int",
DefaultValue: 1234,
},
"host": {
DataType: "string",
DefaultValue: "localhost.localdomain",
},
"enabled": {
DataType: "bool",
DefaultValue: false,
},
"replicaCount": {
DataType: "int",
DefaultValue: 3,
},
},
}
vod, err := ValuesOrDefaults(vals, b)
is.NoError(err)
is.True(vod["enabled"].(bool))
is.Equal(vod["host"].(string), "localhost")
is.Equal(vod["port"].(int), 8080)
is.Equal(vod["replicaCount"].(int), 3)
// This should err out because of type problem
vals["replicaCount"] = "banana"
_, err = ValuesOrDefaults(vals, b)
is.Error(err)
}
func TestValuesOrDefaults_Required(t *testing.T) {
is := assert.New(t)
vals := map[string]interface{}{
"enabled": true,
}
b := &Bundle{
Parameters: map[string]ParameterDefinition{
"minimum": {
DataType: "int",
Required: true,
},
"enabled": {
DataType: "bool",
DefaultValue: false,
},
},
}
_, err := ValuesOrDefaults(vals, b)
is.Error(err)
// It is unclear what the outcome should be when the user supplies
// empty values on purpose. For now, we will assume those meet the
// minimum definition of "required", and that other rules will
// correct for empty values.
//
// Example: It makes perfect sense for a user to specify --set minimum=0
// and in so doing meet the requirement that a value be specified.
vals["minimum"] = 0
res, err := ValuesOrDefaults(vals, b)
is.NoError(err)
is.Equal(0, res["minimum"])
}
func TestValidateVersionTag(t *testing.T) {
is := assert.New(t)
img := InvocationImage{BaseImage{}}
b := Bundle{
Version: "latest",
InvocationImages: []InvocationImage{img},
}
err := b.Validate()
is.EqualError(err, "'latest' is not a valid bundle version")
}
func TestValidateBundle_RequiresInvocationImage(t *testing.T) {
b := Bundle{
Name: "bar",
Version: "0.1.0",
}
err := b.Validate()
if err == nil {
t.Fatal("Validate should have failed because the bundle has no invocation images")
}
b.InvocationImages = append(b.InvocationImages, InvocationImage{})
err = b.Validate()
if err != nil {
t.Fatal(err)
}
}