/
bundle_test.go
239 lines (225 loc) · 7.32 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
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
package internal
import (
"testing"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/operator-framework/api/pkg/manifests"
"github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/operator-framework/api/pkg/validation/errors"
)
func TestValidateBundle(t *testing.T) {
var table = []struct {
description string
directory string
hasError bool
errString string
}{
{
description: "registryv1 valid bundle",
directory: "./testdata/valid_bundle",
hasError: false,
},
{
description: "registryv1 valid bundle with multiple versions in CRD",
directory: "./testdata/valid_bundle_2",
hasError: false,
},
{
description: "registryv1 invalid bundle without CRD etcdclusters v1beta2 in bundle",
directory: "./testdata/invalid_bundle",
hasError: true,
errString: `owned CRD "etcd.database.coreos.com/v1beta2, Kind=EtcdCluster" not found in bundle`,
},
{
description: "registryv1 invalid bundle without CRD etcdclusters v1beta2 in CSV",
directory: "./testdata/invalid_bundle_2",
hasError: true,
errString: `CRD "etcd.database.coreos.com/v1beta2, Kind=EtcdCluster" is present in bundle "etcdoperator.v0.9.4" but not defined in CSV`,
},
{
description: "registryv1 invalid bundle with duplicate CRD etcdclusters in bundle",
directory: "./testdata/invalid_bundle_3",
hasError: true,
errString: `duplicate CRD "test.example.com/v1alpha1, Kind=Test" in bundle "test-operator.v0.0.1"`,
},
{
description: "invalid bundle service account can't match sa in csv",
directory: "./testdata/invalid_bundle_sa",
hasError: true,
errString: `invalid service account found in bundle. This service account etcd-operator in your bundle is not valid, because a service account with the same name was already specified in your CSV. If this was unintentional, please remove the service account manifest from your bundle. If it was intentional to specify a separate service account, please rename the SA in either the bundle manifest or the CSV.`,
},
}
for _, tt := range table {
t.Run(tt.description, func(t *testing.T) {
// Validate the bundle object
bundle, err := manifests.GetBundleFromDir(tt.directory)
require.NoError(t, err)
results := BundleValidator.Validate(bundle)
require.Greater(t, len(results), 0)
if tt.hasError {
require.True(t, results[0].HasError(), "found no error when an error was expected")
require.Contains(t, results[0].Errors[0].Error(), tt.errString)
} else {
require.False(t, results[0].HasError(), "found error when an error was not expected")
}
})
}
}
func TestValidateServiceAccount(t *testing.T) {
csvWithSAs := func(saNames ...string) *v1alpha1.ClusterServiceVersion {
csv := &v1alpha1.ClusterServiceVersion{}
depSpecs := make([]v1alpha1.StrategyDeploymentSpec, len(saNames))
for i, saName := range saNames {
depSpecs[i].Spec.Template.Spec.ServiceAccountName = saName
}
csv.Spec.InstallStrategy.StrategySpec.DeploymentSpecs = depSpecs
return csv
}
var table = []struct {
description string
bundle *manifests.Bundle
hasError bool
errString string
}{
{
description: "an object with the same name as the service account",
bundle: &manifests.Bundle{
CSV: csvWithSAs("foo"),
Objects: []*unstructured.Unstructured{
{Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "foo",
},
"spec": map[string]interface{}{
"template": map[string]interface{}{
"spec": map[string]interface{}{
"serviceAccountName": "foo",
},
},
},
}},
},
},
hasError: false,
},
{
description: "service account included in both CSV and bundle",
bundle: &manifests.Bundle{
CSV: csvWithSAs("foo"),
Objects: []*unstructured.Unstructured{
{Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"name": "foo",
},
"spec": map[string]interface{}{
"template": map[string]interface{}{
"spec": map[string]interface{}{
"serviceAccountName": "foo",
},
},
},
}},
{Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "ServiceAccount",
"metadata": map[string]interface{}{
"name": "foo",
},
}},
},
},
hasError: true,
errString: `invalid service account found in bundle. This service account foo in your bundle is not valid, because a service account with the same name was already specified in your CSV. If this was unintentional, please remove the service account manifest from your bundle. If it was intentional to specify a separate service account, please rename the SA in either the bundle manifest or the CSV.`,
},
}
for _, tt := range table {
t.Run(tt.description, func(t *testing.T) {
// Validate the bundle object
results := BundleValidator.Validate(tt.bundle)
require.Greater(t, len(results), 0)
if tt.hasError {
require.True(t, results[0].HasError(), "found no error when an error was expected")
require.Contains(t, results[0].Errors[0].Error(), tt.errString)
} else {
require.False(t, results[0].HasError(), "found error when an error was not expected")
}
})
}
}
func TestBundleSize(t *testing.T) {
type args struct {
size int64
}
tests := []struct {
name string
args args
wantError bool
wantWarning bool
errStrings []string
warnStrings []string
}{
{
name: "should pass when the size is not bigger or closer of the limit",
args: args{
size: int64(max_bundle_size / 2),
},
},
{
name: "should warn when the size is closer of the limit",
args: args{
size: int64(max_bundle_size - 10),
},
wantWarning: true,
warnStrings: []string{"Warning: : nearing maximum bundle compressed size with gzip: size=~3 MegaByte, max=4 MegaByte"},
},
{
name: "should warn when is not possible to check the size",
wantWarning: true,
warnStrings: []string{"Warning: : unable to check the bundle size"},
},
{
name: "should raise an error when the size is bigger than the limit",
args: args{
size: int64(2 * max_bundle_size),
},
wantError: true,
errStrings: []string{"Error: : maximum bundle compressed size with gzip size exceeded: size=~8 MegaByte, max=4 MegaByte"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bundle := &manifests.Bundle{
CompressedSize: &tt.args.size,
}
result := validateBundleSize(bundle)
var warns, errs []errors.Error
for _, r := range result {
if r.Level == errors.LevelWarn {
warns = append(warns, r)
} else if r.Level == errors.LevelError {
errs = append(errs, r)
}
}
require.Equal(t, tt.wantWarning, len(warns) > 0)
if tt.wantWarning {
require.Equal(t, len(tt.warnStrings), len(warns))
for _, w := range warns {
wString := w.Error()
require.Contains(t, tt.warnStrings, wString)
}
}
require.Equal(t, tt.wantError, len(errs) > 0)
if tt.wantError {
require.Equal(t, len(tt.errStrings), len(errs))
for _, err := range errs {
errString := err.Error()
require.Contains(t, tt.errStrings, errString)
}
}
})
}
}