@@ -4,11 +4,16 @@ import (
4
4
"context"
5
5
"encoding/json"
6
6
"fmt"
7
+ "testing"
7
8
9
+ "github.com/google/go-cmp/cmp"
8
10
. "github.com/onsi/ginkgo/v2"
9
11
. "github.com/onsi/gomega"
12
+ "github.com/stretchr/testify/require"
10
13
11
14
"github.com/operator-framework/deppy/pkg/deppy"
15
+ "github.com/operator-framework/deppy/pkg/deppy/constraint"
16
+ "github.com/operator-framework/deppy/pkg/deppy/input"
12
17
"github.com/operator-framework/operator-registry/alpha/declcfg"
13
18
"github.com/operator-framework/operator-registry/alpha/property"
14
19
@@ -17,6 +22,98 @@ import (
17
22
"github.com/operator-framework/operator-controller/internal/resolution/variablesources"
18
23
)
19
24
25
+ func TestMakeBundleUniquenessVariables (t * testing.T ) {
26
+ const fakeCatalogName = "fake-catalog"
27
+ channel := catalogmetadata.Channel {Channel : declcfg.Channel {Name : "stable" }}
28
+ bundleSet := map [string ]* catalogmetadata.Bundle {
29
+ "test-package.v1.0.0" : {
30
+ Bundle : declcfg.Bundle {
31
+ Name : "test-package.v1.0.0" ,
32
+ Package : "test-package" ,
33
+ Properties : []property.Property {
34
+ {Type : property .TypePackage , Value : json .RawMessage (`{"packageName": "test-package", "version": "1.0.0"}` )},
35
+ {Type : property .TypePackageRequired , Value : json .RawMessage (`{"packageName": "some-package", "versionRange": ">=1.0.0 <2.0.0"}` )},
36
+ },
37
+ },
38
+ CatalogName : fakeCatalogName ,
39
+ InChannels : []* catalogmetadata.Channel {& channel },
40
+ },
41
+ "test-package.v1.0.1" : {
42
+ Bundle : declcfg.Bundle {
43
+ Name : "test-package.v1.0.1" ,
44
+ Package : "test-package" ,
45
+ Properties : []property.Property {
46
+ {Type : property .TypePackage , Value : json .RawMessage (`{"packageName": "test-package", "version": "1.0.1"}` )},
47
+ {Type : property .TypePackageRequired , Value : json .RawMessage (`{"packageName": "some-package", "versionRange": ">=1.0.0 <2.0.0"}` )},
48
+ },
49
+ },
50
+ CatalogName : fakeCatalogName ,
51
+ InChannels : []* catalogmetadata.Channel {& channel },
52
+ },
53
+
54
+ "some-package.v1.0.0" : {
55
+ Bundle : declcfg.Bundle {
56
+ Name : "some-package.v1.0.0" ,
57
+ Package : "some-package" ,
58
+ Properties : []property.Property {
59
+ {Type : property .TypePackage , Value : json .RawMessage (`{"packageName": "some-package", "version": "1.0.0"}` )},
60
+ },
61
+ },
62
+ CatalogName : fakeCatalogName ,
63
+ InChannels : []* catalogmetadata.Channel {& channel },
64
+ },
65
+ }
66
+
67
+ // Input into the testable function must include more than one bundle
68
+ // from the same package to ensure that the function
69
+ // enforces uniqueness correctly.
70
+ // We also need at least one bundle variable to have a dependency
71
+ // on another package. This will help to make sure that the function
72
+ // also creates uniqueness variables for dependencies.
73
+ bundleVariables := []* olmvariables.BundleVariable {
74
+ olmvariables .NewBundleVariable (
75
+ bundleSet ["test-package.v1.0.0" ],
76
+ []* catalogmetadata.Bundle {
77
+ bundleSet ["some-package.v1.0.0" ],
78
+ },
79
+ ),
80
+ olmvariables .NewBundleVariable (
81
+ bundleSet ["test-package.v1.0.1" ],
82
+ []* catalogmetadata.Bundle {
83
+ bundleSet ["some-package.v1.0.0" ],
84
+ },
85
+ ),
86
+ }
87
+
88
+ variables := variablesources .MakeBundleUniquenessVariables (bundleVariables )
89
+
90
+ // Each package in the input must have own uniqueness variable.
91
+ // Each variable expected to have one constraint enforcing at most one
92
+ // of the involved bundles to be allowed in the solution
93
+ expectedVariables := []* olmvariables.BundleUniquenessVariable {
94
+ {
95
+ SimpleVariable : input .NewSimpleVariable (
96
+ "test-package package uniqueness" ,
97
+ constraint .AtMost (
98
+ 1 ,
99
+ deppy .Identifier ("fake-catalog-test-package-test-package.v1.0.0" ),
100
+ deppy .Identifier ("fake-catalog-test-package-test-package.v1.0.1" ),
101
+ ),
102
+ ),
103
+ },
104
+ {
105
+ SimpleVariable : input .NewSimpleVariable (
106
+ "some-package package uniqueness" ,
107
+ constraint .AtMost (
108
+ 1 ,
109
+ deppy .Identifier ("fake-catalog-some-package-some-package.v1.0.0" ),
110
+ ),
111
+ ),
112
+ },
113
+ }
114
+ require .Empty (t , cmp .Diff (variables , expectedVariables , cmp .AllowUnexported (input.SimpleVariable {}, constraint.AtMostConstraint {})))
115
+ }
116
+
20
117
var channel = catalogmetadata.Channel {Channel : declcfg.Channel {Name : "stable" }}
21
118
var bundleSet = map [string ]* catalogmetadata.Bundle {
22
119
// required package bundles
0 commit comments