digaws/tests/test_dig_aws.py

115 lines
3.1 KiB
Python
Raw Normal View History

2020-12-25 20:36:41 +00:00
import json
import sys
import pytest
2024-06-08 11:43:41 +00:00
import digaws.digaws as digaws
2020-12-25 20:36:41 +00:00
import tests
2024-06-08 11:43:41 +00:00
from digaws import __description__, __version__
2020-12-25 20:36:41 +00:00
@pytest.fixture
def test_dig():
2024-06-08 11:43:41 +00:00
return digaws.DigAWS(
ip_ranges=json.loads(tests.AWS_IP_RANGES), output_fields=digaws.OUTPUT_FIELDS
2020-12-26 12:31:05 +00:00
)
2020-12-25 20:36:41 +00:00
def test_cli(capsys):
2024-06-08 11:43:41 +00:00
sys.argv = ["digaws", "-h"]
2020-12-25 20:36:41 +00:00
try:
digaws.main()
except SystemExit as e:
out, _ = capsys.readouterr()
assert __description__ in out
assert e.code == 0
def test_cli_version(capsys, mocker):
2024-06-08 11:43:41 +00:00
sys.argv = ["digaws", "--version"]
2020-12-25 20:36:41 +00:00
try:
digaws.main()
except SystemExit as e:
out, _ = capsys.readouterr()
2024-06-08 11:43:41 +00:00
assert out == f"digaws {__version__}\n"
2020-12-25 20:36:41 +00:00
assert e.code == 0
def test_cli_invocation(capsys, mocker):
2024-06-08 11:43:41 +00:00
sys.argv = [
"digaws",
"52.94.76.0/22",
"2600:1f14:fff:f810:a1c1:f507:a2d1:2dd8",
"--output",
"json",
]
mocker.patch("digaws.digaws.get_aws_ip_ranges", return_value=json.loads(tests.AWS_IP_RANGES))
2020-12-25 20:36:41 +00:00
digaws.main()
out, _ = capsys.readouterr()
assert out == tests.RESPONSE_JSON_JOINED_PRINT
2020-12-26 12:31:05 +00:00
def test_cli_output_plain_fields_invocation(capsys, mocker):
2024-06-08 11:43:41 +00:00
sys.argv = [
"digaws",
"52.94.76.0/22",
"--output=plain",
"--output-fields",
"region",
]
mocker.patch("digaws.digaws.get_aws_ip_ranges", return_value=json.loads(tests.AWS_IP_RANGES))
2020-12-26 12:31:05 +00:00
digaws.main()
out, _ = capsys.readouterr()
2024-06-08 11:43:41 +00:00
assert out == "Region: us-west-2\n\n"
2020-12-26 12:31:05 +00:00
def test_cli_output_json_fields_invocation(capsys, mocker):
2024-06-08 11:43:41 +00:00
sys.argv = [
"digaws",
"2600:1f14:fff:f810:a1c1:f507:a2d1:2dd8",
"--output=json",
"--output-fields",
"service",
"network_border_group",
]
mocker.patch("digaws.digaws.get_aws_ip_ranges", return_value=json.loads(tests.AWS_IP_RANGES))
2020-12-26 12:31:05 +00:00
digaws.main()
out, _ = capsys.readouterr()
assert out == tests.RESPONSE_JSON_FIELDS_PRINT
2020-12-25 20:36:41 +00:00
def test_dig_aws_construct(test_dig):
assert test_dig.ip_prefixes == tests.AWS_IPV4_RANGES_OBJ
assert test_dig.ipv6_prefixes == tests.AWS_IPV6_RANGES_OBJ
def test_lookup(test_dig):
2024-06-08 11:43:41 +00:00
assert str(test_dig._lookup_data("52.94.76.1")[0]["ip_prefix"]) == "52.94.76.0/22"
assert str(test_dig._lookup_data("52.94.76.0/24")[0]["ip_prefix"]) == "52.94.76.0/22"
2020-12-25 20:36:41 +00:00
2024-06-08 11:43:41 +00:00
input = "2600:1f14:fff:f810:a1c1:f507:a2d1:2dd8"
assert str(test_dig._lookup_data(input)[0]["ipv6_prefix"]) == "2600:1f14:fff:f800::/53"
assert str(test_dig._lookup_data(input)[1]["ipv6_prefix"]) == "2600:1f14::/35"
assert str(test_dig._lookup_data("2600:1f14::/36")[0]["ipv6_prefix"]) == "2600:1f14::/35"
2020-12-25 20:36:41 +00:00
with pytest.raises(ValueError) as e:
2024-06-08 11:43:41 +00:00
test_dig.lookup("what are you talking about")
assert e.startswith("Wrong IP or CIDR format")
2020-12-25 20:36:41 +00:00
def test_response_plain_print(test_dig, capsys):
2024-06-08 11:43:41 +00:00
test_dig.lookup("52.94.76.0/22").plain_print()
2020-12-25 20:36:41 +00:00
out, _ = capsys.readouterr()
assert out == tests.RESPONSE_PLAIN_PRINT
def test_response_json_print(test_dig, capsys):
2024-06-08 11:43:41 +00:00
test_dig.lookup("2600:1f14:fff:f810:a1c1:f507:a2d1:2dd8").json_print()
2020-12-25 20:36:41 +00:00
out, _ = capsys.readouterr()
assert out == tests.RESPONSE_JSON_PRINT