-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix failed proxy retrieval on login (#171)
- Loading branch information
Showing
4 changed files
with
189 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import pytest | ||
from packaging.version import Version | ||
|
||
from zabbix_cli import compat | ||
|
||
|
||
def test_packaging_version_release_sanity(): | ||
"""Ensures that the `Version.release` tuple is in the correct format and | ||
supports users running pre-release versions of Zabbix.""" | ||
assert Version("7.0.0").release == (7, 0, 0) | ||
# Test that all types of pre-releases evaluate to the same release tuple | ||
for pr in ["a1", "b1", "rc1", "alpha1", "beta1"]: | ||
assert Version(f"7.0.0{pr}").release == (7, 0, 0) | ||
assert Version(f"7.0.0{pr}").release >= (7, 0, 0) | ||
assert Version(f"7.0.0{pr}").release <= (7, 0, 0) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"version, expect", | ||
[ | ||
# TODO (pederhan): decide on a set of versions we test against | ||
# instead of coming up with them on the fly, such as here. | ||
# Do we test against only major versions or minor versions as well? | ||
(Version("7.0.0"), "proxyid"), | ||
(Version("6.0.0"), "proxy_hostid"), | ||
(Version("5.0.0"), "proxy_hostid"), | ||
(Version("3.0.0"), "proxy_hostid"), | ||
(Version("2.0.0"), "proxy_hostid"), | ||
(Version("1.0.0"), "proxy_hostid"), | ||
], | ||
) | ||
def test_host_proxyid(version: Version, expect: str): | ||
assert compat.host_proxyid(version) == expect | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"version, expect", | ||
[ | ||
(Version("7.0.0"), "username"), | ||
(Version("6.0.0"), "username"), | ||
(Version("6.2.0"), "username"), | ||
(Version("6.4.0"), "username"), | ||
(Version("5.4.0"), "username"), | ||
(Version("5.4.1"), "username"), | ||
(Version("5.3.9"), "user"), | ||
(Version("5.2.0"), "user"), | ||
(Version("5.2"), "user"), | ||
(Version("5.0"), "user"), | ||
(Version("4.0"), "user"), | ||
(Version("2.0"), "user"), | ||
], | ||
) | ||
def test_login_user_name(version: Version, expect: str): | ||
assert compat.login_user_name(version) == expect | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"version, expect", | ||
[ | ||
(Version("7.0.0"), "name"), | ||
(Version("6.0.0"), "host"), | ||
(Version("5.0.0"), "host"), | ||
(Version("3.0.0"), "host"), | ||
(Version("2.0.0"), "host"), | ||
(Version("1.0.0"), "host"), | ||
], | ||
) | ||
def test_proxy_name(version: Version, expect: str): | ||
assert compat.proxy_name(version) == expect | ||
|
||
@pytest.mark.parametrize( | ||
"version, expect", | ||
[ | ||
(Version("7.0.0"), "username"), | ||
(Version("6.4.0"), "username"), | ||
(Version("6.0.0"), "username"), | ||
# NOTE: special case here where we use "alias" instead of "username" | ||
# even though it was deprecated in 5.4.0 (matches historical zabbix_cli behavior) | ||
(Version("5.4.0"), "alias"), | ||
(Version("5.0.0"), "alias"), | ||
(Version("3.0.0"), "alias"), | ||
(Version("2.0.0"), "alias"), | ||
(Version("1.0.0"), "alias"), | ||
], | ||
) | ||
def test_user_name(version: Version, expect: str): | ||
assert compat.user_name(version) == expect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
"""Compatibility functions for different Zabbix versions.""" | ||
|
||
from packaging.version import Version | ||
|
||
# TODO (pederhan): rewrite these functions as some sort of declarative data | ||
# structure that can be used to determine correct parameters based on version | ||
# if we end up with a lot of these functions. For now, this is fine. | ||
# OR we could turn it into a mixin class? | ||
|
||
# Compatibility methods for Zabbix API objects properties and method parameter names (same thing) | ||
# Returns the appropriate property name for the given Zabbix version. | ||
# | ||
# FORMAT: <object>_<property> | ||
# EXAMPLE: user_name() (User object, name property) | ||
# | ||
# DEV NOTE: All functions follow the same pattern: | ||
# Early return if the version is older than the version where the property | ||
# was deprecated, otherwise return the new property name as the default. | ||
|
||
|
||
def host_proxyid(version: Version) -> str: | ||
# https://support.zabbix.com/browse/ZBXNEXT-8500 | ||
# https://www.zabbix.com/documentation/7.0/en/manual/api/changes#host | ||
if version.release < (7, 0, 0): | ||
return "proxy_hostid" | ||
return "proxyid" | ||
|
||
|
||
def login_user_name(version: Version) -> str: | ||
# https://support.zabbix.com/browse/ZBXNEXT-8085 | ||
# Deprecated in 5.4.0, removed in 6.4.0 | ||
# login uses different parameter names than the User object before 6.4 | ||
# From 6.4 and onwards, login and user.<method> use the same parameter names | ||
# See: user_name | ||
if version.release < (5, 4, 0): | ||
return "user" | ||
return "username" | ||
|
||
|
||
def proxy_name(version: Version) -> str: | ||
# https://support.zabbix.com/browse/ZBXNEXT-8500 | ||
# https://www.zabbix.com/documentation/7.0/en/manual/api/changes#proxy | ||
if version.release < (7, 0, 0): | ||
return "host" | ||
return "name" | ||
|
||
|
||
def user_name(version: Version) -> str: | ||
# https://support.zabbix.com/browse/ZBXNEXT-8085 | ||
# Deprecated in 5.4, removed in 6.4 | ||
# However, historically we have used "alias" as the parameter name | ||
# pre-6.0.0, so we maintain that behavior here | ||
if version.release < (6, 0, 0): | ||
return "alias" | ||
return "username" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters