-
Notifications
You must be signed in to change notification settings - Fork 300
/
olm_test.go
217 lines (193 loc) · 9.06 KB
/
olm_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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//go:build e2e
// +build e2e
package e2e
import (
"context"
"fmt"
"testing"
"time"
. "github.com/onsi/gomega"
hyperv1 "github.com/openshift/hypershift/api/v1beta1"
"github.com/openshift/hypershift/hypershift-operator/controllers/manifests"
e2eutil "github.com/openshift/hypershift/test/e2e/util"
operatorsv1 "github.com/operator-framework/api/pkg/operators/v1"
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
crclient "sigs.k8s.io/controller-runtime/pkg/client"
)
const (
guestClusterCatalogSourceName = "guestcluster-operators"
redhatOperatorsCatalogSourceName = "redhat-operators"
redhatOperatorsDeploymentName = "redhat-operators-catalog"
openshiftMarketplaceNamespace = "openshift-marketplace"
)
// TestOLM executes a suite of olm tests which ensure that olm is
// behaving as expected on the guest cluster.
func TestOLM(t *testing.T) {
// Skip test until https://issues.redhat.com/browse/OCPBUGS-4600 is fixed
// Pod restarts are already ignored for *-catalog pods
t.SkipNow()
t.Parallel()
ctx, cancel := context.WithCancel(testContext)
defer cancel()
// Create a cluster
clusterOpts := globalOpts.DefaultClusterOptions(t)
e2eutil.NewHypershiftTest(t, ctx, func(t *testing.T, g Gomega, mgtClient crclient.Client, hostedCluster *hyperv1.HostedCluster) {
// Get guest client
t.Logf("Waiting for guest client to become available")
guestClient := e2eutil.WaitForGuestClient(t, ctx, mgtClient, hostedCluster)
guestNamespace := manifests.HostedControlPlaneNamespace(hostedCluster.Namespace, hostedCluster.Name).Name
t.Logf("Hosted control plane namespace is %s", guestNamespace)
waitCtx, cancel := context.WithTimeout(ctx, 15*time.Minute)
defer cancel()
t.Logf("Retrieving Red Hat catalogSource deployment image")
redhatCatalogDeployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: redhatOperatorsDeploymentName,
Namespace: guestNamespace,
},
}
err := wait.PollImmediateUntilWithContext(waitCtx, 5*time.Second, func(ctx context.Context) (bool, error) {
if err := mgtClient.Get(ctx, crclient.ObjectKeyFromObject(redhatCatalogDeployment), redhatCatalogDeployment); err != nil {
t.Logf("failed to get Red Hat Catalog deployment %s/%s: %s", redhatCatalogDeployment.Namespace, redhatCatalogDeployment.Name, err)
return false, nil
}
return true, nil
})
t.Logf("Creating guest cluster catalogSource using the Red Hat operators image from the hosted control plane")
guestClusterCatalogSource := &operatorsv1alpha1.CatalogSource{
ObjectMeta: metav1.ObjectMeta{
Name: guestClusterCatalogSourceName,
Namespace: openshiftMarketplaceNamespace,
},
Spec: operatorsv1alpha1.CatalogSourceSpec{
SourceType: operatorsv1alpha1.SourceTypeGrpc,
Image: redhatCatalogDeployment.Spec.Template.Spec.Containers[0].Image,
Publisher: "OLM HyperShift Team",
DisplayName: "OLM HyperShift Test CatalogSource",
},
}
err = wait.PollImmediateUntilWithContext(waitCtx, 5*time.Second, func(ctx context.Context) (bool, error) {
if err := guestClient.Create(ctx, guestClusterCatalogSource); err != nil && !apierrors.IsAlreadyExists(err) {
t.Logf("failed to create guest cluster catalogSource %s/%s: %s", guestClusterCatalogSource.Namespace, guestClusterCatalogSource.Name, err)
return false, nil
}
return true, nil
})
g.Expect(err).NotTo(HaveOccurred(), "unable to create guest cluster catalogSource")
t.Logf("guest cluster catalogSource created")
defer func() {
err = guestClient.Delete(ctx, guestClusterCatalogSource)
g.Expect(err != nil && !apierrors.IsNotFound(err)).To(BeFalse(), fmt.Sprintf("failed to delete guest cluster catalogSource %s/%s: %v", guestClusterCatalogSource.Namespace, guestClusterCatalogSource.Name, err))
}()
t.Run("Guest cluster catalogSources serve content", testGuestClusterCatalogReady(waitCtx, guestClient))
t.Run("Install operator from Red Hat operators catalogSource", testOperatorInstallationFromSource(waitCtx, guestClient, redhatOperatorsCatalogSourceName))
t.Run("Install operator from guest cluster catalogSource", testOperatorInstallationFromSource(waitCtx, guestClient, guestClusterCatalogSourceName))
}).Execute(&clusterOpts, globalOpts.Platform, globalOpts.ArtifactDir, globalOpts.ServiceAccountSigningKey)
}
// testGuestClusterCatalogReady ensures that Catalog Operator is able to connect to the CatalogSource created on the guestCluster.
func testGuestClusterCatalogReady(parentCtx context.Context, client crclient.Client) func(t *testing.T) {
return func(t *testing.T) {
g := NewWithT(t)
ctx, cancel := context.WithCancel(parentCtx)
defer cancel()
guestClusterCatalogSource := &operatorsv1alpha1.CatalogSource{
ObjectMeta: metav1.ObjectMeta{
Name: guestClusterCatalogSourceName,
Namespace: openshiftMarketplaceNamespace,
},
}
// The guest cluster catalogSource should eventually become available
err := wait.PollImmediateUntil(5*time.Second, func() (bool, error) {
if err := client.Get(ctx, crclient.ObjectKeyFromObject(guestClusterCatalogSource), guestClusterCatalogSource); err != nil {
t.Logf("failed to get guestcluster catalogsource %s/%s: %s", guestClusterCatalogSource.Namespace, guestClusterCatalogSource.Name, err)
return false, nil
}
if guestClusterCatalogSource.Status.GRPCConnectionState == nil {
return false, nil
}
return guestClusterCatalogSource.Status.GRPCConnectionState.LastObservedState == "READY", nil
}, ctx.Done())
g.Expect(err).NotTo(HaveOccurred(), "guestCluster catalogSource never became READY")
t.Logf("guestCluster catalogSource became available")
}
}
// testOperatorInstallationFromSource ensures that an operator can be installed from a catalogSource in the openshift-marketplace namespace.
func testOperatorInstallationFromSource(parentCtx context.Context, client crclient.Client, catalogSourceName string) func(t *testing.T) {
return func(t *testing.T) {
g := NewWithT(t)
ctx, cancel := context.WithCancel(parentCtx)
defer cancel()
testNamespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "olm-test-",
},
}
err := client.Create(ctx, testNamespace)
g.Expect(err).NotTo(HaveOccurred(), "failed to create guest cluster namespace")
defer func() {
err = client.Delete(ctx, testNamespace)
g.Expect(err != nil && !apierrors.IsNotFound(err)).To(BeFalse(), fmt.Sprintf("failed to delete test namespace %s: %v", testNamespace.Name, err))
}()
t.Logf("Created namespace %s for test", testNamespace.Name)
operatorGroup := &operatorsv1.OperatorGroup{
ObjectMeta: metav1.ObjectMeta{
Namespace: testNamespace.Name,
Name: "olm-test",
},
Spec: operatorsv1.OperatorGroupSpec{
TargetNamespaces: []string{testNamespace.Name},
},
}
err = client.Create(ctx, operatorGroup)
g.Expect(err).NotTo(HaveOccurred(), "failed to create guest cluster operatorGroup")
t.Logf("Created opeatorGroup %s/%s for test", operatorGroup.Namespace, operatorGroup.Name)
subscription := &operatorsv1alpha1.Subscription{
ObjectMeta: metav1.ObjectMeta{
Namespace: testNamespace.Name,
Name: "olm-test",
},
Spec: &operatorsv1alpha1.SubscriptionSpec{
InstallPlanApproval: "Automatic",
Channel: "stable",
// TODO: Rely on Operator Framework managed package once released
// instead of third party package we have no control over.
Package: "local-storage-operator",
CatalogSourceNamespace: openshiftMarketplaceNamespace,
CatalogSource: catalogSourceName,
},
}
err = client.Create(ctx, subscription)
g.Expect(err).NotTo(HaveOccurred(), "failed to create guest cluster subscription")
t.Logf("Created subscription %s/%s for test", subscription.Namespace, subscription.Name)
// Wait for successful csv installation
t.Logf("Waiting for csv to be installed by subscription")
err = wait.PollImmediateUntil(5*time.Second, func() (bool, error) {
if err := client.Get(ctx, crclient.ObjectKeyFromObject(subscription), subscription); err != nil {
t.Logf("failed to get guestcluster subscription %s/%s: %s", subscription.Namespace, subscription.Name, err)
return false, nil
}
return subscription.Status.InstalledCSV != "", nil
}, ctx.Done())
g.Expect(err).NotTo(HaveOccurred(), "csv never installed")
t.Logf("subscription created csv %s/%s", subscription.Namespace, subscription.Status.InstalledCSV)
// Wait for the CSV to enter the succeeded phase
t.Logf("Waiting for csv to enter the succeeded phase")
csv := &operatorsv1alpha1.ClusterServiceVersion{
ObjectMeta: metav1.ObjectMeta{
Namespace: testNamespace.Name,
Name: subscription.Status.InstalledCSV,
},
}
err = wait.PollImmediateUntil(5*time.Second, func() (bool, error) {
if err := client.Get(ctx, crclient.ObjectKeyFromObject(csv), csv); err != nil {
return false, nil
}
return csv.Status.Phase == operatorsv1alpha1.CSVPhaseSucceeded, nil
}, ctx.Done())
}
}