Skip to content

Commit 46f6a5b

Browse files
committed
Add provider unit tests and small refactor
Signed-off-by: Per G. da Silva <[email protected]>
1 parent fac6176 commit 46f6a5b

File tree

7 files changed

+186
-11
lines changed

7 files changed

+186
-11
lines changed

pkg/controller/registry/resolver/resolver_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,16 @@ func TestRuntimeConstraints(t *testing.T) {
175175
}
176176

177177
for _, testCase := range testCases {
178+
provider, err := runtime_constraints.New(testCase.runtimeConstraints)
179+
require.Nil(t, err)
178180
satResolver := SatResolver{
179181
cache: cache.New(cache.StaticSourceProvider{
180182
catalog: &cache.Snapshot{
181183
Entries: testCase.snapshotEntries,
182184
},
183185
}),
184186
log: logrus.New(),
185-
runtimeConstraintsProvider: runtime_constraints.New(testCase.runtimeConstraints),
187+
runtimeConstraintsProvider: provider,
186188
}
187189
operators, err := satResolver.SolveOperators([]string{namespace}, testCase.csvs, testCase.subs)
188190

@@ -2229,7 +2231,7 @@ func TestNewDefaultSatResolver_BadClusterRuntimeConstraintsFile(t *testing.T) {
22292231
}
22302232
}()
22312233

2232-
runtimeConstraintsFilePath := "testdata/bad_runtime_constraints.json"
2234+
runtimeConstraintsFilePath := "runtime_constraints/testdata/bad_runtime_constraints.json"
22332235
// set the runtime constraints env var to something that isn't a valid filesystem path
22342236
require.Nil(t, os.Setenv(runtime_constraints.RuntimeConstraintEnvVarName, runtimeConstraintsFilePath))
22352237
_ = NewDefaultSatResolver(sourceProvider, catSrcLister, logger)
@@ -2243,7 +2245,7 @@ func TestNewDefaultSatResolver_GoodClusterRuntimeConstraintsFile(t *testing.T) {
22432245
logger := logrus.New()
22442246
t.Cleanup(func() { _ = os.Unsetenv(runtime_constraints.RuntimeConstraintEnvVarName) })
22452247

2246-
runtimeConstraintsFilePath := "testdata/runtime_constraints.json"
2248+
runtimeConstraintsFilePath := "runtime_constraints/testdata/runtime_constraints.json"
22472249
// set the runtime constraints env var to something that isn't a valid filesystem path
22482250
require.Nil(t, os.Setenv(runtime_constraints.RuntimeConstraintEnvVarName, runtimeConstraintsFilePath))
22492251
resolver := NewDefaultSatResolver(sourceProvider, catSrcLister, logger)

pkg/controller/registry/resolver/runtime_constraints/provider.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
const (
14-
maxRuntimeConstraints = 10
14+
MaxRuntimeConstraints = 10
1515
RuntimeConstraintEnvVarName = "RUNTIME_CONSTRAINTS"
1616
)
1717

@@ -23,10 +23,14 @@ func (p *RuntimeConstraintsProvider) Constraints() []cache.Predicate {
2323
return p.runtimeConstraints
2424
}
2525

26-
func New(runtimeConstraints []cache.Predicate) *RuntimeConstraintsProvider {
26+
func New(runtimeConstraints []cache.Predicate) (*RuntimeConstraintsProvider, error) {
27+
if len(runtimeConstraints) >= MaxRuntimeConstraints {
28+
return nil, errors.Errorf("Too many runtime constraints defined (%d/%d)", len(runtimeConstraints), MaxRuntimeConstraints)
29+
}
30+
2731
return &RuntimeConstraintsProvider{
2832
runtimeConstraints: runtimeConstraints,
29-
}
33+
}, nil
3034
}
3135

3236
func NewFromEnv() (*RuntimeConstraintsProvider, error) {
@@ -64,12 +68,9 @@ func NewFromFile(runtimeConstraintsFilePath string) (*RuntimeConstraintsProvider
6468
}
6569
constraints = append(constraints, cache.LabelPredicate(dep.Label))
6670
}
67-
if len(constraints) >= maxRuntimeConstraints {
68-
return nil, errors.Errorf("Too many runtime constraints defined (%d/%d)", len(constraints), maxRuntimeConstraints)
69-
}
7071
}
7172

72-
return New(constraints), nil
73+
return New(constraints)
7374
}
7475

7576
func readRuntimeConstraintsYaml(yamlPath string) (*registry.PropertiesFile, error) {
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package runtime_constraints
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver/cache"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestNew_HappyPath(t *testing.T) {
13+
predicates := []cache.Predicate{
14+
cache.PkgPredicate("etcd"),
15+
cache.LabelPredicate("something"),
16+
}
17+
provider, err := New(predicates)
18+
require.Nil(t, err)
19+
require.Equal(t, predicates, provider.Constraints())
20+
}
21+
22+
func TestNew_TooManyConstraints(t *testing.T) {
23+
predicates := make([]cache.Predicate, MaxRuntimeConstraints+1)
24+
for i := 0; i < len(predicates); i++ {
25+
predicates[i] = cache.PkgPredicate(fmt.Sprintf("etcd-%d", i))
26+
}
27+
provider, err := New(predicates)
28+
require.NotNil(t, err)
29+
require.Nil(t, provider)
30+
}
31+
32+
func TestNewFromFile_HappyPath(t *testing.T) {
33+
provider, err := NewFromFile("testdata/runtime_constraints.json")
34+
require.Nil(t, err)
35+
require.Len(t, provider.Constraints(), 1)
36+
require.Equal(t, provider.Constraints()[0], cache.PkgPredicate("etcd"))
37+
}
38+
39+
func TestNewFromFile_ErrorOnNotFound(t *testing.T) {
40+
provider, err := NewFromFile("testdata/not/a/real/path.json")
41+
require.NotNil(t, err)
42+
require.Nil(t, provider)
43+
}
44+
45+
func TestNewFromFile_TooManyConstraints(t *testing.T) {
46+
provider, err := NewFromFile("testdata/too_many_constraints.json")
47+
require.NotNil(t, err)
48+
require.Nil(t, provider)
49+
}
50+
51+
func TestNewFromEnv_HappyPath(t *testing.T) {
52+
require.Nil(t, os.Setenv(RuntimeConstraintEnvVarName, "testdata/runtime_constraints.json"))
53+
t.Cleanup(func() { _ = os.Unsetenv(RuntimeConstraintEnvVarName) })
54+
55+
provider, err := NewFromEnv()
56+
require.Nil(t, err)
57+
require.Len(t, provider.Constraints(), 1)
58+
require.Equal(t, provider.Constraints()[0], cache.PkgPredicate("etcd"))
59+
}
60+
61+
func TestNewFromEnv_ErrorOnNotFound(t *testing.T) {
62+
testCases := []struct {
63+
title string
64+
value string
65+
}{
66+
{
67+
title: "File not found",
68+
value: "testdata/not/a/real/path.json",
69+
}, {
70+
title: "Bad path",
71+
value: "fsdkljflsdk ropweiropw 4434!@#!#",
72+
}, {
73+
title: "No env var set",
74+
value: "nil",
75+
},
76+
}
77+
78+
for _, testCase := range testCases {
79+
if testCase.value != "nil" {
80+
require.Nil(t, os.Setenv(RuntimeConstraintEnvVarName, testCase.value))
81+
t.Cleanup(func() { _ = os.Unsetenv(RuntimeConstraintEnvVarName) })
82+
}
83+
provider, err := NewFromEnv()
84+
require.NotNil(t, err)
85+
require.Nil(t, provider)
86+
}
87+
}
88+
89+
func TestNewFromEnv_TooManyConstraints(t *testing.T) {
90+
require.Nil(t, os.Setenv(RuntimeConstraintEnvVarName, "testdata/too_many_constraints.json"))
91+
t.Cleanup(func() { _ = os.Unsetenv(RuntimeConstraintEnvVarName) })
92+
provider, err := NewFromEnv()
93+
require.NotNil(t, err)
94+
require.Nil(t, provider)
95+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"properties": [
3+
{
4+
"type": "olm.package",
5+
"value": {
6+
"packageName": "etcd12",
7+
"version": "1.0.0"
8+
}
9+
}, {
10+
"type": "olm.package",
11+
"value": {
12+
"packageName": "etcd11",
13+
"version": "1.0.0"
14+
}
15+
}, {
16+
"type": "olm.package",
17+
"value": {
18+
"packageName": "etcd10",
19+
"version": "1.0.0"
20+
}
21+
}, {
22+
"type": "olm.package",
23+
"value": {
24+
"packageName": "etcd9",
25+
"version": "1.0.0"
26+
}
27+
}, {
28+
"type": "olm.package",
29+
"value": {
30+
"packageName": "etcd8",
31+
"version": "1.0.0"
32+
}
33+
}, {
34+
"type": "olm.package",
35+
"value": {
36+
"packageName": "etcd7",
37+
"version": "1.0.0"
38+
}
39+
}, {
40+
"type": "olm.package",
41+
"value": {
42+
"packageName": "etcd6",
43+
"version": "1.0.0"
44+
}
45+
}, {
46+
"type": "olm.package",
47+
"value": {
48+
"packageName": "etcd5",
49+
"version": "1.0.0"
50+
}
51+
}, {
52+
"type": "olm.package",
53+
"value": {
54+
"packageName": "etcd4",
55+
"version": "1.0.0"
56+
}
57+
}, {
58+
"type": "olm.package",
59+
"value": {
60+
"packageName": "etcd3",
61+
"version": "1.0.0"
62+
}
63+
}, {
64+
"type": "olm.package",
65+
"value": {
66+
"packageName": "etcd2",
67+
"version": "1.0.0"
68+
}
69+
}, {
70+
"type": "olm.package",
71+
"value": {
72+
"packageName": "etcd1",
73+
"version": "1.0.0"
74+
}
75+
}
76+
]
77+
}

test/e2e/runtime_constraints_e2e_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func mustDeployRuntimeConstraintsConfigMap(kubeClient k8scontrollerclient.Client
8585
| {
8686
| "type": "olm.package",
8787
| "value": {
88-
| "packageName": "",
88+
| "packageName": "etcd",
8989
| "version": "1.0.0"
9090
| }
9191
| }

0 commit comments

Comments
 (0)