Skip to content

Commit cbc9f56

Browse files
committed
test: cover grafana
1 parent c577b29 commit cbc9f56

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

test/data/logging.graphml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?><graphml xmlns="http://graphml.graphdrawing.org/xmlns">
2+
<key id="services" attr.name="services" attr.type="string" for="graph" />
3+
<key id="version" attr.name="version" attr.type="string" for="node" />
4+
<key id="image" attr.name="image" attr.type="string" for="node" />
5+
<key id="bitcoin_config" attr.name="bitcoin_config" attr.type="string" for="node" />
6+
<key id="tc_netem" attr.name="tc_netem" attr.type="string" for="node" />
7+
<key id="exporter" attr.name="exporter" attr.type="boolean" for="node" />
8+
<key id="metrics" attr.name="metrics" attr.type="string" for="node" />
9+
<key id="collect_logs" attr.name="collect_logs" attr.type="boolean" for="node" />
10+
<key id="build_args" attr.name="build_args" attr.type="string" for="node" />
11+
<key id="ln" attr.name="ln" attr.type="string" for="node" />
12+
<key id="ln_image" attr.name="ln_image" attr.type="string" for="node" />
13+
<key id="ln_cb_image" attr.name="ln_cb_image" attr.type="string" for="node" />
14+
<key id="ln_config" attr.name="ln_config" attr.type="string" for="node" />
15+
<key id="channel_open" attr.name="channel_open" attr.type="string" for="edge" />
16+
<key id="source_policy" attr.name="source_policy" attr.type="string" for="edge" />
17+
<key id="target_policy" attr.name="target_policy" attr.type="string" for="edge" />
18+
<graph edgedefault="directed">
19+
<node id="0">
20+
<data key="version">27.0</data>
21+
<data key="exporter">true</data>
22+
</node>
23+
<node id="1">
24+
<data key="version">27.0</data>
25+
<data key="exporter">true</data>
26+
<data key="metrics">txrate=getchaintxstats(10)["txrate"]</data>
27+
</node>
28+
<node id="2">
29+
<data key="version">27.0</data>
30+
</node>
31+
<edge id="1" source="0" target="1"></edge>
32+
<edge id="2" source="1" target="2"></edge>
33+
<edge id="3" source="2" target="0"></edge>
34+
</graph>
35+
</graphml>

test/logging_test.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

Comments
 (0)