Skip to content

Commit 6622b20

Browse files
committed
Add resolver creation unit tests
Signed-off-by: Per G. da Silva <[email protected]>
1 parent 59368bf commit 6622b20

File tree

3 files changed

+93
-8
lines changed

3 files changed

+93
-8
lines changed

pkg/controller/registry/resolver/resolver.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package resolver
33
import (
44
"context"
55
"encoding/json"
6-
"errors"
76
"fmt"
8-
"os"
97
"sort"
108
"strings"
119

@@ -41,12 +39,8 @@ type SatResolver struct {
4139
func NewDefaultSatResolver(rcp cache.SourceProvider, catsrcLister v1alpha1listers.CatalogSourceLister, logger logrus.FieldLogger) *SatResolver {
4240
runtimeConstraintProvider, err := runtime_constraints.NewFromEnv()
4341
if err != nil {
44-
if errors.Is(err, os.ErrNotExist) {
45-
logger.Warning("No cluster runtime constraints file found. Proceeding without...")
46-
} else {
47-
logger.Errorf("Error creating runtime constraints from file: %s", err)
48-
panic(err)
49-
}
42+
logger.Errorf("Error creating runtime constraints from file: %s", err)
43+
panic(err)
5044
}
5145

5246
// Two solutions:

pkg/controller/registry/resolver/resolver_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package resolver
33
import (
44
"errors"
55
"fmt"
6+
"os"
67
"testing"
78

89
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver/runtime_constraints"
@@ -25,6 +26,23 @@ import (
2526
opregistry "github.com/operator-framework/operator-registry/pkg/registry"
2627
)
2728

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+
2846
func TestSolveOperators(t *testing.T) {
2947
APISet := cache.APISet{opregistry.APIKey{Group: "g", Version: "v", Kind: "k", Plural: "ks"}: struct{}{}}
3048
Provides := APISet
@@ -2161,3 +2179,75 @@ func TestNewOperatorFromCSV(t *testing.T) {
21612179
})
21622180
}
21632181
}
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is a poorly formatted runtime constraints file

0 commit comments

Comments
 (0)