| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package chartengine |
| 4 | |
| 5 | import ( |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/netdata/netdata/go/plugins/plugin/framework/chartengine/internal/program" |
| 9 | "github.com/stretchr/testify/assert" |
| 10 | ) |
| 11 | |
| 12 | func TestEnforceChartInstanceCapsSoftWhenAllExistingAreActive(t *testing.T) { |
| 13 | tests := map[string]struct{}{"all existing active instances are kept": {}} |
| 14 | |
| 15 | for name := range tests { |
| 16 | t.Run(name, func(t *testing.T) { |
| 17 | const currentSuccessSeq = 42 |
| 18 | const templateID = "g0.c0" |
| 19 | |
| 20 | lifecycle := program.LifecyclePolicy{ |
| 21 | MaxInstances: 1, |
| 22 | } |
| 23 | state := newMaterializedState() |
| 24 | state.charts["win_nic_traffic_eth0"] = &materializedChartState{ |
| 25 | templateID: templateID, |
| 26 | lifecycle: lifecycle, |
| 27 | lastSeenSuccessSeq: currentSuccessSeq, |
| 28 | dimensions: make(map[string]*materializedDimensionState), |
| 29 | } |
| 30 | state.charts["win_nic_traffic_eth1"] = &materializedChartState{ |
| 31 | templateID: templateID, |
| 32 | lifecycle: lifecycle, |
| 33 | lastSeenSuccessSeq: currentSuccessSeq, |
| 34 | dimensions: make(map[string]*materializedDimensionState), |
| 35 | } |
| 36 | |
| 37 | chartsByID := map[string]*chartState{ |
| 38 | "win_nic_traffic_eth0": { |
| 39 | templateID: templateID, |
| 40 | lifecycle: lifecycle, |
| 41 | entries: make(map[string]*dimBuildEntry), |
| 42 | }, |
| 43 | "win_nic_traffic_eth1": { |
| 44 | templateID: templateID, |
| 45 | lifecycle: lifecycle, |
| 46 | entries: make(map[string]*dimBuildEntry), |
| 47 | }, |
| 48 | } |
| 49 | |
| 50 | removeCharts := enforceChartInstanceCaps(currentSuccessSeq, chartsByID, &state) |
| 51 | assert.Empty(t, removeCharts) |
| 52 | assert.Len(t, chartsByID, 2) |
| 53 | assert.Len(t, state.charts, 2) |
| 54 | }) |
| 55 | } |
| 56 | } |