-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathoperatorhub_test.go
263 lines (237 loc) · 7.38 KB
/
operatorhub_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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package internal
import (
"os"
"testing"
"github.com/operator-framework/api/pkg/manifests"
"github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/stretchr/testify/require"
)
func TestValidateBundleOperatorHub(t *testing.T) {
var table = []struct {
description string
directory string
hasError bool
errStrings []string
}{
{
description: "registryv1 bundle/valid bundle",
directory: "./testdata/valid_bundle",
hasError: false,
},
{
description: "registryv1 bundle/invald bundle operatorhubio",
directory: "./testdata/invalid_bundle_operatorhub",
hasError: true,
errStrings: []string{
`Error: Value : (etcdoperator.v0.9.4) csv.Spec.Provider.Name not specified`,
`Error: Value : (etcdoperator.v0.9.4) csv.Spec.Maintainers elements should contain both name and email`,
`Error: Value : (etcdoperator.v0.9.4) csv.Spec.Maintainers email invalidemail is invalid: mail: missing '@' or angle-addr`,
`Error: Value : (etcdoperator.v0.9.4) csv.Spec.Links elements should contain both name and url`,
`Error: Value : (etcdoperator.v0.9.4) csv.Spec.Links url https//coreos.com/operators/etcd/docs/latest/ is invalid: parse "https//coreos.com/operators/etcd/docs/latest/": invalid URI for request`,
`Error: Value : (etcdoperator.v0.9.4) csv.Metadata.Annotations.Capabilities Installs and stuff is not a valid capabilities level`,
`Error: Value : (etcdoperator.v0.9.4) csv.Spec.Icon should only have one element`,
`Error: Value : (etcdoperator.v0.9.4) csv.Metadata.Annotations["categories"] value Magic is not in the set of default categories`,
`Error: Value : (etcdoperator.v0.9.4) csv.Spec.Version must be set`,
},
},
}
for _, tt := range table {
// Validate the bundle object
bundle, err := manifests.GetBundleFromDir(tt.directory)
require.NoError(t, err)
results := OperatorHubValidator.Validate(bundle)
if len(results) > 0 {
require.Equal(t, results[0].HasError(), tt.hasError)
if results[0].HasError() {
require.Equal(t, len(tt.errStrings), len(results[0].Errors))
for _, err := range results[0].Errors {
errString := err.Error()
require.Contains(t, tt.errStrings, errString)
}
}
}
}
}
func TestCustomCategories(t *testing.T) {
var table = []struct {
description string
directory string
hasError bool
errStrings []string
custom bool
}{
{
description: "valid bundle custom categories",
directory: "./testdata/valid_bundle_custom_categories",
hasError: false,
custom: true,
},
{
description: "valid bundle standard categories",
directory: "./testdata/valid_bundle",
hasError: false,
custom: false,
},
}
for _, tt := range table {
if tt.custom {
os.Setenv("OPERATOR_BUNDLE_CATEGORIES", "./testdata/categories.json")
} else {
os.Setenv("OPERATOR_BUNDLE_CATEGORIES", "")
}
// Validate the bundle object
bundle, err := manifests.GetBundleFromDir(tt.directory)
require.NoError(t, err)
results := OperatorHubValidator.Validate(bundle)
if len(results) > 0 {
require.Equal(t, results[0].HasError(), tt.hasError)
if results[0].HasError() {
require.Equal(t, len(tt.errStrings), len(results[0].Errors))
for _, err := range results[0].Errors {
errString := err.Error()
require.Contains(t, tt.errStrings, errString)
}
}
}
}
}
func TestExtractCategories(t *testing.T) {
path := "./testdata/categories.json"
categories, err := extractCategories(path)
if err != nil {
t.Fatalf("extracting categories.json: %s", err)
}
expected := map[string]struct{}{
"Cloud Pak": {},
"Registry": {},
"MyCoolThing": {},
"This/Or & That": {},
}
for key := range categories {
if _, ok := expected[key]; !ok {
t.Fatalf("did not find key %s", key)
}
}
}
func TestCheckSpecIcon(t *testing.T) {
validIcon := v1alpha1.Icon{
Data: "iVBORw0KGgoAAAANSUhEUgAAAOEAAADZCAYAAADWmle6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAGXRFWHRTb2Z0d2Fy",
MediaType: "image/png",
}
invalidIcon := v1alpha1.Icon{
MediaType: "image/png",
}
invalidMediaTypeIcon := validIcon
invalidMediaTypeIcon.MediaType = "invalid"
type args struct {
icon []v1alpha1.Icon
}
tests := []struct {
name string
args args
wantError bool
wantWarning bool
errStrings []string
warnStrings []string
}{
{
name: "should work with a valid value",
args: args{icon: []v1alpha1.Icon{validIcon}},
},
{
name: "should return an warning when the icon is not provided",
wantWarning: true,
warnStrings: []string{"csv.Spec.Icon not specified"},
},
{
name: "should fail when the data informed for the icon is invalid",
args: args{icon: []v1alpha1.Icon{invalidIcon}},
wantError: true,
errStrings: []string{"csv.Spec.Icon elements should contain both data and mediatype"},
},
{
name: "should fail when the data informed has not a valid MediaType",
args: args{icon: []v1alpha1.Icon{invalidMediaTypeIcon}},
wantError: true,
errStrings: []string{"csv.Spec.Icon invalid does not have a valid mediatype"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
csv := v1alpha1.ClusterServiceVersion{}
csv.Spec.Icon = tt.args.icon
checks := CSVChecks{csv: csv, errs: []error{}, warns: []error{}}
result := checkSpecIcon(checks)
require.Equal(t, tt.wantWarning, len(result.warns) > 0)
if tt.wantWarning {
require.Equal(t, len(tt.warnStrings), len(result.warns))
for _, w := range result.warns {
wString := w.Error()
require.Contains(t, tt.warnStrings, wString)
}
}
require.Equal(t, tt.wantError, len(result.errs) > 0)
if tt.wantError {
require.Equal(t, len(tt.errStrings), len(result.errs))
for _, err := range result.errs {
errString := err.Error()
require.Contains(t, tt.errStrings, errString)
}
}
})
}
}
func TestCheckSpecMinKubeVersion(t *testing.T) {
type args struct {
minKubeVersion string
}
tests := []struct {
name string
args args
wantError bool
wantWarning bool
errStrings []string
warnStrings []string
}{
{
name: "should work with a valid value",
args: args{minKubeVersion: "1.16"},
},
{
name: "should return a warning when the minKubeVersion is not informed ",
args: args{minKubeVersion: ""},
wantWarning: true,
warnStrings: []string{minKubeVersionWarnMessage},
},
{
name: "should fail when an invalid value is informed",
args: args{minKubeVersion: "alpha1"},
wantError: true,
errStrings: []string{"csv.Spec.MinKubeVersion has an invalid value: alpha1"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
csv := v1alpha1.ClusterServiceVersion{}
csv.Spec.MinKubeVersion = tt.args.minKubeVersion
checks := CSVChecks{csv: csv, errs: []error{}, warns: []error{}}
result := checkSpecMinKubeVersion(checks)
require.Equal(t, tt.wantWarning, len(result.warns) > 0)
if tt.wantWarning {
require.Equal(t, len(tt.warnStrings), len(result.warns))
for _, w := range result.warns {
wString := w.Error()
require.Contains(t, tt.warnStrings, wString)
}
}
require.Equal(t, tt.wantError, len(result.errs) > 0)
if tt.wantError {
require.Equal(t, len(tt.errStrings), len(result.errs))
for _, err := range result.errs {
errString := err.Error()
require.Contains(t, tt.errStrings, errString)
}
}
})
}
}