Skip to content

Commit 5f37a67

Browse files
perdasilvaPer Goncalves da Silva
andauthored
Update NewBundleFS test utility to builder pattern (#2237)
Signed-off-by: Per Goncalves da Silva <[email protected]> Co-authored-by: Per Goncalves da Silva <[email protected]>
1 parent f3557e0 commit 5f37a67

File tree

6 files changed

+308
-92
lines changed

6 files changed

+308
-92
lines changed

internal/operator-controller/applier/boxcutter_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ import (
2424
"sigs.k8s.io/controller-runtime/pkg/client"
2525
"sigs.k8s.io/controller-runtime/pkg/client/fake"
2626

27+
"github.com/operator-framework/api/pkg/operators/v1alpha1"
28+
2729
ocv1 "github.com/operator-framework/operator-controller/api/v1"
2830
"github.com/operator-framework/operator-controller/internal/operator-controller/applier"
2931
"github.com/operator-framework/operator-controller/internal/operator-controller/controllers"
3032
"github.com/operator-framework/operator-controller/internal/operator-controller/labels"
3133
"github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/bundle"
3234
"github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/render"
3335
testutils "github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/util/testing"
36+
"github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/util/testing/bundlefs"
3437
)
3538

3639
func Test_RegistryV1BundleRenderer_Render_Success(t *testing.T) {
@@ -52,7 +55,9 @@ func Test_RegistryV1BundleRenderer_Render_Success(t *testing.T) {
5255
},
5356
},
5457
}
55-
bundleFS := testutils.NewBundleFS()
58+
bundleFS := bundlefs.Builder().
59+
WithPackageName("some-package").
60+
WithCSV(testutils.MakeCSV(testutils.WithInstallModeSupportFor(v1alpha1.InstallModeTypeAllNamespaces))).Build()
5661

5762
objs, err := r.Render(bundleFS, &ocv1.ClusterExtension{
5863
Spec: ocv1.ClusterExtensionSpec{
@@ -74,7 +79,9 @@ func Test_RegistryV1BundleRenderer_Render_Failure(t *testing.T) {
7479
},
7580
},
7681
}
77-
bundleFS := testutils.NewBundleFS()
82+
bundleFS := bundlefs.Builder().
83+
WithPackageName("some-package").
84+
WithCSV(testutils.MakeCSV(testutils.WithInstallModeSupportFor(v1alpha1.InstallModeTypeAllNamespaces))).Build()
7885

7986
objs, err := r.Render(bundleFS, &ocv1.ClusterExtension{
8087
Spec: ocv1.ClusterExtensionSpec{

internal/operator-controller/rukpak/bundle/source/source.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ type BundleSource interface {
2424
GetBundle() (bundle.RegistryV1, error)
2525
}
2626

27+
type RegistryV1Properties struct {
28+
Properties []property.Property `json:"properties"`
29+
}
30+
2731
// identitySource is a bundle source that returns itself
2832
type identitySource bundle.RegistryV1
2933

@@ -158,11 +162,7 @@ func copyMetadataPropertiesToCSV(csv *v1alpha1.ClusterServiceVersion, fsys fs.FS
158162

159163
// Otherwise, we need to parse the properties.yaml file and
160164
// append its properties into the CSV annotation.
161-
type registryV1Properties struct {
162-
Properties []property.Property `json:"properties"`
163-
}
164-
165-
var metadataProperties registryV1Properties
165+
var metadataProperties RegistryV1Properties
166166
if err := yaml.Unmarshal(metadataPropertiesJSON, &metadataProperties); err != nil {
167167
return fmt.Errorf("failed to unmarshal metadata/properties.yaml: %w", err)
168168
}

internal/operator-controller/rukpak/bundle/source/source_test.go

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ package source_test
22

33
import (
44
"io/fs"
5-
"strings"
65
"testing"
7-
"testing/fstest"
86

97
"github.com/stretchr/testify/require"
8+
corev1 "k8s.io/api/core/v1"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/utils/ptr"
11+
12+
"github.com/operator-framework/api/pkg/operators/v1alpha1"
1013

1114
"github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/bundle"
1215
"github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/bundle/source"
13-
. "github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/util/testing"
16+
testutils "github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/util/testing"
17+
"github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/util/testing/bundlefs"
1418
)
1519

1620
const (
@@ -27,7 +31,18 @@ func Test_FromBundle_Success(t *testing.T) {
2731
}
2832

2933
func Test_FromFS_Success(t *testing.T) {
30-
rv1, err := source.FromFS(NewBundleFS()).GetBundle()
34+
bundleFS := bundlefs.Builder().
35+
WithPackageName("test").
36+
WithBundleProperty("from-file-key", "from-file-value").
37+
WithBundleResource("csv.yaml", ptr.To(testutils.MakeCSV(
38+
testutils.WithName("test.v1.0.0"),
39+
testutils.WithAnnotations(map[string]string{
40+
"olm.properties": `[{"type":"from-csv-annotations-key", "value":"from-csv-annotations-value"}]`,
41+
}),
42+
testutils.WithInstallModeSupportFor(v1alpha1.InstallModeTypeAllNamespaces)),
43+
)).Build()
44+
45+
rv1, err := source.FromFS(bundleFS).GetBundle()
3146
require.NoError(t, err)
3247

3348
t.Log("Check package name is correctly taken from metadata/annotations.yaml")
@@ -44,16 +59,30 @@ func Test_FromFS_Fails(t *testing.T) {
4459
}{
4560
{
4661
name: "bundle missing ClusterServiceVersion manifest",
47-
FS: removePaths(NewBundleFS(), BundlePathCSV),
62+
FS: bundlefs.Builder().
63+
WithPackageName("test").
64+
WithBundleProperty("foo", "bar").
65+
WithBundleResource("service.yaml", &corev1.Service{
66+
TypeMeta: metav1.TypeMeta{
67+
Kind: "Service",
68+
APIVersion: corev1.SchemeGroupVersion.String(),
69+
},
70+
}).Build(),
4871
}, {
4972
name: "bundle missing metadata/annotations.yaml",
50-
FS: removePaths(NewBundleFS(), BundlePathAnnotations),
73+
FS: bundlefs.Builder().
74+
WithBundleProperty("foo", "bar").
75+
WithBundleResource("csv.yaml", ptr.To(testutils.MakeCSV())).Build(),
5176
}, {
52-
name: "bundle missing metadata/ directory",
53-
FS: removePaths(NewBundleFS(), "metadata/"),
77+
name: "metadata/annotations.yaml missing package name annotation",
78+
FS: bundlefs.Builder().
79+
WithBundleProperty("foo", "bar").
80+
WithBundleResource("csv.yaml", ptr.To(testutils.MakeCSV())).Build(),
5481
}, {
55-
name: "bundle missing manifests/ directory",
56-
FS: removePaths(NewBundleFS(), "manifests/"),
82+
name: "bundle missing manifests directory",
83+
FS: bundlefs.Builder().
84+
WithPackageName("test").
85+
WithBundleProperty("foo", "bar").Build(),
5786
},
5887
} {
5988
t.Run(tt.name, func(t *testing.T) {
@@ -62,14 +91,3 @@ func Test_FromFS_Fails(t *testing.T) {
6291
})
6392
}
6493
}
65-
66-
func removePaths(mapFs fstest.MapFS, paths ...string) fstest.MapFS {
67-
for k := range mapFs {
68-
for _, path := range paths {
69-
if strings.HasPrefix(k, path) {
70-
delete(mapFs, k)
71-
}
72-
}
73-
}
74-
return mapFs
75-
}

internal/operator-controller/rukpak/util/testing/bundlefs.go

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package bundlefs
2+
3+
import (
4+
"fmt"
5+
"path/filepath"
6+
"strings"
7+
"testing/fstest"
8+
9+
"sigs.k8s.io/controller-runtime/pkg/client"
10+
"sigs.k8s.io/yaml"
11+
12+
"github.com/operator-framework/api/pkg/operators/v1alpha1"
13+
"github.com/operator-framework/operator-registry/alpha/property"
14+
15+
"github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/bundle/source"
16+
registry "github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/operator-registry"
17+
)
18+
19+
const (
20+
BundlePathAnnotations = "metadata/annotations.yaml"
21+
BundlePathProperties = "metadata/properties.yaml"
22+
BundlePathManifests = "manifests"
23+
)
24+
25+
// BundleFSBuilder builds a registry+v1 bundle filesystem
26+
type BundleFSBuilder interface {
27+
WithPackageName(packageName string) BundleFSBuilder
28+
WithChannels(channels ...string) BundleFSBuilder
29+
WithDefaultChannel(channel string) BundleFSBuilder
30+
WithBundleProperty(propertyType string, value string) BundleFSBuilder
31+
WithBundleResource(resourceName string, resource client.Object) BundleFSBuilder
32+
WithCSV(csv v1alpha1.ClusterServiceVersion) BundleFSBuilder
33+
Build() fstest.MapFS
34+
}
35+
36+
// bundleFSBuilder builds a registry+v1 bundle filesystem
37+
type bundleFSBuilder struct {
38+
annotations *registry.Annotations
39+
properties []property.Property
40+
resources map[string]client.Object
41+
}
42+
43+
func Builder() BundleFSBuilder {
44+
return &bundleFSBuilder{}
45+
}
46+
47+
// WithPackageName is an option for NewBundleFS used to set the package name annotation in the
48+
// bundle filesystem metadata/annotations.yaml file
49+
func (b *bundleFSBuilder) WithPackageName(packageName string) BundleFSBuilder {
50+
if b.annotations == nil {
51+
b.annotations = &registry.Annotations{}
52+
}
53+
b.annotations.PackageName = packageName
54+
return b
55+
}
56+
57+
// WithChannels is an option for NewBundleFS used to set the channels annotation in the
58+
// bundle filesystem metadata/annotations.yaml file
59+
func (b *bundleFSBuilder) WithChannels(channels ...string) BundleFSBuilder {
60+
if b.annotations == nil {
61+
b.annotations = &registry.Annotations{}
62+
}
63+
b.annotations.Channels = strings.Join(channels, ",")
64+
return b
65+
}
66+
67+
// WithDefaultChannel is an option for NewBundleFS used to set the channel annotation in the
68+
// bundle filesystem metadata/annotations.yaml file
69+
func (b *bundleFSBuilder) WithDefaultChannel(channel string) BundleFSBuilder {
70+
if b.annotations == nil {
71+
b.annotations = &registry.Annotations{}
72+
}
73+
b.annotations.DefaultChannelName = channel
74+
return b
75+
}
76+
77+
// WithBundleProperty is an options for NewBundleFS used to add a property to the list of properties
78+
// in the bundle filesystem metadata/properties.yaml file
79+
func (b *bundleFSBuilder) WithBundleProperty(propertyType string, value string) BundleFSBuilder {
80+
b.properties = append(b.properties, property.Property{
81+
Type: propertyType,
82+
Value: []byte(`"` + value + `"`),
83+
})
84+
return b
85+
}
86+
87+
// WithBundleResource is an option for NewBundleFS use to add the yaml representation of resource to the
88+
// path manifests/<resourceName>.yaml on the bundles filesystem
89+
func (b *bundleFSBuilder) WithBundleResource(resourceName string, resource client.Object) BundleFSBuilder {
90+
if b.resources == nil {
91+
b.resources = make(map[string]client.Object)
92+
}
93+
b.resources[resourceName] = resource
94+
return b
95+
}
96+
97+
// WithCSV is an optiona for NewBundleFS used to add the yaml representation of csv to the
98+
// path manifests/csv.yaml on the bundle filesystem
99+
func (b *bundleFSBuilder) WithCSV(csv v1alpha1.ClusterServiceVersion) BundleFSBuilder {
100+
if b.resources == nil {
101+
b.resources = make(map[string]client.Object)
102+
}
103+
b.resources["csv.yaml"] = &csv
104+
return b
105+
}
106+
107+
// Build creates a registry+v1 bundle filesystem with the applied options
108+
// By default, an empty registry+v1 bundle filesystem will be returned
109+
func (b *bundleFSBuilder) Build() fstest.MapFS {
110+
bundleFS := fstest.MapFS{}
111+
112+
// Add annotations metadata
113+
if b.annotations != nil {
114+
annotationsYml, err := yaml.Marshal(registry.AnnotationsFile{
115+
Annotations: *b.annotations,
116+
})
117+
if err != nil {
118+
panic(fmt.Errorf("error building bundle fs: %w", err))
119+
}
120+
bundleFS[BundlePathAnnotations] = &fstest.MapFile{Data: annotationsYml}
121+
}
122+
123+
// Add property metadata
124+
if len(b.properties) > 0 {
125+
propertiesYml, err := yaml.Marshal(source.RegistryV1Properties{
126+
Properties: b.properties,
127+
})
128+
if err != nil {
129+
panic(fmt.Errorf("error building bundle fs: %w", err))
130+
}
131+
bundleFS[BundlePathProperties] = &fstest.MapFile{Data: propertiesYml}
132+
}
133+
134+
// Add resources
135+
for name, obj := range b.resources {
136+
resourcePath := filepath.Join(BundlePathManifests, name)
137+
resourceYml, err := yaml.Marshal(obj)
138+
if err != nil {
139+
panic(fmt.Errorf("error building bundle fs: %w", err))
140+
}
141+
bundleFS[resourcePath] = &fstest.MapFile{Data: resourceYml}
142+
}
143+
144+
return bundleFS
145+
}

0 commit comments

Comments
 (0)