def test_metrics_unknown_client(monkeypatch, caplog): """error logs explain that the chosen protocol is unknown""" monkeypatch.setenv(metrics.HOST_ENV_VAR, "127.0.0.1") monkeypatch.setenv(metrics.PROTOCOL_ENV_VAR, "foobar") response = app.create_app().test_client().get("/metrics") assert b"py_air_control_sampling_error" in response.data assert "Unknown protocol 'foobar'" in caplog.text
def test_metrics_pyairctrl_failure(mock_http_client, monkeypatch, caplog): """error logs explain that there was a failure getting the status from pyairctrl""" mock_http_client["get_status"].side_effect = Exception("Some foobar error") monkeypatch.setenv(metrics.HOST_ENV_VAR, "127.0.0.1") response = app.create_app().test_client().get("/metrics") assert b"py_air_control_sampling_error" in response.data assert "Could not read values from air control device" in caplog.text assert "Some foobar error" in caplog.text
def test_metrics_no_host_provided(caplog): """ error logs explain that the purifier host has to be provided through an env var """ response = app.create_app().test_client().get("/metrics") assert b"py_air_control_sampling_error" in response.data assert "Please specify the host address" in caplog.text assert metrics.HOST_ENV_VAR in caplog.text
def test_metrics_failure(monkeypatch): """metrics endpoint should produce a sampling error counter on error""" monkeypatch.setenv(metrics.HOST_ENV_VAR, "127.0.0.1") test_client = app.create_app().test_client() response = test_client.get("/metrics") assert b"py_air_control_sampling_error_total 2.0\n" in response.data response = test_client.get("/metrics") assert b"py_air_control_sampling_error_total 3.0\n" in response.data
def test_metrics_fetched_again(mock_http_client): """check that status is fetched every time metrics are pulled""" assert mock_http_client["get_status"].call_count == 0 test_client = app.create_app(host="1.2.3.4", protocol=metrics.HTTP_PROTOCOL).test_client() assert mock_http_client["get_status"].call_count == 1 test_client.get("/metrics") assert mock_http_client["get_status"].call_count == 2 test_client.get("/metrics") assert mock_http_client["get_status"].call_count == 3
def test_metrics(mock_http_client, monkeypatch): """metrics endpoint produces the expected metrics""" monkeypatch.setenv(metrics.HOST_ENV_VAR, "127.0.0.1") response = app.create_app().test_client().get("/metrics") assert b"py_air_control_air_quality 1.0\n" in response.data assert b"py_air_control_is_manual 1.0\n" in response.data assert b"py_air_control_is_on 1.0\n" in response.data assert b"py_air_control_pm25 2.0\n" in response.data assert b"py_air_control_speed 0.0\n" in response.data assert b'py_air_control_filter_hours{id="0",type=""} 0.0\n' in response.data assert b'py_air_control_filter_hours{id="1",type="A3"} 185.0\n' in response.data assert b'py_air_control_filter_hours{id="2",type="C7"} 2228.0\n' in response.data assert b"IAI allergen index" in response.data
def test_create_app(mock_collector): """ check that we can create an exporter with explicit host and protocol parameters """ app.create_app(host="1.2.3.4", protocol="foobar") mock_collector.assert_called_once_with(host="1.2.3.4", protocol="foobar")
def test_host_and_protocol_parameters(mock_get_status): """check that we can provide the host and protocol through app parameters""" app.create_app(host="1.2.3.4", protocol="foobar").test_client().get("/metrics") mock_get_status.assert_called_with(host="1.2.3.4", protocol="foobar")
def main(host, protocol, listen_address, listen_port): app.create_app(host=host, protocol=protocol).run( host=listen_address, port=listen_port )