github.com/netdata/go.d.plugin@v0.58.1/agent/module/charts_test.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package module 4 5 import ( 6 "fmt" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func createTestChart(id string) *Chart { 14 return &Chart{ 15 ID: id, 16 Title: "Title", 17 Units: "units", 18 Fam: "family", 19 Ctx: "context", 20 Type: Line, 21 Dims: Dims{ 22 {ID: "dim1", Algo: Absolute}, 23 }, 24 Vars: Vars{ 25 {ID: "var1", Value: 1}, 26 }, 27 } 28 } 29 30 func TestDimAlgo_String(t *testing.T) { 31 cases := []struct { 32 expected string 33 actual fmt.Stringer 34 }{ 35 {"absolute", Absolute}, 36 {"incremental", Incremental}, 37 {"percentage-of-absolute-row", PercentOfAbsolute}, 38 {"percentage-of-incremental-row", PercentOfIncremental}, 39 {"absolute", DimAlgo("wrong")}, 40 } 41 42 for _, v := range cases { 43 assert.Equal(t, v.expected, v.actual.String()) 44 } 45 } 46 47 func TestChartType_String(t *testing.T) { 48 cases := []struct { 49 expected string 50 actual fmt.Stringer 51 }{ 52 {"line", Line}, 53 {"area", Area}, 54 {"stacked", Stacked}, 55 {"line", ChartType("wrong")}, 56 } 57 58 for _, v := range cases { 59 assert.Equal(t, v.expected, v.actual.String()) 60 } 61 } 62 63 func TestOpts_String(t *testing.T) { 64 cases := []struct { 65 expected string 66 actual fmt.Stringer 67 }{ 68 {"", Opts{}}, 69 { 70 "detail hidden obsolete store_first", 71 Opts{Detail: true, Hidden: true, Obsolete: true, StoreFirst: true}, 72 }, 73 { 74 "detail hidden obsolete store_first", 75 Opts{Detail: true, Hidden: true, Obsolete: true, StoreFirst: true}, 76 }, 77 } 78 79 for _, v := range cases { 80 assert.Equal(t, v.expected, v.actual.String()) 81 } 82 } 83 84 func TestDimOpts_String(t *testing.T) { 85 cases := []struct { 86 expected string 87 actual fmt.Stringer 88 }{ 89 {"", DimOpts{}}, 90 { 91 "hidden nooverflow noreset obsolete", 92 DimOpts{Hidden: true, NoOverflow: true, NoReset: true, Obsolete: true}, 93 }, 94 { 95 "hidden obsolete", 96 DimOpts{Hidden: true, NoOverflow: false, NoReset: false, Obsolete: true}, 97 }, 98 } 99 100 for _, v := range cases { 101 assert.Equal(t, v.expected, v.actual.String()) 102 } 103 } 104 105 func TestCharts_Copy(t *testing.T) { 106 orig := &Charts{ 107 createTestChart("1"), 108 createTestChart("2"), 109 } 110 copied := orig.Copy() 111 112 require.False(t, orig == copied, "Charts copy points to the same address") 113 require.Len(t, *orig, len(*copied)) 114 115 for idx := range *orig { 116 compareCharts(t, (*orig)[idx], (*copied)[idx]) 117 } 118 } 119 120 func TestChart_Copy(t *testing.T) { 121 orig := createTestChart("1") 122 123 compareCharts(t, orig, orig.Copy()) 124 } 125 126 func TestCharts_Add(t *testing.T) { 127 charts := Charts{} 128 chart1 := createTestChart("1") 129 chart2 := createTestChart("2") 130 chart3 := createTestChart("") 131 132 // OK case 133 assert.NoError(t, charts.Add( 134 chart1, 135 chart2, 136 )) 137 assert.Len(t, charts, 2) 138 139 // NG case 140 assert.Error(t, charts.Add( 141 chart3, 142 chart1, 143 chart2, 144 )) 145 assert.Len(t, charts, 2) 146 147 assert.True(t, charts[0] == chart1) 148 assert.True(t, charts[1] == chart2) 149 } 150 151 func TestCharts_Add_SameID(t *testing.T) { 152 charts := Charts{} 153 chart1 := createTestChart("1") 154 chart2 := createTestChart("1") 155 156 assert.NoError(t, charts.Add(chart1)) 157 assert.Error(t, charts.Add(chart2)) 158 assert.Len(t, charts, 1) 159 160 charts = Charts{} 161 chart1 = createTestChart("1") 162 chart2 = createTestChart("1") 163 164 assert.NoError(t, charts.Add(chart1)) 165 chart1.MarkRemove() 166 assert.NoError(t, charts.Add(chart2)) 167 assert.Len(t, charts, 2) 168 } 169 170 func TestCharts_Get(t *testing.T) { 171 chart := createTestChart("1") 172 charts := Charts{ 173 chart, 174 } 175 176 // OK case 177 assert.True(t, chart == charts.Get("1")) 178 // NG case 179 assert.Nil(t, charts.Get("2")) 180 } 181 182 func TestCharts_Has(t *testing.T) { 183 chart := createTestChart("1") 184 charts := &Charts{ 185 chart, 186 } 187 188 // OK case 189 assert.True(t, charts.Has("1")) 190 // NG case 191 assert.False(t, charts.Has("2")) 192 } 193 194 func TestCharts_Remove(t *testing.T) { 195 chart := createTestChart("1") 196 charts := &Charts{ 197 chart, 198 } 199 200 // OK case 201 assert.NoError(t, charts.Remove("1")) 202 assert.Len(t, *charts, 0) 203 204 // NG case 205 assert.Error(t, charts.Remove("2")) 206 } 207 208 func TestChart_AddDim(t *testing.T) { 209 chart := createTestChart("1") 210 dim := &Dim{ID: "dim2"} 211 212 // OK case 213 assert.NoError(t, chart.AddDim(dim)) 214 assert.Len(t, chart.Dims, 2) 215 216 // NG case 217 assert.Error(t, chart.AddDim(dim)) 218 assert.Len(t, chart.Dims, 2) 219 } 220 221 func TestChart_AddVar(t *testing.T) { 222 chart := createTestChart("1") 223 variable := &Var{ID: "var2"} 224 225 // OK case 226 assert.NoError(t, chart.AddVar(variable)) 227 assert.Len(t, chart.Vars, 2) 228 229 // NG case 230 assert.Error(t, chart.AddVar(variable)) 231 assert.Len(t, chart.Vars, 2) 232 } 233 234 func TestChart_GetDim(t *testing.T) { 235 chart := &Chart{ 236 Dims: Dims{ 237 {ID: "1"}, 238 {ID: "2"}, 239 }, 240 } 241 242 // OK case 243 assert.True(t, chart.GetDim("1") != nil && chart.GetDim("1").ID == "1") 244 245 // NG case 246 assert.Nil(t, chart.GetDim("3")) 247 } 248 249 func TestChart_RemoveDim(t *testing.T) { 250 chart := createTestChart("1") 251 252 // OK case 253 assert.NoError(t, chart.RemoveDim("dim1")) 254 assert.Len(t, chart.Dims, 0) 255 256 // NG case 257 assert.Error(t, chart.RemoveDim("dim2")) 258 } 259 260 func TestChart_HasDim(t *testing.T) { 261 chart := createTestChart("1") 262 263 // OK case 264 assert.True(t, chart.HasDim("dim1")) 265 // NG case 266 assert.False(t, chart.HasDim("dim2")) 267 } 268 269 func TestChart_MarkNotCreated(t *testing.T) { 270 chart := createTestChart("1") 271 272 chart.MarkNotCreated() 273 assert.False(t, chart.created) 274 } 275 276 func TestChart_MarkRemove(t *testing.T) { 277 chart := createTestChart("1") 278 279 chart.MarkRemove() 280 assert.True(t, chart.remove) 281 assert.True(t, chart.Obsolete) 282 } 283 284 func TestChart_MarkDimRemove(t *testing.T) { 285 chart := createTestChart("1") 286 287 assert.Error(t, chart.MarkDimRemove("dim99", false)) 288 assert.NoError(t, chart.MarkDimRemove("dim1", true)) 289 assert.True(t, chart.GetDim("dim1").Obsolete) 290 assert.True(t, chart.GetDim("dim1").Hidden) 291 assert.True(t, chart.GetDim("dim1").remove) 292 } 293 294 func TestChart_check(t *testing.T) { 295 // OK case 296 chart := createTestChart("1") 297 assert.NoError(t, checkChart(chart)) 298 299 // NG case 300 chart = createTestChart("1") 301 chart.ID = "" 302 assert.Error(t, checkChart(chart)) 303 304 chart = createTestChart("1") 305 chart.ID = "invalid id" 306 assert.Error(t, checkChart(chart)) 307 308 chart = createTestChart("1") 309 chart.Title = "" 310 assert.Error(t, checkChart(chart)) 311 312 chart = createTestChart("1") 313 chart.Units = "" 314 assert.Error(t, checkChart(chart)) 315 316 chart = createTestChart("1") 317 chart.Dims = Dims{ 318 {ID: "1"}, 319 {ID: "1"}, 320 } 321 assert.Error(t, checkChart(chart)) 322 323 chart = createTestChart("1") 324 chart.Vars = Vars{ 325 {ID: "1"}, 326 {ID: "1"}, 327 } 328 assert.Error(t, checkChart(chart)) 329 } 330 331 func TestDim_check(t *testing.T) { 332 // OK case 333 dim := &Dim{ID: "id"} 334 assert.NoError(t, checkDim(dim)) 335 336 // NG case 337 dim = &Dim{ID: "id"} 338 dim.ID = "" 339 assert.Error(t, checkDim(dim)) 340 341 dim = &Dim{ID: "id"} 342 dim.ID = "invalid id" 343 assert.Error(t, checkDim(dim)) 344 } 345 346 func TestVar_check(t *testing.T) { 347 // OK case 348 v := &Var{ID: "id"} 349 assert.NoError(t, checkVar(v)) 350 351 // NG case 352 v = &Var{ID: "id"} 353 v.ID = "" 354 assert.Error(t, checkVar(v)) 355 356 v = &Var{ID: "id"} 357 v.ID = "invalid id" 358 assert.Error(t, checkVar(v)) 359 } 360 361 func compareCharts(t *testing.T, orig, copied *Chart) { 362 // 1. compare chart pointers 363 // 2. compare Dims, Vars length 364 // 3. compare Dims, Vars pointers 365 366 assert.False(t, orig == copied, "Chart copy ChartsFunc points to the same address") 367 368 require.Len(t, orig.Dims, len(copied.Dims)) 369 require.Len(t, orig.Vars, len(copied.Vars)) 370 371 for idx := range (*orig).Dims { 372 assert.False(t, orig.Dims[idx] == copied.Dims[idx], "Chart copy dim points to the same address") 373 assert.Equal(t, orig.Dims[idx], copied.Dims[idx], "Chart copy dim isn't equal to orig") 374 } 375 376 for idx := range (*orig).Vars { 377 assert.False(t, orig.Vars[idx] == copied.Vars[idx], "Chart copy var points to the same address") 378 assert.Equal(t, orig.Vars[idx], copied.Vars[idx], "Chart copy var isn't equal to orig") 379 } 380 }