|
| 1 | +package solvers |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1" |
| 7 | + "github.com/devfile/devworkspace-operator/pkg/constants" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + corev1 "k8s.io/api/core/v1" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/runtime" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 14 | +) |
| 15 | + |
| 16 | +func TestGetDiscoverableServicesForEndpoints(t *testing.T) { |
| 17 | + testLog := zap.New(zap.UseDevMode(true)) |
| 18 | + scheme := runtime.NewScheme() |
| 19 | + _ = corev1.AddToScheme(scheme) |
| 20 | + _ = controllerv1alpha1.AddToScheme(scheme) |
| 21 | + |
| 22 | + discoverableEndpoint := controllerv1alpha1.Endpoint{ |
| 23 | + Name: "test-endpoint", |
| 24 | + TargetPort: 8080, |
| 25 | + Exposure: controllerv1alpha1.PublicEndpointExposure, |
| 26 | + Attributes: controllerv1alpha1.Attributes{}. |
| 27 | + PutBoolean(string(controllerv1alpha1.DiscoverableAttribute), true), |
| 28 | + } |
| 29 | + endpoints := map[string]controllerv1alpha1.EndpointList{ |
| 30 | + "machine1": {discoverableEndpoint}, |
| 31 | + } |
| 32 | + |
| 33 | + meta := DevWorkspaceMetadata{ |
| 34 | + DevWorkspaceId: "current-workspace", |
| 35 | + Namespace: "test-namespace", |
| 36 | + } |
| 37 | + |
| 38 | + tests := []struct { |
| 39 | + name string |
| 40 | + existing []runtime.Object |
| 41 | + expectErr bool |
| 42 | + expectErrType error |
| 43 | + expectMsg string |
| 44 | + }{ |
| 45 | + { |
| 46 | + name: "No existing service", |
| 47 | + existing: []runtime.Object{}, |
| 48 | + expectErr: false, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "Existing service with different owner in same namespace", |
| 52 | + existing: []runtime.Object{ |
| 53 | + &corev1.Service{ |
| 54 | + ObjectMeta: metav1.ObjectMeta{ |
| 55 | + Name: "test-endpoint", |
| 56 | + Namespace: "test-namespace", |
| 57 | + Labels: map[string]string{ |
| 58 | + constants.DevWorkspaceIDLabel: "other-workspace", |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + expectErr: true, |
| 64 | + expectErrType: &ServiceConflictError{}, |
| 65 | + expectMsg: "conflicts with existing service", |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "Existing service with same owner (reconciliation)", |
| 69 | + existing: []runtime.Object{ |
| 70 | + &corev1.Service{ |
| 71 | + ObjectMeta: metav1.ObjectMeta{ |
| 72 | + Name: "test-endpoint", |
| 73 | + Namespace: "test-namespace", |
| 74 | + Labels: map[string]string{ |
| 75 | + constants.DevWorkspaceIDLabel: "current-workspace", |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + expectErr: false, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "Service with same name in different namespace (should not conflict)", |
| 84 | + existing: []runtime.Object{ |
| 85 | + &corev1.Service{ |
| 86 | + ObjectMeta: metav1.ObjectMeta{ |
| 87 | + Name: "test-endpoint", |
| 88 | + Namespace: "other-namespace", |
| 89 | + Labels: map[string]string{ |
| 90 | + constants.DevWorkspaceIDLabel: "other-workspace", |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + expectErr: false, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "Service without workspace ID label", |
| 99 | + existing: []runtime.Object{ |
| 100 | + &corev1.Service{ |
| 101 | + ObjectMeta: metav1.ObjectMeta{ |
| 102 | + Name: "test-endpoint", |
| 103 | + Namespace: "test-namespace", |
| 104 | + Labels: map[string]string{}, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + expectErr: true, |
| 109 | + expectErrType: &ServiceConflictError{}, |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + for _, tt := range tests { |
| 114 | + t.Run(tt.name, func(t *testing.T) { |
| 115 | + fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(tt.existing...).Build() |
| 116 | + _, err := GetDiscoverableServicesForEndpoints(endpoints, meta, fakeClient, testLog) |
| 117 | + |
| 118 | + if tt.expectErr { |
| 119 | + assert.Error(t, err, "Expected an error but got none") |
| 120 | + if tt.expectErrType != nil { |
| 121 | + assert.IsType(t, tt.expectErrType, err, "Error is of unexpected type") |
| 122 | + } |
| 123 | + if tt.expectMsg != "" { |
| 124 | + assert.Contains(t, err.Error(), tt.expectMsg) |
| 125 | + } |
| 126 | + } else { |
| 127 | + assert.NoError(t, err, "Got unexpected error") |
| 128 | + } |
| 129 | + }) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestGetDiscoverableServicesForEndpoints_MultipleEndpoints(t *testing.T) { |
| 134 | + testLog := zap.New(zap.UseDevMode(true)) |
| 135 | + scheme := runtime.NewScheme() |
| 136 | + _ = corev1.AddToScheme(scheme) |
| 137 | + _ = controllerv1alpha1.AddToScheme(scheme) |
| 138 | + |
| 139 | + endpoints := map[string]controllerv1alpha1.EndpointList{ |
| 140 | + "machine1": { |
| 141 | + { |
| 142 | + Name: "postgresql", |
| 143 | + TargetPort: 5432, |
| 144 | + Exposure: controllerv1alpha1.InternalEndpointExposure, |
| 145 | + Attributes: controllerv1alpha1.Attributes{}. |
| 146 | + PutBoolean(string(controllerv1alpha1.DiscoverableAttribute), true), |
| 147 | + }, |
| 148 | + { |
| 149 | + Name: "redis", |
| 150 | + TargetPort: 6379, |
| 151 | + Exposure: controllerv1alpha1.InternalEndpointExposure, |
| 152 | + Attributes: controllerv1alpha1.Attributes{}. |
| 153 | + PutBoolean(string(controllerv1alpha1.DiscoverableAttribute), true), |
| 154 | + }, |
| 155 | + { |
| 156 | + Name: "http", |
| 157 | + TargetPort: 8080, |
| 158 | + Exposure: controllerv1alpha1.PublicEndpointExposure, |
| 159 | + // Not discoverable |
| 160 | + }, |
| 161 | + }, |
| 162 | + } |
| 163 | + |
| 164 | + meta := DevWorkspaceMetadata{ |
| 165 | + DevWorkspaceId: "current-workspace", |
| 166 | + Namespace: "test-namespace", |
| 167 | + PodSelector: map[string]string{ |
| 168 | + constants.DevWorkspaceIDLabel: "current-workspace", |
| 169 | + }, |
| 170 | + } |
| 171 | + |
| 172 | + t.Run("Multiple discoverable endpoints without conflicts", func(t *testing.T) { |
| 173 | + fakeClient := fake.NewClientBuilder().WithScheme(scheme).Build() |
| 174 | + services, err := GetDiscoverableServicesForEndpoints(endpoints, meta, fakeClient, testLog) |
| 175 | + assert.NoError(t, err) |
| 176 | + assert.Len(t, services, 2, "Should create 2 discoverable services (postgresql and redis, not http)") |
| 177 | + |
| 178 | + serviceNames := make(map[string]bool) |
| 179 | + for _, svc := range services { |
| 180 | + serviceNames[svc.Name] = true |
| 181 | + } |
| 182 | + assert.True(t, serviceNames["postgresql"], "Should have postgresql service") |
| 183 | + assert.True(t, serviceNames["redis"], "Should have redis service") |
| 184 | + assert.False(t, serviceNames["http"], "Should not have http service (not discoverable)") |
| 185 | + }) |
| 186 | + |
| 187 | + t.Run("Conflict on one of multiple endpoints", func(t *testing.T) { |
| 188 | + existingService := &corev1.Service{ |
| 189 | + ObjectMeta: metav1.ObjectMeta{ |
| 190 | + Name: "postgresql", |
| 191 | + Namespace: "test-namespace", |
| 192 | + Labels: map[string]string{ |
| 193 | + constants.DevWorkspaceIDLabel: "other-workspace", |
| 194 | + }, |
| 195 | + }, |
| 196 | + } |
| 197 | + fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(existingService).Build() |
| 198 | + _, err := GetDiscoverableServicesForEndpoints(endpoints, meta, fakeClient, testLog) |
| 199 | + assert.Error(t, err, "Should error when one endpoint conflicts") |
| 200 | + assert.IsType(t, &ServiceConflictError{}, err) |
| 201 | + assert.Contains(t, err.Error(), "postgresql") |
| 202 | + }) |
| 203 | +} |
0 commit comments