Skip to content

Commit df687f4

Browse files
committed
Refactor unit tests and remove not predicate
Signed-off-by: Per G. da Silva <[email protected]>
1 parent 19493a2 commit df687f4

File tree

3 files changed

+90
-96
lines changed

3 files changed

+90
-96
lines changed

pkg/controller/registry/resolver/cache/predicates.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -282,24 +282,6 @@ func (p orPredicate) String() string {
282282
return b.String()
283283
}
284284

285-
type notPredicate struct {
286-
predicate Predicate
287-
}
288-
289-
func Not(predicate Predicate) Predicate {
290-
return notPredicate{
291-
predicate: predicate,
292-
}
293-
}
294-
295-
func (p notPredicate) Test(o *Entry) bool {
296-
return !p.predicate.Test(o)
297-
}
298-
299-
func (p notPredicate) String() string {
300-
return fmt.Sprintf("not %s", p.predicate.String())
301-
}
302-
303285
type booleanPredicate struct {
304286
result bool
305287
}

pkg/controller/registry/resolver/resolver_test.go

Lines changed: 87 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,78 @@ func (l *fakeCatalogSourceLister) CatalogSources(namespace string) listersv1alph
4343
return nil
4444
}
4545

46+
func TestNewDefaultSatResolver_NoClusterRuntimeConstraints(t *testing.T) {
47+
// Ensure no runtime constraints are loaded if the runtime constraints env
48+
// var is not set
49+
sourceProvider := &fakeSourceProvider{}
50+
catSrcLister := &fakeCatalogSourceLister{}
51+
logger := logrus.New()
52+
53+
// Unset the runtime constraints file path environment variable
54+
// signaling that no runtime constraints should be considered by the resolver
55+
require.Nil(t, os.Unsetenv(runtime_constraints.RuntimeConstraintEnvVarName))
56+
resolver := NewDefaultSatResolver(sourceProvider, catSrcLister, logger)
57+
require.Nil(t, resolver.runtimeConstraintsProvider)
58+
}
59+
60+
func TestNewDefaultSatResolver_BadClusterRuntimeConstraintsEnvVar(t *testing.T) {
61+
// Ensure TestNewDefaultSatResolver panics if the runtime constraints
62+
// environment variable does not point to an existing file or valid path
63+
sourceProvider := &fakeSourceProvider{}
64+
catSrcLister := &fakeCatalogSourceLister{}
65+
logger := logrus.New()
66+
t.Cleanup(func() { _ = os.Unsetenv(runtime_constraints.RuntimeConstraintEnvVarName) })
67+
68+
// This test expects a panic to happen
69+
defer func() {
70+
if r := recover(); r == nil {
71+
t.Errorf("The code did not panic")
72+
}
73+
}()
74+
75+
// Set the runtime constraints env var to something that isn't a valid filesystem path
76+
require.Nil(t, os.Setenv(runtime_constraints.RuntimeConstraintEnvVarName, "%#$%#$ %$#%#$%"))
77+
_ = NewDefaultSatResolver(sourceProvider, catSrcLister, logger)
78+
}
79+
80+
func TestNewDefaultSatResolver_BadClusterRuntimeConstraintsFile(t *testing.T) {
81+
// Ensure TestNewDefaultSatResolver panics if the runtime constraints
82+
// environment variable points to a poorly formatted runtime constraints file
83+
sourceProvider := &fakeSourceProvider{}
84+
catSrcLister := &fakeCatalogSourceLister{}
85+
logger := logrus.New()
86+
t.Cleanup(func() { _ = os.Unsetenv(runtime_constraints.RuntimeConstraintEnvVarName) })
87+
88+
// This test expects a panic to happen
89+
defer func() {
90+
if r := recover(); r == nil {
91+
t.Errorf("The code did not panic")
92+
}
93+
}()
94+
95+
runtimeConstraintsFilePath := "runtime_constraints/testdata/bad_runtime_constraints.json"
96+
// set the runtime constraints env var to something that isn't a valid filesystem path
97+
require.Nil(t, os.Setenv(runtime_constraints.RuntimeConstraintEnvVarName, runtimeConstraintsFilePath))
98+
_ = NewDefaultSatResolver(sourceProvider, catSrcLister, logger)
99+
}
100+
101+
func TestNewDefaultSatResolver_GoodClusterRuntimeConstraintsFile(t *testing.T) {
102+
// Ensure TestNewDefaultSatResolver loads the runtime constraints
103+
// defined in a well formatted file point to by the runtime constraints env var
104+
sourceProvider := &fakeSourceProvider{}
105+
catSrcLister := &fakeCatalogSourceLister{}
106+
logger := logrus.New()
107+
t.Cleanup(func() { _ = os.Unsetenv(runtime_constraints.RuntimeConstraintEnvVarName) })
108+
109+
runtimeConstraintsFilePath := "runtime_constraints/testdata/runtime_constraints.json"
110+
// set the runtime constraints env var to something that isn't a valid filesystem path
111+
require.Nil(t, os.Setenv(runtime_constraints.RuntimeConstraintEnvVarName, runtimeConstraintsFilePath))
112+
resolver := NewDefaultSatResolver(sourceProvider, catSrcLister, logger)
113+
runtimeConstraints := resolver.runtimeConstraintsProvider.Constraints()
114+
require.Len(t, runtimeConstraints, 1)
115+
require.Equal(t, "with package: etcd", runtimeConstraints[0].String())
116+
}
117+
46118
func TestSolveOperators(t *testing.T) {
47119
APISet := cache.APISet{opregistry.APIKey{Group: "g", Version: "v", Kind: "k", Plural: "ks"}: struct{}{}}
48120
Provides := APISet
@@ -113,11 +185,16 @@ func TestRuntimeConstraints(t *testing.T) {
113185

114186
// packageA requires an API that can be provided by B or C
115187
packageA := genOperator("packageA.v1", "0.0.1", "", "packageA", "alpha", catalog.Name, catalog.Namespace, APISet, nil, nil, "", false)
188+
packageA.Properties = append(packageA.Properties, newLabelProperty("filterOut=yes"), newLabelProperty("theBest=yes"))
189+
116190
packageB := genOperator("packageB.v1", "1.0.0", "", "packageB", "alpha", catalog.Name, catalog.Namespace, nil, APISet, nil, "", false)
191+
packageB.Properties = append(packageB.Properties, newLabelProperty("filterOut=no"), newLabelProperty("theBest=no"))
192+
117193
packageC := genOperator("packageC.v1", "1.0.0", "", "packageC", "alpha", catalog.Name, catalog.Namespace, nil, APISet, nil, "", false)
118-
packageD := genOperator("packageD.v1", "1.0.0", "", "packageD", "alpha", catalog.Name, catalog.Namespace, nil, nil, nil, "", false)
194+
packageC.Properties = append(packageC.Properties, newLabelProperty("filterOut=no"), newLabelProperty("theBest=yes"))
119195

120196
// Existing operators
197+
packageD := genOperator("packageD.v1", "1.0.0", "", "packageD", "alpha", catalog.Name, catalog.Namespace, nil, nil, nil, "", false)
121198
existingPackageD := existingOperator(namespace, "packageD.v1", "packageD", "alpha", "", nil, nil, nil, nil)
122199
existingPackageD.Annotations = map[string]string{"operatorframework.io/properties": `{"properties":[{"type":"olm.package","value":{"packageName":"packageD","version":"1.0.0"}}]}`}
123200

@@ -143,7 +220,7 @@ func TestRuntimeConstraints(t *testing.T) {
143220
title: "Runtime constraints only accept packages A and C",
144221
snapshotEntries: []*cache.Entry{packageA, packageB, packageC, packageD},
145222
runtimeConstraints: []cache.Predicate{
146-
cache.Or(cache.PkgPredicate("packageA"), cache.PkgPredicate("packageC")),
223+
cache.LabelPredicate("theBest=yes"),
147224
},
148225
expectedOperators: cache.OperatorSet{"packageA.v1": packageA, "packageC.v1": packageC},
149226
csvs: nil,
@@ -154,7 +231,7 @@ func TestRuntimeConstraints(t *testing.T) {
154231
title: "Existing packages are ignored",
155232
snapshotEntries: []*cache.Entry{packageA, packageB, packageC, packageD},
156233
runtimeConstraints: []cache.Predicate{
157-
cache.Or(cache.PkgPredicate("packageA"), cache.PkgPredicate("packageC")),
234+
cache.LabelPredicate("theBest=yes"),
158235
},
159236
expectedOperators: cache.OperatorSet{"packageA.v1": packageA, "packageC.v1": packageC},
160237
csvs: []*v1alpha1.ClusterServiceVersion{existingPackageD},
@@ -165,12 +242,12 @@ func TestRuntimeConstraints(t *testing.T) {
165242
title: "Runtime constraints don't allow A",
166243
snapshotEntries: []*cache.Entry{packageA, packageB, packageC, packageD},
167244
runtimeConstraints: []cache.Predicate{
168-
cache.Not(cache.PkgPredicate("packageA")),
245+
cache.LabelPredicate("filterOut=no"),
169246
},
170247
expectedOperators: nil,
171248
csvs: nil,
172249
subs: []*v1alpha1.Subscription{packageASub},
173-
err: "test-catalog/test-namespace/alpha/packageA.v1 violates a cluster runtime constraint: not with package: packageA",
250+
err: "test-catalog/test-namespace/alpha/packageA.v1 violates a cluster runtime constraint: with label: filterOut=no",
174251
},
175252
}
176253

@@ -2182,74 +2259,9 @@ func TestNewOperatorFromCSV(t *testing.T) {
21822259
}
21832260
}
21842261

2185-
func TestNewDefaultSatResolver_NoClusterRuntimeConstraints(t *testing.T) {
2186-
// Ensure no runtime constraints are loaded if the runtime constraints env
2187-
// var is not set
2188-
sourceProvider := &fakeSourceProvider{}
2189-
catSrcLister := &fakeCatalogSourceLister{}
2190-
logger := logrus.New()
2191-
2192-
// Unset the runtime constraints file path environment variable
2193-
// signaling that no runtime constraints should be considered by the resolver
2194-
require.Nil(t, os.Unsetenv(runtime_constraints.RuntimeConstraintEnvVarName))
2195-
resolver := NewDefaultSatResolver(sourceProvider, catSrcLister, logger)
2196-
require.Nil(t, resolver.runtimeConstraintsProvider)
2197-
}
2198-
2199-
func TestNewDefaultSatResolver_BadClusterRuntimeConstraintsEnvVar(t *testing.T) {
2200-
// Ensure TestNewDefaultSatResolver panics if the runtime constraints
2201-
// environment variable does not point to an existing file or valid path
2202-
sourceProvider := &fakeSourceProvider{}
2203-
catSrcLister := &fakeCatalogSourceLister{}
2204-
logger := logrus.New()
2205-
t.Cleanup(func() { _ = os.Unsetenv(runtime_constraints.RuntimeConstraintEnvVarName) })
2206-
2207-
// This test expects a panic to happen
2208-
defer func() {
2209-
if r := recover(); r == nil {
2210-
t.Errorf("The code did not panic")
2211-
}
2212-
}()
2213-
2214-
// Set the runtime constraints env var to something that isn't a valid filesystem path
2215-
require.Nil(t, os.Setenv(runtime_constraints.RuntimeConstraintEnvVarName, "%#$%#$ %$#%#$%"))
2216-
_ = NewDefaultSatResolver(sourceProvider, catSrcLister, logger)
2217-
}
2218-
2219-
func TestNewDefaultSatResolver_BadClusterRuntimeConstraintsFile(t *testing.T) {
2220-
// Ensure TestNewDefaultSatResolver panics if the runtime constraints
2221-
// environment variable points to a poorly formatted runtime constraints file
2222-
sourceProvider := &fakeSourceProvider{}
2223-
catSrcLister := &fakeCatalogSourceLister{}
2224-
logger := logrus.New()
2225-
t.Cleanup(func() { _ = os.Unsetenv(runtime_constraints.RuntimeConstraintEnvVarName) })
2226-
2227-
// This test expects a panic to happen
2228-
defer func() {
2229-
if r := recover(); r == nil {
2230-
t.Errorf("The code did not panic")
2231-
}
2232-
}()
2233-
2234-
runtimeConstraintsFilePath := "runtime_constraints/testdata/bad_runtime_constraints.json"
2235-
// set the runtime constraints env var to something that isn't a valid filesystem path
2236-
require.Nil(t, os.Setenv(runtime_constraints.RuntimeConstraintEnvVarName, runtimeConstraintsFilePath))
2237-
_ = NewDefaultSatResolver(sourceProvider, catSrcLister, logger)
2238-
}
2239-
2240-
func TestNewDefaultSatResolver_GoodClusterRuntimeConstraintsFile(t *testing.T) {
2241-
// Ensure TestNewDefaultSatResolver loads the runtime constraints
2242-
// defined in a well formatted file point to by the runtime constraints env var
2243-
sourceProvider := &fakeSourceProvider{}
2244-
catSrcLister := &fakeCatalogSourceLister{}
2245-
logger := logrus.New()
2246-
t.Cleanup(func() { _ = os.Unsetenv(runtime_constraints.RuntimeConstraintEnvVarName) })
2247-
2248-
runtimeConstraintsFilePath := "runtime_constraints/testdata/runtime_constraints.json"
2249-
// set the runtime constraints env var to something that isn't a valid filesystem path
2250-
require.Nil(t, os.Setenv(runtime_constraints.RuntimeConstraintEnvVarName, runtimeConstraintsFilePath))
2251-
resolver := NewDefaultSatResolver(sourceProvider, catSrcLister, logger)
2252-
runtimeConstraints := resolver.runtimeConstraintsProvider.Constraints()
2253-
require.Len(t, runtimeConstraints, 1)
2254-
require.Equal(t, "with package: etcd", runtimeConstraints[0].String())
2262+
func newLabelProperty(label string) *api.Property {
2263+
return &api.Property{
2264+
Type: opregistry.LabelType,
2265+
Value: fmt.Sprintf(`{"label": "%s"}`, label),
2266+
}
22552267
}

pkg/controller/registry/resolver/source_registry.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func legacyDependenciesToProperties(dependencies []*api.Dependency) ([]*api.Prop
182182
var result []*api.Property
183183
for _, dependency := range dependencies {
184184
switch dependency.Type {
185-
case "olm.gvk":
185+
case opregistry.GVKType:
186186
type gvk struct {
187187
Group string `json:"group"`
188188
Version string `json:"version"`
@@ -205,7 +205,7 @@ func legacyDependenciesToProperties(dependencies []*api.Dependency) ([]*api.Prop
205205
Type: "olm.gvk.required",
206206
Value: string(vb),
207207
})
208-
case "olm.package":
208+
case opregistry.PackageType:
209209
var vfrom struct {
210210
PackageName string `json:"packageName"`
211211
VersionRange string `json:"version"`
@@ -228,7 +228,7 @@ func legacyDependenciesToProperties(dependencies []*api.Dependency) ([]*api.Prop
228228
Type: "olm.package.required",
229229
Value: string(vb),
230230
})
231-
case "olm.label":
231+
case opregistry.LabelType:
232232
result = append(result, &api.Property{
233233
Type: "olm.label.required",
234234
Value: dependency.Value,

0 commit comments

Comments
 (0)