@@ -3,11 +3,12 @@ package internal
3
3
import (
4
4
"testing"
5
5
6
- "github.com/operator-framework/api/pkg/manifests"
7
- "github.com/operator-framework/api/pkg/operators/v1alpha1"
6
+ "github.com/stretchr/testify/require"
8
7
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
9
8
10
- "github.com/stretchr/testify/require"
9
+ "github.com/operator-framework/api/pkg/manifests"
10
+ "github.com/operator-framework/api/pkg/operators/v1alpha1"
11
+ "github.com/operator-framework/api/pkg/validation/errors"
11
12
)
12
13
13
14
func TestValidateBundle (t * testing.T ) {
@@ -160,3 +161,79 @@ func TestValidateServiceAccount(t *testing.T) {
160
161
})
161
162
}
162
163
}
164
+
165
+ func TestBundleSize (t * testing.T ) {
166
+ type args struct {
167
+ size int64
168
+ }
169
+ tests := []struct {
170
+ name string
171
+ args args
172
+ wantError bool
173
+ wantWarning bool
174
+ errStrings []string
175
+ warnStrings []string
176
+ }{
177
+ {
178
+ name : "should pass when the size is not bigger or closer of the limit" ,
179
+ args : args {
180
+ size : int64 (max_bundle_size / 2 ),
181
+ },
182
+ },
183
+ {
184
+ name : "should warn when the size is closer of the limit" ,
185
+ args : args {
186
+ size : int64 (max_bundle_size - 10 ),
187
+ },
188
+ wantWarning : true ,
189
+ warnStrings : []string {"Warning: : nearing maximum bundle compressed size with gzip: size=~3 MegaByte, max=4 MegaByte" },
190
+ },
191
+ {
192
+ name : "should warn when is not possible to check the size" ,
193
+ wantWarning : true ,
194
+ warnStrings : []string {"Warning: : unable to check the bundle size" },
195
+ },
196
+ {
197
+ name : "should raise an error when the size is bigger than the limit" ,
198
+ args : args {
199
+ size : int64 (2 * max_bundle_size ),
200
+ },
201
+ wantError : true ,
202
+ errStrings : []string {"Error: : maximum bundle compressed size with gzip size exceeded: size=~8 MegaByte, max=4 MegaByte" },
203
+ },
204
+ }
205
+ for _ , tt := range tests {
206
+ t .Run (tt .name , func (t * testing.T ) {
207
+ bundle := & manifests.Bundle {
208
+ CompressedSize : & tt .args .size ,
209
+ }
210
+ result := validateBundleSize (bundle )
211
+
212
+ var warns , errs []errors.Error
213
+ for _ , r := range result {
214
+ if r .Level == errors .LevelWarn {
215
+ warns = append (warns , r )
216
+ } else if r .Level == errors .LevelError {
217
+ errs = append (errs , r )
218
+ }
219
+ }
220
+ require .Equal (t , tt .wantWarning , len (warns ) > 0 )
221
+ if tt .wantWarning {
222
+ require .Equal (t , len (tt .warnStrings ), len (warns ))
223
+ for _ , w := range warns {
224
+ wString := w .Error ()
225
+ require .Contains (t , tt .warnStrings , wString )
226
+ }
227
+ }
228
+
229
+ require .Equal (t , tt .wantError , len (errs ) > 0 )
230
+ if tt .wantError {
231
+ require .Equal (t , len (tt .errStrings ), len (errs ))
232
+ for _ , err := range errs {
233
+ errString := err .Error ()
234
+ require .Contains (t , tt .errStrings , errString )
235
+ }
236
+ }
237
+ })
238
+ }
239
+ }
0 commit comments