Skip to content

Commit 0eb506a

Browse files
camilamacedo86perdasilva
authored andcommitted
fix: ensure that all load bundle methods will check the size (#227)
Upstream-repository: api Upstream-commit: bbe9c3235aff58180a439b7f1b4b05aade99cc65
1 parent 9376f83 commit 0eb506a

File tree

3 files changed

+100
-14
lines changed

3 files changed

+100
-14
lines changed

staging/api/pkg/manifests/bundleloader.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,7 @@ func (b *bundleLoader) LoadBundle() error {
3636
errs = append(errs, err)
3737
}
3838

39-
// Compress the bundle to check its size
40-
if data, err := os.ReadFile(b.dir); err == nil {
41-
if content, err := encoding.GzipBase64Encode(data); err != nil {
42-
total := int64(len(content))
43-
b.bundle.CompressedSize = &total
44-
}
45-
}
39+
errs = append(errs, b.calculateCompressedBundleSize())
4640

4741
if !b.foundCSV {
4842
errs = append(errs, fmt.Errorf("unable to find a csv in bundle directory %s", b.dir))
@@ -53,6 +47,35 @@ func (b *bundleLoader) LoadBundle() error {
5347
return utilerrors.NewAggregate(errs)
5448
}
5549

50+
// Compress the bundle to check its size
51+
func (b *bundleLoader) calculateCompressedBundleSize() error {
52+
err := filepath.Walk(b.dir,
53+
func(path string, info os.FileInfo, err error) error {
54+
if err != nil {
55+
return err
56+
}
57+
58+
if !info.IsDir() {
59+
if data, err := os.ReadFile(path); err == nil {
60+
content, err := encoding.GzipBase64Encode(data)
61+
if err != nil {
62+
return err
63+
}
64+
total := int64(len(content))
65+
if b.bundle.CompressedSize != nil {
66+
total += *b.bundle.CompressedSize
67+
}
68+
b.bundle.CompressedSize = &total
69+
}
70+
}
71+
return nil
72+
})
73+
if err != nil {
74+
return err
75+
}
76+
return nil
77+
}
78+
5679
// collectWalkErrs calls the given walk func and appends any non-nil, non skip dir error returned to the given errors slice.
5780
func collectWalkErrs(walk filepath.WalkFunc, errs *[]error) filepath.WalkFunc {
5881
return func(path string, f os.FileInfo, err error) (walkErr error) {

staging/api/pkg/validation/internal/bundle_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,43 @@ func TestBundleSize(t *testing.T) {
237237
})
238238
}
239239
}
240+
241+
func Test_EnsureGetBundleSizeValue(t *testing.T) {
242+
type args struct {
243+
annotations map[string]string
244+
bundleDir string
245+
imageIndexPath string
246+
}
247+
tests := []struct {
248+
name string
249+
args args
250+
wantWarning bool
251+
warnStrings []string
252+
}{
253+
{
254+
name: "should calculate the bundle size and not raise warnings when a valid bundle is informed",
255+
args: args{
256+
bundleDir: "./testdata/valid_bundle",
257+
},
258+
},
259+
}
260+
261+
for _, tt := range tests {
262+
t.Run(tt.name, func(t *testing.T) {
263+
264+
// Validate the bundle object
265+
bundle, err := manifests.GetBundleFromDir(tt.args.bundleDir)
266+
require.NoError(t, err)
267+
268+
results := validateBundle(bundle)
269+
require.Equal(t, tt.wantWarning, len(results.Warnings) > 0)
270+
if tt.wantWarning {
271+
require.Equal(t, len(tt.warnStrings), len(results.Warnings))
272+
for _, w := range results.Warnings {
273+
wString := w.Error()
274+
require.Contains(t, tt.warnStrings, wString)
275+
}
276+
}
277+
})
278+
}
279+
}

vendor/github.com/operator-framework/api/pkg/manifests/bundleloader.go

Lines changed: 30 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)