|
| 1 | +import pytest |
| 2 | +from tofupy import Tofu |
| 3 | +import requests |
| 4 | +import os |
| 5 | +import time |
| 6 | + |
| 7 | +coral_uri = os.environ.get("TF_VAR_coral_uri") |
| 8 | +headers = {"Authorization": "Bearer "+os.environ.get("TF_VAR_auth_token")} |
| 9 | + |
| 10 | +@pytest.fixture(scope="session") |
| 11 | +def terraform_rest_setup(): |
| 12 | + working_dir = os.path.join(os.path.dirname(__file__), "..") |
| 13 | + var_file = os.path.join(working_dir, "example-config.tfvars") |
| 14 | + tf = Tofu(cwd=working_dir) |
| 15 | + tf.init() |
| 16 | + tf.apply(extra_args=["--var-file="+var_file]) |
| 17 | + # add_consumers() |
| 18 | + yield |
| 19 | + tf.apply(extra_args=["--var-file="+var_file],destroy=True) |
| 20 | + |
| 21 | +def api_get_request(resource): |
| 22 | + return requests.get(coral_uri+"/"+resource,headers=headers).json() |
| 23 | + |
| 24 | +def contains_only(actual, expected): |
| 25 | + return len(actual) == len(expected) and set(expected).issubset(actual) |
| 26 | + |
| 27 | +def test_resource_classes_created(terraform_rest_setup): |
| 28 | + resp = api_get_request("resource_class") |
| 29 | + created_resource_classes = [c["name"] for c in resp] |
| 30 | + |
| 31 | + assert contains_only(created_resource_classes, ["DISK_GB", "MEMORY_MB", "VCPU"]) |
| 32 | + |
| 33 | +def test_resource_provider_created(terraform_rest_setup): |
| 34 | + providers = api_get_request("resource_provider") |
| 35 | + provider_names = [p["name"] for p in providers] |
| 36 | + |
| 37 | + assert contains_only(provider_names, ["Test Provider"]) |
| 38 | + |
| 39 | +def test_accounts_created(terraform_rest_setup): |
| 40 | + accounts = api_get_request("account") |
| 41 | + account_names = [a["name"] for a in accounts] |
| 42 | + |
| 43 | + assert contains_only(account_names, ["TestAccount1","TestAccount2"]) |
| 44 | + |
| 45 | +def test_rpas_created(terraform_rest_setup): |
| 46 | + rpas = api_get_request("resource_provider_account") |
| 47 | + accounts = api_get_request("account") |
| 48 | + rpa_account_urls = [a["account"] for a in rpas] |
| 49 | + account_urls = [a["url"] for a in accounts] |
| 50 | + assert contains_only(rpa_account_urls, account_urls) |
| 51 | + |
| 52 | +def test_allocations_created(terraform_rest_setup): |
| 53 | + allocations = api_get_request("allocation") |
| 54 | + allocations_names = [a["name"] for a in allocations] |
| 55 | + |
| 56 | + assert contains_only(allocations_names, ["Q1-0","Q1-1","Q2-0"]) |
| 57 | + |
| 58 | +def test_allocation_user_mappings(terraform_rest_setup): |
| 59 | + allocations = api_get_request("allocation") |
| 60 | + q1_allocations = [a for a in allocations if a["name"][:2] == "Q1"] |
| 61 | + q2_allocations = [a for a in allocations if a["name"][:2] == "Q2"] |
| 62 | + |
| 63 | + accounts = api_get_request("account") |
| 64 | + account_urls = {a["name"]: a["url"] for a in accounts} |
| 65 | + |
| 66 | + q1_allocated_accounts = [a["account"] for a in q1_allocations] |
| 67 | + q2_allocated_accounts = [a["account"] for a in q2_allocations] |
| 68 | + |
| 69 | + assert contains_only(q1_allocated_accounts, [account_urls["TestAccount1"],account_urls["TestAccount2"]]) |
| 70 | + assert contains_only(q2_allocated_accounts, [account_urls["TestAccount1"]]) |
| 71 | + |
| 72 | +def test_allocation_date_mappings(terraform_rest_setup): |
| 73 | + allocations = api_get_request("allocation") |
| 74 | + q1_allocations = [a for a in allocations if a["name"][:2] == "Q1"] |
| 75 | + q2_allocations = [a for a in allocations if a["name"][:2] == "Q2"] |
| 76 | + |
| 77 | + q1_allocation_starts = [a["start"] for a in q1_allocations] |
| 78 | + q2_allocation_starts = [a["start"] for a in q2_allocations] |
| 79 | + |
| 80 | + assert len(set(q1_allocation_starts)) == 1 |
| 81 | + assert len(q2_allocation_starts) == 1 |
| 82 | + assert q1_allocation_starts[0] != q2_allocation_starts[0] |
| 83 | + |
| 84 | +def to_resource_map(allocation_resources): |
| 85 | + return {a["resource_class"]["name"] : a["resource_hours"] for a in allocation_resources} |
| 86 | + |
| 87 | +def test_only_allocation_resources_returned(terraform_rest_setup): |
| 88 | + allocation_id = api_get_request("allocation")[0]["id"] |
| 89 | + assert len(api_get_request("allocation/"+str(allocation_id)+"/resources")) == 3 |
| 90 | + |
| 91 | +def test_resource_allocations_created(terraform_rest_setup): |
| 92 | + allocations = api_get_request("allocation") |
| 93 | + allocation_resources = { |
| 94 | + a["name"]: to_resource_map(api_get_request("allocation/"+str(a["id"])+"/resources")) |
| 95 | + for a in allocations |
| 96 | + } |
| 97 | + assert allocation_resources["Q1-0"] == {"VCPU": 40000, "MEMORY_MB": 4423680, "DISK_GB": 108000} |
| 98 | + assert allocation_resources["Q1-1"] == {"VCPU": 20000, "MEMORY_MB": 2000000, "DISK_GB": 200000} |
| 99 | + assert allocation_resources["Q2-0"] == {"VCPU": 80000, "MEMORY_MB": 8000000, "DISK_GB": 300000} |
0 commit comments