forked from operator-framework/operator-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclusterextension_registryv1_validation_test.go
151 lines (141 loc) · 6.14 KB
/
clusterextension_registryv1_validation_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
package controllers_test
import (
"context"
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/rand"
ctrl "sigs.k8s.io/controller-runtime"
crfinalizer "sigs.k8s.io/controller-runtime/pkg/finalizer"
"github.com/operator-framework/operator-registry/alpha/declcfg"
"github.com/operator-framework/operator-registry/alpha/property"
"github.com/operator-framework/rukpak/pkg/source"
ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
"github.com/operator-framework/operator-controller/internal/catalogmetadata"
"github.com/operator-framework/operator-controller/internal/controllers"
testutil "github.com/operator-framework/operator-controller/test/util"
)
func TestClusterExtensionRegistryV1DisallowDependencies(t *testing.T) {
ctx := context.Background()
cl := newClient(t)
for _, tt := range []struct {
name string
bundle *catalogmetadata.Bundle
wantErr string
}{
{
name: "package with no dependencies",
bundle: &catalogmetadata.Bundle{
Bundle: declcfg.Bundle{
Name: "fake-catalog/no-dependencies-package/alpha/1.0.0",
Package: "no-dependencies-package",
Image: "quay.io/fake-catalog/no-dependencies-package@sha256:3e281e587de3d03011440685fc4fb782672beab044c1ebadc42788ce05a21c35",
Properties: []property.Property{
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"no-dependencies-package","version":"1.0.0"}`)},
},
},
CatalogName: "fake-catalog",
},
},
{
name: "package with olm.package.required property",
bundle: &catalogmetadata.Bundle{
Bundle: declcfg.Bundle{
Name: "fake-catalog/package-required-test/alpha/1.0.0",
Package: "package-required-test",
Image: "quay.io/fake-catalog/package-required-test@sha256:3e281e587de3d03011440685fc4fb782672beab044c1ebadc42788ce05a21c35",
Properties: []property.Property{
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"package-required-test","version":"1.0.0"}`)},
{Type: property.TypePackageRequired, Value: json.RawMessage("content-is-not-relevant")},
},
},
CatalogName: "fake-catalog",
},
wantErr: `bundle "fake-catalog/package-required-test/alpha/1.0.0" has a dependency declared via property "olm.package.required" which is currently not supported`,
},
{
name: "package with olm.gvk.required property",
bundle: &catalogmetadata.Bundle{
Bundle: declcfg.Bundle{
Name: "fake-catalog/gvk-required-test/alpha/1.0.0",
Package: "gvk-required-test",
Image: "quay.io/fake-catalog/gvk-required-test@sha256:3e281e587de3d03011440685fc4fb782672beab044c1ebadc42788ce05a21c35",
Properties: []property.Property{
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"gvk-required-test","version":"1.0.0"}`)},
{Type: property.TypeGVKRequired, Value: json.RawMessage(`content-is-not-relevant`)},
},
},
CatalogName: "fake-catalog",
},
wantErr: `bundle "fake-catalog/gvk-required-test/alpha/1.0.0" has a dependency declared via property "olm.gvk.required" which is currently not supported`,
},
{
name: "package with olm.constraint property",
bundle: &catalogmetadata.Bundle{
Bundle: declcfg.Bundle{
Name: "fake-catalog/constraint-test/alpha/1.0.0",
Package: "constraint-test",
Image: "quay.io/fake-catalog/constraint-test@sha256:3e281e587de3d03011440685fc4fb782672beab044c1ebadc42788ce05a21c35",
Properties: []property.Property{
{Type: property.TypePackage, Value: json.RawMessage(`{"packageName":"constraint-test","version":"1.0.0"}`)},
{Type: property.TypeConstraint, Value: json.RawMessage(`content-is-not-relevant`)},
},
},
CatalogName: "fake-catalog",
},
wantErr: `bundle "fake-catalog/constraint-test/alpha/1.0.0" has a dependency declared via property "olm.constraint" which is currently not supported`,
},
} {
t.Run(tt.name, func(t *testing.T) {
defer func() {
require.NoError(t, cl.DeleteAllOf(ctx, &ocv1alpha1.ClusterExtension{}))
}()
fakeCatalogClient := testutil.NewFakeCatalogClient([]*catalogmetadata.Bundle{tt.bundle})
mockUnpacker := unpacker.(*MockUnpacker)
// Set up the Unpack method to return a result with StatePending
mockUnpacker.On("Unpack", mock.Anything, mock.AnythingOfType("*v1alpha2.BundleDeployment")).Return(&source.Result{
State: source.StatePending,
}, nil)
// Create and configure the mock InstalledBundleGetter
mockInstalledBundleGetter := &MockInstalledBundleGetter{}
reconciler := &controllers.ClusterExtensionReconciler{
Client: cl,
BundleProvider: &fakeCatalogClient,
ActionClientGetter: helmClientGetter,
Unpacker: unpacker,
InstalledBundleGetter: mockInstalledBundleGetter,
Finalizers: crfinalizer.NewFinalizers(),
}
installNamespace := fmt.Sprintf("test-ns-%s", rand.String(8))
extKey := types.NamespacedName{Name: fmt.Sprintf("cluster-extension-test-%s", rand.String(8))}
clusterExtension := &ocv1alpha1.ClusterExtension{
ObjectMeta: metav1.ObjectMeta{Name: extKey.Name},
Spec: ocv1alpha1.ClusterExtensionSpec{
PackageName: tt.bundle.Package,
InstallNamespace: installNamespace,
},
}
require.NoError(t, cl.Create(ctx, clusterExtension))
res, err := reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: extKey})
require.Equal(t, ctrl.Result{}, res)
if tt.wantErr == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tt.wantErr)
// In case of an error we want it to be included in the installed condition
require.NoError(t, cl.Get(ctx, extKey, clusterExtension))
cond := apimeta.FindStatusCondition(clusterExtension.Status.Conditions, ocv1alpha1.TypeInstalled)
require.NotNil(t, cond)
require.Equal(t, metav1.ConditionFalse, cond.Status)
require.Equal(t, ocv1alpha1.ReasonInstallationFailed, cond.Reason)
require.Equal(t, tt.wantErr, cond.Message)
}
})
}
}