gpestana / kapacitor-unit

Testing framework for Kapacitor TICKscripts
MIT License
76 stars 19 forks source link

tests need to delete topic between runs #41

Open DreadPirateShawn opened 4 years ago

DreadPirateShawn commented 4 years ago

The following tick + test will work fine:

var weather = batch
    | query('''
        SELECT mean(temperature)
        FROM "weather"."default"."temperature"
        ''')
            .period(5m)
            .every(10m)

    weather
    | alert().id('Temperature')
        .topic('weather')
        .message('Temperature alert - batch')
        .warn(lambda: "mean" > 80)
        .crit(lambda: "mean" > 100)
        .stateChangesOnly()
    .log('/tmp/temperature_batch.tick.log')
tests:

  - name: "Alert weather:: batch"
    task_name: alert_weather_batch.tick
    db: weather
    rp: default
    type: batch
    data:
      - temperature,location=us-midwest temperature=110
      - temperature,location=us-midwest temperature=91
    expects:
      ok: 0
      warn: 0
      crit: 1

However, if a topic is added to the tick:

    | alert().id('Temperature')
        .topic('weather')

...then the test will only work once, and will fail on subsequent runs.

This is due to .stateChangesOnly() combined with the lack of topic state deletion between test runs.

That is to say -- in kapacitor, the "weather" topic tracks the critical state of the task, so even deleting and recreating the task between tests in not sufficient to reset the state.

The problem is confirmed for batch, unconfirmed for stream.

DreadPirateShawn commented 4 years ago

Topic cleanup added in https://github.com/DreadPirateShawn/kapacitor-unit

(With apologies, I've fully forked rather than pushing upstream PRs, since the upstream doesn't really have an active maintainer.)