| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package snmp |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/stretchr/testify/assert" |
| 9 | "github.com/stretchr/testify/require" |
| 10 | |
| 11 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 12 | "github.com/netdata/netdata/go/plugins/plugin/go.d/collector/snmp/ddsnmp" |
| 13 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/snmputils" |
| 14 | ) |
| 15 | |
| 16 | func TestCollector_AddProfileScalarMetricChart_LabelsIncludeMetricTags(t *testing.T) { |
| 17 | collr := New() |
| 18 | collr.Hostname = "192.0.2.1" |
| 19 | collr.sysInfo = &snmputils.SysInfo{ |
| 20 | Name: "test-device", |
| 21 | Vendor: "test-vendor", |
| 22 | } |
| 23 | |
| 24 | collr.addProfileScalarMetricChart(ddsnmp.Metric{ |
| 25 | Name: "license.status", |
| 26 | Value: 1, |
| 27 | Tags: map[string]string{ |
| 28 | "component": "vpn", |
| 29 | "_component": "private-vpn", |
| 30 | "_license_state_raw": "active", |
| 31 | }, |
| 32 | Profile: &ddsnmp.ProfileMetrics{ |
| 33 | Tags: map[string]string{"profile_tag": "profile_value"}, |
| 34 | }, |
| 35 | }) |
| 36 | |
| 37 | chart := collr.Charts().Get("snmp_device_prof_license_status") |
| 38 | require.NotNil(t, chart) |
| 39 | |
| 40 | assert.Equal(t, map[string]string{ |
| 41 | "address": "192.0.2.1", |
| 42 | "component": "vpn", |
| 43 | "license_state_raw": "active", |
| 44 | "profile_tag": "profile_value", |
| 45 | "sysName": "test-device", |
| 46 | "vendor": "test-vendor", |
| 47 | }, chartLabels(chart)) |
| 48 | } |
| 49 | |
| 50 | func TestAddMetricTagLabels_PrefersUnprefixedTags(t *testing.T) { |
| 51 | labels := map[string]string{ |
| 52 | "empty_profile_label": "", |
| 53 | "profile_tag": "profile-value", |
| 54 | "vendor": "device-vendor", |
| 55 | } |
| 56 | |
| 57 | addMetricTagLabels(labels, map[string]string{ |
| 58 | "component": "vpn", |
| 59 | "empty_metric_label": "", |
| 60 | "profile_tag": "metric-profile-value", |
| 61 | "vendor": "", |
| 62 | "_component": "private-vpn", |
| 63 | "_empty_metric_label": "metric-fallback", |
| 64 | "_empty_profile_label": "profile-fallback", |
| 65 | "_license_state_raw": "active", |
| 66 | "_vendor": "private-vendor", |
| 67 | "_": "ignored", |
| 68 | }) |
| 69 | |
| 70 | assert.Equal(t, map[string]string{ |
| 71 | "component": "vpn", |
| 72 | "empty_metric_label": "metric-fallback", |
| 73 | "empty_profile_label": "profile-fallback", |
| 74 | "license_state_raw": "active", |
| 75 | "profile_tag": "profile-value", |
| 76 | "vendor": "device-vendor", |
| 77 | }, labels) |
| 78 | } |
| 79 | |
| 80 | func TestLicenseChartsSkipGaps(t *testing.T) { |
| 81 | tests := map[string]struct { |
| 82 | skip bool |
| 83 | }{ |
| 84 | licenseRemainingTimeChart.ID: {skip: licenseRemainingTimeChart.SkipGaps}, |
| 85 | licenseAuthorizationRemainingTimeChart.ID: {skip: licenseAuthorizationRemainingTimeChart.SkipGaps}, |
| 86 | licenseCertificateRemainingTimeChart.ID: {skip: licenseCertificateRemainingTimeChart.SkipGaps}, |
| 87 | licenseGraceRemainingTimeChart.ID: {skip: licenseGraceRemainingTimeChart.SkipGaps}, |
| 88 | licenseUsagePercentChart.ID: {skip: licenseUsagePercentChart.SkipGaps}, |
| 89 | licenseStateChart.ID: {skip: licenseStateChart.SkipGaps}, |
| 90 | } |
| 91 | |
| 92 | for name, tc := range tests { |
| 93 | t.Run(name, func(t *testing.T) { |
| 94 | assert.True(t, tc.skip, "chart %s must skip gaps to avoid empty licensing charts", name) |
| 95 | }) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func TestCollector_AddLicenseCharts_LazyBySignalClass(t *testing.T) { |
| 100 | tests := map[string]struct { |
| 101 | agg licenseAggregate |
| 102 | present []string |
| 103 | absent []string |
| 104 | }{ |
| 105 | "state only": { |
| 106 | agg: licenseAggregate{hasStateCounts: true}, |
| 107 | present: []string{ |
| 108 | licenseStateChart.ID, |
| 109 | }, |
| 110 | absent: []string{ |
| 111 | licenseRemainingTimeChart.ID, |
| 112 | licenseAuthorizationRemainingTimeChart.ID, |
| 113 | licenseCertificateRemainingTimeChart.ID, |
| 114 | licenseGraceRemainingTimeChart.ID, |
| 115 | licenseUsagePercentChart.ID, |
| 116 | }, |
| 117 | }, |
| 118 | "expiry and usage only": { |
| 119 | agg: licenseAggregate{hasRemainingTime: true, hasUsagePercent: true}, |
| 120 | present: []string{ |
| 121 | licenseRemainingTimeChart.ID, |
| 122 | licenseUsagePercentChart.ID, |
| 123 | }, |
| 124 | absent: []string{ |
| 125 | licenseAuthorizationRemainingTimeChart.ID, |
| 126 | licenseCertificateRemainingTimeChart.ID, |
| 127 | licenseGraceRemainingTimeChart.ID, |
| 128 | licenseStateChart.ID, |
| 129 | }, |
| 130 | }, |
| 131 | } |
| 132 | |
| 133 | for name, tc := range tests { |
| 134 | t.Run(name, func(t *testing.T) { |
| 135 | collr := New() |
| 136 | collr.sysInfo = &snmputils.SysInfo{} |
| 137 | collr.addLicenseCharts(tc.agg) |
| 138 | collr.addLicenseCharts(tc.agg) |
| 139 | |
| 140 | for _, id := range tc.present { |
| 141 | chart := collr.Charts().Get(id) |
| 142 | require.NotNil(t, chart, "expected chart %s", id) |
| 143 | assert.Equal(t, "licensing", chartLabels(chart)["component"]) |
| 144 | } |
| 145 | for _, id := range tc.absent { |
| 146 | assert.Nil(t, collr.Charts().Get(id), "unexpected chart %s", id) |
| 147 | } |
| 148 | }) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | func chartLabels(chart *collectorapi.Chart) map[string]string { |
| 153 | labels := make(map[string]string, len(chart.Labels)) |
| 154 | for _, label := range chart.Labels { |
| 155 | labels[label.Key] = label.Value |
| 156 | } |
| 157 | return labels |
| 158 | } |