"""Integration: the Disk page's hosts side by side with their growth (#5304)
and the overview's disk line (#5306), from the totals ingest records as
metrics. Requires STEWARD_DATABASE_URL."""
from __future__ import annotations
import asyncio
import importlib
import os
import time
import uuid
from datetime import datetime, timedelta, timezone
import pytest
pytestmark = pytest.mark.integration
@pytest.fixture
def app():
if not os.environ.get("STEWARD_DATABASE_URL"):
pytest.skip("needs Postgres")
from steward.app import create_app
return create_app(testing=False)
def _docker(sub):
"""The app's loaded docker plugin module (see test_docker_recent_usage)."""
from steward.core.plugin_manager import get_loaded_plugin_modules
loaded = get_loaded_plugin_modules().get("docker")
base = loaded.__name__ if loaded is not None else "plugins.docker"
return importlib.import_module(f"{base}.{sub}")
def test_the_disk_page_says_how_much_each_host_grew(app):
from steward.models.hosts import Host
from steward.models.metrics import PluginMetric
from steward.models.users import User, UserRole
m = _docker("models")
disk_hosts = _docker("service").disk_hosts
tag = uuid.uuid4().hex[:8]
hid, uid = str(uuid.uuid4()), str(uuid.uuid4())
name = f"disk-{tag}"
now = datetime.now(timezone.utc)
gib = 1 << 30
async def go():
async with app.db_sessionmaker() as s:
async with s.begin():
s.add(User(id=uid, username=f"dp-{tag}", email=f"dp-{tag}@test",
password_hash="x", role=UserRole.viewer, is_active=True))
s.add(Host(id=hid, name=name, address="10.9.6.1"))
async with s.begin():
s.add(m.DockerDiskUsage(host_id=hid, images_size=30 * gib, containers_size=2 * gib,
volumes_size=10 * gib, build_cache_size=0, images_reclaimable=5 * gib,
images_total=12, images_active=8, updated_at=now))
s.add(m.DockerImage(host_id=hid, image_id="a" * 12, repo_tag=f"ghcr.io/x/app{tag}:1",
size=gib, shared_size=0, containers=1, updated_at=now))
s.add(m.DockerImage(host_id=hid, image_id="b" * 12, repo_tag=f"ghcr.io/x/app{tag}:0",
size=gib, shared_size=0, containers=0, updated_at=now))
s.add_all([PluginMetric(source_module="docker", resource_name=name, metric_name="disk_total_bytes",
value=float(v * gib), recorded_at=now - timedelta(days=d))
for d, v in ((6, 40), (3, 41), (0, 42))])
async with s.begin():
rows = await disk_hosts(s, now - timedelta(days=7))
client = app.test_client()
async with client.session_transaction() as sess:
sess.update(user_id=uid, user_role="viewer", username=f"dp-{tag}", login_at=time.time())
frag = await (await client.get("/plugins/docker/disk/hosts?range=7d")).get_data(as_text=True)
page = await (await client.get("/plugins/docker/disk")).get_data(as_text=True)
index = await (await client.get("/plugins/docker/")).get_data(as_text=True)
async with app.db_sessionmaker() as s:
async with s.begin():
await s.delete(await s.get(Host, hid))
await s.delete(await s.get(User, uid))
return rows, frag, page, index
rows, frag, page, index = asyncio.run(go())
(row,) = [r for r in rows if r["host_id"] == hid]
assert row["total"] == 42 * gib and row["reclaimable"] == 5 * gib
assert row["growth"]["change"] == 2 * gib and row["growth"]["from"] == 40 * gib
assert f'{name}' in frag and "+2.0 GiB" in frag and "{name}' in index and "+2.0 GiB in 7d" in index