| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package testrandom |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "os" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/collecttest" |
| 11 | |
| 12 | "github.com/stretchr/testify/assert" |
| 13 | "github.com/stretchr/testify/require" |
| 14 | ) |
| 15 | |
| 16 | var ( |
| 17 | dataConfigJSON, _ = os.ReadFile("testdata/config.json") |
| 18 | dataConfigYAML, _ = os.ReadFile("testdata/config.yaml") |
| 19 | ) |
| 20 | |
| 21 | func Test_testDataIsValid(t *testing.T) { |
| 22 | for name, data := range map[string][]byte{ |
| 23 | "dataConfigJSON": dataConfigJSON, |
| 24 | "dataConfigYAML": dataConfigYAML, |
| 25 | } { |
| 26 | require.NotNil(t, data, name) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestCollector_ConfigurationSerialize(t *testing.T) { |
| 31 | collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML) |
| 32 | } |
| 33 | |
| 34 | func TestNew(t *testing.T) { |
| 35 | assert.IsType(t, (*Collector)(nil), New()) |
| 36 | } |
| 37 | |
| 38 | func TestCollector_Init(t *testing.T) { |
| 39 | tests := map[string]struct { |
| 40 | config Config |
| 41 | wantFail bool |
| 42 | }{ |
| 43 | "success on default config": { |
| 44 | config: New().Config, |
| 45 | }, |
| 46 | "success when only 'charts' set": { |
| 47 | config: Config{ |
| 48 | Charts: ConfigCharts{ |
| 49 | Num: 1, |
| 50 | Dims: 2, |
| 51 | }, |
| 52 | }, |
| 53 | }, |
| 54 | "success when only 'hidden_charts' set": { |
| 55 | config: Config{ |
| 56 | HiddenCharts: ConfigCharts{ |
| 57 | Num: 1, |
| 58 | Dims: 2, |
| 59 | }, |
| 60 | }, |
| 61 | }, |
| 62 | "success when 'charts' and 'hidden_charts' set": { |
| 63 | config: Config{ |
| 64 | Charts: ConfigCharts{ |
| 65 | Num: 1, |
| 66 | Dims: 2, |
| 67 | }, |
| 68 | HiddenCharts: ConfigCharts{ |
| 69 | Num: 1, |
| 70 | Dims: 2, |
| 71 | }, |
| 72 | }, |
| 73 | }, |
| 74 | "fails when 'charts' and 'hidden_charts' set, but 'num' == 0": { |
| 75 | wantFail: true, |
| 76 | config: Config{ |
| 77 | Charts: ConfigCharts{ |
| 78 | Num: 0, |
| 79 | Dims: 2, |
| 80 | }, |
| 81 | HiddenCharts: ConfigCharts{ |
| 82 | Num: 0, |
| 83 | Dims: 2, |
| 84 | }, |
| 85 | }, |
| 86 | }, |
| 87 | "fails when only 'charts' set, 'num' > 0, but 'dimensions' == 0": { |
| 88 | wantFail: true, |
| 89 | config: Config{ |
| 90 | Charts: ConfigCharts{ |
| 91 | Num: 1, |
| 92 | Dims: 0, |
| 93 | }, |
| 94 | }, |
| 95 | }, |
| 96 | "fails when only 'hidden_charts' set, 'num' > 0, but 'dimensions' == 0": { |
| 97 | wantFail: true, |
| 98 | config: Config{ |
| 99 | HiddenCharts: ConfigCharts{ |
| 100 | Num: 1, |
| 101 | Dims: 0, |
| 102 | }, |
| 103 | }, |
| 104 | }, |
| 105 | } |
| 106 | |
| 107 | for name, test := range tests { |
| 108 | t.Run(name, func(t *testing.T) { |
| 109 | collr := New() |
| 110 | collr.Config = test.config |
| 111 | |
| 112 | if test.wantFail { |
| 113 | assert.Error(t, collr.Init(context.Background())) |
| 114 | } else { |
| 115 | assert.NoError(t, collr.Init(context.Background())) |
| 116 | } |
| 117 | }) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | func TestCollector_Check(t *testing.T) { |
| 122 | tests := map[string]struct { |
| 123 | prepare func() *Collector |
| 124 | wantFail bool |
| 125 | }{ |
| 126 | "success on default": {prepare: prepareTRDefault}, |
| 127 | "success when only 'charts' set": {prepare: prepareTROnlyCharts}, |
| 128 | "success when only 'hidden_charts' set": {prepare: prepareTROnlyHiddenCharts}, |
| 129 | "success when 'charts' and 'hidden_charts' set": {prepare: prepareTRChartsAndHiddenCharts}, |
| 130 | } |
| 131 | |
| 132 | for name, test := range tests { |
| 133 | t.Run(name, func(t *testing.T) { |
| 134 | collr := test.prepare() |
| 135 | require.NoError(t, collr.Init(context.Background())) |
| 136 | |
| 137 | if test.wantFail { |
| 138 | assert.Error(t, collr.Check(context.Background())) |
| 139 | } else { |
| 140 | assert.NoError(t, collr.Check(context.Background())) |
| 141 | } |
| 142 | }) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func TestCollector_Charts(t *testing.T) { |
| 147 | tests := map[string]struct { |
| 148 | prepare func(t *testing.T) *Collector |
| 149 | wantNil bool |
| 150 | }{ |
| 151 | "not initialized collector": { |
| 152 | wantNil: true, |
| 153 | prepare: func(t *testing.T) *Collector { |
| 154 | return New() |
| 155 | }, |
| 156 | }, |
| 157 | "initialized collector": { |
| 158 | prepare: func(t *testing.T) *Collector { |
| 159 | collr := New() |
| 160 | require.NoError(t, collr.Init(context.Background())) |
| 161 | return collr |
| 162 | }, |
| 163 | }, |
| 164 | } |
| 165 | |
| 166 | for name, test := range tests { |
| 167 | t.Run(name, func(t *testing.T) { |
| 168 | collr := test.prepare(t) |
| 169 | |
| 170 | if test.wantNil { |
| 171 | assert.Nil(t, collr.Charts()) |
| 172 | } else { |
| 173 | assert.NotNil(t, collr.Charts()) |
| 174 | } |
| 175 | }) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | func TestCollector_Cleanup(t *testing.T) { |
| 180 | assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) |
| 181 | } |
| 182 | |
| 183 | func TestCollector_Collect(t *testing.T) { |
| 184 | tests := map[string]struct { |
| 185 | prepare func() *Collector |
| 186 | wantCollected map[string]int64 |
| 187 | }{ |
| 188 | "default config": { |
| 189 | prepare: prepareTRDefault, |
| 190 | wantCollected: map[string]int64{ |
| 191 | "random_0_random0": 1, |
| 192 | "random_0_random1": -1, |
| 193 | "random_0_random2": 1, |
| 194 | "random_0_random3": -1, |
| 195 | }, |
| 196 | }, |
| 197 | "only 'charts' set": { |
| 198 | prepare: prepareTROnlyCharts, |
| 199 | wantCollected: map[string]int64{ |
| 200 | "random_0_random0": 1, |
| 201 | "random_0_random1": -1, |
| 202 | "random_0_random2": 1, |
| 203 | "random_0_random3": -1, |
| 204 | "random_0_random4": 1, |
| 205 | "random_1_random0": 1, |
| 206 | "random_1_random1": -1, |
| 207 | "random_1_random2": 1, |
| 208 | "random_1_random3": -1, |
| 209 | "random_1_random4": 1, |
| 210 | }, |
| 211 | }, |
| 212 | "only 'hidden_charts' set": { |
| 213 | prepare: prepareTROnlyHiddenCharts, |
| 214 | wantCollected: map[string]int64{ |
| 215 | "hidden_random_0_random0": 1, |
| 216 | "hidden_random_0_random1": -1, |
| 217 | "hidden_random_0_random2": 1, |
| 218 | "hidden_random_0_random3": -1, |
| 219 | "hidden_random_0_random4": 1, |
| 220 | "hidden_random_1_random0": 1, |
| 221 | "hidden_random_1_random1": -1, |
| 222 | "hidden_random_1_random2": 1, |
| 223 | "hidden_random_1_random3": -1, |
| 224 | "hidden_random_1_random4": 1, |
| 225 | }, |
| 226 | }, |
| 227 | "'charts' and 'hidden_charts' set": { |
| 228 | prepare: prepareTRChartsAndHiddenCharts, |
| 229 | wantCollected: map[string]int64{ |
| 230 | "hidden_random_0_random0": 1, |
| 231 | "hidden_random_0_random1": -1, |
| 232 | "hidden_random_0_random2": 1, |
| 233 | "hidden_random_0_random3": -1, |
| 234 | "hidden_random_0_random4": 1, |
| 235 | "hidden_random_1_random0": 1, |
| 236 | "hidden_random_1_random1": -1, |
| 237 | "hidden_random_1_random2": 1, |
| 238 | "hidden_random_1_random3": -1, |
| 239 | "hidden_random_1_random4": 1, |
| 240 | "random_0_random0": 1, |
| 241 | "random_0_random1": -1, |
| 242 | "random_0_random2": 1, |
| 243 | "random_0_random3": -1, |
| 244 | "random_0_random4": 1, |
| 245 | "random_1_random0": 1, |
| 246 | "random_1_random1": -1, |
| 247 | "random_1_random2": 1, |
| 248 | "random_1_random3": -1, |
| 249 | "random_1_random4": 1, |
| 250 | }, |
| 251 | }, |
| 252 | } |
| 253 | |
| 254 | for name, test := range tests { |
| 255 | t.Run(name, func(t *testing.T) { |
| 256 | collr := test.prepare() |
| 257 | require.NoError(t, collr.Init(context.Background())) |
| 258 | |
| 259 | mx := collr.Collect(context.Background()) |
| 260 | |
| 261 | assert.Equal(t, test.wantCollected, mx) |
| 262 | collecttest.TestMetricsHasAllChartsDims(t, collr.Charts(), mx) |
| 263 | }) |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | func prepareTRDefault() *Collector { |
| 268 | return prepareTR(New().Config) |
| 269 | } |
| 270 | |
| 271 | func prepareTROnlyCharts() *Collector { |
| 272 | return prepareTR(Config{ |
| 273 | Charts: ConfigCharts{ |
| 274 | Num: 2, |
| 275 | Dims: 5, |
| 276 | }, |
| 277 | }) |
| 278 | } |
| 279 | |
| 280 | func prepareTROnlyHiddenCharts() *Collector { |
| 281 | return prepareTR(Config{ |
| 282 | HiddenCharts: ConfigCharts{ |
| 283 | Num: 2, |
| 284 | Dims: 5, |
| 285 | }, |
| 286 | }) |
| 287 | } |
| 288 | |
| 289 | func prepareTRChartsAndHiddenCharts() *Collector { |
| 290 | return prepareTR(Config{ |
| 291 | Charts: ConfigCharts{ |
| 292 | Num: 2, |
| 293 | Dims: 5, |
| 294 | }, |
| 295 | HiddenCharts: ConfigCharts{ |
| 296 | Num: 2, |
| 297 | Dims: 5, |
| 298 | }, |
| 299 | }) |
| 300 | } |
| 301 | |
| 302 | func prepareTR(cfg Config) *Collector { |
| 303 | collr := New() |
| 304 | collr.Config = cfg |
| 305 | collr.randInt = func() int64 { return 1 } |
| 306 | return collr |
| 307 | } |