| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package vsphere |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/stretchr/testify/require" |
| 10 | |
| 11 | "github.com/netdata/netdata/go/plugins/plugin/go.d/collector/vsphere/match" |
| 12 | rs "github.com/netdata/netdata/go/plugins/plugin/go.d/collector/vsphere/resources" |
| 13 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/collecttest" |
| 14 | ) |
| 15 | |
| 16 | func TestCollector_AddsUserMetadataLabels(t *testing.T) { |
| 17 | collr, _, teardown := prepareVSphereSim(t) |
| 18 | defer teardown() |
| 19 | |
| 20 | require.NoError(t, collr.Init(context.Background())) |
| 21 | collr.scraper = mockScraper{collr.scraper} |
| 22 | for _, vm := range collr.resources.VMs { |
| 23 | vm.Labels = map[string]string{ |
| 24 | "vsphere_custom_attribute_owner": "platform", |
| 25 | "vsphere_tag_service": "payments", |
| 26 | } |
| 27 | } |
| 28 | for _, host := range collr.resources.Hosts { |
| 29 | host.Labels = map[string]string{"vsphere_tag_env": "prod"} |
| 30 | } |
| 31 | |
| 32 | require.NotEmpty(t, collectScalarSeriesForTest(t, collr)) |
| 33 | |
| 34 | vm := firstSortedVM(t, collr) |
| 35 | host := firstSortedHost(t, collr) |
| 36 | createdCharts, _ := v2CreatedChartsAndDims(buildV2PlanForTest(t, collr)) |
| 37 | |
| 38 | vmChartID := findChartIDByLabelsAndContext(t, createdCharts, "vsphere.vm_cpu_utilization", map[string]string{"id": vm.ID}) |
| 39 | require.Equal(t, "platform", createdCharts[vmChartID].Labels["vsphere_custom_attribute_owner"]) |
| 40 | require.Equal(t, "payments", createdCharts[vmChartID].Labels["vsphere_tag_service"]) |
| 41 | |
| 42 | hostChartID := findChartIDByLabelsAndContext(t, createdCharts, "vsphere.host_cpu_utilization", map[string]string{"id": host.ID}) |
| 43 | require.Equal(t, "prod", createdCharts[hostChartID].Labels["vsphere_tag_env"]) |
| 44 | collecttest.AssertChartCoverage(t, collr, collecttest.ChartCoverageExpectation{}) |
| 45 | } |
| 46 | |
| 47 | func TestClusterNameLabels(t *testing.T) { |
| 48 | tests := map[string]struct { |
| 49 | host *rs.Host |
| 50 | vm *rs.VM |
| 51 | want string |
| 52 | }{ |
| 53 | "standalone host dummy cluster": { |
| 54 | host: &rs.Host{ |
| 55 | Name: "Host1", |
| 56 | Hier: rs.HostHierarchy{Cluster: rs.HierarchyValue{ |
| 57 | ID: "domain-s1", |
| 58 | Name: "Host1", |
| 59 | }}, |
| 60 | }, |
| 61 | vm: &rs.VM{ |
| 62 | Hier: rs.VMHierarchy{ |
| 63 | Cluster: rs.HierarchyValue{ID: "domain-s1", Name: "Host1"}, |
| 64 | Host: rs.HierarchyValue{Name: "Host1"}, |
| 65 | }, |
| 66 | }, |
| 67 | want: "", |
| 68 | }, |
| 69 | "real cluster with same name as host": { |
| 70 | host: &rs.Host{ |
| 71 | Name: "Host1", |
| 72 | Hier: rs.HostHierarchy{Cluster: rs.HierarchyValue{ |
| 73 | ID: "domain-c1", |
| 74 | Name: "Host1", |
| 75 | }}, |
| 76 | }, |
| 77 | vm: &rs.VM{ |
| 78 | Hier: rs.VMHierarchy{ |
| 79 | Cluster: rs.HierarchyValue{ID: "domain-c1", Name: "Host1"}, |
| 80 | Host: rs.HierarchyValue{Name: "Host1"}, |
| 81 | }, |
| 82 | }, |
| 83 | want: "Host1", |
| 84 | }, |
| 85 | } |
| 86 | |
| 87 | for name, tc := range tests { |
| 88 | t.Run(name, func(t *testing.T) { |
| 89 | require.Equal(t, tc.want, getHostClusterName(tc.host)) |
| 90 | require.Equal(t, tc.want, getVMClusterName(tc.vm)) |
| 91 | }) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func TestCollector_Init_ReturnsFalseIfInvalidUserMetadataLabelConfig(t *testing.T) { |
| 96 | tests := map[string]struct { |
| 97 | setup func(*Collector) |
| 98 | want string |
| 99 | }{ |
| 100 | "invalid tag category pattern": { |
| 101 | setup: func(c *Collector) { c.TagCategories = []string{"["} }, |
| 102 | want: "tag_categories has invalid pattern", |
| 103 | }, |
| 104 | "empty negative tag category pattern": { |
| 105 | setup: func(c *Collector) { c.TagCategories = []string{"!"} }, |
| 106 | want: "tag_categories has invalid empty negative pattern", |
| 107 | }, |
| 108 | "all-negative custom attribute pattern list": { |
| 109 | setup: func(c *Collector) { c.CustomAttributes = []string{"!Secret"} }, |
| 110 | want: "custom_attributes must include at least one positive pattern", |
| 111 | }, |
| 112 | } |
| 113 | |
| 114 | for name, tc := range tests { |
| 115 | t.Run(name, func(t *testing.T) { |
| 116 | collr := New() |
| 117 | collr.URL = "https://vcenter.local" |
| 118 | collr.Username = "user" |
| 119 | collr.Password = "pass" |
| 120 | tc.setup(collr) |
| 121 | |
| 122 | require.ErrorContains(t, collr.Init(context.Background()), tc.want) |
| 123 | }) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | func TestPatternListMatcherPreservesUserMetadataListItems(t *testing.T) { |
| 128 | m, err := match.NewPatternListMatcher("custom_attributes", []string{"!Business Secret", "Cost Center", "Business*"}) |
| 129 | require.NoError(t, err) |
| 130 | |
| 131 | require.True(t, m.MatchString("Cost Center")) |
| 132 | require.True(t, m.MatchString("Business Unit")) |
| 133 | require.False(t, m.MatchString("Cost")) |
| 134 | require.False(t, m.MatchString("Business Secret")) |
| 135 | } |