| 1472 | } |
| 1473 | |
| 1474 | func TestService(t *testing.T) { |
| 1475 | const ( |
| 1476 | testNamespace = "test-namespace" |
| 1477 | testService = "nvidia-dcgm-exporter" |
| 1478 | ) |
| 1479 | |
| 1480 | // Helper to create scheme with required types |
| 1481 | scheme := runtime.NewScheme() |
| 1482 | require.NoError(t, corev1.AddToScheme(scheme)) |
| 1483 | require.NoError(t, gpuv1.AddToScheme(scheme)) |
| 1484 | |
| 1485 | // Template Service |
| 1486 | service := corev1.Service{ |
| 1487 | ObjectMeta: metav1.ObjectMeta{Name: testService}, |
| 1488 | Spec: corev1.ServiceSpec{}, |
| 1489 | } |
| 1490 | |
| 1491 | // Helper to create controller with given spec |
| 1492 | newController := func(k8s client.Client, scheme *runtime.Scheme, spec gpuv1.ClusterPolicySpec) ClusterPolicyController { |
| 1493 | clusterPolicy := &gpuv1.ClusterPolicy{Spec: spec} |
| 1494 | resources := []Resources{{Service: service}} |
| 1495 | return ClusterPolicyController{ |
| 1496 | client: k8s, |
| 1497 | ctx: context.Background(), |
| 1498 | singleton: clusterPolicy, |
| 1499 | scheme: scheme, |
| 1500 | operatorNamespace: testNamespace, |
| 1501 | resources: resources, |
| 1502 | stateNames: []string{"state-dcgm-exporter"}, |
| 1503 | idx: 0, |
| 1504 | logger: ctrl.Log.WithName("test"), |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | localPolicy := corev1.ServiceInternalTrafficPolicyLocal |
| 1509 | |
| 1510 | tests := []struct { |
| 1511 | description string |
| 1512 | k8sObjects []client.Object |
| 1513 | clusterPolicySpec gpuv1.ClusterPolicySpec |
| 1514 | expectedState gpuv1.State |
| 1515 | expectService bool |
| 1516 | expectedType corev1.ServiceType |
| 1517 | expectedPolicy *corev1.ServiceInternalTrafficPolicy |
| 1518 | expectedIP string // For ClusterIP preservation test |
| 1519 | }{ |
| 1520 | { |
| 1521 | description: "create and preprocess", |
| 1522 | k8sObjects: nil, |
| 1523 | clusterPolicySpec: gpuv1.ClusterPolicySpec{ |
| 1524 | DCGMExporter: gpuv1.DCGMExporterSpec{ |
| 1525 | Enabled: ptr.To(true), |
| 1526 | ServiceSpec: &gpuv1.DCGMExporterServiceConfig{ |
| 1527 | Type: corev1.ServiceTypeNodePort, |
| 1528 | InternalTrafficPolicy: &localPolicy, |
| 1529 | }, |
| 1530 | }, |
| 1531 | }, |