|
13 | 13 |
|
14 | 14 | @pytest_twisted.inlineCallbacks |
15 | 15 | @pytest.mark.grafana |
16 | | -def test_mqtt_to_grafana(machinery, create_influxdb, reset_influxdb, reset_grafana): |
| 16 | +def test_mqtt_to_grafana_single(machinery, create_influxdb, reset_influxdb, reset_grafana): |
17 | 17 | """ |
18 | 18 | Publish single reading in JSON format to MQTT broker and proof |
19 | 19 | that a corresponding datasource and a dashboard was created in Grafana. |
@@ -44,3 +44,55 @@ def test_mqtt_to_grafana(machinery, create_influxdb, reset_influxdb, reset_grafa |
44 | 44 | target = dashboard['dashboard']['rows'][0]['panels'][0]['targets'][0] |
45 | 45 | assert target['measurement'] == settings.influx_measurement_sensors |
46 | 46 | assert 'temperature' in target['query'] or 'humidity' in target['query'] |
| 47 | + |
| 48 | + |
| 49 | +@pytest_twisted.inlineCallbacks |
| 50 | +@pytest.mark.grafana |
| 51 | +def test_mqtt_to_grafana_bulk(machinery, create_influxdb, reset_influxdb, reset_grafana): |
| 52 | + """ |
| 53 | + Publish multiple readings in JSON format to MQTT broker and proof |
| 54 | + that a corresponding datasource and a dashboard was created in Grafana. |
| 55 | + """ |
| 56 | + |
| 57 | + # Submit multiple measurements, without timestamp. |
| 58 | + data = [ |
| 59 | + { |
| 60 | + 'temperature': 21.42, |
| 61 | + 'humidity': 41.55, |
| 62 | + }, |
| 63 | + { |
| 64 | + 'temperature': 42.84, |
| 65 | + 'humidity': 83.1, |
| 66 | + 'voltage': 4.2, |
| 67 | + }, |
| 68 | + { |
| 69 | + 'weight': 10.10, |
| 70 | + }, |
| 71 | + ] |
| 72 | + yield mqtt_json_sensor(settings.mqtt_topic_json, data) |
| 73 | + |
| 74 | + # Wait for some time to process the message. |
| 75 | + yield sleep(PROCESS_DELAY) |
| 76 | + yield sleep(PROCESS_DELAY) |
| 77 | + yield sleep(PROCESS_DELAY) |
| 78 | + |
| 79 | + # Proof that Grafana is well provisioned. |
| 80 | + logger.info('Grafana: Checking datasource') |
| 81 | + datasource_names = [] |
| 82 | + for datasource in grafana.client.datasources.get(): |
| 83 | + datasource_names.append(datasource['name']) |
| 84 | + assert settings.influx_database in datasource_names |
| 85 | + |
| 86 | + logger.info('Grafana: Checking dashboard') |
| 87 | + dashboard_name = settings.grafana_dashboards[0] |
| 88 | + dashboard = grafana.client.dashboards.db[dashboard_name].get() |
| 89 | + targets = dashboard['dashboard']['rows'][0]['panels'][0]['targets'] |
| 90 | + |
| 91 | + # Validate table name. |
| 92 | + assert targets[0]['measurement'] == settings.influx_measurement_sensors |
| 93 | + |
| 94 | + # Validate field names. |
| 95 | + fields = set() |
| 96 | + for target in targets: |
| 97 | + fields.add(target["fields"][0]["name"]) |
| 98 | + assert fields == set(["temperature", "humidity", "weight", "voltage"]) |
0 commit comments