@@ -11,14 +11,25 @@ import (
11
11
. "github.com/onsi/gomega"
12
12
"github.com/operator-framework/deppy/pkg/deppy"
13
13
"github.com/operator-framework/deppy/pkg/deppy/input"
14
+ "github.com/operator-framework/operator-controller/api/v1alpha1"
14
15
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
15
16
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/bundles_and_dependencies"
16
17
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/crd_constraints"
17
18
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/olm"
18
19
"github.com/operator-framework/operator-controller/internal/resolution/variable_sources/required_package"
19
20
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21
+ "k8s.io/apimachinery/pkg/runtime"
22
+ utilruntime "k8s.io/apimachinery/pkg/util/runtime"
23
+ "sigs.k8s.io/controller-runtime/pkg/client"
24
+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
20
25
)
21
26
27
+ func FakeClient (objects ... client.Object ) client.Client {
28
+ scheme := runtime .NewScheme ()
29
+ utilruntime .Must (v1alpha1 .AddToScheme (scheme ))
30
+ return fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (objects ... ).Build ()
31
+ }
32
+
22
33
func TestGlobalConstraints (t * testing.T ) {
23
34
RegisterFailHandler (Fail )
24
35
RunSpecs (t , "OLMVariableSource Suite" )
@@ -59,7 +70,7 @@ func withVersionRange(versionRange string) opOption {
59
70
}
60
71
}
61
72
62
- func operator (name string , opts ... opOption ) operatorsv1alpha1.Operator {
73
+ func operator (name string , opts ... opOption ) * operatorsv1alpha1.Operator {
63
74
op := operatorsv1alpha1.Operator {
64
75
ObjectMeta : metav1.ObjectMeta {
65
76
Name : name ,
@@ -73,7 +84,7 @@ func operator(name string, opts ...opOption) operatorsv1alpha1.Operator {
73
84
Fail (err .Error ())
74
85
}
75
86
}
76
- return op
87
+ return & op
77
88
}
78
89
79
90
var _ = Describe ("OLMVariableSource" , func () {
@@ -84,20 +95,30 @@ var _ = Describe("OLMVariableSource", func() {
84
95
})
85
96
86
97
It ("should produce RequiredPackage variables" , func () {
87
- olmVariableSource := olm .NewOLMVariableSource (operator ("prometheus" ), operator ("packageA" ))
98
+ cl := FakeClient (operator ("prometheus" ), operator ("packageA" ))
99
+
100
+ olmVariableSource := olm .NewOLMVariableSource (cl )
88
101
variables , err := olmVariableSource .GetVariables (context .Background (), testEntitySource )
89
102
Expect (err ).ToNot (HaveOccurred ())
90
103
91
104
packageRequiredVariables := filterVariables [* required_package.RequiredPackageVariable ](variables )
92
105
Expect (packageRequiredVariables ).To (HaveLen (2 ))
93
- Expect (packageRequiredVariables [0 ].Identifier ()).To (Equal (deppy .IdentifierFromString ("required package prometheus" )))
94
- Expect (packageRequiredVariables [0 ].BundleEntities ()).To (HaveLen (2 ))
95
- Expect (packageRequiredVariables [1 ].Identifier ()).To (Equal (deppy .IdentifierFromString ("required package packageA" )))
96
- Expect (packageRequiredVariables [1 ].BundleEntities ()).To (HaveLen (1 ))
106
+ Expect (packageRequiredVariables ).To (WithTransform (func (bvars []* required_package.RequiredPackageVariable ) map [deppy.Identifier ]int {
107
+ out := map [deppy.Identifier ]int {}
108
+ for _ , variable := range bvars {
109
+ out [variable .Identifier ()] = len (variable .BundleEntities ())
110
+ }
111
+ return out
112
+ }, Equal (map [deppy.Identifier ]int {
113
+ deppy .IdentifierFromString ("required package prometheus" ): 2 ,
114
+ deppy .IdentifierFromString ("required package packageA" ): 1 ,
115
+ })))
97
116
})
98
117
99
118
It ("should produce BundleVariables variables" , func () {
100
- olmVariableSource := olm .NewOLMVariableSource (operator ("prometheus" ), operator ("packageA" ))
119
+ cl := FakeClient (operator ("prometheus" ), operator ("packageA" ))
120
+
121
+ olmVariableSource := olm .NewOLMVariableSource (cl )
101
122
variables , err := olmVariableSource .GetVariables (context .Background (), testEntitySource )
102
123
Expect (err ).ToNot (HaveOccurred ())
103
124
@@ -109,15 +130,17 @@ var _ = Describe("OLMVariableSource", func() {
109
130
out = append (out , variable .BundleEntity ().Entity )
110
131
}
111
132
return out
112
- }, Equal ([]* input.Entity {
133
+ }, ConsistOf ([]* input.Entity {
113
134
entityFromCache ("operatorhub/prometheus/0.47.0" ),
114
135
entityFromCache ("operatorhub/prometheus/0.37.0" ),
115
136
entityFromCache ("operatorhub/packageA/2.0.0" ),
116
137
})))
117
138
})
118
139
119
140
It ("should produce version filtered BundleVariables variables" , func () {
120
- olmVariableSource := olm .NewOLMVariableSource (operator ("prometheus" , withVersionRange (">0.40.0" )), operator ("packageA" ))
141
+ cl := FakeClient (operator ("prometheus" , withVersionRange (">0.40.0" )), operator ("packageA" ))
142
+
143
+ olmVariableSource := olm .NewOLMVariableSource (cl )
121
144
variables , err := olmVariableSource .GetVariables (context .Background (), testEntitySource )
122
145
Expect (err ).ToNot (HaveOccurred ())
123
146
@@ -129,7 +152,7 @@ var _ = Describe("OLMVariableSource", func() {
129
152
out = append (out , variable .BundleEntity ().Entity )
130
153
}
131
154
return out
132
- }, Equal ([]* input.Entity {
155
+ }, ConsistOf ([]* input.Entity {
133
156
entityFromCache ("operatorhub/prometheus/0.47.0" ),
134
157
// filtered out
135
158
// entityFromCache("operatorhub/prometheus/0.37.0"),
@@ -138,7 +161,9 @@ var _ = Describe("OLMVariableSource", func() {
138
161
})
139
162
140
163
It ("should produce GlobalConstraints variables" , func () {
141
- olmVariableSource := olm .NewOLMVariableSource (operator ("prometheus" ), operator ("packageA" ))
164
+ cl := FakeClient (operator ("prometheus" ), operator ("packageA" ))
165
+
166
+ olmVariableSource := olm .NewOLMVariableSource (cl )
142
167
variables , err := olmVariableSource .GetVariables (context .Background (), testEntitySource )
143
168
Expect (err ).ToNot (HaveOccurred ())
144
169
@@ -166,7 +191,9 @@ var _ = Describe("OLMVariableSource", func() {
166
191
})
167
192
168
193
It ("should return an errors when they occur" , func () {
169
- olmVariableSource := olm .NewOLMVariableSource (operator ("prometheus" ), operator ("packageA" ))
194
+ cl := FakeClient (operator ("prometheus" ), operator ("packageA" ))
195
+
196
+ olmVariableSource := olm .NewOLMVariableSource (cl )
170
197
_ , err := olmVariableSource .GetVariables (context .Background (), FailEntitySource {})
171
198
Expect (err ).To (HaveOccurred ())
172
199
})
0 commit comments