|
| 1 | +"""Test the interaction with the Glances API.""" |
| 2 | +import pytest |
| 3 | +from pytest_httpx import HTTPXMock |
| 4 | + |
| 5 | +from glances_api import Glances |
| 6 | + |
| 7 | +PLUGINS_LIST_RESPONSE = [ |
| 8 | + "alert", |
| 9 | + "amps", |
| 10 | + "cloud", |
| 11 | + "connections", |
| 12 | + "core", |
| 13 | + "cpu", |
| 14 | + "diskio", |
| 15 | + "docker", |
| 16 | + "folders", |
| 17 | + "fs", |
| 18 | +] |
| 19 | + |
| 20 | +RESPONSE = { |
| 21 | + "cpu": { |
| 22 | + "total": 10.6, |
| 23 | + "user": 7.6, |
| 24 | + "system": 2.1, |
| 25 | + "idle": 88.8, |
| 26 | + "nice": 0.0, |
| 27 | + "iowait": 0.6, |
| 28 | + }, |
| 29 | + "diskio": [ |
| 30 | + { |
| 31 | + "time_since_update": 1, |
| 32 | + "disk_name": "nvme0n1", |
| 33 | + "read_count": 12, |
| 34 | + "write_count": 466, |
| 35 | + "read_bytes": 184320, |
| 36 | + "write_bytes": 23863296, |
| 37 | + "key": "disk_name", |
| 38 | + }, |
| 39 | + ], |
| 40 | + "system": { |
| 41 | + "os_name": "Linux", |
| 42 | + "hostname": "fedora-35", |
| 43 | + "platform": "64bit", |
| 44 | + "linux_distro": "Fedora Linux 35", |
| 45 | + "os_version": "5.15.6-200.fc35.x86_64", |
| 46 | + "hr_name": "Fedora Linux 35 64bit", |
| 47 | + }, |
| 48 | + "uptime": "3 days, 10:25:20", |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.asyncio |
| 53 | +async def test_non_existing_endpoint(httpx_mock: HTTPXMock): |
| 54 | + """Test a non-exisiting endpoint.""" |
| 55 | + httpx_mock.add_response(json={}) |
| 56 | + |
| 57 | + client = Glances() |
| 58 | + await client.get_data("some-data") |
| 59 | + |
| 60 | + assert client.values == None |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.asyncio |
| 64 | +async def test_plugins_list(httpx_mock: HTTPXMock): |
| 65 | + """Test the plugins list response.""" |
| 66 | + httpx_mock.add_response(json=PLUGINS_LIST_RESPONSE) |
| 67 | + |
| 68 | + client = Glances() |
| 69 | + await client.get_data("pluginslist") |
| 70 | + |
| 71 | + assert len(client.plugins) == 10 |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.asyncio |
| 75 | +async def test_exisiting_endpoint(httpx_mock: HTTPXMock): |
| 76 | + """Test the a valid endpoint.""" |
| 77 | + httpx_mock.add_response(json=RESPONSE) |
| 78 | + |
| 79 | + client = Glances() |
| 80 | + await client.get_metrics("cpu") |
| 81 | + |
| 82 | + assert client.values["total"] == 10.6 |
| 83 | + assert client.values["system"] == 2.1 |
0 commit comments