| 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 | "github.com/stretchr/testify/require" |
| 11 | ) |
| 12 | |
| 13 | func TestChartLabelAccumulatorIntersectsLabels(t *testing.T) { |
| 14 | tests := map[string]struct { |
| 15 | observed []map[string]string |
| 16 | want map[string]string |
| 17 | }{ |
| 18 | "auto intersection keeps only common labels and excludes dimension key": { |
| 19 | observed: []map[string]string{ |
| 20 | { |
| 21 | collectJobLabel: "mysql-local", |
| 22 | "env": "prod", |
| 23 | "instance": "db1", |
| 24 | "mode": "read", |
| 25 | "region": "us", |
| 26 | "selector_fixed": "x", |
| 27 | }, |
| 28 | { |
| 29 | collectJobLabel: "mysql-local", |
| 30 | "env": "prod", |
| 31 | "instance": "db1", |
| 32 | "mode": "write", |
| 33 | "region": "us", |
| 34 | "selector_fixed": "x", |
| 35 | }, |
| 36 | { |
| 37 | collectJobLabel: "mysql-local", |
| 38 | "env": "prod", |
| 39 | "instance": "db1", |
| 40 | "mode": "read", |
| 41 | "region": "eu", |
| 42 | "selector_fixed": "x", |
| 43 | }, |
| 44 | }, |
| 45 | want: map[string]string{ |
| 46 | "env": "prod", |
| 47 | "instance": "db1", |
| 48 | }, |
| 49 | }, |
| 50 | } |
| 51 | |
| 52 | for name, tc := range tests { |
| 53 | t.Run(name, func(t *testing.T) { |
| 54 | chart := program.Chart{ |
| 55 | Identity: program.ChartIdentity{ |
| 56 | InstanceByLabels: []program.InstanceLabelSelector{ |
| 57 | {Key: "instance"}, |
| 58 | }, |
| 59 | }, |
| 60 | Labels: program.LabelPolicy{ |
| 61 | Mode: program.PromotionModeAutoIntersection, |
| 62 | Exclusions: program.LabelExclusions{ |
| 63 | SelectorConstrainedKeys: []string{"selector_fixed"}, |
| 64 | }, |
| 65 | }, |
| 66 | } |
| 67 | |
| 68 | acc := newChartLabelAccumulator(chart) |
| 69 | require.NotNil(t, acc) |
| 70 | |
| 71 | for _, labels := range tc.observed { |
| 72 | err := acc.observe(sortedLabelView(labels), "mode") |
| 73 | require.NoError(t, err) |
| 74 | } |
| 75 | |
| 76 | got, err := acc.materialize() |
| 77 | require.NoError(t, err) |
| 78 | assert.Equal(t, tc.want, got) |
| 79 | assert.NotContains(t, got, "mode") |
| 80 | assert.NotContains(t, got, "selector_fixed") |
| 81 | assert.NotContains(t, got, collectJobLabel) |
| 82 | }) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func TestCompileInstanceLabelPlanExcludeWinsRegardlessOfTokenOrder(t *testing.T) { |
| 87 | tests := map[string]struct { |
| 88 | selectors []program.InstanceLabelSelector |
| 89 | }{ |
| 90 | "exclude after explicit": { |
| 91 | selectors: []program.InstanceLabelSelector{ |
| 92 | {Key: "host"}, |
| 93 | {Exclude: true, Key: "host"}, |
| 94 | {IncludeAll: true}, |
| 95 | }, |
| 96 | }, |
| 97 | "exclude before explicit": { |
| 98 | selectors: []program.InstanceLabelSelector{ |
| 99 | {Exclude: true, Key: "host"}, |
| 100 | {Key: "host"}, |
| 101 | {IncludeAll: true}, |
| 102 | }, |
| 103 | }, |
| 104 | } |
| 105 | |
| 106 | for name, tc := range tests { |
| 107 | t.Run(name, func(t *testing.T) { |
| 108 | plan := compileInstanceLabelPlan(program.ChartIdentity{ |
| 109 | InstanceByLabels: tc.selectors, |
| 110 | }) |
| 111 | assert.True(t, plan.includeAll) |
| 112 | assert.Empty(t, plan.explicitKeys) |
| 113 | assert.Empty(t, plan.explicitSet) |
| 114 | assert.Contains(t, plan.excludeSet, "host") |
| 115 | }) |
| 116 | } |
| 117 | } |