|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + appsv1 "k8s.io/api/apps/v1" |
| 9 | + corev1 "k8s.io/api/core/v1" |
| 10 | + rbacv1 "k8s.io/api/rbac/v1" |
| 11 | + apimeta "k8s.io/apimachinery/pkg/api/meta" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 14 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 15 | + "k8s.io/apimachinery/pkg/types" |
| 16 | + "k8s.io/client-go/dynamic" |
| 17 | + "k8s.io/utils/ptr" |
| 18 | + "os" |
| 19 | + "testing" |
| 20 | + |
| 21 | + ocv1 "github.com/operator-framework/operator-controller/api/v1" |
| 22 | + utils "github.com/operator-framework/operator-controller/internal/shared/util/testutils" |
| 23 | +) |
| 24 | + |
| 25 | +var dynamicClient dynamic.Interface |
| 26 | + |
| 27 | +func TestNoop(t *testing.T) { |
| 28 | + t.Log("Running experimental-e2e tests") |
| 29 | + defer utils.CollectTestArtifacts(t, artifactName, c, cfg) |
| 30 | +} |
| 31 | + |
| 32 | +func TestWebhookSupport(t *testing.T) { |
| 33 | + t.Log("Test support for bundles with webhooks") |
| 34 | + defer utils.CollectTestArtifacts(t, artifactName, c, cfg) |
| 35 | + |
| 36 | + if dynamicClient == nil { |
| 37 | + var err error |
| 38 | + dynamicClient, err = dynamic.NewForConfig(cfg) |
| 39 | + require.NoError(t, err) |
| 40 | + } |
| 41 | + |
| 42 | + t.Log("By creating install namespace, and necessary rbac resources") |
| 43 | + namespace := corev1.Namespace{ |
| 44 | + ObjectMeta: metav1.ObjectMeta{ |
| 45 | + Name: "webhook-operator", |
| 46 | + }, |
| 47 | + } |
| 48 | + require.NoError(t, c.Create(t.Context(), &namespace)) |
| 49 | + t.Cleanup(func() { |
| 50 | + require.NoError(t, c.Delete(context.Background(), &namespace)) |
| 51 | + }) |
| 52 | + |
| 53 | + serviceAccount := corev1.ServiceAccount{ |
| 54 | + ObjectMeta: metav1.ObjectMeta{ |
| 55 | + Name: "webhook-operator-installer", |
| 56 | + Namespace: namespace.GetName(), |
| 57 | + }, |
| 58 | + } |
| 59 | + require.NoError(t, c.Create(t.Context(), &serviceAccount)) |
| 60 | + t.Cleanup(func() { |
| 61 | + require.NoError(t, c.Delete(context.Background(), &serviceAccount)) |
| 62 | + }) |
| 63 | + |
| 64 | + clusterRoleBinding := &rbacv1.ClusterRoleBinding{ |
| 65 | + ObjectMeta: metav1.ObjectMeta{ |
| 66 | + Name: "webhook-operator-installer", |
| 67 | + }, |
| 68 | + Subjects: []rbacv1.Subject{ |
| 69 | + { |
| 70 | + Kind: "ServiceAccount", |
| 71 | + APIGroup: corev1.GroupName, |
| 72 | + Name: serviceAccount.GetName(), |
| 73 | + Namespace: serviceAccount.GetNamespace(), |
| 74 | + }, |
| 75 | + }, |
| 76 | + RoleRef: rbacv1.RoleRef{ |
| 77 | + APIGroup: rbacv1.GroupName, |
| 78 | + Kind: "ClusterRole", |
| 79 | + Name: "cluster-admin", |
| 80 | + }, |
| 81 | + } |
| 82 | + require.NoError(t, c.Create(t.Context(), clusterRoleBinding)) |
| 83 | + t.Cleanup(func() { |
| 84 | + require.NoError(t, c.Delete(context.Background(), clusterRoleBinding)) |
| 85 | + }) |
| 86 | + |
| 87 | + t.Log("By creating the webhook-operator ClusterCatalog") |
| 88 | + extensionCatalog := &ocv1.ClusterCatalog{ |
| 89 | + ObjectMeta: metav1.ObjectMeta{ |
| 90 | + Name: "webhook-operator-catalog", |
| 91 | + }, |
| 92 | + Spec: ocv1.ClusterCatalogSpec{ |
| 93 | + Source: ocv1.CatalogSource{ |
| 94 | + Type: ocv1.SourceTypeImage, |
| 95 | + Image: &ocv1.ImageSource{ |
| 96 | + Ref: fmt.Sprintf("%s/e2e/test-catalog:v1", os.Getenv("CLUSTER_REGISTRY_HOST")), |
| 97 | + PollIntervalMinutes: ptr.To(1), |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + } |
| 102 | + require.NoError(t, c.Create(t.Context(), extensionCatalog)) |
| 103 | + t.Cleanup(func() { |
| 104 | + require.NoError(t, c.Delete(context.Background(), extensionCatalog)) |
| 105 | + }) |
| 106 | + |
| 107 | + t.Log("By waiting for the catalog to serve its metadata") |
| 108 | + require.EventuallyWithT(t, func(ct *assert.CollectT) { |
| 109 | + require.NoError(ct, c.Get(context.Background(), types.NamespacedName{Name: extensionCatalog.GetName()}, extensionCatalog)) |
| 110 | + cond := apimeta.FindStatusCondition(extensionCatalog.Status.Conditions, ocv1.TypeServing) |
| 111 | + require.NotNil(ct, cond) |
| 112 | + require.Equal(ct, metav1.ConditionTrue, cond.Status) |
| 113 | + require.Equal(ct, ocv1.ReasonAvailable, cond.Reason) |
| 114 | + }, pollDuration, pollInterval) |
| 115 | + |
| 116 | + t.Log("By installing the webhook-operator ClusterExtension") |
| 117 | + clusterExtension := &ocv1.ClusterExtension{ |
| 118 | + ObjectMeta: metav1.ObjectMeta{ |
| 119 | + Name: "webhook-operator-extension", |
| 120 | + }, |
| 121 | + Spec: ocv1.ClusterExtensionSpec{ |
| 122 | + Source: ocv1.SourceConfig{ |
| 123 | + SourceType: "Catalog", |
| 124 | + Catalog: &ocv1.CatalogFilter{ |
| 125 | + PackageName: "webhook-operator", |
| 126 | + Selector: &metav1.LabelSelector{ |
| 127 | + MatchLabels: map[string]string{"olm.operatorframework.io/metadata.name": extensionCatalog.Name}, |
| 128 | + }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + Namespace: namespace.GetName(), |
| 132 | + ServiceAccount: ocv1.ServiceAccountReference{ |
| 133 | + Name: serviceAccount.GetName(), |
| 134 | + }, |
| 135 | + }, |
| 136 | + } |
| 137 | + require.NoError(t, c.Create(t.Context(), clusterExtension)) |
| 138 | + t.Cleanup(func() { |
| 139 | + require.NoError(t, c.Delete(context.Background(), clusterExtension)) |
| 140 | + }) |
| 141 | + |
| 142 | + t.Log("By waiting for webhook-operator extension to be installed successfully") |
| 143 | + require.EventuallyWithT(t, func(ct *assert.CollectT) { |
| 144 | + require.NoError(ct, c.Get(t.Context(), types.NamespacedName{Name: clusterExtension.Name}, clusterExtension)) |
| 145 | + cond := apimeta.FindStatusCondition(clusterExtension.Status.Conditions, ocv1.TypeInstalled) |
| 146 | + require.NotNil(ct, cond) |
| 147 | + require.Equal(ct, metav1.ConditionTrue, cond.Status) |
| 148 | + require.Equal(ct, ocv1.ReasonSucceeded, cond.Reason) |
| 149 | + require.Contains(ct, cond.Message, "Installed bundle") |
| 150 | + require.NotNil(ct, clusterExtension.Status.Install) |
| 151 | + require.NotEmpty(ct, clusterExtension.Status.Install.Bundle) |
| 152 | + }, pollDuration, pollInterval) |
| 153 | + |
| 154 | + t.Log("By waiting for webhook-operator deployment to be available") |
| 155 | + require.EventuallyWithT(t, func(ct *assert.CollectT) { |
| 156 | + deployment := &appsv1.Deployment{} |
| 157 | + require.NoError(ct, c.Get(t.Context(), types.NamespacedName{Namespace: namespace.GetName(), Name: "webhook-operator-controller-manager"}, deployment)) |
| 158 | + available := false |
| 159 | + for _, cond := range deployment.Status.Conditions { |
| 160 | + if cond.Type == appsv1.DeploymentAvailable { |
| 161 | + available = cond.Status == corev1.ConditionTrue |
| 162 | + } |
| 163 | + } |
| 164 | + require.True(ct, available) |
| 165 | + }, pollDuration, pollInterval) |
| 166 | + |
| 167 | + v1Gvr := schema.GroupVersionResource{ |
| 168 | + Group: "webhook.operators.coreos.io", |
| 169 | + Version: "v1", |
| 170 | + Resource: "webhooktests", |
| 171 | + } |
| 172 | + v1Client := dynamicClient.Resource(v1Gvr).Namespace(namespace.GetName()) |
| 173 | + |
| 174 | + t.Log("By eventually seeing that invalid CR creation is rejected by the validating webhook") |
| 175 | + require.EventuallyWithT(t, func(ct *assert.CollectT) { |
| 176 | + obj := getWebhookOperatorResource("invalid-test-cr", namespace.GetName(), false) |
| 177 | + _, err := v1Client.Create(t.Context(), obj, metav1.CreateOptions{}) |
| 178 | + require.Error(ct, err) |
| 179 | + require.Contains(ct, err.Error(), "Invalid value: false: Spec.Valid must be true") |
| 180 | + }, pollDuration, pollInterval) |
| 181 | + |
| 182 | + var ( |
| 183 | + res *unstructured.Unstructured |
| 184 | + err error |
| 185 | + obj = getWebhookOperatorResource("valid-test-cr", namespace.GetName(), true) |
| 186 | + ) |
| 187 | + |
| 188 | + t.Log("By eventually creating a valid CR") |
| 189 | + require.EventuallyWithT(t, func(ct *assert.CollectT) { |
| 190 | + res, err = v1Client.Create(t.Context(), obj, metav1.CreateOptions{}) |
| 191 | + require.NoError(ct, err) |
| 192 | + }, pollDuration, pollInterval) |
| 193 | + t.Cleanup(func() { |
| 194 | + require.NoError(t, v1Client.Delete(context.Background(), obj.GetName(), metav1.DeleteOptions{})) |
| 195 | + }) |
| 196 | + |
| 197 | + require.Equal(t, map[string]interface{}{ |
| 198 | + "valid": true, |
| 199 | + "mutate": true, |
| 200 | + }, res.Object["spec"]) |
| 201 | + |
| 202 | + t.Log("By checking a valid CR is converted to v2 by the conversion webhook") |
| 203 | + v2Gvr := schema.GroupVersionResource{ |
| 204 | + Group: "webhook.operators.coreos.io", |
| 205 | + Version: "v2", |
| 206 | + Resource: "webhooktests", |
| 207 | + } |
| 208 | + v2Client := dynamicClient.Resource(v2Gvr).Namespace(namespace.GetName()) |
| 209 | + |
| 210 | + t.Log("By eventually getting the valid CR with a v2 client") |
| 211 | + require.EventuallyWithT(t, func(ct *assert.CollectT) { |
| 212 | + res, err = v2Client.Get(t.Context(), obj.GetName(), metav1.GetOptions{}) |
| 213 | + require.NoError(ct, err) |
| 214 | + }, pollDuration, pollInterval) |
| 215 | + |
| 216 | + t.Log("and verifying that the CR is correctly converted") |
| 217 | + require.Equal(t, map[string]interface{}{ |
| 218 | + "conversion": map[string]interface{}{ |
| 219 | + "valid": true, |
| 220 | + "mutate": true, |
| 221 | + }, |
| 222 | + }, res.Object["spec"]) |
| 223 | +} |
| 224 | + |
| 225 | +func getWebhookOperatorResource(name string, namespace string, valid bool) *unstructured.Unstructured { |
| 226 | + return &unstructured.Unstructured{ |
| 227 | + Object: map[string]interface{}{ |
| 228 | + "apiVersion": "webhook.operators.coreos.io/v1", |
| 229 | + "kind": "webhooktests", |
| 230 | + "metadata": map[string]interface{}{ |
| 231 | + "name": name, |
| 232 | + "namespace": namespace, |
| 233 | + }, |
| 234 | + "spec": map[string]interface{}{ |
| 235 | + "valid": valid, |
| 236 | + }, |
| 237 | + }, |
| 238 | + } |
| 239 | +} |
0 commit comments