-
Notifications
You must be signed in to change notification settings - Fork 48
/
required_package_test.go
194 lines (185 loc) · 6.35 KB
/
required_package_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package variablesources_test
import (
"encoding/json"
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/operator-framework/deppy/pkg/deppy/constraint"
"github.com/operator-framework/deppy/pkg/deppy/input"
"github.com/operator-framework/operator-registry/alpha/declcfg"
"github.com/operator-framework/operator-registry/alpha/property"
operatorsv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
"github.com/operator-framework/operator-controller/internal/catalogmetadata"
olmvariables "github.com/operator-framework/operator-controller/internal/resolution/variables"
"github.com/operator-framework/operator-controller/internal/resolution/variablesources"
)
func TestMakeRequiredPackageVariables(t *testing.T) {
stableChannel := catalogmetadata.Channel{Channel: declcfg.Channel{
Name: "stable",
}}
betaChannel := catalogmetadata.Channel{Channel: declcfg.Channel{
Name: "beta",
}}
bundleSet := map[string]*catalogmetadata.Bundle{
// Bundles which belong to test-package we will be using
// to assert wether the testable function filters out the data
// correctly.
"test-package.v1.0.0": {
Bundle: declcfg.Bundle{
Name: "test-package.v1.0.0",
Package: "test-package",
Properties: []property.Property{
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "1.0.0"}`)},
},
},
InChannels: []*catalogmetadata.Channel{&stableChannel},
},
"test-package.v3.0.0": {
Bundle: declcfg.Bundle{
Name: "test-package.v3.0.0",
Package: "test-package",
Properties: []property.Property{
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "3.0.0"}`)},
},
},
InChannels: []*catalogmetadata.Channel{&stableChannel, &betaChannel},
},
"test-package.v2.0.0": {
Bundle: declcfg.Bundle{
Name: "test-package.v2.0.0",
Package: "test-package",
Properties: []property.Property{
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package", "version": "2.0.0"}`)},
},
},
InChannels: []*catalogmetadata.Channel{&stableChannel},
},
// We need at least one bundle from different package
// to make sure that we are filtering it out.
"test-package-2.v1.0.0": {
Bundle: declcfg.Bundle{
Name: "test-package-2.v1.0.0",
Package: "test-package-2",
Properties: []property.Property{
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName": "test-package-2", "version": "1.0.0"}`)},
},
},
InChannels: []*catalogmetadata.Channel{&stableChannel},
},
}
allBundles := make([]*catalogmetadata.Bundle, 0, len(bundleSet))
for _, bundle := range bundleSet {
allBundles = append(allBundles, bundle)
}
fakeOperator := func(packageName, channelName, versionRange string) operatorsv1alpha1.Operator {
return operatorsv1alpha1.Operator{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("op-%s-%s-%s", packageName, channelName, versionRange),
},
Spec: operatorsv1alpha1.OperatorSpec{
PackageName: packageName,
Version: versionRange,
Channel: channelName,
},
}
}
for _, tt := range []struct {
name string
operators []operatorsv1alpha1.Operator
expectedResult []*olmvariables.RequiredPackageVariable
expectedError string
}{
{
name: "package name only",
operators: []operatorsv1alpha1.Operator{
fakeOperator("test-package", "", ""),
},
expectedResult: []*olmvariables.RequiredPackageVariable{
olmvariables.NewRequiredPackageVariable("test-package", []*catalogmetadata.Bundle{
bundleSet["test-package.v3.0.0"],
bundleSet["test-package.v2.0.0"],
bundleSet["test-package.v1.0.0"],
}),
},
},
{
name: "package name and channel",
operators: []operatorsv1alpha1.Operator{
fakeOperator("test-package", "beta", ""),
},
expectedResult: []*olmvariables.RequiredPackageVariable{
olmvariables.NewRequiredPackageVariable("test-package", []*catalogmetadata.Bundle{
bundleSet["test-package.v3.0.0"],
}),
},
},
{
name: "package name and version range",
operators: []operatorsv1alpha1.Operator{
fakeOperator("test-package", "", ">=1.0.0 !=2.0.0 <3.0.0"),
},
expectedResult: []*olmvariables.RequiredPackageVariable{
olmvariables.NewRequiredPackageVariable("test-package", []*catalogmetadata.Bundle{
bundleSet["test-package.v1.0.0"],
}),
},
},
{
name: "package name and invalid version range",
operators: []operatorsv1alpha1.Operator{
fakeOperator("test-package", "", "not a valid semver"),
},
expectedError: `invalid version range "not a valid semver"`,
},
{
name: "not found: package name only",
operators: []operatorsv1alpha1.Operator{
fakeOperator("non-existent-test-package", "", ""),
},
expectedError: `no package "non-existent-test-package" found`,
},
{
name: "not found: package name and channel",
operators: []operatorsv1alpha1.Operator{
fakeOperator("non-existent-test-package", "stable", ""),
},
expectedError: `no package "non-existent-test-package" found in channel "stable"`,
},
{
name: "not found: package name and version range",
operators: []operatorsv1alpha1.Operator{
fakeOperator("non-existent-test-package", "", "1.0.0"),
},
expectedError: `no package "non-existent-test-package" matching version "1.0.0" found`,
},
{
name: "not found: package name with channel and version range",
operators: []operatorsv1alpha1.Operator{
fakeOperator("non-existent-test-package", "stable", "1.0.0"),
},
expectedError: `no package "non-existent-test-package" matching version "1.0.0" found in channel "stable"`,
},
} {
t.Run(tt.name, func(t *testing.T) {
vars, err := variablesources.MakeRequiredPackageVariables(allBundles, tt.operators)
if tt.expectedError == "" {
assert.NoError(t, err)
} else {
assert.ErrorContains(t, err, tt.expectedError)
}
gocmpopts := []cmp.Option{
cmpopts.IgnoreUnexported(catalogmetadata.Bundle{}),
cmp.AllowUnexported(
olmvariables.RequiredPackageVariable{},
input.SimpleVariable{},
constraint.DependencyConstraint{},
),
}
require.Empty(t, cmp.Diff(vars, tt.expectedResult, gocmpopts...))
})
}
}