-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathmetadata_test.go
More file actions
134 lines (111 loc) · 3.5 KB
/
metadata_test.go
File metadata and controls
134 lines (111 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package vcenterreceiver
import (
"os"
"path/filepath"
"strings"
"testing"
"gopkg.in/yaml.v3"
)
// TestVSANMetricsFromMetadata ensures hasEnabledVSANMetrics() covers all vSAN metrics
// defined in metadata.yaml. This test will fail if a new vSAN metric is added to
// metadata.yaml but not included in hasEnabledVSANMetrics().
func TestVSANMetricsFromMetadata(t *testing.T) {
vSANMetricsFromYAML := getVSANMetricsFromMetadataYAML(t)
checkedMetrics := getCheckedVSANMetrics()
// Convert to sets for comparison
yamlSet := make(map[string]bool)
for _, metric := range vSANMetricsFromYAML {
yamlSet[metric] = true
}
checkedSet := make(map[string]bool)
for _, metric := range checkedMetrics {
checkedSet[metric] = true
}
var missingInFunction []string
for metric := range yamlSet {
if !checkedSet[metric] {
missingInFunction = append(missingInFunction, metric)
}
}
var extraInFunction []string
for metric := range checkedSet {
if !yamlSet[metric] {
extraInFunction = append(extraInFunction, metric)
}
}
if len(missingInFunction) > 0 {
t.Errorf("hasEnabledVSANMetrics() is missing these vSAN metrics from metadata.yaml:")
for _, metric := range missingInFunction {
t.Errorf(" - %s", metric)
}
t.Error("Please add the missing metrics to hasEnabledVSANMetrics() function")
}
if len(extraInFunction) > 0 {
t.Errorf("hasEnabledVSANMetrics() checks these metrics not found in metadata.yaml:")
for _, metric := range extraInFunction {
t.Errorf(" - %s", metric)
}
t.Error("Please remove non-vSAN metrics from hasEnabledVSANMetrics() function")
}
if len(missingInFunction) == 0 && len(extraInFunction) == 0 {
t.Logf("All %d vSAN metrics from metadata.yaml are properly checked", len(vSANMetricsFromYAML))
}
}
type metadataYAML struct {
Metrics map[string]any `yaml:"metrics"`
}
func getVSANMetricsFromMetadataYAML(t *testing.T) []string {
metadataPath := findMetadataYAML(t)
data, err := os.ReadFile(metadataPath)
if err != nil {
t.Fatalf("Failed to read metadata.yaml: %v", err)
}
var metadata metadataYAML
if err := yaml.Unmarshal(data, &metadata); err != nil {
t.Fatalf("Failed to parse metadata.yaml: %v", err)
}
// Extract vSAN metrics
var vSANMetrics []string
for metricName := range metadata.Metrics {
if strings.Contains(metricName, "vsan") {
vSANMetrics = append(vSANMetrics, metricName)
}
}
return vSANMetrics
}
func findMetadataYAML(t *testing.T) string {
dir, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get current directory: %v", err)
}
// Look for metadata.yaml in current directory
metadataPath := filepath.Join(dir, "metadata.yaml")
if _, err := os.Stat(metadataPath); err == nil {
return metadataPath
}
t.Fatalf("Could not find metadata.yaml")
return ""
}
// getCheckedVSANMetrics returns the metrics checked in hasEnabledVSANMetrics()
func getCheckedVSANMetrics() []string {
// This should match exactly what's in hasEnabledVSANMetrics()
return []string{
// Cluster vSAN metrics
"vcenter.cluster.vsan.congestions",
"vcenter.cluster.vsan.latency.avg",
"vcenter.cluster.vsan.operations",
"vcenter.cluster.vsan.throughput",
// Host vSAN metrics
"vcenter.host.vsan.cache.hit_rate",
"vcenter.host.vsan.congestions",
"vcenter.host.vsan.latency.avg",
"vcenter.host.vsan.operations",
"vcenter.host.vsan.throughput",
// VM vSAN metrics
"vcenter.vm.vsan.latency.avg",
"vcenter.vm.vsan.operations",
"vcenter.vm.vsan.throughput",
}
}