1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
import pytest
from packaging.version import Version
from pyzabbix import ZabbixAPI, ZabbixAPIException
@pytest.mark.parametrize(
"server, expected",
[
("http://example.com", "http://example.com/api_jsonrpc.php"),
("http://example.com/", "http://example.com/api_jsonrpc.php"),
("http://example.com/base", "http://example.com/base/api_jsonrpc.php"),
("http://example.com/base/", "http://example.com/base/api_jsonrpc.php"),
],
)
def test_server_url_correction(server, expected):
assert ZabbixAPI(server).url == expected
def _zabbix_requests_mock_factory(requests_mock, *args, **kwargs):
requests_mock.post(
"http://example.com/api_jsonrpc.php",
request_headers={
"Content-Type": "application/json-rpc",
"User-Agent": "python/pyzabbix",
"Cache-Control": "no-cache",
},
*args,
**kwargs,
)
def test_login(requests_mock):
_zabbix_requests_mock_factory(
requests_mock,
json={
"jsonrpc": "2.0",
"result": "0424bd59b807674191e7d77572075f33",
"id": 0,
},
)
zapi = ZabbixAPI("http://example.com", detect_version=False)
zapi.login("mylogin", "mypass")
# Check request
assert requests_mock.last_request.json() == {
"jsonrpc": "2.0",
"method": "user.login",
"params": {"user": "mylogin", "password": "mypass"},
"id": 0,
}
# Check login
assert zapi.auth == "0424bd59b807674191e7d77572075f33"
def test_login_with_context(requests_mock):
_zabbix_requests_mock_factory(
requests_mock,
json={
"jsonrpc": "2.0",
"result": "0424bd59b807674191e7d77572075f33",
"id": 0,
},
)
with ZabbixAPI("http://example.com", detect_version=False) as zapi:
zapi.login("mylogin", "mypass")
assert zapi.auth == "0424bd59b807674191e7d77572075f33"
@pytest.mark.parametrize(
"version",
[
("4.0.0"),
("5.4.0"),
("6.2.0"),
("6.2.0beta1"),
("6.2.2alpha1"),
],
)
def test_login_with_version_detect(requests_mock, version):
_zabbix_requests_mock_factory(
requests_mock,
[
{
"json": {
"jsonrpc": "2.0",
"result": version,
"id": 0,
}
},
{
"json": {
"jsonrpc": "2.0",
"result": "0424bd59b807674191e7d77572075f33",
"id": 0,
}
},
],
)
with ZabbixAPI("http://example.com") as zapi:
zapi.login("mylogin", "mypass")
assert zapi.auth == "0424bd59b807674191e7d77572075f33"
def test_attr_syntax_kwargs(requests_mock):
_zabbix_requests_mock_factory(
requests_mock,
json={
"jsonrpc": "2.0",
"result": [{"hostid": 1234}],
"id": 0,
},
)
zapi = ZabbixAPI("http://example.com", detect_version=False)
zapi.auth = "some_auth_key"
result = zapi.host.get(hostids=5)
# Check request
assert requests_mock.last_request.json() == {
"jsonrpc": "2.0",
"method": "host.get",
"params": {"hostids": 5},
"auth": "some_auth_key",
"id": 0,
}
# Check response
assert result == [{"hostid": 1234}]
def test_attr_syntax_args(requests_mock):
_zabbix_requests_mock_factory(
requests_mock,
json={
"jsonrpc": "2.0",
"result": {"itemids": ["22982", "22986"]},
"id": 0,
},
)
zapi = ZabbixAPI("http://example.com", detect_version=False)
zapi.auth = "some_auth_key"
result = zapi.host.delete("22982", "22986")
# Check request
assert requests_mock.last_request.json() == {
"jsonrpc": "2.0",
"method": "host.delete",
"params": ["22982", "22986"],
"auth": "some_auth_key",
"id": 0,
}
# Check response
assert result == {"itemids": ["22982", "22986"]}
def test_attr_syntax_args_and_kwargs_raises():
with pytest.raises(
TypeError,
match="Found both args and kwargs",
):
zapi = ZabbixAPI("http://example.com")
zapi.host.delete("22982", hostids=5)
@pytest.mark.parametrize(
"version",
[
("4.0.0"),
("4.0.0rc1"),
("6.2.0beta1"),
("6.2.2alpha1"),
],
)
def test_detecting_version(requests_mock, version):
_zabbix_requests_mock_factory(
requests_mock,
json={
"jsonrpc": "2.0",
"result": version,
"id": 0,
},
)
zapi = ZabbixAPI("http://example.com")
zapi.login("mylogin", "mypass")
assert zapi.api_version() == version
@pytest.mark.parametrize(
"data",
[
(None),
('No groups for host "Linux server".'),
],
)
def test_error_response(requests_mock, data):
_zabbix_requests_mock_factory(
requests_mock,
json={
"jsonrpc": "2.0",
"error": {
"code": -32602,
"message": "Invalid params.",
**({} if data is None else {"data": data}),
},
"id": 0,
},
)
with pytest.raises(
ZabbixAPIException,
match="Error -32602: Invalid params., No data."
if data is None
else f"Error -32602: Invalid params., {data}",
):
zapi = ZabbixAPI("http://example.com")
zapi.host.get()
def test_empty_response(requests_mock):
_zabbix_requests_mock_factory(
requests_mock,
body="",
)
with pytest.raises(ZabbixAPIException, match="Received empty response"):
zapi = ZabbixAPI("http://example.com")
zapi.login("mylogin", "mypass")
@pytest.mark.parametrize(
"version",
[
("4.0.0"),
("5.4.0"),
("6.2.0"),
],
)
def test_do_request(requests_mock, version):
_zabbix_requests_mock_factory(
requests_mock,
json={
"jsonrpc": "2.0",
"result": [{"hostid": 1234}],
"id": 0,
},
)
zapi = ZabbixAPI("http://example.com", detect_version=False)
zapi.version = Version(version)
zapi.auth = "some_auth_key"
result = zapi["host"]["get"]()
# Check response
assert result == [{"hostid": 1234}]
# Check request
found = requests_mock.last_request
expect_json = {
"jsonrpc": "2.0",
"method": "host.get",
"params": {},
"id": 0,
}
expect_headers = {
"Cache-Control": "no-cache",
"Content-Type": "application/json-rpc",
"User-Agent": "python/pyzabbix",
}
if zapi.version < Version("6.4.0"):
expect_json["auth"] = "some_auth_key"
else:
expect_headers["Authorization"] = "Bearer some_auth_key"
assert found.json() == expect_json
assert found.headers.items() >= expect_headers.items()
|