def test_post_vpcs(): body = {"name": "my-vpc-2"} res = session.post( f"{API_ENDPOINT}/v1/vpcs?version=2021-05-06&generation=2", json=body) optional_body_data = [ "VPCPrototype", "address_prefix_management", "classic_access", "name", "resource_group", ] for k, v in body.items(): assert k in optional_body_data if k == "address_prefix_management": assert v in ["auto", "manual"] elif k == "classic_access": assert v in ["true", "false"] elif k == "name": assert 1 <= len(v) <= 63 assert re.match(SUBNET_NAME_REGEX, v) elif k == "resource_group": assert re.match(RESOURCE_GROUP_ID_REGEX, v.get("id")) check_required_params(res) check_valid_vpc(res.json())
def test_vpc_by_id(): vpc_id = _adapter.vpc_id res = session.get( f"{API_ENDPOINT}/v1/vpcs/{vpc_id}?version=2021-05-06&generation=2") check_required_params(res) # testing response check_valid_vpc(res.json())
def test_get_subnet_by_id(): subnet_id = _adapter.subnet_id res = session.get( f"{API_ENDPOINT}/v1/subnets/{subnet_id}?version=2021-05-06&generation=2" ) check_required_params(res) check_valid_subnet(res.json())
def test_delete_vpc_by_id(): vpc_id = _adapter.vpc_id res = session.delete( f"{API_ENDPOINT}/v1/vpcs/{vpc_id}?version=2021-05-06&generation=2") check_required_params(res) assert re.search(r"v1/vpcs/(.*?)\?[vg]", res.url) assert res.status_code == 204
def test_key_by_id(): key_id = _adapter.key_id res = session.get( f"{API_ENDPOINT}/v1/keys/{key_id}?version=2021-05-06&generation=2") check_required_params(res) # testing response check_valid_key(res.json())
def test_floating_ips_by_id(): floating_ips_id = _adapter.floating_ips_id res = session.get( f"{API_ENDPOINT}/v1/floating_ips/{floating_ips_id}?version=2021-05-06&generation=2" ) check_required_params(res) # testing response check_valid_floating_ip(res.json())
def test_get_vpcs(): res = session.get( f"{API_ENDPOINT}/v1/vpcs?version=2021-05-06&generation=2") query_params = dict(parse_qsl(urlparse(res.url).query)) optional_params = ["start", "limit", "resource_group.id", "classic_access"] # -- testing request params check_required_params(res) for k, v in query_params.items(): # check that query has the correct params assert k in (REQUIRED_PARAMS + optional_params) # check if query params has any value assert v if k == "limit": assert v.isdigit() assert 1 <= v <= 100 elif k == "classic_access": assert v in ["true", "false"] # -- testing response required_response_keys = ["limit", "first", "total_count", "vpcs"] response_keys = required_response_keys + ["next"] data = res.json() for k in required_response_keys: assert k in data.keys() for k, v in data.items(): assert k in response_keys if k == "total_count": assert v >= 0 elif k == "vpcs" and v: for vpc in v: check_valid_vpc(vpc) elif k == "limit": assert 1 <= v <= 100 elif k == "next": assert re.match(URL_REGEX, v.get("href", "")) else: assert v
def test_get_subnets(): res = session.get( f"{API_ENDPOINT}/v1/subnets?version=2021-05-06&generation=2") query_params = dict(parse_qsl(urlparse(res.url).query)) optional_params = [ "start", "limit", "resource_group.id", "routing_table.id", "routing_table.name", "ipv4_cidr_block", ] check_required_params(res) for k, v in query_params.items(): assert k in (optional_params + REQUIRED_PARAMS) # check if query contains optional params if k in optional_params: # check if query params has any value assert v if k == "limit": assert v.isdigit() assert 1 <= v <= 100 data = res.json() required_response_keys = ["first", "limit", "subnets", "total_count"] optional_response_key = ["next"] for k, v in data.items(): assert k in (required_response_keys + optional_response_key) if k == "limit": assert 1 <= v <= 100 if k == "total_count": assert v >= 0 if k == "next": assert re.match(URL_REGEX, v.get("href")) subnets = data.get("subnets", []) for subnet in subnets: check_valid_subnet(subnet)
def test_post_floating_ips(): body = { "name": "my-floating-ip-1", "target": {"id": "5a07e83d-c1f3-4df2-bcec-41b09c006847"}, } res = session.post( f"{API_ENDPOINT}/v1/floating_ips?version=2021-05-06&generation=2", json=body ) allowed_body_data = ["name", "resource_group", "target"] for k, v in body.items(): assert k in allowed_body_data check_required_params(res) check_valid_floating_ip(res.json())
def test_post_keys(): body = { "name": "my-key-1", "public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCwM/8slSaAsMkP91qWDKD20+ZNq6dLu+qoE/oglPwJJBxpDU/T0wJ2LmfHSsRUNxX9ZA/CMkHsFJ0Gbuqvz0lx4TPN4AGNIr5bWj4VZp2LXibQ3QJkhkK3qdhUIyTY34qtEea8UYviZUAmROe2MMTpYayxsbnTDMw/RJjQ/d3W9hPSGQl7XLmI8Qvpxg5OBuopVllzUOF00hYyLaqtmpr+DaXHt0AVHu3HfH07TDGrVlqbrKo+BPLxv7bOvVJ5z5Ab/H1KcrgiVipcCunqCK+wnLTSeeIQF7x/lclo/SvWalSj+zQ+GfipZpI5R5ZHpFkPAq8OLly6ErKsqnwN8H2r", "type": "rsa", } res = session.post( f"{API_ENDPOINT}/v1/keys?version=2021-05-06&generation=2", json=body) allowed_body_data = ["public_key", "name", "resource_group", "type"] for k, v in body.items(): assert k in allowed_body_data check_required_params(res) check_valid_key(res.json())
def test_patch_vpc_by_id(): vpc_id = _adapter.vpc_id body = {"name": "test2-updated"} res = session.patch( f"{API_ENDPOINT}/v1/vpcs/{vpc_id}?version=2021-05-06&generation=2", json=body) assert re.search(r"v1/vpcs/(.*?)\?[vg]", res.url) check_required_params(res) data = res.json() name = data.get("name", "") assert "name" in body.keys() assert re.match(SUBNET_NAME_REGEX, name) assert 1 <= len(name) <= 63 check_valid_vpc(data) assert data.get("name") == body.get("name")
def test_get_keys(): res = session.get( f"{API_ENDPOINT}/v1/keys?version=2021-05-06&generation=2") query_params = dict(parse_qsl(urlparse(res.url).query)) optional_params = ["resource_group.id"] response = res.json()["keys"] # -- testing request params check_required_params(res) for k, v in query_params.items(): # check that query has the correct params assert k in (REQUIRED_PARAMS + optional_params) # check if query params has any value assert v # -- testing response for data in response: check_valid_key(data)
def test_patch_subnet_by_id(): subnet_id = _adapter.subnet_id body = {"name": "my-subnet-1-modified"} res = session.patch( f"{API_ENDPOINT}/v1/subnets/{subnet_id}?version=2021-05-06&generation=2", data=body, ) assert re.search(r"v1/subnets/(.*?)\?[vg]", res.url) check_required_params(res) request_body = dict(parse_qsl(res.request.body)) name = request_body.get("name", "") assert "name" in request_body.keys() assert re.match(SUBNET_NAME_REGEX, name) assert 1 <= len(name) <= 63 assert body["name"] == res.json().get("name") check_valid_subnet(res.json())
def test_post_instances(): body = { "boot_volume_attachment": { "volume": { "encryption_key": { "crn": "crn:[...]" }, "name": "my-boot-volume", "profile": { "name": "general-purpose" }, } }, "image": { "id": "9aaf3bcb-dcd7-4de7-bb60-24e39ff9d366" }, "keys": [{ "id": "363f6d70-0000-0001-0000-00000013b96c" }], "name": "my-instance", "placement_target": { "id": "0787-8c2a09be-ee18-4af2-8ef4-6a6060732221" }, "primary_network_interface": { "name": "my-network-interface", "subnet": { "id": "bea6a632-5e13-42a4-b4b8-31dc877abfe4" }, }, "profile": { "name": "bx2-2x8" }, "volume_attachments": [{ "volume": { "capacity": 1000, "encryption_key": { "crn": "crn:[...]" }, "name": "my-data-volume", "profile": { "name": "5iops-tier" }, } }], "vpc": { "id": "f0aae929-7047-46d1-92e1-9102b07a7f6f" }, "zone": { "name": "us-south-1" }, } res = session.post( f"{API_ENDPOINT}/v1/instances?version=2021-05-06&generation=2", json=body) allowed_body_data = [ "keys", "name", "network_interfaces", "placement_target", "profile", "resource_group", "user_data", "volume_attachments", "vpc", "image", "boot_volume_attachment", "primary_network_interface", 'zone', ] for k, v in body.items(): assert k in allowed_body_data check_required_params(res) check_valid_instance(res.json())
def test_post_subnets(): body = { "name": "my-subnet-1", "ipv4_cidr_block": "10.0.1.0/24", "ip_version": "ipv4", "zone": { "name": "us-south-1" }, "vpc": { "id": _adapter.vpc_id }, "network_acl": { "id": "r006-848ce071-a794-4948-833b-82fba500dc61" }, } res = session.post( f"{API_ENDPOINT}/v1/subnets?version=2021-05-06&generation=2", json=body) check_required_params(res) allowed_body_params = [ "vpc", "ip_version", "name", "network_acl", "resource_group", "routing_table", # to double check "ipv4_cidr_block", "total_ipv4_address_count", "zone", ] required_body_data = ["vpc"] req_body = json.loads(res.request.body) for param in required_body_data: assert param in req_body.keys() vpc_body = req_body.get("vpc") check_valid_params(RESPONSE_KEYS, vpc_body) for k, v in vpc_body.items(): assert k in ID_KEYS for k, v in req_body.items(): assert k in allowed_body_params + ["public_gateway"] if k == "ip_version": assert v in ["ipv4"] if k == "name": assert 1 <= len(v) <= 63 assert re.match(SUBNET_NAME_REGEX, v) if k == "network_acl": assert len(v.keys()) == 1 check_valid_params(ID_KEYS, v) if k == "public_gateway": assert len(v.keys()) == 1 check_valid_params(ID_KEYS, v) if k == "resource_group": assert re.match(RESOURCE_GROUP_ID_REGEX, v.get("id")) if k == "routing_table": assert len(v.keys()) == 1 check_valid_params(["id", "href"], v) # testing for response for k in ID_KEYS + allowed_body_params: assert k in res.json().keys() check_valid_subnet(res.json())