|
1 | 1 | import json |
2 | 2 | import tempfile |
| 3 | +from io import StringIO |
3 | 4 | from typing import Any, List |
4 | | -from unittest.mock import MagicMock, patch |
| 5 | +from unittest.mock import MagicMock, mock_open, patch |
5 | 6 |
|
6 | 7 | import pytest |
7 | 8 | import yaml |
@@ -894,3 +895,90 @@ def test_public_offer_product_update_instance_type_pricing_change_exception( |
894 | 895 | ["--product-id", "some-prod-id", "--config", "./tests/test_config.yaml", "--no-allow-price-change"], |
895 | 896 | ) |
896 | 897 | assert res.exit_code == 1 and res.exc_info is not None and "Restricted listings" in res.exc_info[1].args[0] |
| 898 | + |
| 899 | + |
| 900 | +@pytest.mark.parametrize( |
| 901 | + "key1, key2, value", |
| 902 | + [ |
| 903 | + ("description", "product_title", "test"), |
| 904 | + ("description", "categories", ["Migration"]), |
| 905 | + ("description", "long_description", "test_long_description\n"), |
| 906 | + ("version", "version_title", "Test Ubuntu AMI"), |
| 907 | + ("version", "usage_instructions", "test_usage_instruction\n"), |
| 908 | + ("version", "ami_id", "ami-12345678910"), |
| 909 | + ("description", "support_description", "test_support_description\n"), |
| 910 | + ("region", "commercial_regions", ["us-east-1", "us-east-2"]), |
| 911 | + ("region", "future_region_support", True), |
| 912 | + ], |
| 913 | +) |
| 914 | +@patch("awsmp._driver.get_entity_details") |
| 915 | +@patch("awsmp._driver.get_public_offer_id") |
| 916 | +def test_public_offer_product_download_product(mock_get_public_offer_id, mock_get_entity_details, key1, key2, value): |
| 917 | + with open("./tests/test_config.json") as f: |
| 918 | + mock_prod_resp = json.load(f) |
| 919 | + mock_prod_resp.pop("Terms") |
| 920 | + |
| 921 | + mock_prod_resp["Versions"]["CreationDate"] = "2025-01-01" |
| 922 | + mock_prod_resp["Versions"] = [mock_prod_resp["Versions"]] |
| 923 | + |
| 924 | + with open("./tests/test_config.json") as f: |
| 925 | + mock_offer_resp = {"Terms": json.load(f)["Terms"]} |
| 926 | + |
| 927 | + mock_get_entity_details.side_effect = [mock_prod_resp, mock_offer_resp] |
| 928 | + mock_get_public_offer_id.return_value = "test-offer-id" |
| 929 | + |
| 930 | + runner = CliRunner() |
| 931 | + config_file = tempfile.NamedTemporaryFile() |
| 932 | + res = runner.invoke( |
| 933 | + cli.ami_product_download, |
| 934 | + ["--product-id", "some-prod-id", "--config", config_file.name], |
| 935 | + ) |
| 936 | + |
| 937 | + with open(config_file.name, "r") as f: |
| 938 | + config = yaml.safe_load(f) |
| 939 | + |
| 940 | + assert config["product"][key1][key2] == value |
| 941 | + |
| 942 | + |
| 943 | +@pytest.mark.parametrize( |
| 944 | + "key1, key2, value", |
| 945 | + [ |
| 946 | + ("offer", "refund_policy", "test_refund_policy_term\n"), |
| 947 | + ( |
| 948 | + "offer", |
| 949 | + "instance_types", |
| 950 | + [ |
| 951 | + {"name": "a1.large", "hourly": "0.004", "yearly": "24.528"}, |
| 952 | + {"name": "a1.xlarge", "hourly": "0.007", "yearly": "49.056"}, |
| 953 | + ], |
| 954 | + ), |
| 955 | + ("offer", "eula_document", [{"type": ""}]), |
| 956 | + ], |
| 957 | +) |
| 958 | +@patch("awsmp._driver.get_entity_details") |
| 959 | +@patch("awsmp._driver.get_public_offer_id") |
| 960 | +def test_public_offer_product_download_offer(mock_get_public_offer_id, mock_get_entity_details, key1, key2, value): |
| 961 | + with open("./tests/test_config.json") as f: |
| 962 | + mock_prod_resp = json.load(f) |
| 963 | + mock_prod_resp.pop("Terms") |
| 964 | + |
| 965 | + mock_prod_resp["Versions"]["CreationDate"] = "2025-01-01" |
| 966 | + mock_prod_resp["Versions"] = [mock_prod_resp["Versions"]] |
| 967 | + |
| 968 | + with open("./tests/test_config.json") as f: |
| 969 | + mock_offer_resp = {"Terms": json.load(f)["Terms"]} |
| 970 | + |
| 971 | + mock_get_entity_details.side_effect = [mock_prod_resp, mock_offer_resp] |
| 972 | + mock_get_public_offer_id.return_value = "test-offer-id" |
| 973 | + |
| 974 | + runner = CliRunner() |
| 975 | + config_file = tempfile.NamedTemporaryFile() |
| 976 | + res = runner.invoke( |
| 977 | + cli.ami_product_download, |
| 978 | + ["--product-id", "some-prod-id", "--config", config_file.name], |
| 979 | + ) |
| 980 | + |
| 981 | + with open(config_file.name, "r") as f: |
| 982 | + config = yaml.safe_load(f) |
| 983 | + |
| 984 | + assert config[key1][key2] == value |
0 commit comments