|
13 | 13 |
|
14 | 14 | package devworkspacerouting_test |
15 | 15 |
|
| 16 | +import ( |
| 17 | + "time" |
| 18 | + |
| 19 | + controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1" |
| 20 | + "github.com/devfile/devworkspace-operator/pkg/common" |
| 21 | + "github.com/devfile/devworkspace-operator/pkg/constants" |
| 22 | + . "github.com/onsi/gomega" |
| 23 | + routeV1 "github.com/openshift/api/route/v1" |
| 24 | + crclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 25 | + |
| 26 | + corev1 "k8s.io/api/core/v1" |
| 27 | + networkingv1 "k8s.io/api/networking/v1" |
| 28 | + k8sErrors "k8s.io/apimachinery/pkg/api/errors" |
| 29 | + "k8s.io/apimachinery/pkg/types" |
| 30 | +) |
| 31 | + |
16 | 32 | const ( |
17 | | - testNamespace = "devworkspace-test" |
| 33 | + timeout = 10 * time.Second |
| 34 | + interval = 250 * time.Millisecond |
| 35 | + |
| 36 | + testNamespace = "devworkspace-test" |
| 37 | + devWorkspaceRoutingName = "test-devworkspacerouting" |
| 38 | + testWorkspaceID = "test-id" |
| 39 | + testMachineName = "test-machine-name" |
| 40 | + |
| 41 | + exposedEndPointName = "test-endpoint" |
| 42 | + exposedTargetPort = 7777 |
| 43 | + discoverableEndpointName = "discoverable-endpoint" |
| 44 | + discoverableTargetPort = 7979 |
| 45 | + nonExposedEndpointName = "non-exposed-endpoint" |
| 46 | + nonExposedTargetPort = 8989 |
18 | 47 | ) |
| 48 | + |
| 49 | +func createPreparingDWR(workspaceID string, name string) *controllerv1alpha1.DevWorkspaceRouting { |
| 50 | + mainAttributes := controllerv1alpha1.Attributes{} |
| 51 | + mainAttributes.PutString("type", "main") |
| 52 | + exposedEndpoint := controllerv1alpha1.Endpoint{ |
| 53 | + Name: exposedEndPointName, |
| 54 | + Attributes: mainAttributes, |
| 55 | + // Target port must be within range 1 and 65535 |
| 56 | + // Created service will be invalid on cluster and error will be logged |
| 57 | + // DWR will continue trying to reconcile however, keeping it stuck in preparing phase |
| 58 | + TargetPort: 0, |
| 59 | + } |
| 60 | + machineEndpointsMap := map[string]controllerv1alpha1.EndpointList{ |
| 61 | + testMachineName: { |
| 62 | + exposedEndpoint, |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + dwr := &controllerv1alpha1.DevWorkspaceRouting{ |
| 67 | + Spec: controllerv1alpha1.DevWorkspaceRoutingSpec{ |
| 68 | + DevWorkspaceId: workspaceID, |
| 69 | + RoutingClass: controllerv1alpha1.DevWorkspaceRoutingBasic, |
| 70 | + Endpoints: machineEndpointsMap, |
| 71 | + PodSelector: map[string]string{ |
| 72 | + constants.DevWorkspaceIDLabel: workspaceID, |
| 73 | + }, |
| 74 | + }, |
| 75 | + } |
| 76 | + dwr.SetName(name) |
| 77 | + dwr.SetNamespace(testNamespace) |
| 78 | + Expect(k8sClient.Create(ctx, dwr)).Should(Succeed()) |
| 79 | + return dwr |
| 80 | +} |
| 81 | + |
| 82 | +func createDWR(workspaceID string, name string) *controllerv1alpha1.DevWorkspaceRouting { |
| 83 | + mainAttributes := controllerv1alpha1.Attributes{} |
| 84 | + mainAttributes.PutString("type", "main") |
| 85 | + discoverableAttributes := controllerv1alpha1.Attributes{} |
| 86 | + discoverableAttributes.PutBoolean(string(controllerv1alpha1.DiscoverableAttribute), true) |
| 87 | + |
| 88 | + exposedEndpoint := controllerv1alpha1.Endpoint{ |
| 89 | + Name: exposedEndPointName, |
| 90 | + Exposure: controllerv1alpha1.PublicEndpointExposure, |
| 91 | + Attributes: mainAttributes, |
| 92 | + TargetPort: exposedTargetPort, |
| 93 | + } |
| 94 | + nonExposedEndpoint := controllerv1alpha1.Endpoint{ |
| 95 | + Name: nonExposedEndpointName, |
| 96 | + Exposure: controllerv1alpha1.NoneEndpointExposure, |
| 97 | + TargetPort: nonExposedTargetPort, |
| 98 | + } |
| 99 | + discoverableEndpoint := controllerv1alpha1.Endpoint{ |
| 100 | + Name: discoverableEndpointName, |
| 101 | + Exposure: controllerv1alpha1.PublicEndpointExposure, |
| 102 | + Attributes: discoverableAttributes, |
| 103 | + TargetPort: discoverableTargetPort, |
| 104 | + } |
| 105 | + machineEndpointsMap := map[string]controllerv1alpha1.EndpointList{ |
| 106 | + "test-machine-name": { |
| 107 | + exposedEndpoint, |
| 108 | + nonExposedEndpoint, |
| 109 | + discoverableEndpoint, |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + dwr := &controllerv1alpha1.DevWorkspaceRouting{ |
| 114 | + Spec: controllerv1alpha1.DevWorkspaceRoutingSpec{ |
| 115 | + DevWorkspaceId: workspaceID, |
| 116 | + RoutingClass: controllerv1alpha1.DevWorkspaceRoutingBasic, |
| 117 | + Endpoints: machineEndpointsMap, |
| 118 | + PodSelector: map[string]string{ |
| 119 | + constants.DevWorkspaceIDLabel: workspaceID, |
| 120 | + }, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + dwr.SetName(name) |
| 125 | + dwr.SetNamespace(testNamespace) |
| 126 | + |
| 127 | + Expect(k8sClient.Create(ctx, dwr)).Should(Succeed()) |
| 128 | + return dwr |
| 129 | +} |
| 130 | + |
| 131 | +func deleteService(serviceName string, namespace string) { |
| 132 | + createdService := &corev1.Service{} |
| 133 | + serviceNamespacedName := namespacedName(serviceName, namespace) |
| 134 | + Eventually(func() bool { |
| 135 | + err := k8sClient.Get(ctx, serviceNamespacedName, createdService) |
| 136 | + return err == nil |
| 137 | + }, timeout, interval).Should(BeTrue(), "Service should exist in cluster") |
| 138 | + deleteObject(createdService) |
| 139 | +} |
| 140 | + |
| 141 | +func deleteRoute(endpointName string, namespace string) { |
| 142 | + createdRoute := routeV1.Route{} |
| 143 | + routeNamespacedName := namespacedName(common.RouteName(testWorkspaceID, endpointName), namespace) |
| 144 | + Eventually(func() bool { |
| 145 | + err := k8sClient.Get(ctx, routeNamespacedName, &createdRoute) |
| 146 | + return err == nil |
| 147 | + }, timeout, interval).Should(BeTrue(), "Route should exist in cluster") |
| 148 | + deleteObject(&createdRoute) |
| 149 | +} |
| 150 | + |
| 151 | +func deleteIngress(endpointName string, namespace string) { |
| 152 | + createdIngress := networkingv1.Ingress{} |
| 153 | + ingressNamespacedName := namespacedName(common.RouteName(testWorkspaceID, endpointName), namespace) |
| 154 | + Eventually(func() bool { |
| 155 | + err := k8sClient.Get(ctx, ingressNamespacedName, &createdIngress) |
| 156 | + return err == nil |
| 157 | + }, timeout, interval).Should(BeTrue(), "Ingress should exist in cluster") |
| 158 | + deleteObject(&createdIngress) |
| 159 | +} |
| 160 | + |
| 161 | +func deleteDevWorkspaceRouting(name string) { |
| 162 | + dwrNN := namespacedName(name, testNamespace) |
| 163 | + dwr := &controllerv1alpha1.DevWorkspaceRouting{} |
| 164 | + dwr.Name = name |
| 165 | + dwr.Namespace = testNamespace |
| 166 | + // Do nothing if already deleted |
| 167 | + err := k8sClient.Delete(ctx, dwr) |
| 168 | + if k8sErrors.IsNotFound(err) { |
| 169 | + return |
| 170 | + } |
| 171 | + Expect(err).Should(BeNil()) |
| 172 | + |
| 173 | + Eventually(func() bool { |
| 174 | + err := k8sClient.Get(ctx, dwrNN, dwr) |
| 175 | + return err != nil && k8sErrors.IsNotFound(err) |
| 176 | + }, 10*time.Second, 250*time.Millisecond).Should(BeTrue(), "DevWorkspaceRouting not deleted after timeout") |
| 177 | +} |
| 178 | + |
| 179 | +func deleteObject(obj crclient.Object) { |
| 180 | + Expect(k8sClient.Delete(ctx, obj)).Should(Succeed()) |
| 181 | + Eventually(func() bool { |
| 182 | + err := k8sClient.Get(ctx, namespacedName(obj.GetName(), obj.GetNamespace()), obj) |
| 183 | + return k8sErrors.IsNotFound(err) |
| 184 | + }, 10*time.Second, 250*time.Millisecond).Should(BeTrue(), "Deleting %s with name %s", obj.GetObjectKind(), obj.GetName()) |
| 185 | +} |
| 186 | + |
| 187 | +func namespacedName(name, namespace string) types.NamespacedName { |
| 188 | + return types.NamespacedName{ |
| 189 | + Name: name, |
| 190 | + Namespace: namespace, |
| 191 | + } |
| 192 | +} |
0 commit comments