|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import logging |
| 4 | +import os |
| 5 | +import threading |
| 6 | +from datetime import datetime |
| 7 | +from pathlib import Path |
| 8 | +from subprocess import PIPE, Popen, run |
| 9 | + |
| 10 | +import requests |
| 11 | +from test_base import TestBase |
| 12 | + |
| 13 | + |
| 14 | +class LoggingTest(TestBase): |
| 15 | + def __init__(self): |
| 16 | + super().__init__() |
| 17 | + self.graph_file_path = Path(os.path.dirname(__file__)) / "data" / "logging.graphml" |
| 18 | + self.scripts_dir = Path(os.path.dirname(__file__)) / ".." / "resources" / "scripts" |
| 19 | + self.connect_logging_process = None |
| 20 | + self.connect_logging_thread = None |
| 21 | + self.connect_logging_logger = logging.getLogger("cnct_log") |
| 22 | + |
| 23 | + def run_test(self): |
| 24 | + self.start_server() |
| 25 | + try: |
| 26 | + self.start_logging() |
| 27 | + self.setup_network() |
| 28 | + self.test_prometheus_and_grafana() |
| 29 | + finally: |
| 30 | + if self.connect_logging_process is not None: |
| 31 | + self.log.info("Terminating background connect_logging.sh process...") |
| 32 | + self.connect_logging_process.terminate() |
| 33 | + self.stop_server() |
| 34 | + |
| 35 | + def start_logging(self): |
| 36 | + self.log.info("Running install_logging.sh") |
| 37 | + # Block until complete |
| 38 | + run([f"{self.scripts_dir / 'install_logging.sh'}"]) |
| 39 | + self.log.info("Running connect_logging.sh") |
| 40 | + # Stays alive in background |
| 41 | + self.connect_logging_process = Popen( |
| 42 | + [f"{self.scripts_dir / 'connect_logging.sh'}"], |
| 43 | + stdout=PIPE, |
| 44 | + stderr=PIPE, |
| 45 | + bufsize=1, |
| 46 | + universal_newlines=True, |
| 47 | + ) |
| 48 | + self.log.info("connect_logging.sh started...") |
| 49 | + self.connect_logging_thread = threading.Thread( |
| 50 | + target=self.output_reader, |
| 51 | + args=(self.connect_logging_process.stdout, self.connect_logging_logger.info), |
| 52 | + ) |
| 53 | + self.connect_logging_thread.daemon = True |
| 54 | + self.connect_logging_thread.start() |
| 55 | + |
| 56 | + self.log.info("Waiting for RPC") |
| 57 | + self.wait_for_rpc("scenarios_available") |
| 58 | + |
| 59 | + def setup_network(self): |
| 60 | + self.log.info("Setting up network") |
| 61 | + self.log.info(self.warcli(f"network start {self.graph_file_path}")) |
| 62 | + self.wait_for_all_tanks_status(target="running", timeout=10 * 60) |
| 63 | + self.wait_for_all_edges() |
| 64 | + |
| 65 | + def make_grafana_api_request(self, ds_uid, start, metric): |
| 66 | + self.log.info("Making Grafana request...") |
| 67 | + data = { |
| 68 | + "queries": [{"expr": metric, "datasource": {"type": "prometheus", "uid": ds_uid}}], |
| 69 | + "from": f"{start}", |
| 70 | + "to": "now", |
| 71 | + } |
| 72 | + reply = requests.post("http://localhost:3000/api/ds/query", json=data) |
| 73 | + assert reply.status_code == 200 |
| 74 | + |
| 75 | + # Default ref ID is "A", only inspecting one "frame" |
| 76 | + return reply.json()["results"]["A"]["frames"][0]["data"]["values"] |
| 77 | + |
| 78 | + def test_prometheus_and_grafana(self): |
| 79 | + self.log.info("Starting network activity scenarios") |
| 80 | + self.warcli("scenarios run miner_std --allnodes --interval=5 --mature") |
| 81 | + self.warcli("scenarios run tx_flood --interval=1") |
| 82 | + |
| 83 | + prometheus_ds = requests.get("http://localhost:3000/api/datasources/name/Prometheus") |
| 84 | + assert prometheus_ds.status_code == 200 |
| 85 | + prometheus_uid = prometheus_ds.json()["uid"] |
| 86 | + self.log.info(f"Got Prometheus data source uid from Grafana: {prometheus_uid}") |
| 87 | + |
| 88 | + start = int(datetime.now().timestamp() * 1000) |
| 89 | + |
| 90 | + def get_five_values_for_metric(metric): |
| 91 | + data = self.make_grafana_api_request(prometheus_uid, start, metric) |
| 92 | + if len(data) < 1: |
| 93 | + self.log.info(f"No Grafana data yet for {metric}") |
| 94 | + return False |
| 95 | + timestamps = data[0] |
| 96 | + values = data[1] |
| 97 | + self.log.info(f"Grafana data: {metric} times: {timestamps}") |
| 98 | + self.log.info(f"Grafana data: {metric} values: {values}") |
| 99 | + return len(values) > 5 |
| 100 | + |
| 101 | + self.wait_for_predicate(lambda: get_five_values_for_metric("blocks")) |
| 102 | + self.wait_for_predicate(lambda: get_five_values_for_metric("txrate")) |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + test = LoggingTest() |
| 107 | + test.run_test() |
0 commit comments