@@ -3,6 +3,7 @@ package resolver
3
3
import (
4
4
"errors"
5
5
"fmt"
6
+ "os"
6
7
"testing"
7
8
8
9
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver/runtime_constraints"
@@ -25,6 +26,23 @@ import (
25
26
opregistry "github.com/operator-framework/operator-registry/pkg/registry"
26
27
)
27
28
29
+ type fakeSourceProvider struct {
30
+ }
31
+
32
+ func (f * fakeSourceProvider ) Sources (namespaces ... string ) map [cache.SourceKey ]cache.Source {
33
+ return nil
34
+ }
35
+
36
+ type fakeCatalogSourceLister struct {}
37
+
38
+ func (l * fakeCatalogSourceLister ) List (selector labels.Selector ) (ret []* v1alpha1.CatalogSource , err error ) {
39
+ return nil , nil
40
+ }
41
+
42
+ func (l * fakeCatalogSourceLister ) CatalogSources (namespace string ) listersv1alpha1.CatalogSourceNamespaceLister {
43
+ return nil
44
+ }
45
+
28
46
func TestSolveOperators (t * testing.T ) {
29
47
APISet := cache.APISet {opregistry.APIKey {Group : "g" , Version : "v" , Kind : "k" , Plural : "ks" }: struct {}{}}
30
48
Provides := APISet
@@ -2161,3 +2179,75 @@ func TestNewOperatorFromCSV(t *testing.T) {
2161
2179
})
2162
2180
}
2163
2181
}
2182
+
2183
+ func TestNewDefaultSatResolver_NoClusterRuntimeConstraints (t * testing.T ) {
2184
+ // Ensure no runtime constraints are loaded if the runtime constraints env
2185
+ // var is not set
2186
+ sourceProvider := & fakeSourceProvider {}
2187
+ catSrcLister := & fakeCatalogSourceLister {}
2188
+ logger := logrus .New ()
2189
+
2190
+ // Unset the runtime constraints file path environment variable
2191
+ // signaling that no runtime constraints should be considered by the resolver
2192
+ require .Nil (t , os .Unsetenv (runtime_constraints .RuntimeConstraintEnvVarName ))
2193
+ resolver := NewDefaultSatResolver (sourceProvider , catSrcLister , logger )
2194
+ require .Nil (t , resolver .runtimeConstraintsProvider )
2195
+ }
2196
+
2197
+ func TestNewDefaultSatResolver_BadClusterRuntimeConstraintsEnvVar (t * testing.T ) {
2198
+ // Ensure TestNewDefaultSatResolver panics if the runtime constraints
2199
+ // environment variable does not point to an existing file or valid path
2200
+ sourceProvider := & fakeSourceProvider {}
2201
+ catSrcLister := & fakeCatalogSourceLister {}
2202
+ logger := logrus .New ()
2203
+ t .Cleanup (func () { _ = os .Unsetenv (runtime_constraints .RuntimeConstraintEnvVarName ) })
2204
+
2205
+ // This test expects a panic to happen
2206
+ defer func () {
2207
+ if r := recover (); r == nil {
2208
+ t .Errorf ("The code did not panic" )
2209
+ }
2210
+ }()
2211
+
2212
+ // Set the runtime constraints env var to something that isn't a valid filesystem path
2213
+ require .Nil (t , os .Setenv (runtime_constraints .RuntimeConstraintEnvVarName , "%#$%#$ %$#%#$%" ))
2214
+ _ = NewDefaultSatResolver (sourceProvider , catSrcLister , logger )
2215
+ }
2216
+
2217
+ func TestNewDefaultSatResolver_BadClusterRuntimeConstraintsFile (t * testing.T ) {
2218
+ // Ensure TestNewDefaultSatResolver panics if the runtime constraints
2219
+ // environment variable points to a poorly formatted runtime constraints file
2220
+ sourceProvider := & fakeSourceProvider {}
2221
+ catSrcLister := & fakeCatalogSourceLister {}
2222
+ logger := logrus .New ()
2223
+ t .Cleanup (func () { _ = os .Unsetenv (runtime_constraints .RuntimeConstraintEnvVarName ) })
2224
+
2225
+ // This test expects a panic to happen
2226
+ defer func () {
2227
+ if r := recover (); r == nil {
2228
+ t .Errorf ("The code did not panic" )
2229
+ }
2230
+ }()
2231
+
2232
+ runtimeConstraintsFilePath := "testdata/bad_runtime_constraints.yaml"
2233
+ // set the runtime constraints env var to something that isn't a valid filesystem path
2234
+ require .Nil (t , os .Setenv (runtime_constraints .RuntimeConstraintEnvVarName , runtimeConstraintsFilePath ))
2235
+ _ = NewDefaultSatResolver (sourceProvider , catSrcLister , logger )
2236
+ }
2237
+
2238
+ func TestNewDefaultSatResolver_GoodClusterRuntimeConstraintsFile (t * testing.T ) {
2239
+ // Ensure TestNewDefaultSatResolver loads the runtime constraints
2240
+ // defined in a well formatted file point to by the runtime constraints env var
2241
+ sourceProvider := & fakeSourceProvider {}
2242
+ catSrcLister := & fakeCatalogSourceLister {}
2243
+ logger := logrus .New ()
2244
+ t .Cleanup (func () { _ = os .Unsetenv (runtime_constraints .RuntimeConstraintEnvVarName ) })
2245
+
2246
+ runtimeConstraintsFilePath := "testdata/runtime_constraints.yaml"
2247
+ // set the runtime constraints env var to something that isn't a valid filesystem path
2248
+ require .Nil (t , os .Setenv (runtime_constraints .RuntimeConstraintEnvVarName , runtimeConstraintsFilePath ))
2249
+ resolver := NewDefaultSatResolver (sourceProvider , catSrcLister , logger )
2250
+ runtimeConstraints := resolver .runtimeConstraintsProvider .Constraints ()
2251
+ require .Len (t , runtimeConstraints , 1 )
2252
+ require .Equal (t , "with package: etcd" , runtimeConstraints [0 ].String ())
2253
+ }
0 commit comments