@@ -2,10 +2,13 @@ package resolver
22
33import (
44 "fmt"
5+ "os"
56 "strings"
67 "testing"
78 "time"
89
10+ "github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver/runtime_constraints"
11+
912 "github.com/sirupsen/logrus"
1013 "github.com/stretchr/testify/assert"
1114 "github.com/stretchr/testify/require"
4750 Requires4 = APISet4
4851)
4952
53+ func TestNewOperatorStepResolver_NoClusterRuntimeConstraints (t * testing.T ) {
54+ // Ensure no runtime constraints are loaded if the runtime constraints env
55+ // var is not set
56+ logger := logrus .New ()
57+ lister := operatorlister .NewLister ()
58+
59+ // Unset the runtime constraints file path environment variable
60+ // signaling that no runtime constraints should be considered by the resolver
61+ require .Nil (t , os .Unsetenv (runtime_constraints .RuntimeConstraintEnvVarName ))
62+ resolver := NewOperatorStepResolver (lister , nil , nil , "" , nil , logger )
63+ require .Nil (t , resolver .satResolver .runtimeConstraintsProvider )
64+ }
65+
66+ func TestNewOperatorStepResolver_BadClusterRuntimeConstraintsEnvVar (t * testing.T ) {
67+ // Ensure TestNewDefaultSatResolver panics if the runtime constraints
68+ // environment variable does not point to an existing file or valid path
69+ lister := operatorlister .NewLister ()
70+ logger := logrus .New ()
71+ t .Cleanup (func () { _ = os .Unsetenv (runtime_constraints .RuntimeConstraintEnvVarName ) })
72+
73+ // This test expects a panic to happen
74+ defer func () {
75+ if r := recover (); r == nil {
76+ t .Errorf ("The code did not panic" )
77+ }
78+ }()
79+
80+ // Set the runtime constraints env var to something that isn't a valid filesystem path
81+ require .Nil (t , os .Setenv (runtime_constraints .RuntimeConstraintEnvVarName , "%#$%#$ %$#%#$%" ))
82+ _ = NewOperatorStepResolver (lister , nil , nil , "" , nil , logger )
83+ }
84+
85+ func TestNewOperatorStepResolver_BadClusterRuntimeConstraintsFile (t * testing.T ) {
86+ // Ensure TestNewDefaultSatResolver panics if the runtime constraints
87+ // environment variable points to a poorly formatted runtime constraints file
88+ lister := operatorlister .NewLister ()
89+ logger := logrus .New ()
90+ t .Cleanup (func () { _ = os .Unsetenv (runtime_constraints .RuntimeConstraintEnvVarName ) })
91+
92+ // This test expects a panic to happen
93+ defer func () {
94+ if r := recover (); r == nil {
95+ t .Errorf ("The code did not panic" )
96+ }
97+ }()
98+
99+ runtimeConstraintsFilePath := "runtime_constraints/testdata/bad_runtime_constraints.json"
100+ // set the runtime constraints env var to something that isn't a valid filesystem path
101+ require .Nil (t , os .Setenv (runtime_constraints .RuntimeConstraintEnvVarName , runtimeConstraintsFilePath ))
102+ _ = NewOperatorStepResolver (lister , nil , nil , "" , nil , logger )
103+ }
104+
105+ func TestNewOperatorStepResolver_GoodClusterRuntimeConstraintsFile (t * testing.T ) {
106+ // Ensure TestNewDefaultSatResolver loads the runtime constraints
107+ // defined in a well formatted file point to by the runtime constraints env var
108+ lister := operatorlister .NewLister ()
109+ logger := logrus .New ()
110+ t .Cleanup (func () { _ = os .Unsetenv (runtime_constraints .RuntimeConstraintEnvVarName ) })
111+
112+ runtimeConstraintsFilePath := "runtime_constraints/testdata/runtime_constraints.json"
113+ // set the runtime constraints env var to something that isn't a valid filesystem path
114+ require .Nil (t , os .Setenv (runtime_constraints .RuntimeConstraintEnvVarName , runtimeConstraintsFilePath ))
115+ resolver := NewOperatorStepResolver (lister , nil , nil , "" , nil , logger )
116+ runtimeConstraints := resolver .satResolver .runtimeConstraintsProvider .Constraints ()
117+ require .Len (t , runtimeConstraints , 1 )
118+ require .Equal (t , "with package: etcd" , runtimeConstraints [0 ].String ())
119+ }
120+
50121func TestResolver (t * testing.T ) {
51122 const namespace = "catsrc-namespace"
52123 catalog := resolvercache.SourceKey {Name : "catsrc" , Namespace : namespace }
0 commit comments