| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package snmp |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/netdata/netdata/go/plugins/plugin/go.d/collector/snmp/ddsnmp" |
| 10 | |
| 11 | "github.com/stretchr/testify/assert" |
| 12 | ) |
| 13 | |
| 14 | func TestAggregateLicenseRows_FromTypedRows(t *testing.T) { |
| 15 | now := time.Date(2026, 4, 9, 12, 0, 0, 0, time.UTC) |
| 16 | earliest := now.Add(2 * time.Hour).Unix() |
| 17 | latest := now.Add(48 * time.Hour).Unix() |
| 18 | |
| 19 | tests := map[string]struct { |
| 20 | rows []ddsnmp.LicenseRow |
| 21 | assert func(t *testing.T, agg licenseAggregate) |
| 22 | }{ |
| 23 | "selects min expiry and max usage": { |
| 24 | rows: []ddsnmp.LicenseRow{ |
| 25 | typedLicenseRow("a", "First", withExpiry(latest), withUsage(30), withCapacity(100)), |
| 26 | typedLicenseRow("b", "Second", withExpiry(earliest), withUsage(95), withCapacity(100)), |
| 27 | }, |
| 28 | assert: func(t *testing.T, agg licenseAggregate) { |
| 29 | assert.True(t, agg.hasRemainingTime) |
| 30 | assert.Equal(t, earliest-now.Unix(), agg.remainingTime) |
| 31 | assert.True(t, agg.hasUsagePercent) |
| 32 | assert.EqualValues(t, 95, agg.usagePercent) |
| 33 | }, |
| 34 | }, |
| 35 | "perpetual rows skip expiry aggregation": { |
| 36 | rows: []ddsnmp.LicenseRow{ |
| 37 | typedLicenseRow("perp", "Perpetual", withExpiry(now.Add(time.Hour).Unix()), withPerpetual()), |
| 38 | typedLicenseRow("real", "Subscription", withExpiry(now.Add(24*time.Hour).Unix())), |
| 39 | }, |
| 40 | assert: func(t *testing.T, agg licenseAggregate) { |
| 41 | assert.True(t, agg.hasRemainingTime) |
| 42 | assert.Equal(t, int64((24 * time.Hour).Seconds()), agg.remainingTime) |
| 43 | }, |
| 44 | }, |
| 45 | "unlimited rows skip usage aggregation": { |
| 46 | rows: []ddsnmp.LicenseRow{ |
| 47 | typedLicenseRow("limited", "Limited", withUsagePercent(60)), |
| 48 | typedLicenseRow("infinite", "Infinite", withUsagePercent(100), withUnlimited()), |
| 49 | }, |
| 50 | assert: func(t *testing.T, agg licenseAggregate) { |
| 51 | assert.True(t, agg.hasUsagePercent) |
| 52 | assert.EqualValues(t, 60, agg.usagePercent) |
| 53 | }, |
| 54 | }, |
| 55 | "state bucket counts include informational": { |
| 56 | rows: []ddsnmp.LicenseRow{ |
| 57 | typedLicenseRow("a", "Healthy A", withState(0, "")), |
| 58 | typedLicenseRow("b", "Healthy B", withState(0, "")), |
| 59 | typedLicenseRow("c", "Informational", withState(1, "evaluation")), |
| 60 | typedLicenseRow("d", "Degraded", withState(1, "")), |
| 61 | typedLicenseRow("e", "Broken", withState(2, "")), |
| 62 | typedLicenseRow("f", "Ignored", withState(0, "none")), |
| 63 | }, |
| 64 | assert: func(t *testing.T, agg licenseAggregate) { |
| 65 | assert.True(t, agg.hasStateCounts) |
| 66 | assert.EqualValues(t, 2, agg.stateHealthy) |
| 67 | assert.EqualValues(t, 1, agg.stateInformational) |
| 68 | assert.EqualValues(t, 1, agg.stateDegraded) |
| 69 | assert.EqualValues(t, 1, agg.stateBroken) |
| 70 | assert.EqualValues(t, 1, agg.stateIgnored) |
| 71 | }, |
| 72 | }, |
| 73 | "ignored rows do not drive signal aggregation": { |
| 74 | rows: []ddsnmp.LicenseRow{ |
| 75 | typedLicenseRow("ignored", "Ignored", |
| 76 | withRawState("not applicable"), |
| 77 | withExpiry(now.Add(time.Hour).Unix()), |
| 78 | withUsagePercent(99), |
| 79 | ), |
| 80 | typedLicenseRow("healthy", "Healthy", |
| 81 | withExpiry(now.Add(24*time.Hour).Unix()), |
| 82 | withUsagePercent(60), |
| 83 | ), |
| 84 | }, |
| 85 | assert: func(t *testing.T, agg licenseAggregate) { |
| 86 | assert.True(t, agg.hasRemainingTime) |
| 87 | assert.Equal(t, int64((24 * time.Hour).Seconds()), agg.remainingTime) |
| 88 | assert.True(t, agg.hasUsagePercent) |
| 89 | assert.EqualValues(t, 60, agg.usagePercent) |
| 90 | assert.True(t, agg.hasStateCounts) |
| 91 | assert.EqualValues(t, 1, agg.stateHealthy) |
| 92 | assert.EqualValues(t, 1, agg.stateIgnored) |
| 93 | }, |
| 94 | }, |
| 95 | "writeTo omits absent timer and usage signals": { |
| 96 | rows: []ddsnmp.LicenseRow{ |
| 97 | typedLicenseRow("a", "A", withState(0, "")), |
| 98 | }, |
| 99 | assert: func(t *testing.T, agg licenseAggregate) { |
| 100 | mx := make(map[string]int64) |
| 101 | agg.writeTo(mx) |
| 102 | assert.NotContains(t, mx, metricIDLicenseRemainingTime) |
| 103 | assert.NotContains(t, mx, metricIDLicenseUsagePercent) |
| 104 | assert.Contains(t, mx, metricIDLicenseStateHealthy) |
| 105 | assert.Contains(t, mx, metricIDLicenseStateInformational) |
| 106 | }, |
| 107 | }, |
| 108 | } |
| 109 | |
| 110 | for name, tc := range tests { |
| 111 | t.Run(name, func(t *testing.T) { |
| 112 | rows := extractLicenseRows(profileWithRows(tc.rows...), now) |
| 113 | tc.assert(t, aggregateLicenseRows(rows, now)) |
| 114 | }) |
| 115 | } |
| 116 | } |