| 1 |
import argparse |
| 2 |
import datetime |
| 3 |
import json |
| 4 |
import sys |
| 5 |
import time |
| 6 |
from typing import AsyncGenerator |
| 7 |
|
| 8 |
import pytest |
| 9 |
import pytest_asyncio |
| 10 |
|
| 11 |
# Skip import when PCP is not available (for example in our tox env without system packages) |
| 12 |
try: |
| 13 |
from cpmapi import ( |
| 14 |
PM_ID_NULL, |
| 15 |
PM_INDOM_NULL, |
| 16 |
PM_SEM_COUNTER, |
| 17 |
PM_SEM_DISCRETE, |
| 18 |
PM_SEM_INSTANT, |
| 19 |
PM_TYPE_64, |
| 20 |
PM_TYPE_DOUBLE, |
| 21 |
PM_TYPE_STRING, |
| 22 |
PM_TYPE_U32, |
| 23 |
PM_TYPE_U64, |
| 24 |
) |
| 25 |
from pcp import pmi |
| 26 |
|
| 27 |
# This comes from inside of the PCP code. It's fixed upstream but not yet |
| 28 |
# released. See https://github.com/performancecopilot/pcp/pull/2330 |
| 29 |
pytestmark = pytest.mark.filterwarnings( |
| 30 |
r"ignore:datetime\.datetime\.utcfromtimestamp\(\) is deprecated:DeprecationWarning" |
| 31 |
) |
| 32 |
|
| 33 |
except ImportError: |
| 34 |
pytestmark = pytest.mark.skip("PCP not available") |
| 35 |
|
| 36 |
from cockpit.bridge import Bridge |
| 37 |
|
| 38 |
from .mocktransport import MockTransport |
| 39 |
|
| 40 |
|
| 41 |
@pytest_asyncio.fixture |
| 42 |
async def no_init_transport() -> AsyncGenerator[MockTransport, None]: |
| 43 |
bridge = Bridge(argparse.Namespace(privileged=False, beipack=False)) |
| 44 |
transport = MockTransport(bridge) |
| 45 |
try: |
| 46 |
yield transport |
| 47 |
finally: |
| 48 |
await transport.stop() |
| 49 |
|
| 50 |
|
| 51 |
@pytest.fixture |
| 52 |
def transport(no_init_transport: MockTransport) -> MockTransport: |
| 53 |
no_init_transport.init() |
| 54 |
return no_init_transport |
| 55 |
|
| 56 |
|
| 57 |
@pytest.fixture |
| 58 |
def broken_archive(tmpdir_factory): |
| 59 |
pcp_dir = tmpdir_factory.mktemp('mock-archives') |
| 60 |
|
| 61 |
with open(pcp_dir / '0.index', 'w') as f: |
| 62 |
f.write("not a pcp index file") |
| 63 |
with open(pcp_dir / '0.meta', 'w') as f: |
| 64 |
f.write("not a pcp meta file") |
| 65 |
with open(pcp_dir / '0.0', 'w') as f: |
| 66 |
f.write("not a pcp sample file") |
| 67 |
|
| 68 |
return pcp_dir |
| 69 |
|
| 70 |
|
| 71 |
@pytest.fixture |
| 72 |
def discrete_metric_archive(tmpdir_factory): |
| 73 |
pcp_dir = tmpdir_factory.mktemp('discrete-archive') |
| 74 |
archive_1 = pmi.pmiLogImport(f"{pcp_dir}/0") |
| 75 |
archive_1.pmiAddMetric("mock.value", PM_ID_NULL, PM_TYPE_64, PM_INDOM_NULL, |
| 76 |
PM_SEM_DISCRETE, archive_1.pmiUnits(0, 0, 0, 0, 0, 0)) |
| 77 |
|
| 78 |
archive_1.pmiPutValue("mock.value", None, "4") |
| 79 |
archive_1.pmiWrite(0, 0) |
| 80 |
|
| 81 |
archive_1.pmiEnd() |
| 82 |
|
| 83 |
return pcp_dir |
| 84 |
|
| 85 |
|
| 86 |
@pytest.fixture |
| 87 |
def big_archive(tmpdir_factory): |
| 88 |
pcp_dir = tmpdir_factory.mktemp('big-archive') |
| 89 |
archive_1 = pmi.pmiLogImport(f"{pcp_dir}/0") |
| 90 |
archive_1.pmiAddMetric("mock.value", PM_ID_NULL, PM_TYPE_64, PM_INDOM_NULL, |
| 91 |
PM_SEM_INSTANT, archive_1.pmiUnits(0, 0, 0, 0, 0, 0)) |
| 92 |
for i in range(1000): |
| 93 |
archive_1.pmiPutValue("mock.value", None, str(i)) |
| 94 |
archive_1.pmiWrite(i, 0) |
| 95 |
|
| 96 |
archive_1.pmiEnd() |
| 97 |
|
| 98 |
return pcp_dir |
| 99 |
|
| 100 |
|
| 101 |
@pytest.fixture |
| 102 |
def multi_file_archive(tmpdir_factory): |
| 103 |
pcp_dir = tmpdir_factory.mktemp('mock-archives') |
| 104 |
archive_1 = pmi.pmiLogImport(f"{pcp_dir}/0") |
| 105 |
archive_1.pmiAddMetric("mock.value", PM_ID_NULL, PM_TYPE_U32, PM_INDOM_NULL, |
| 106 |
PM_SEM_INSTANT, archive_1.pmiUnits(0, 0, 0, 0, 0, 0)) |
| 107 |
archive_1.pmiPutValue("mock.value", None, "10") |
| 108 |
archive_1.pmiWrite(0, 0) |
| 109 |
archive_1.pmiPutValue("mock.value", None, "11") |
| 110 |
archive_1.pmiWrite(1, 0) |
| 111 |
archive_1.pmiPutValue("mock.value", None, "12") |
| 112 |
archive_1.pmiWrite(2, 0) |
| 113 |
archive_1.pmiEnd() |
| 114 |
|
| 115 |
archive_2 = pmi.pmiLogImport(f"{pcp_dir}/1") |
| 116 |
archive_2.pmiAddMetric("mock.value", PM_ID_NULL, PM_TYPE_U32, |
| 117 |
PM_INDOM_NULL, PM_SEM_INSTANT, |
| 118 |
archive_2.pmiUnits(0, 0, 0, 0, 0, 0)) |
| 119 |
archive_2.pmiAddMetric("mock.late", PM_ID_NULL, PM_TYPE_U32, PM_INDOM_NULL, |
| 120 |
PM_SEM_INSTANT, archive_2.pmiUnits(0, 0, 0, 0, 0, 0)) |
| 121 |
archive_2.pmiPutValue("mock.value", None, "13") |
| 122 |
archive_2.pmiPutValue("mock.late", None, "30") |
| 123 |
archive_2.pmiWrite(3, 0) |
| 124 |
archive_2.pmiPutValue("mock.value", None, "14") |
| 125 |
archive_2.pmiPutValue("mock.late", None, "31") |
| 126 |
archive_2.pmiWrite(4, 0) |
| 127 |
archive_2.pmiPutValue("mock.value", None, "15") |
| 128 |
archive_2.pmiPutValue("mock.late", None, "32") |
| 129 |
archive_2.pmiWrite(5, 0) |
| 130 |
archive_2.pmiEnd() |
| 131 |
|
| 132 |
return pcp_dir |
| 133 |
|
| 134 |
|
| 135 |
@pytest.fixture |
| 136 |
def timestamps_archive(tmpdir_factory): |
| 137 |
pcp_dir = tmpdir_factory.mktemp('timestamps-archives') |
| 138 |
archive_1 = pmi.pmiLogImport(f"{pcp_dir}/0") |
| 139 |
|
| 140 |
archive_1.pmiAddMetric("mock.value", PM_ID_NULL, PM_TYPE_U32, PM_INDOM_NULL, |
| 141 |
PM_SEM_INSTANT, archive_1.pmiUnits(0, 0, 0, 0, 0, 0)) |
| 142 |
|
| 143 |
timestamp = int(datetime.datetime.fromisoformat('2023-01-01').timestamp()) |
| 144 |
archive_1.pmiPutValue("mock.value", None, "10") |
| 145 |
archive_1.pmiWrite(timestamp, 0) |
| 146 |
|
| 147 |
timestamp = int(datetime.datetime.fromisoformat('2023-06-01').timestamp()) |
| 148 |
archive_1.pmiPutValue("mock.value", None, "11") |
| 149 |
archive_1.pmiWrite(timestamp, 0) |
| 150 |
|
| 151 |
timestamp = int(datetime.datetime.fromisoformat('2023-12-01').timestamp()) |
| 152 |
archive_1.pmiPutValue("mock.value", None, "12") |
| 153 |
archive_1.pmiWrite(timestamp, 0) |
| 154 |
|
| 155 |
archive_1.pmiEnd() |
| 156 |
|
| 157 |
return pcp_dir |
| 158 |
|
| 159 |
|
| 160 |
@pytest.fixture |
| 161 |
def instances_archive(tmpdir_factory): |
| 162 |
pcp_dir = tmpdir_factory.mktemp('instances-archives') |
| 163 |
archive_1 = pmi.pmiLogImport(f"{pcp_dir}/0") |
| 164 |
|
| 165 |
domain = 60 # Linux kernel |
| 166 |
pmid = archive_1.pmiID(domain, 2, 0) |
| 167 |
indom = archive_1.pmiInDom(domain, 2) |
| 168 |
units = archive_1.pmiUnits(0, 0, 0, 0, 0, 0) |
| 169 |
|
| 170 |
archive_1.pmiAddMetric("kernel.all.load", pmid, PM_TYPE_DOUBLE, indom, |
| 171 |
PM_SEM_INSTANT, units) |
| 172 |
archive_1.pmiAddInstance(indom, "1 minute", 1) |
| 173 |
archive_1.pmiAddInstance(indom, "5 minute", 5) |
| 174 |
archive_1.pmiAddInstance(indom, "15 minute", 15) |
| 175 |
|
| 176 |
archive_1.pmiPutValue("kernel.all.load", "1 minute", "1.0") |
| 177 |
archive_1.pmiPutValue("kernel.all.load", "5 minute", "5.0") |
| 178 |
archive_1.pmiPutValue("kernel.all.load", "15 minute", "15.0") |
| 179 |
archive_1.pmiWrite(0, 0) |
| 180 |
|
| 181 |
archive_1.pmiEnd() |
| 182 |
|
| 183 |
return pcp_dir |
| 184 |
|
| 185 |
|
| 186 |
@pytest.fixture |
| 187 |
def instances_change_archive(tmpdir_factory): |
| 188 |
pcp_dir = tmpdir_factory.mktemp('instances-change-archives') |
| 189 |
archive_1 = pmi.pmiLogImport(f"{pcp_dir}/0") |
| 190 |
|
| 191 |
domain = 60 # Linux kernel |
| 192 |
pmid = archive_1.pmiID(domain, 2, 0) |
| 193 |
indom = archive_1.pmiInDom(domain, 2) |
| 194 |
units = archive_1.pmiUnits(0, 0, 0, 0, 0, 0) |
| 195 |
|
| 196 |
archive_1.pmiAddMetric("network.interface.total.bytes", pmid, PM_TYPE_U32, indom, |
| 197 |
PM_SEM_COUNTER, units) |
| 198 |
archive_1.pmiAddInstance(indom, "lo", 1) |
| 199 |
archive_1.pmiAddInstance(indom, "eth0", 2) |
| 200 |
archive_1.pmiAddInstance(indom, "eth1", 3) |
| 201 |
|
| 202 |
archive_1.pmiPutValue("network.interface.total.bytes", "lo", "1") |
| 203 |
archive_1.pmiPutValue("network.interface.total.bytes", "eth0", "0") |
| 204 |
archive_1.pmiPutValue("network.interface.total.bytes", "eth1", "1") |
| 205 |
archive_1.pmiWrite(1597663539413, 0) |
| 206 |
|
| 207 |
archive_1.pmiPutValue("network.interface.total.bytes", "lo", "2") |
| 208 |
archive_1.pmiPutValue("network.interface.total.bytes", "eth0", "1") |
| 209 |
archive_1.pmiWrite(1597663539483, 0) |
| 210 |
|
| 211 |
archive_1.pmiEnd() |
| 212 |
|
| 213 |
return pcp_dir |
| 214 |
|
| 215 |
|
| 216 |
@pytest.fixture |
| 217 |
def instances_rate_archive(tmpdir_factory): |
| 218 |
pcp_dir = tmpdir_factory.mktemp('instances-rate-archives') |
| 219 |
archive_1 = pmi.pmiLogImport(f"{pcp_dir}/0") |
| 220 |
|
| 221 |
domain = 60 # Linux kernel |
| 222 |
pmid = archive_1.pmiID(domain, 2, 0) |
| 223 |
indom = archive_1.pmiInDom(domain, 2) |
| 224 |
units = archive_1.pmiUnits(0, 0, 0, 0, 0, 0) |
| 225 |
|
| 226 |
archive_1.pmiAddMetric("disk.value", pmid, PM_TYPE_U32, indom, |
| 227 |
PM_SEM_INSTANT, units) |
| 228 |
archive_1.pmiAddInstance(indom, "sda", 1) |
| 229 |
archive_1.pmiAddInstance(indom, "sdb", 2) |
| 230 |
archive_1.pmiAddInstance(indom, "sdc", 3) |
| 231 |
|
| 232 |
archive_1.pmiPutValue("disk.value", "sda", "1") |
| 233 |
archive_1.pmiPutValue("disk.value", "sdb", "1") |
| 234 |
archive_1.pmiPutValue("disk.value", "sdc", "1") |
| 235 |
archive_1.pmiWrite(0, 0) |
| 236 |
|
| 237 |
archive_1.pmiPutValue("disk.value", "sda", "1") |
| 238 |
archive_1.pmiPutValue("disk.value", "sdb", "1") |
| 239 |
archive_1.pmiPutValue("disk.value", "sdc", "2") |
| 240 |
archive_1.pmiWrite(1, 0) |
| 241 |
|
| 242 |
archive_1.pmiPutValue("disk.value", "sda", "1") |
| 243 |
archive_1.pmiPutValue("disk.value", "sdb", "10") |
| 244 |
archive_1.pmiPutValue("disk.value", "sdc", "3") |
| 245 |
archive_1.pmiWrite(2, 0) |
| 246 |
|
| 247 |
archive_1.pmiEnd() |
| 248 |
|
| 249 |
return pcp_dir |
| 250 |
|
| 251 |
|
| 252 |
@pytest.fixture |
| 253 |
def unsupported_metric_archive(tmpdir_factory): |
| 254 |
"""Cockpit only support float/integer type metrics""" |
| 255 |
|
| 256 |
pcp_dir = tmpdir_factory.mktemp('unsupported-metric-archives') |
| 257 |
archive_1 = pmi.pmiLogImport(f"{pcp_dir}/0") |
| 258 |
archive_1.pmiAddMetric("mock.value", PM_ID_NULL, PM_TYPE_STRING, PM_INDOM_NULL, |
| 259 |
PM_SEM_INSTANT, archive_1.pmiUnits(0, 0, 0, 0, 0, 0)) |
| 260 |
|
| 261 |
archive_1.pmiPutValue("mock.value", None, b"10") |
| 262 |
archive_1.pmiWrite(0, 0) |
| 263 |
archive_1.pmiEnd() |
| 264 |
|
| 265 |
return pcp_dir |
| 266 |
|
| 267 |
|
| 268 |
@pytest.fixture |
| 269 |
def mem_avail_archive(tmpdir_factory): |
| 270 |
pcp_dir = tmpdir_factory.mktemp('mem-avail-archives') |
| 271 |
archive_1 = pmi.pmiLogImport(f"{pcp_dir}/0") |
| 272 |
|
| 273 |
# https://github.com/performancecopilot/pcp/blob/766a78e631998e97196eeed9cc36631f30add74b/src/collectl2pcp/metrics.c#L339 |
| 274 |
# pminfo -m -f "mem.util.available" |
| 275 |
domain = 60 # Linux kernel |
| 276 |
pmid = archive_1.pmiID(domain, 1, 58) |
| 277 |
units = archive_1.pmiUnits(1, 0, 0, 1, 0, 0) |
| 278 |
|
| 279 |
archive_1.pmiAddMetric("mem.util.available", pmid, PM_TYPE_U64, PM_INDOM_NULL, |
| 280 |
PM_SEM_INSTANT, units) |
| 281 |
|
| 282 |
archive_1.pmiPutValue("mem.util.available", None, "19362828") |
| 283 |
archive_1.pmiWrite(0, 0) |
| 284 |
|
| 285 |
archive_1.pmiEnd() |
| 286 |
|
| 287 |
return pcp_dir |
| 288 |
|
| 289 |
|
| 290 |
def assert_metrics_meta(meta, source, timestamp=0, interval=1000): |
| 291 |
assert meta['timestamp'] == timestamp |
| 292 |
assert meta['interval'] == interval |
| 293 |
assert meta['source'] == source |
| 294 |
|
| 295 |
|
| 296 |
@pytest.mark.asyncio |
| 297 |
async def test_pcp_open_error(transport, multi_file_archive): |
| 298 |
await transport.check_open('metrics1', source=str(multi_file_archive), interval=-10, problem='protocol-error', |
| 299 |
reply_keys={'message': 'invalid "interval" value: -10'}) |
| 300 |
await transport.check_open('metrics1', problem='protocol-error', |
| 301 |
reply_keys={'message': 'no "source" option specified for metrics channel'}) |
| 302 |
await transport.check_open('metrics1', source="bazinga", problem='not-supported', |
| 303 |
reply_keys={'message': 'unsupported "source" option specified for metrics: bazinga'}) |
| 304 |
await transport.check_open('metrics1', source="/non-existent", problem='not-found') |
| 305 |
await transport.check_open('metrics1', source=str(multi_file_archive), |
| 306 |
metrics=[{"name": ""}], |
| 307 |
problem='protocol-error', |
| 308 |
reply_keys={'message': 'invalid "metrics" option was specified (no name for metric)'}) |
| 309 |
await transport.check_open('metrics1', source=str(multi_file_archive), limit=-10, problem='protocol-error', |
| 310 |
reply_keys={'message': 'invalid "limit" value: -10'}) |
| 311 |
invalid_timestamp_size = sys.maxsize * 1000 + 3 |
| 312 |
await transport.check_open('metrics1', source=str(multi_file_archive), timestamp=invalid_timestamp_size, |
| 313 |
problem='protocol-error', |
| 314 |
reply_keys={'message': f'invalid "timestamp" value: {invalid_timestamp_size}'}) |
| 315 |
await transport.check_open('metrics1', problem='protocol-error', |
| 316 |
reply_keys={'message': 'no "source" option specified for metrics channel'}) |
| 317 |
|
| 318 |
|
| 319 |
@pytest.mark.asyncio |
| 320 |
async def test_pcp_open(transport, multi_file_archive): |
| 321 |
await transport.check_open('metrics1', source=str(multi_file_archive), |
| 322 |
metrics=[{"name": "mock.value"}]) |
| 323 |
|
| 324 |
_, data = await transport.next_frame() |
| 325 |
# first message is always the meta message |
| 326 |
meta = json.loads(data) |
| 327 |
assert_metrics_meta(meta, str(multi_file_archive)) |
| 328 |
|
| 329 |
metrics = meta['metrics'] |
| 330 |
assert len(metrics) == 1 |
| 331 |
|
| 332 |
metric = metrics[0] |
| 333 |
assert metric['name'] == 'mock.value' |
| 334 |
assert 'derive' not in metric |
| 335 |
assert metric['semantics'] == 'instant' |
| 336 |
|
| 337 |
# assert_sample (tc, "[[10],[11],[12]]"); |
| 338 |
_, data = await transport.next_frame() |
| 339 |
data = json.loads(data) |
| 340 |
assert data == [[10], [11], [12]] |
| 341 |
|
| 342 |
_, data = await transport.next_frame() |
| 343 |
# first message is always the meta message |
| 344 |
meta = json.loads(data) |
| 345 |
|
| 346 |
metrics = meta['metrics'] |
| 347 |
assert len(metrics) == 1 |
| 348 |
|
| 349 |
metric = metrics[0] |
| 350 |
assert metric['name'] == 'mock.value' |
| 351 |
assert 'derive' not in metric |
| 352 |
assert metric['semantics'] == 'instant' |
| 353 |
|
| 354 |
_, data = await transport.next_frame() |
| 355 |
data = json.loads(data) |
| 356 |
assert data == [[13], [14], [15]] |
| 357 |
|
| 358 |
|
| 359 |
@pytest.mark.asyncio |
| 360 |
async def test_pcp_big_archive(transport, big_archive): |
| 361 |
await transport.check_open('metrics1', source=str(big_archive), |
| 362 |
metrics=[{"name": "mock.value"}]) |
| 363 |
|
| 364 |
_, data = await transport.next_frame() |
| 365 |
# first message is always the meta message |
| 366 |
meta = json.loads(data) |
| 367 |
|
| 368 |
assert_metrics_meta(meta, str(big_archive)) |
| 369 |
metrics = meta['metrics'] |
| 370 |
assert len(metrics) == 1 |
| 371 |
|
| 372 |
metric = metrics[0] |
| 373 |
assert metric['name'] == 'mock.value' |
| 374 |
assert 'derive' not in metric |
| 375 |
assert metric['semantics'] == 'instant' |
| 376 |
|
| 377 |
_, data = await transport.next_frame() |
| 378 |
data = json.loads(data) |
| 379 |
# archives batch size is hardcoded to 60 |
| 380 |
assert data == [[i] for i in range(60)] |
| 381 |
|
| 382 |
|
| 383 |
@pytest.mark.asyncio |
| 384 |
async def test_pcp_instances_option(transport, instances_archive): |
| 385 |
await transport.check_open('metrics1', source=str(instances_archive), |
| 386 |
metrics=[{"name": "kernel.all.load"}], instances=["1 minute"]) |
| 387 |
|
| 388 |
_, data = await transport.next_frame() |
| 389 |
# first message is always the meta message |
| 390 |
meta = json.loads(data) |
| 391 |
|
| 392 |
assert_metrics_meta(meta, str(instances_archive)) |
| 393 |
|
| 394 |
metrics = meta['metrics'] |
| 395 |
assert len(metrics) == 1 |
| 396 |
|
| 397 |
metric = metrics[0] |
| 398 |
assert metric['name'] == 'kernel.all.load' |
| 399 |
assert 'derive' not in metric |
| 400 |
assert metric['semantics'] == 'instant' |
| 401 |
assert metric['instances'] == ['1 minute'] |
| 402 |
|
| 403 |
_, data = await transport.next_frame() |
| 404 |
data = json.loads(data) |
| 405 |
assert data == [[[1.0]]] |
| 406 |
|
| 407 |
|
| 408 |
@pytest.mark.asyncio |
| 409 |
async def test_pcp_omit_instances_option(transport, instances_archive): |
| 410 |
await transport.check_open('metrics1', source=str(instances_archive), |
| 411 |
metrics=[{"name": "kernel.all.load"}], omit_instances=["1 minute"]) |
| 412 |
|
| 413 |
_, data = await transport.next_frame() |
| 414 |
# first message is always the meta message |
| 415 |
meta = json.loads(data) |
| 416 |
|
| 417 |
assert_metrics_meta(meta, str(instances_archive)) |
| 418 |
|
| 419 |
metrics = meta['metrics'] |
| 420 |
assert len(metrics) == 1 |
| 421 |
|
| 422 |
metric = metrics[0] |
| 423 |
assert metric['name'] == 'kernel.all.load' |
| 424 |
assert 'derive' not in metric |
| 425 |
assert metric['semantics'] == 'instant' |
| 426 |
assert metric['instances'] == ['15 minute', '5 minute'] |
| 427 |
|
| 428 |
_, data = await transport.next_frame() |
| 429 |
data = json.loads(data) |
| 430 |
assert data == [[[15.0, 5.0]]] |
| 431 |
|
| 432 |
|
| 433 |
@pytest.mark.asyncio |
| 434 |
async def test_pcp_instances(transport, instances_archive): |
| 435 |
await transport.check_open('metrics1', source=str(instances_archive), |
| 436 |
metrics=[{"name": "kernel.all.load"}]) |
| 437 |
|
| 438 |
_, data = await transport.next_frame() |
| 439 |
# first message is always the meta message |
| 440 |
meta = json.loads(data) |
| 441 |
|
| 442 |
assert_metrics_meta(meta, str(instances_archive)) |
| 443 |
|
| 444 |
metrics = meta['metrics'] |
| 445 |
assert len(metrics) == 1 |
| 446 |
|
| 447 |
metric = metrics[0] |
| 448 |
assert metric['name'] == 'kernel.all.load' |
| 449 |
assert 'derive' not in metric |
| 450 |
assert metric['semantics'] == 'instant' |
| 451 |
assert metric['instances'] == ['15 minute', '1 minute', '5 minute'] |
| 452 |
|
| 453 |
_, data = await transport.next_frame() |
| 454 |
data = json.loads(data) |
| 455 |
assert data == [[[15.0, 1.0, 5.0]]] |
| 456 |
|
| 457 |
|
| 458 |
@pytest.mark.asyncio |
| 459 |
async def test_pcp_timestamp(transport, timestamps_archive): |
| 460 |
timestamp = int(datetime.datetime.fromisoformat('2023-07-01').timestamp()) * 1000 |
| 461 |
await transport.check_open('metrics1', source=str(timestamps_archive), |
| 462 |
metrics=[{"name": "mock.value"}], limit=1, |
| 463 |
timestamp=timestamp) |
| 464 |
|
| 465 |
_, data = await transport.next_frame() |
| 466 |
# first message is always the meta message |
| 467 |
meta = json.loads(data) |
| 468 |
|
| 469 |
assert_metrics_meta(meta, str(timestamps_archive), timestamp=timestamp) |
| 470 |
|
| 471 |
metrics = meta['metrics'] |
| 472 |
assert len(metrics) == 1 |
| 473 |
|
| 474 |
metric = metrics[0] |
| 475 |
assert metric['name'] == 'mock.value' |
| 476 |
|
| 477 |
_, data = await transport.next_frame() |
| 478 |
data = json.loads(data) |
| 479 |
assert data == [[11.0]] |
| 480 |
|
| 481 |
|
| 482 |
@pytest.mark.asyncio |
| 483 |
async def test_pcp_negative_timestamp(transport, timestamps_archive): |
| 484 |
""" Given a negative timestamp the current time is taken and subtracted |
| 485 |
with the given timestamp """ |
| 486 |
|
| 487 |
timestamp = int(datetime.datetime.fromisoformat('2023-07-01').timestamp()) * 1000 |
| 488 |
relative_timestamp = int(time.time() * 1000) - timestamp |
| 489 |
await transport.check_open('metrics1', source=str(timestamps_archive), |
| 490 |
metrics=[{"name": "mock.value"}], limit=1, |
| 491 |
timestamp=-relative_timestamp) |
| 492 |
|
| 493 |
_, data = await transport.next_frame() |
| 494 |
# first message is always the meta message |
| 495 |
meta = json.loads(data) |
| 496 |
# time.time() is not exact |
| 497 |
assert (meta['timestamp'] - timestamp) < 1000 # 1s |
| 498 |
|
| 499 |
metrics = meta['metrics'] |
| 500 |
assert len(metrics) == 1 |
| 501 |
|
| 502 |
metric = metrics[0] |
| 503 |
assert metric['name'] == 'mock.value' |
| 504 |
|
| 505 |
_, data = await transport.next_frame() |
| 506 |
data = json.loads(data) |
| 507 |
assert data == [[11.0]] |
| 508 |
|
| 509 |
|
| 510 |
@pytest.mark.asyncio |
| 511 |
async def test_pcp_limit_archive(transport, big_archive): |
| 512 |
await transport.check_open('metrics1', source=str(big_archive), |
| 513 |
limit=30, |
| 514 |
metrics=[{"name": "mock.value"}]) |
| 515 |
|
| 516 |
_, data = await transport.next_frame() |
| 517 |
# first message is always the meta message |
| 518 |
meta = json.loads(data) |
| 519 |
assert_metrics_meta(meta, str(big_archive)) |
| 520 |
|
| 521 |
_, data = await transport.next_frame() |
| 522 |
data = json.loads(data) |
| 523 |
assert data == [[i] for i in range(30)] |
| 524 |
|
| 525 |
_, data = await transport.next_frame() |
| 526 |
data = json.loads(data) |
| 527 |
assert data['command'] == 'close' |
| 528 |
|
| 529 |
|
| 530 |
@pytest.mark.asyncio |
| 531 |
async def test_pcp_broken_archive(transport, broken_archive): |
| 532 |
await transport.check_open('metrics1', source=str(broken_archive), |
| 533 |
metrics=[{"name": "mock.value", "derive": "rate"}], |
| 534 |
problem='not-found', |
| 535 |
reply_keys={'message': f'could not read archive {broken_archive}/0.index'}) |
| 536 |
|
| 537 |
|
| 538 |
@pytest.mark.asyncio |
| 539 |
async def test_pcp_instances_change(transport, instances_change_archive): |
| 540 |
await transport.check_open('metrics1', source=str(instances_change_archive), |
| 541 |
metrics=[{"name": "network.interface.total.bytes"}], limit=2) |
| 542 |
|
| 543 |
_, data = await transport.next_frame() |
| 544 |
# first message is always the meta message |
| 545 |
meta = json.loads(data) |
| 546 |
|
| 547 |
assert_metrics_meta(meta, str(instances_change_archive), 1597663539413000, 1000) |
| 548 |
metrics = meta['metrics'][0] |
| 549 |
assert metrics['instances'] == ['eth1', 'lo', 'eth0'] |
| 550 |
|
| 551 |
_, data = await transport.next_frame() |
| 552 |
data = json.loads(data) |
| 553 |
assert data == [[[1, 1, 0]]] |
| 554 |
|
| 555 |
# eth1 is unplugged, new meta message |
| 556 |
_, data = await transport.next_frame() |
| 557 |
meta = json.loads(data) |
| 558 |
assert_metrics_meta(meta, str(instances_change_archive), 1597663539414000, 1000) |
| 559 |
metrics = meta['metrics'][0] |
| 560 |
assert metrics['instances'] == ['lo', 'eth0'] |
| 561 |
|
| 562 |
_, data = await transport.next_frame() |
| 563 |
data = json.loads(data) |
| 564 |
assert data == [[[1, 0]]] |
| 565 |
|
| 566 |
|
| 567 |
@pytest.mark.asyncio |
| 568 |
async def test_pcp_scale_memory_unit(transport, mem_avail_archive): |
| 569 |
await transport.check_open('metrics1', source=str(mem_avail_archive), |
| 570 |
metrics=[{"name": "mem.util.available", "units": "bytes"}], limit=1) |
| 571 |
|
| 572 |
_, data = await transport.next_frame() |
| 573 |
meta = json.loads(data) |
| 574 |
|
| 575 |
assert_metrics_meta(meta, str(mem_avail_archive), 0, 1000) |
| 576 |
metric = meta['metrics'][0] |
| 577 |
assert metric['name'] == "mem.util.available" |
| 578 |
assert metric['units'] == "byte" |
| 579 |
assert metric['semantics'] == "instant" |
| 580 |
|
| 581 |
_, data = await transport.next_frame() |
| 582 |
data = json.loads(data) |
| 583 |
assert data == [[19827535872]] |
| 584 |
|
| 585 |
|
| 586 |
@pytest.mark.asyncio |
| 587 |
async def test_pcp_discrete_metric(transport, discrete_metric_archive): |
| 588 |
await transport.check_open('metrics1', source=str(discrete_metric_archive), |
| 589 |
metrics=[{"name": "mock.value"}]) |
| 590 |
_, data = await transport.next_frame() |
| 591 |
|
| 592 |
meta = json.loads(data) |
| 593 |
assert_metrics_meta(meta, str(discrete_metric_archive), 0, 1000) |
| 594 |
metric = meta['metrics'][0] |
| 595 |
assert metric['name'] == "mock.value" |
| 596 |
assert metric['units'] == "" |
| 597 |
assert metric['semantics'] == "discrete" |
| 598 |
|
| 599 |
_, data = await transport.next_frame() |
| 600 |
data = json.loads(data) |
| 601 |
assert data == [[4]] |
| 602 |
|
| 603 |
|
| 604 |
@pytest.mark.asyncio |
| 605 |
async def test_pcp_rate_metric(transport, multi_file_archive): |
| 606 |
await transport.check_open('metrics1', source=str(multi_file_archive), |
| 607 |
metrics=[{"name": "mock.value", "derive": "rate"}]) |
| 608 |
_, data = await transport.next_frame() |
| 609 |
|
| 610 |
meta = json.loads(data) |
| 611 |
assert_metrics_meta(meta, str(multi_file_archive)) |
| 612 |
metric = meta['metrics'][0] |
| 613 |
assert metric['name'] == "mock.value" |
| 614 |
assert metric['units'] == "" |
| 615 |
assert metric['semantics'] == "instant" |
| 616 |
|
| 617 |
_, data = await transport.next_frame() |
| 618 |
data = json.loads(data) |
| 619 |
assert data == [[False], [1.0], [1.0]] |
| 620 |
|
| 621 |
# new archive, new meta |
| 622 |
_, data = await transport.next_frame() |
| 623 |
meta = json.loads(data) |
| 624 |
assert_metrics_meta(meta, str(multi_file_archive), timestamp=3000) |
| 625 |
|
| 626 |
_, data = await transport.next_frame() |
| 627 |
data = json.loads(data) |
| 628 |
assert data == [[False], [1.0], [1.0]] |
| 629 |
|
| 630 |
|
| 631 |
@pytest.mark.asyncio |
| 632 |
async def test_pcp_rate_instances(transport, instances_rate_archive): |
| 633 |
await transport.check_open('metrics1', source=str(instances_rate_archive), |
| 634 |
metrics=[{"name": "disk.value", "derive": "rate"}]) |
| 635 |
_, data = await transport.next_frame() |
| 636 |
|
| 637 |
meta = json.loads(data) |
| 638 |
assert_metrics_meta(meta, str(instances_rate_archive)) |
| 639 |
metric = meta['metrics'][0] |
| 640 |
assert metric['name'] == "disk.value" |
| 641 |
assert metric['derive'] == "rate" |
| 642 |
assert metric['semantics'] == "instant" |
| 643 |
|
| 644 |
_, data = await transport.next_frame() |
| 645 |
data = json.loads(data) |
| 646 |
assert data == [[[False, False, False]], [[1.0, 0.0, 0.0]], [[1.0, 0.0, 9.0]]] |
| 647 |
|
| 648 |
_, data = await transport.next_frame() |
| 649 |
data = json.loads(data) |
| 650 |
print(data) |
| 651 |
|
| 652 |
|
| 653 |
@pytest.mark.asyncio |
| 654 |
async def test_pcp_unsupported_metric(transport, unsupported_metric_archive): |
| 655 |
"""Verify that the PCP channel does not crash on an unknown metric type""" |
| 656 |
await transport.check_open('metrics1', source=str(unsupported_metric_archive), |
| 657 |
metrics=[{"name": "mock.value"}]) |
| 658 |
_, data = await transport.next_frame() |
| 659 |
|
| 660 |
meta = json.loads(data) |
| 661 |
assert_metrics_meta(meta, str(unsupported_metric_archive), 0, 1000) |