|
| 1 | +// Copyright Contributors to the Open Cluster Management project |
| 2 | +package klusterlet |
| 3 | + |
| 4 | +import ( |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + operatorv1 "open-cluster-management.io/api/operator/v1" |
| 10 | + "open-cluster-management.io/ocm/pkg/operator/helpers/chart" |
| 11 | +) |
| 12 | + |
| 13 | +func TestMergeKlusterletFile(t *testing.T) { |
| 14 | + // Create a temporary klusterlet YAML file |
| 15 | + tmpDir := t.TempDir() |
| 16 | + klusterletFile := filepath.Join(tmpDir, "test-klusterlet.yaml") |
| 17 | + |
| 18 | + klusterletYAML := `apiVersion: operator.open-cluster-management.io/v1 |
| 19 | +kind: Klusterlet |
| 20 | +metadata: |
| 21 | + name: test-klusterlet |
| 22 | +spec: |
| 23 | + clusterName: test-cluster |
| 24 | + namespace: test-namespace |
| 25 | + deployOption: |
| 26 | + mode: Singleton |
| 27 | + registrationConfiguration: |
| 28 | + clientCertExpirationSeconds: 86400 |
| 29 | + featureGates: |
| 30 | + - feature: DefaultClusterSet |
| 31 | + mode: Enable |
| 32 | + workConfiguration: |
| 33 | + featureGates: |
| 34 | + - feature: ManifestWorkReplicaSet |
| 35 | + mode: Enable |
| 36 | + resourceRequirement: |
| 37 | + type: ResourceRequirement |
| 38 | + resourceRequirements: |
| 39 | + requests: |
| 40 | + cpu: 100m |
| 41 | + memory: 128Mi |
| 42 | + priorityClassName: system-cluster-critical` |
| 43 | + |
| 44 | + if err := os.WriteFile(klusterletFile, []byte(klusterletYAML), 0644); err != nil { |
| 45 | + t.Fatalf("Failed to write test klusterlet file: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + // Create a default chart config |
| 49 | + chartConfig := chart.NewDefaultKlusterletChartConfig() |
| 50 | + |
| 51 | + // Test the merge function |
| 52 | + err := MergeKlusterletFile(klusterletFile, chartConfig) |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("MergeKlusterletFile failed: %v", err) |
| 55 | + } |
| 56 | + |
| 57 | + // Verify the merge results |
| 58 | + if chartConfig.Klusterlet.ClusterName != "test-cluster" { |
| 59 | + t.Errorf("Expected ClusterName to be 'test-cluster', got '%s'", chartConfig.Klusterlet.ClusterName) |
| 60 | + } |
| 61 | + |
| 62 | + if chartConfig.Klusterlet.Namespace != "test-namespace" { |
| 63 | + t.Errorf("Expected Namespace to be 'test-namespace', got '%s'", chartConfig.Klusterlet.Namespace) |
| 64 | + } |
| 65 | + |
| 66 | + if chartConfig.Klusterlet.Mode != operatorv1.InstallModeSingleton { |
| 67 | + t.Errorf("Expected Mode to be 'Singleton', got '%s'", chartConfig.Klusterlet.Mode) |
| 68 | + } |
| 69 | + |
| 70 | + if chartConfig.Klusterlet.RegistrationConfiguration.ClientCertExpirationSeconds != 86400 { |
| 71 | + t.Errorf("Expected ClientCertExpirationSeconds to be 86400, got %d", chartConfig.Klusterlet.RegistrationConfiguration.ClientCertExpirationSeconds) |
| 72 | + } |
| 73 | + |
| 74 | + if len(chartConfig.Klusterlet.RegistrationConfiguration.FeatureGates) != 1 { |
| 75 | + t.Errorf("Expected 1 registration feature gate, got %d", len(chartConfig.Klusterlet.RegistrationConfiguration.FeatureGates)) |
| 76 | + } |
| 77 | + |
| 78 | + if len(chartConfig.Klusterlet.WorkConfiguration.FeatureGates) != 1 { |
| 79 | + t.Errorf("Expected 1 work feature gate, got %d", len(chartConfig.Klusterlet.WorkConfiguration.FeatureGates)) |
| 80 | + } |
| 81 | + |
| 82 | + if chartConfig.PriorityClassName != "system-cluster-critical" { |
| 83 | + t.Errorf("Expected PriorityClassName to be 'system-cluster-critical', got '%s'", chartConfig.PriorityClassName) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestMergeKlusterletFileNotFound(t *testing.T) { |
| 88 | + chartConfig := chart.NewDefaultKlusterletChartConfig() |
| 89 | + |
| 90 | + err := MergeKlusterletFile("/nonexistent/file.yaml", chartConfig) |
| 91 | + if err == nil { |
| 92 | + t.Error("Expected error for non-existent file, got nil") |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestMergeKlusterletFileInvalidYAML(t *testing.T) { |
| 97 | + tmpDir := t.TempDir() |
| 98 | + klusterletFile := filepath.Join(tmpDir, "invalid-klusterlet.yaml") |
| 99 | + |
| 100 | + invalidYAML := `invalid: yaml: content: [unclosed` |
| 101 | + if err := os.WriteFile(klusterletFile, []byte(invalidYAML), 0644); err != nil { |
| 102 | + t.Fatalf("Failed to write invalid klusterlet file: %v", err) |
| 103 | + } |
| 104 | + |
| 105 | + chartConfig := chart.NewDefaultKlusterletChartConfig() |
| 106 | + |
| 107 | + err := MergeKlusterletFile(klusterletFile, chartConfig) |
| 108 | + if err == nil { |
| 109 | + t.Error("Expected error for invalid YAML, got nil") |
| 110 | + } |
| 111 | +} |
0 commit comments