Skip to content

Commit

Permalink
feat: add prometheus basic unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
aimxhaisse committed Jul 22, 2024
1 parent 8ebd42b commit 26516dd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
2 changes: 1 addition & 1 deletion tests/assets/config.sepolia.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
beacon_url: http://localhost:3500/
beacon_timeout_sec: 90
network: sepolia
metrics_port: 4242
metrics_port: 8000

# We have data for:
#
Expand Down
66 changes: 49 additions & 17 deletions tests/test_sepolia.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import requests
import vcr

from pathlib import Path
Expand All @@ -17,30 +18,61 @@ class SepoliaTestCase(VCRTestCase):
- slot 5493884 (timestamp=1721660208)
- slot 6356780 (timestamp=1721661324)
"""

def get_metrics(self):
url = 'http://localhost:8000/metrics'
response = requests.get(url)
self.assertEqual(response.status_code, 200)
result = {}
for line in response.text.split('\n'):
line = line.strip()
if line and not line.startswith('#'):
key, value = line.split(' ')
result[key] = float(value)
return result

def slot_5493884(self):
pass
metrics = self.get_metrics()

self.assertEqual(metrics['eth_slot{network="sepolia"}'], 5493884.0)
self.assertEqual(metrics['eth_epoch{network="sepolia"}'], 171683.0)

def slot_5493887(self):
metrics = self.get_metrics()

self.assertEqual(metrics['eth_slot{network="sepolia"}'], 5493887.0)
self.assertEqual(metrics['eth_epoch{network="sepolia"}'], 171683.0)

@vcr.use_cassette('tests/assets/cassettes/test_sepolia.yaml')
def test_sepolia(self):
"""Main entrypoint for entire Sepolia unit test.
"""
self.watcher = ValidatorWatcher(
Path(assets.__file__).parent / "config.sepolia.yaml"
)

callbacks = {
5493884: self.slot_5493884
}
def ignore_metrics_cb(request):
if request.uri == 'http://localhost:8000/metrics':
return None
return request

v = vcr.VCR(before_record=ignore_metrics_cb)

with v.use_cassette('tests/assets/cassettes/test_sepolia.yaml'):
self.watcher = ValidatorWatcher(
Path(assets.__file__).parent / "config.sepolia.yaml"
)

callbacks = {
5493884: self.slot_5493884,
5493887: self.slot_5493887,
}

called = {}
called = {}

def slot_hook(slot: int):
if slot in callbacks:
callbacks[slot]()
called[slot] = True
def slot_hook(slot: int):
if slot in callbacks:
callbacks[slot]()
called[slot] = True

self.watcher._slot_hook = slot_hook
self.watcher.run()
self.watcher._slot_hook = slot_hook
self.watcher.run()

for slot in callbacks.keys():
self.assertTrue(called.get(slot))
for slot in callbacks.keys():
self.assertTrue(called.get(slot))

0 comments on commit 26516dd

Please sign in to comment.