|
| 1 | +package clusterconfig |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 8 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 9 | + "k8s.io/apimachinery/pkg/runtime" |
| 10 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 11 | + "k8s.io/apimachinery/pkg/runtime/serializer/yaml" |
| 12 | + "k8s.io/client-go/dynamic" |
| 13 | + dynamicfake "k8s.io/client-go/dynamic/fake" |
| 14 | +) |
| 15 | + |
| 16 | +func createMockCostManagementMetricsConfig(t *testing.T, c dynamic.Interface, data string) { |
| 17 | + decUnstructured1 := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme) |
| 18 | + testCostManagementMetricsConfig := &unstructured.Unstructured{} |
| 19 | + _, _, err := decUnstructured1.Decode([]byte(data), nil, testCostManagementMetricsConfig) |
| 20 | + if err != nil { |
| 21 | + t.Fatal("unable to decode CostManagementMetricsConfig YAML", err) |
| 22 | + } |
| 23 | + |
| 24 | + _, _ = c. |
| 25 | + Resource(costManagementMetricsConfigResource). |
| 26 | + Create(context.Background(), testCostManagementMetricsConfig, metav1.CreateOptions{}) |
| 27 | +} |
| 28 | + |
| 29 | +func Test_CostManagementMetricsConfigs(t *testing.T) { |
| 30 | + // Initialize the fake dynamic client. |
| 31 | + costMgmtMetricsConfigClient := dynamicfake.NewSimpleDynamicClientWithCustomListKinds( |
| 32 | + runtime.NewScheme(), map[schema.GroupVersionResource]string{ |
| 33 | + costManagementMetricsConfigResource: "CostManagementMetricsConfigList", |
| 34 | + }) |
| 35 | + |
| 36 | + records, errs := gatherCostManagementMetricsConfigs(context.Background(), costMgmtMetricsConfigClient) |
| 37 | + if len(errs) > 0 { |
| 38 | + t.Fatalf("unexpected errors: %#v", errs) |
| 39 | + } |
| 40 | + // 0 records because there is no CostManagementMetricsConfigs yet. |
| 41 | + if len(records) != 0 { |
| 42 | + t.Fatalf("unexpected number or records in the first run: %d", len(records)) |
| 43 | + } |
| 44 | + |
| 45 | + // Create first CostManagementMetricsConfig resource. |
| 46 | + costMgmtMetricsConfigYAML1 := `apiVersion: costmanagement-metrics-cfg.openshift.io/v1beta1 |
| 47 | +kind: CostManagementMetricsConfig |
| 48 | +metadata: |
| 49 | + name: costmanagementmetricscfg-sample-1 |
| 50 | +` |
| 51 | + |
| 52 | + createMockCostManagementMetricsConfig(t, costMgmtMetricsConfigClient, costMgmtMetricsConfigYAML1) |
| 53 | + records, errs = gatherCostManagementMetricsConfigs(context.Background(), costMgmtMetricsConfigClient) |
| 54 | + if len(errs) > 0 { |
| 55 | + t.Fatalf("unexpected errors: %#v", errs) |
| 56 | + } |
| 57 | + // 1 record because there is now 1 CostManagementMetricsConfig resource. |
| 58 | + if len(records) != 1 { |
| 59 | + t.Fatalf("unexpected number or records in the second run: %d", len(records)) |
| 60 | + } |
| 61 | + |
| 62 | + // Create second CostManagementMetricsConfig resource. |
| 63 | + costMgmtMetricsConfigYAML2 := `apiVersion: costmanagement-metrics-cfg.openshift.io/v1beta1 |
| 64 | +kind: CostManagementMetricsConfig |
| 65 | +metadata: |
| 66 | + name: costmanagementmetricscfg-sample-2 |
| 67 | +` |
| 68 | + |
| 69 | + createMockCostManagementMetricsConfig(t, costMgmtMetricsConfigClient, costMgmtMetricsConfigYAML2) |
| 70 | + records, errs = gatherCostManagementMetricsConfigs(context.Background(), costMgmtMetricsConfigClient) |
| 71 | + if len(errs) > 0 { |
| 72 | + t.Fatalf("unexpected errors: %#v", errs) |
| 73 | + } |
| 74 | + // 2 record because there are now 2 CostManagementMetricsConfig resource. |
| 75 | + if len(records) != 2 { |
| 76 | + t.Fatalf("unexpected number or records in the third run: %d", len(records)) |
| 77 | + } |
| 78 | + |
| 79 | + // Create third CostManagementMetricsConfig resource. |
| 80 | + costMgmtMetricsConfigYAML3 := `apiVersion: costmanagement-metrics-cfg.openshift.io/v1beta1 |
| 81 | +kind: CostManagementMetricsConfig |
| 82 | +metadata: |
| 83 | + name: costmanagementmetricscfg-sample-3 |
| 84 | + spec: |
| 85 | + authentication: |
| 86 | + type: basic |
| 87 | + secret_name: console_basic_auth |
| 88 | +` |
| 89 | + |
| 90 | + createMockCostManagementMetricsConfig(t, costMgmtMetricsConfigClient, costMgmtMetricsConfigYAML3) |
| 91 | + records, errs = gatherCostManagementMetricsConfigs(context.Background(), costMgmtMetricsConfigClient) |
| 92 | + if len(errs) > 0 { |
| 93 | + t.Fatalf("unexpected errors: %#v", errs) |
| 94 | + } |
| 95 | + // 3 record because there are now 3 CostManagementMetricsConfig resource. |
| 96 | + if len(records) != 3 { |
| 97 | + t.Fatalf("unexpected number or records in the fourth run: %d", len(records)) |
| 98 | + } |
| 99 | + |
| 100 | + // Create fourth CostManagementMetricsConfig resource. |
| 101 | + costMgmtMetricsConfigYAML4 := `apiVersion: costmanagement-metrics-cfg.openshift.io/v1beta1 |
| 102 | +kind: CostManagementMetricsConfig |
| 103 | +metadata: |
| 104 | + name: costmanagementmetricscfg-sample-4 |
| 105 | + spec: |
| 106 | + authentication: |
| 107 | + type: token |
| 108 | +` |
| 109 | + |
| 110 | + createMockCostManagementMetricsConfig(t, costMgmtMetricsConfigClient, costMgmtMetricsConfigYAML4) |
| 111 | + records, errs = gatherCostManagementMetricsConfigs(context.Background(), costMgmtMetricsConfigClient) |
| 112 | + if len(errs) > 0 { |
| 113 | + t.Fatalf("unexpected errors: %#v", errs) |
| 114 | + } |
| 115 | + // 4 record because there are now 4 CostManagementMetricsConfig resource. |
| 116 | + if len(records) != 4 { |
| 117 | + t.Fatalf("unexpected number or records in the fifth run: %d", len(records)) |
| 118 | + } |
| 119 | +} |
0 commit comments