|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +// Copyright 2024 InfluxData, Inc. All rights reserved. |
| 4 | +// Use of this source code is governed by MIT |
| 5 | +// license that can be found in the LICENSE fil |
| 6 | +package api_test |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "testing" |
| 13 | + |
| 14 | + influxdb2 "github.com/influxdata/influxdb-client-go/v2" |
| 15 | + "github.com/influxdata/influxdb-client-go/v2/domain" |
| 16 | +) |
| 17 | + |
| 18 | +var msg = "Check: ${ r._check_name } is: ${ r._level }" |
| 19 | +var flux = `from(bucket: "foo") |> range(start: -1d, stop: now()) |> aggregateWindow(every: 1m, fn: mean) |> filter(fn: (r) => r._field == "usage_user") |> yield()` |
| 20 | +var every = "1h" |
| 21 | +var offset = "0s" |
| 22 | +var timeSince = "90m" |
| 23 | +var staleTime = "30m" |
| 24 | +var level = domain.CheckStatusLevelCRIT |
| 25 | + |
| 26 | +func TestCreateGetDeleteThresholdCheck(t *testing.T) { |
| 27 | + ctx := context.Background() |
| 28 | + client := influxdb2.NewClient(serverURL, authToken) |
| 29 | + |
| 30 | + greater := domain.GreaterThreshold{} |
| 31 | + greater.Value = 10.0 |
| 32 | + lc := domain.CheckStatusLevelCRIT |
| 33 | + greater.Level = &lc |
| 34 | + greater.AllValues = &[]bool{true}[0] |
| 35 | + |
| 36 | + lesser := domain.LesserThreshold{} |
| 37 | + lesser.Value = 1.0 |
| 38 | + lo := domain.CheckStatusLevelOK |
| 39 | + lesser.Level = &lo |
| 40 | + |
| 41 | + rang := domain.RangeThreshold{} |
| 42 | + rang.Min = 3.0 |
| 43 | + rang.Max = 8.0 |
| 44 | + lw := domain.CheckStatusLevelWARN |
| 45 | + rang.Level = &lw |
| 46 | + |
| 47 | + org, err := client.OrganizationsAPI().FindOrganizationByName(ctx, "my-org") |
| 48 | + require.Nil(t, err) |
| 49 | + |
| 50 | + thresholds := []domain.Threshold{&greater, &lesser, &rang} |
| 51 | + |
| 52 | + check := domain.ThresholdCheck{ |
| 53 | + CheckBaseExtend: domain.CheckBaseExtend{ |
| 54 | + CheckBase: domain.CheckBase{ |
| 55 | + Name: "ThresholdCheck test", |
| 56 | + OrgID: *org.Id, |
| 57 | + Query: domain.DashboardQuery{Text: &flux}, |
| 58 | + Status: domain.TaskStatusTypeActive, |
| 59 | + }, |
| 60 | + Every: &every, |
| 61 | + Offset: &offset, |
| 62 | + StatusMessageTemplate: &msg, |
| 63 | + }, |
| 64 | + Thresholds: &thresholds, |
| 65 | + } |
| 66 | + params := domain.CreateCheckAllParams{ |
| 67 | + Body: domain.CreateCheckJSONRequestBody(&check), |
| 68 | + } |
| 69 | + nc, err := client.APIClient().CreateCheck(context.Background(), ¶ms) |
| 70 | + require.Nil(t, err) |
| 71 | + tc := validateTC(t, nc, *org.Id) |
| 72 | + |
| 73 | + gp := domain.GetChecksIDAllParams{CheckID: *tc.Id} |
| 74 | + |
| 75 | + c, err := client.APIClient().GetChecksID(context.Background(), &gp) |
| 76 | + require.Nil(t, err) |
| 77 | + tc = validateTC(t, c, *org.Id) |
| 78 | + |
| 79 | + dp := domain.DeleteChecksIDAllParams{ |
| 80 | + CheckID: *tc.Id, |
| 81 | + } |
| 82 | + |
| 83 | + err = client.APIClient().DeleteChecksID(context.Background(), &dp) |
| 84 | + require.Nil(t, err) |
| 85 | + |
| 86 | + _, err = client.APIClient().GetChecksID(context.Background(), &gp) |
| 87 | + require.NotNil(t, err) |
| 88 | +} |
| 89 | + |
| 90 | +func TestCreateGetDeleteDeadmanCheck(t *testing.T) { |
| 91 | + client := influxdb2.NewClient(serverURL, authToken) |
| 92 | + |
| 93 | + ctx := context.Background() |
| 94 | + |
| 95 | + org, err := client.OrganizationsAPI().FindOrganizationByName(ctx, "my-org") |
| 96 | + require.Nil(t, err) |
| 97 | + |
| 98 | + check := domain.DeadmanCheck{ |
| 99 | + CheckBaseExtend: domain.CheckBaseExtend{ |
| 100 | + CheckBase: domain.CheckBase{ |
| 101 | + Name: "DeadmanCheck test", |
| 102 | + OrgID: *org.Id, |
| 103 | + Query: domain.DashboardQuery{Text: &flux}, |
| 104 | + Status: domain.TaskStatusTypeActive, |
| 105 | + }, |
| 106 | + Every: &every, |
| 107 | + Offset: &offset, |
| 108 | + StatusMessageTemplate: &msg, |
| 109 | + }, |
| 110 | + TimeSince: &timeSince, |
| 111 | + StaleTime: &staleTime, |
| 112 | + Level: &level, |
| 113 | + } |
| 114 | + params := domain.CreateCheckAllParams{ |
| 115 | + Body: domain.CreateCheckJSONRequestBody(&check), |
| 116 | + } |
| 117 | + nc, err := client.APIClient().CreateCheck(context.Background(), ¶ms) |
| 118 | + require.Nil(t, err) |
| 119 | + dc := validateDC(t, nc, *org.Id) |
| 120 | + |
| 121 | + gp := domain.GetChecksIDAllParams{CheckID: *dc.Id} |
| 122 | + |
| 123 | + c, err := client.APIClient().GetChecksID(context.Background(), &gp) |
| 124 | + require.Nil(t, err) |
| 125 | + dc = validateDC(t, c, *org.Id) |
| 126 | + |
| 127 | + dp := domain.DeleteChecksIDAllParams{ |
| 128 | + CheckID: *dc.Id, |
| 129 | + } |
| 130 | + |
| 131 | + err = client.APIClient().DeleteChecksID(context.Background(), &dp) |
| 132 | + require.Nil(t, err) |
| 133 | + |
| 134 | +} |
| 135 | + |
| 136 | +func TestUpdateThresholdCheck(t *testing.T) { |
| 137 | + ctx := context.Background() |
| 138 | + client := influxdb2.NewClient(serverURL, authToken) |
| 139 | + |
| 140 | + greater := domain.GreaterThreshold{} |
| 141 | + greater.Value = 10.0 |
| 142 | + lc := domain.CheckStatusLevelCRIT |
| 143 | + greater.Level = &lc |
| 144 | + greater.AllValues = &[]bool{true}[0] |
| 145 | + |
| 146 | + org, err := client.OrganizationsAPI().FindOrganizationByName(ctx, "my-org") |
| 147 | + require.Nil(t, err) |
| 148 | + |
| 149 | + thresholds := []domain.Threshold{&greater} |
| 150 | + |
| 151 | + ev := "1m" |
| 152 | + |
| 153 | + check := domain.ThresholdCheck{ |
| 154 | + CheckBaseExtend: domain.CheckBaseExtend{ |
| 155 | + CheckBase: domain.CheckBase{ |
| 156 | + Name: "ThresholdCheck update test", |
| 157 | + OrgID: *org.Id, |
| 158 | + Query: domain.DashboardQuery{Text: &flux}, |
| 159 | + Status: domain.TaskStatusTypeActive, |
| 160 | + }, |
| 161 | + Every: &ev, |
| 162 | + Offset: &offset, |
| 163 | + StatusMessageTemplate: &msg, |
| 164 | + }, |
| 165 | + Thresholds: &thresholds, |
| 166 | + } |
| 167 | + params := domain.CreateCheckAllParams{ |
| 168 | + Body: domain.CreateCheckJSONRequestBody(&check), |
| 169 | + } |
| 170 | + nc, err := client.APIClient().CreateCheck(context.Background(), ¶ms) |
| 171 | + require.Nil(t, err) |
| 172 | + require.NotNil(t, nc) |
| 173 | + tc := nc.(*domain.ThresholdCheck) |
| 174 | + |
| 175 | + lesser := domain.LesserThreshold{} |
| 176 | + lesser.Value = 1.0 |
| 177 | + lo := domain.CheckStatusLevelOK |
| 178 | + lesser.Level = &lo |
| 179 | + |
| 180 | + rang := domain.RangeThreshold{} |
| 181 | + rang.Min = 3.0 |
| 182 | + rang.Max = 8.0 |
| 183 | + lw := domain.CheckStatusLevelWARN |
| 184 | + rang.Level = &lw |
| 185 | + |
| 186 | + thresholds = []domain.Threshold{&greater, &lesser, &rang} |
| 187 | + tc.Thresholds = &thresholds |
| 188 | + tc.Every = &every |
| 189 | + tc.Name = "ThresholdCheck test" |
| 190 | + |
| 191 | + updateParams := domain.PutChecksIDAllParams{ |
| 192 | + CheckID: *tc.Id, |
| 193 | + Body: tc, |
| 194 | + } |
| 195 | + nc, err = client.APIClient().PutChecksID(context.Background(), &updateParams) |
| 196 | + require.Nil(t, err) |
| 197 | + require.NotNil(t, nc) |
| 198 | + tc = validateTC(t, nc, *org.Id) |
| 199 | + |
| 200 | + dp := domain.DeleteChecksIDAllParams{ |
| 201 | + CheckID: *tc.Id, |
| 202 | + } |
| 203 | + |
| 204 | + err = client.APIClient().DeleteChecksID(context.Background(), &dp) |
| 205 | + require.Nil(t, err) |
| 206 | +} |
| 207 | + |
| 208 | +func TestGetChecks(t *testing.T) { |
| 209 | + client := influxdb2.NewClient(serverURL, authToken) |
| 210 | + |
| 211 | + ctx := context.Background() |
| 212 | + |
| 213 | + org, err := client.OrganizationsAPI().FindOrganizationByName(ctx, "my-org") |
| 214 | + require.Nil(t, err) |
| 215 | + |
| 216 | + check := domain.DeadmanCheck{ |
| 217 | + CheckBaseExtend: domain.CheckBaseExtend{ |
| 218 | + CheckBase: domain.CheckBase{ |
| 219 | + Name: "DeadmanCheck test", |
| 220 | + OrgID: *org.Id, |
| 221 | + Query: domain.DashboardQuery{Text: &flux}, |
| 222 | + Status: domain.TaskStatusTypeActive, |
| 223 | + }, |
| 224 | + Every: &every, |
| 225 | + Offset: &offset, |
| 226 | + StatusMessageTemplate: &msg, |
| 227 | + }, |
| 228 | + TimeSince: &timeSince, |
| 229 | + StaleTime: &staleTime, |
| 230 | + Level: &level, |
| 231 | + } |
| 232 | + params := domain.CreateCheckAllParams{ |
| 233 | + Body: domain.CreateCheckJSONRequestBody(&check), |
| 234 | + } |
| 235 | + nc, err := client.APIClient().CreateCheck(context.Background(), ¶ms) |
| 236 | + require.Nil(t, err) |
| 237 | + validateDC(t, nc, *org.Id) |
| 238 | + |
| 239 | + greater := domain.GreaterThreshold{} |
| 240 | + greater.Value = 10.0 |
| 241 | + lc := domain.CheckStatusLevelCRIT |
| 242 | + greater.Level = &lc |
| 243 | + greater.AllValues = &[]bool{true}[0] |
| 244 | + |
| 245 | + lesser := domain.LesserThreshold{} |
| 246 | + lesser.Value = 1.0 |
| 247 | + lo := domain.CheckStatusLevelOK |
| 248 | + lesser.Level = &lo |
| 249 | + |
| 250 | + rang := domain.RangeThreshold{} |
| 251 | + rang.Min = 3.0 |
| 252 | + rang.Max = 8.0 |
| 253 | + lw := domain.CheckStatusLevelWARN |
| 254 | + rang.Level = &lw |
| 255 | + |
| 256 | + thresholds := []domain.Threshold{&greater, &lesser, &rang} |
| 257 | + |
| 258 | + check2 := domain.ThresholdCheck{ |
| 259 | + CheckBaseExtend: domain.CheckBaseExtend{ |
| 260 | + CheckBase: domain.CheckBase{ |
| 261 | + Name: "ThresholdCheck test", |
| 262 | + OrgID: *org.Id, |
| 263 | + Query: domain.DashboardQuery{Text: &flux}, |
| 264 | + Status: domain.TaskStatusTypeActive, |
| 265 | + }, |
| 266 | + Every: &every, |
| 267 | + Offset: &offset, |
| 268 | + StatusMessageTemplate: &msg, |
| 269 | + }, |
| 270 | + Thresholds: &thresholds, |
| 271 | + } |
| 272 | + params2 := domain.CreateCheckAllParams{ |
| 273 | + Body: domain.CreateCheckJSONRequestBody(&check2), |
| 274 | + } |
| 275 | + nc, err = client.APIClient().CreateCheck(context.Background(), ¶ms2) |
| 276 | + require.Nil(t, err) |
| 277 | + validateTC(t, nc, *org.Id) |
| 278 | + |
| 279 | + gp := domain.GetChecksParams{ |
| 280 | + OrgID: *org.Id, |
| 281 | + } |
| 282 | + checks, err := client.APIClient().GetChecks(context.Background(), &gp) |
| 283 | + require.Nil(t, err) |
| 284 | + require.NotNil(t, checks) |
| 285 | + require.NotNil(t, checks.Checks) |
| 286 | + assert.Len(t, *checks.Checks, 2) |
| 287 | + dc := validateDC(t, (*checks.Checks)[0], *org.Id) |
| 288 | + tc := validateTC(t, (*checks.Checks)[1], *org.Id) |
| 289 | + |
| 290 | + dp := domain.DeleteChecksIDAllParams{ |
| 291 | + CheckID: *dc.Id, |
| 292 | + } |
| 293 | + err = client.APIClient().DeleteChecksID(context.Background(), &dp) |
| 294 | + require.Nil(t, err) |
| 295 | + |
| 296 | + dp2 := domain.DeleteChecksIDAllParams{ |
| 297 | + CheckID: *tc.Id, |
| 298 | + } |
| 299 | + err = client.APIClient().DeleteChecksID(context.Background(), &dp2) |
| 300 | + require.Nil(t, err) |
| 301 | + |
| 302 | + checks, err = client.APIClient().GetChecks(context.Background(), &gp) |
| 303 | + require.Nil(t, err) |
| 304 | + require.NotNil(t, checks) |
| 305 | + assert.Nil(t, checks.Checks) |
| 306 | +} |
| 307 | + |
| 308 | +func validateDC(t *testing.T, nc domain.Check, orgID string) *domain.DeadmanCheck { |
| 309 | + require.NotNil(t, nc) |
| 310 | + require.Equal(t, "deadman", nc.Type()) |
| 311 | + dc := nc.(*domain.DeadmanCheck) |
| 312 | + require.NotNil(t, dc) |
| 313 | + assert.Equal(t, "DeadmanCheck test", dc.Name) |
| 314 | + assert.Equal(t, orgID, dc.OrgID) |
| 315 | + assert.Equal(t, msg, *dc.StatusMessageTemplate) |
| 316 | + assert.Equal(t, flux, *dc.Query.Text) |
| 317 | + assert.Equal(t, every, *dc.Every) |
| 318 | + assert.Equal(t, offset, *dc.Offset) |
| 319 | + assert.Equal(t, domain.TaskStatusTypeActive, dc.Status) |
| 320 | + assert.Equal(t, timeSince, *dc.TimeSince) |
| 321 | + assert.Equal(t, staleTime, *dc.StaleTime) |
| 322 | + assert.Equal(t, domain.CheckStatusLevelCRIT, *dc.Level) |
| 323 | + return dc |
| 324 | +} |
| 325 | + |
| 326 | +func validateTC(t *testing.T, check domain.Check, orgID string) *domain.ThresholdCheck { |
| 327 | + require.NotNil(t, check) |
| 328 | + require.Equal(t, "threshold", check.Type()) |
| 329 | + tc := check.(*domain.ThresholdCheck) |
| 330 | + require.NotNil(t, tc) |
| 331 | + assert.Equal(t, "ThresholdCheck test", tc.Name) |
| 332 | + assert.Equal(t, orgID, tc.OrgID) |
| 333 | + assert.Equal(t, msg, *tc.StatusMessageTemplate) |
| 334 | + assert.Equal(t, flux, *tc.Query.Text) |
| 335 | + assert.Equal(t, every, *tc.Every) |
| 336 | + assert.Equal(t, offset, *tc.Offset) |
| 337 | + assert.Equal(t, domain.TaskStatusTypeActive, tc.Status) |
| 338 | + assert.Len(t, *tc.Thresholds, 3) |
| 339 | + require.Equal(t, "greater", (*tc.Thresholds)[0].Type()) |
| 340 | + gt := (*tc.Thresholds)[0].(*domain.GreaterThreshold) |
| 341 | + require.NotNil(t, gt) |
| 342 | + assert.Equal(t, float32(10.0), gt.Value) |
| 343 | + assert.Equal(t, domain.CheckStatusLevelCRIT, *gt.Level) |
| 344 | + assert.Equal(t, true, *gt.AllValues) |
| 345 | + require.Equal(t, "lesser", (*tc.Thresholds)[1].Type()) |
| 346 | + lt := (*tc.Thresholds)[1].(*domain.LesserThreshold) |
| 347 | + require.NotNil(t, lt) |
| 348 | + assert.Equal(t, float32(1.0), lt.Value) |
| 349 | + assert.Equal(t, domain.CheckStatusLevelOK, *lt.Level) |
| 350 | + require.Equal(t, "range", (*tc.Thresholds)[2].Type()) |
| 351 | + rt := (*tc.Thresholds)[2].(*domain.RangeThreshold) |
| 352 | + require.NotNil(t, rt) |
| 353 | + assert.Equal(t, float32(3.0), rt.Min) |
| 354 | + assert.Equal(t, float32(8.0), rt.Max) |
| 355 | + assert.Equal(t, domain.CheckStatusLevelWARN, *rt.Level) |
| 356 | + return tc |
| 357 | +} |
0 commit comments