Repository: vllm-project/aibrix
File: pkg/controller/stormservice/sync_test.go (Go)

Code before:

			err := r.syncHeadlessService(context.TODO(), tt.stormService)

			if (err != nil) != tt.wantError {
				t.Errorf("syncHeadlessService() error = %v, wantError %v", err, tt.wantError)
				return
			}

			// Check if service was created/updated
			service := &corev1.Service{}
			err = fakeClient.Get(context.TODO(), client.ObjectKey{
				Name:      tt.stormService.Name,
				Namespace: tt.stormService.Namespace,
			}, service)

			if err != nil {
				t.Errorf("Failed to get service: %v", err)
				return
			}

			// Verify service properties
			if service.Spec.ClusterIP != corev1.ClusterIPNone {
				t.Errorf("Expected ClusterIP to be None, got %s", service.Spec.ClusterIP)
			}

			if tt.existingService == nil {
				if len(service.OwnerReferences) == 0 {
					t.Error("Expected service to have an owner reference")
				} else {
					ownerRef := service.OwnerReferences[0]
					if ownerRef.Kind != orchestrationv1alpha1.StormServiceKind || ownerRef.UID != service.UID {
						t.Errorf("Expected owner reference to be %s %s, got %s %s", orchestrationv1alpha1.StormServiceKind, service.UID, ownerRef.Kind, ownerRef.UID)
					}
				}
			}

			expectedSelector := map[string]string{constants.StormServiceNameLabelKey: tt.stormService.Name}
			if !reflect.DeepEqual(service.Spec.Selector, expectedSelector) {
				t.Errorf("Expected selector %v, got %v", expectedSelector, service.Spec.Selector)
			}

			if service.Spec.Type != corev1.ServiceTypeClusterIP {
				t.Errorf("Expected service type ClusterIP, got %v", service.Spec.Type)
			}

			if service.Spec.PublishNotReadyAddresses != true {
				t.Errorf("Expected PublishNotReadyAddresses to be true, got %v", service.Spec.PublishNotReadyAddresses)
			}
		})
	}
}

Review comment (suggestion):
This is a review error... It seems that `if tt.existingService != nil {`  should be correct here...

😅 

Code after:

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			var objs []client.Object
			if tt.existingService != nil {
				objs = append(objs, tt.existingService)
			}

			fakeClient := fake.NewClientBuilder().
				WithScheme(scheme).
				WithObjects(objs...).
				Build()

			r := &StormServiceReconciler{
				Client:        fakeClient,
				EventRecorder: &record.FakeRecorder{},
			}

			err := r.syncHeadlessService(context.TODO(), tt.stormService)

			if (err != nil) != tt.wantError {
				t.Errorf("syncHeadlessService() error = %v, wantError %v", err, tt.wantError)
				return
			}

			// Check if service was created/updated
			service := &corev1.Service{}
			err = fakeClient.Get(context.TODO(), client.ObjectKey{
				Name:      tt.stormService.Name,
				Namespace: tt.stormService.Namespace,
			}, service)

			if err != nil {
				t.Errorf("Failed to get service: %v", err)
				return
			}

			// Verify service properties
			if service.Spec.ClusterIP != corev1.ClusterIPNone {
				t.Errorf("Expected ClusterIP to be None, got %s", service.Spec.ClusterIP)
			}

			if len(service.OwnerReferences) == 0 {
				t.Error("Expected service to have an owner reference")
			} else {
				ownerRef := service.OwnerReferences[0]
				if ownerRef.Kind != orchestrationv1alpha1.StormServiceKind || ownerRef.UID != tt.stormService.UID {
					t.Errorf("Expected owner reference to be %s %s, got %s %s", orchestrationv1alpha1.StormServiceKind, tt.stormService.UID, ownerRef.Kind, ownerRef.UID)
				}
			}