|
7 | 7 | */ |
8 | 8 | import { describe, expect, it, test } from 'vitest' |
9 | 9 |
|
10 | | -import { diskCan, genName, instanceCan, parsePortRange, synthesizeData } from './util' |
| 10 | +import { |
| 11 | + diskCan, |
| 12 | + genName, |
| 13 | + instanceCan, |
| 14 | + parsePortRange, |
| 15 | + resendableAlertIds, |
| 16 | + subscriptionRegex, |
| 17 | + synthesizeData, |
| 18 | +} from './util' |
| 19 | + |
| 20 | +describe('subscriptionRegex', () => { |
| 21 | + it('matches exact class names', () => { |
| 22 | + expect(subscriptionRegex('instance.create').test('instance.create')).toBe(true) |
| 23 | + expect(subscriptionRegex('instance.create').test('instance.created')).toBe(false) |
| 24 | + }) |
| 25 | + |
| 26 | + it('* matches exactly one segment', () => { |
| 27 | + const re = subscriptionRegex('disk.*') |
| 28 | + expect(re.test('disk.create')).toBe(true) |
| 29 | + expect(re.test('disk.snapshot.create')).toBe(false) |
| 30 | + expect(re.test('disk')).toBe(false) |
| 31 | + }) |
| 32 | + |
| 33 | + it('* can appear at the head', () => { |
| 34 | + const re = subscriptionRegex('*.create') |
| 35 | + expect(re.test('disk.create')).toBe(true) |
| 36 | + expect(re.test('instance.create')).toBe(true) |
| 37 | + expect(re.test('instance.ephemeral_ip.create')).toBe(false) |
| 38 | + }) |
| 39 | + |
| 40 | + it('* can appear mid-string', () => { |
| 41 | + const re = subscriptionRegex('hardware.*.insert') |
| 42 | + expect(re.test('hardware.sled.insert')).toBe(true) |
| 43 | + expect(re.test('hardware.power_shelf.psu.insert')).toBe(false) |
| 44 | + expect(re.test('hardware.insert')).toBe(false) |
| 45 | + }) |
| 46 | + |
| 47 | + it('** matches one or more segments', () => { |
| 48 | + const re = subscriptionRegex('hardware.**') |
| 49 | + expect(re.test('hardware.power_shelf.psu.insert')).toBe(true) |
| 50 | + expect(re.test('hardware.psu')).toBe(true) |
| 51 | + expect(re.test('hardware')).toBe(false) |
| 52 | + |
| 53 | + const suffix = subscriptionRegex('**.delete') |
| 54 | + expect(suffix.test('project.delete')).toBe(true) |
| 55 | + expect(suffix.test('instance.ephemeral_ip.delete')).toBe(true) |
| 56 | + expect(suffix.test('delete')).toBe(false) |
| 57 | + |
| 58 | + const infix = subscriptionRegex('hardware.**.insert') |
| 59 | + expect(infix.test('hardware.sled.insert')).toBe(true) |
| 60 | + expect(infix.test('hardware.power_shelf.psu.insert')).toBe(true) |
| 61 | + expect(infix.test('hardware.insert')).toBe(false) |
| 62 | + }) |
| 63 | + |
| 64 | + it('does not match substrings within a segment', () => { |
| 65 | + expect(subscriptionRegex('instance.**').test('silo.instance_quota.hit')).toBe(false) |
| 66 | + expect(subscriptionRegex('disk.*').test('bigdisk.create')).toBe(false) |
| 67 | + }) |
| 68 | + |
| 69 | + it('does not match leading/trailing segments without a star pattern', () => { |
| 70 | + expect(subscriptionRegex('instance.**').test('silo.instance.hit')).toBe(false) |
| 71 | + expect(subscriptionRegex('**.power_shelf').test('hardware.power_shelf.psu')).toBe(false) |
| 72 | + }) |
| 73 | +}) |
11 | 74 |
|
12 | 75 | describe('parsePortRange', () => { |
13 | 76 | describe('parses', () => { |
@@ -197,3 +260,76 @@ test('diskCan', () => { |
197 | 260 | // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
198 | 261 | diskCan.abc |
199 | 262 | }) |
| 263 | + |
| 264 | +describe('resendableAlertIds', () => { |
| 265 | + // the rule only reads these four fields, so build them directly |
| 266 | + type Delivery = Parameters<typeof resendableAlertIds>[0][number] |
| 267 | + |
| 268 | + const d = ( |
| 269 | + alertId: string, |
| 270 | + state: Delivery['state'], |
| 271 | + trigger: Delivery['trigger'], |
| 272 | + alertClass = 'hardware.sled.fault' |
| 273 | + ): Delivery => ({ alertId, state, trigger, alertClass }) |
| 274 | + |
| 275 | + const ids = (deliveries: Delivery[]) => [...resendableAlertIds(deliveries)].sort() |
| 276 | + |
| 277 | + it('is empty when there are no deliveries', () => { |
| 278 | + expect(ids([])).toEqual([]) |
| 279 | + }) |
| 280 | + |
| 281 | + it('includes an alert whose only delivery failed', () => { |
| 282 | + expect(ids([d('a', 'failed', 'alert')])).toEqual(['a']) |
| 283 | + }) |
| 284 | + |
| 285 | + it('excludes delivered and pending alerts', () => { |
| 286 | + expect(ids([d('a', 'delivered', 'alert'), d('b', 'pending', 'alert')])).toEqual([]) |
| 287 | + }) |
| 288 | + |
| 289 | + // the bug this rule replaced: it counted failed delivery records, so an |
| 290 | + // alert that had already been resent successfully was requeued forever |
| 291 | + it('excludes an alert that has a failed record but also a successful resend', () => { |
| 292 | + const deliveries = [d('a', 'failed', 'alert'), d('a', 'delivered', 'resend')] |
| 293 | + expect(ids(deliveries)).toEqual([]) |
| 294 | + }) |
| 295 | + |
| 296 | + // a resend in flight takes the alert out of the set, so a second probe does |
| 297 | + // not double-queue it |
| 298 | + it('excludes an alert with a resend still pending', () => { |
| 299 | + const deliveries = [d('a', 'failed', 'alert'), d('a', 'pending', 'resend')] |
| 300 | + expect(ids(deliveries)).toEqual([]) |
| 301 | + }) |
| 302 | + |
| 303 | + it('counts an alert once no matter how many times it failed', () => { |
| 304 | + const deliveries = [d('a', 'failed', 'alert'), d('a', 'failed', 'resend')] |
| 305 | + expect(ids(deliveries)).toEqual(['a']) |
| 306 | + }) |
| 307 | + |
| 308 | + it('ignores probe deliveries entirely', () => { |
| 309 | + const deliveries = [ |
| 310 | + d('probe-alert', 'delivered', 'probe', 'probe'), |
| 311 | + d('probe-alert', 'failed', 'probe', 'probe'), |
| 312 | + d('a', 'failed', 'alert'), |
| 313 | + ] |
| 314 | + expect(ids(deliveries)).toEqual(['a']) |
| 315 | + }) |
| 316 | + |
| 317 | + // a successful probe of an alert does not mean the alert itself landed, so it |
| 318 | + // must not settle the alert. matches omicron's triggered_by != probe filter |
| 319 | + it('does not let a probe-triggered success settle a real alert', () => { |
| 320 | + const deliveries = [d('a', 'failed', 'alert'), d('a', 'delivered', 'probe')] |
| 321 | + expect(ids(deliveries)).toEqual(['a']) |
| 322 | + }) |
| 323 | + |
| 324 | + it('handles several alerts at once', () => { |
| 325 | + const deliveries = [ |
| 326 | + d('w', 'failed', 'alert'), |
| 327 | + d('x', 'delivered', 'alert'), |
| 328 | + d('y', 'failed', 'alert'), |
| 329 | + d('y', 'failed', 'resend'), |
| 330 | + d('z', 'failed', 'alert'), |
| 331 | + d('z', 'pending', 'resend'), |
| 332 | + ] |
| 333 | + expect(ids(deliveries)).toEqual(['w', 'y']) |
| 334 | + }) |
| 335 | +}) |
0 commit comments