Skip to content

Commit 07fd34e

Browse files
committed
Add unit test for generic constraint
Signed-off-by: Vu Dinh <[email protected]>
1 parent f379574 commit 07fd34e

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

pkg/controller/registry/resolver/resolver_test.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,3 +2066,122 @@ func TestNewOperatorFromCSV(t *testing.T) {
20662066
})
20672067
}
20682068
}
2069+
2070+
func TestSolveOperators_GenericConstraint(t *testing.T) {
2071+
Provides1 := cache.APISet{opregistry.APIKey{"g", "v", "k", "ks"}: struct{}{}}
2072+
Provides2 := cache.APISet{opregistry.APIKey{"g2", "v", "k", "ks"}: struct{}{}}
2073+
2074+
namespace := "olm"
2075+
catalog := cache.SourceKey{Name: "community", Namespace: namespace}
2076+
2077+
deps1 := []*api.Dependency{
2078+
{
2079+
Type: "olm.constraint",
2080+
Value: `{"message":"gvk-constraint",
2081+
"cel":{"rule":"properties.exists(p, p.type == "olm.gvk" && p.value == {"group": "g", 'version': "v", 'kind': "k"})"}}`,
2082+
},
2083+
}
2084+
deps2 := []*api.Dependency{
2085+
{
2086+
Type: "olm.constraint",
2087+
Value: `{"message":"gvk2-constraint",
2088+
"cel":{"rule":"properties.exists(p, p.type == "olm.gvk" && p.value == {"group": "g2", 'version': "v", 'kind': "k"})"}}`,
2089+
},
2090+
}
2091+
deps3 := []*api.Dependency{
2092+
{
2093+
Type: "olm.constraint",
2094+
Value: `{"message":"compound-constraint",
2095+
"cel":{"rule":"properties.exists(p, p.type == "olm.package" && (semver_compare(p.value, 1.0.1) == 0))"}}`,
2096+
},
2097+
}
2098+
2099+
tests := []struct {
2100+
name string
2101+
isErr bool
2102+
subs []*v1alpha1.Subscription
2103+
catalog cache.Source
2104+
expected cache.OperatorSet
2105+
}{
2106+
{
2107+
// generic constraint for satisfiable gvk dependency
2108+
name: "Generic Constraint/Satisfiable GVK Dependency",
2109+
isErr: false,
2110+
subs: []*v1alpha1.Subscription{
2111+
newSub(namespace, "packageA", "stable", catalog),
2112+
},
2113+
catalog: &cache.Snapshot{
2114+
Entries: []*cache.Entry{
2115+
genOperator("opA.v1.0.0", "1.0.0", "", "packageA", "stable", catalog.Name, catalog.Namespace, nil, nil, deps1, "", false),
2116+
genOperator("opB.v1.0.0", "1.0.0", "", "packageB", "stable", catalog.Name, catalog.Namespace, nil, Provides1, nil, "stable", false),
2117+
},
2118+
},
2119+
expected: cache.OperatorSet{
2120+
"opA.v1.0.0": genOperator("opA.v1.0.0", "1.0.0", "", "packageA", "stable", catalog.Name, catalog.Namespace, nil, nil, deps1, "", false),
2121+
"opB.v1.0.0": genOperator("opB.v1.0.0", "1.0.0", "", "packageB", "stable", catalog.Name, catalog.Namespace, nil, Provides1, nil, "stable", false),
2122+
},
2123+
},
2124+
{
2125+
// generic constraint for NotSatisfiable gvk dependency
2126+
name: "Generic Constraint/NotSatisfiable GVK Dependency",
2127+
isErr: true,
2128+
subs: []*v1alpha1.Subscription{
2129+
newSub(namespace, "packageA", "stable", catalog),
2130+
},
2131+
catalog: &cache.Snapshot{
2132+
Entries: []*cache.Entry{
2133+
genOperator("opA.v1.0.0", "1.0.0", "", "packageA", "stable", catalog.Name, catalog.Namespace, nil, nil, deps2, "", false),
2134+
genOperator("opB.v1.0.0", "1.0.0", "", "packageB", "stable", catalog.Name, catalog.Namespace, nil, Provides2, nil, "", false),
2135+
},
2136+
},
2137+
// unable to find satisfiable gvk dependency
2138+
// resolve into nothing
2139+
expected: cache.OperatorSet{},
2140+
},
2141+
{
2142+
// generic constraint for package constraint
2143+
name: "Generic Constraint/Satisfiable Package Dependency",
2144+
isErr: false,
2145+
subs: []*v1alpha1.Subscription{
2146+
newSub(namespace, "packageA", "stable", catalog),
2147+
},
2148+
catalog: &cache.Snapshot{
2149+
Entries: []*cache.Entry{
2150+
genOperator("opA.v1.0.0", "1.0.0", "", "packageA", "stable", catalog.Name, catalog.Namespace, nil, nil, deps3, "", false),
2151+
genOperator("opB.v1.0.0", "1.0.0", "", "packageB", "stable", catalog.Name, catalog.Namespace, nil, nil, nil, "", false),
2152+
genOperator("opB.v1.0.1", "1.0.1", "opB.v1.0.0", "packageB", "stable", catalog.Name, catalog.Namespace, nil, nil, nil, "stable", false),
2153+
genOperator("opB.v1.0.2", "1.0.2", "opB.v1.0.1", "packageB", "stable", catalog.Name, catalog.Namespace, nil, nil, nil, "stable", false),
2154+
},
2155+
},
2156+
expected: cache.OperatorSet{
2157+
"opA.v1.0.0": genOperator("opA.v1.0.1", "1.0.1", "", "packageA", "stable", catalog.Name, catalog.Namespace, nil, nil, deps3, "", false),
2158+
"opB.v1.0.1": genOperator("opB.v1.0.1", "1.0.1", "opB.v1.0.0", "packageB", "stable", catalog.Name, catalog.Namespace, nil, nil, nil, "stable", false),
2159+
},
2160+
},
2161+
}
2162+
2163+
for _, tt := range tests {
2164+
t.Run(tt.name, func(t *testing.T) {
2165+
var err error
2166+
var operators cache.OperatorSet
2167+
satResolver := SatResolver{
2168+
cache: cache.New(cache.StaticSourceProvider{
2169+
catalog: tt.catalog,
2170+
}),
2171+
log: logrus.New(),
2172+
}
2173+
2174+
operators, err = satResolver.SolveOperators([]string{"olm"}, []*v1alpha1.ClusterServiceVersion{}, tt.subs)
2175+
if tt.isErr {
2176+
assert.Error(t, err)
2177+
} else {
2178+
assert.NoError(t, err)
2179+
}
2180+
for k := range tt.expected {
2181+
require.NotNil(t, operators[k])
2182+
assert.EqualValues(t, k, operators[k].Name)
2183+
}
2184+
assert.Equal(t, len(tt.expected), len(operators))
2185+
})
2186+
}
2187+
}

pkg/controller/registry/resolver/source_registry.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ func legacyDependenciesToProperties(dependencies []*api.Dependency) ([]*api.Prop
233233
Type: "olm.label.required",
234234
Value: dependency.Value,
235235
})
236+
case "olm.constraint":
237+
result = append(result, &api.Property{
238+
Type: "olm.constraint",
239+
Value: dependency.Value,
240+
})
236241
}
237242
}
238243
return result, nil

0 commit comments

Comments
 (0)