|
1 | 1 | import uuid |
| 2 | +import time |
| 3 | +from typing import Generator |
2 | 4 |
|
3 | 5 | import pytest |
4 | 6 |
|
5 | | -from scaleway.block.v1 import BlockV1API |
6 | | -from scaleway.instance.v1 import InstanceV1API, VolumeServerTemplate, VolumeVolumeType, BootType |
7 | 7 | from scaleway_core.client import Client |
| 8 | +from scaleway.instance.v1.api import InstanceV1API |
| 9 | +from scaleway.instance.v1.types import VolumeVolumeType, BootType, Server, VolumeServerTemplate |
| 10 | +from scaleway.block.v1alpha1 import BlockV1Alpha1API, Volume |
| 11 | +from scaleway.block.v1alpha1.types import Volume, CreateVolumeRequestFromEmpty |
8 | 12 | from vcr_config import scw_vcr |
9 | 13 |
|
10 | 14 | server_name = f"test-sdk-python-{uuid.uuid4().hex[:6]}" |
11 | 15 | max_retry = 10 |
12 | 16 | interval = 0.01 |
13 | | -volume_size = 10000000000 |
| 17 | +volume_size = 10_000_000_000 |
14 | 18 | commercial_type = "DEV1-S" |
15 | 19 | zone = "fr-par-1" |
16 | | -image_id = "c00ae53c-1e29-4087-a384-47f3c5c1cd84" |
17 | 20 |
|
18 | | -@pytest.fixture(scope="module") |
| 21 | + |
| 22 | +@pytest.fixture |
19 | 23 | @scw_vcr.use_cassette |
20 | | -def instance_api(): |
| 24 | +def apis() -> Generator[tuple[InstanceV1API, BlockV1Alpha1API, str, Server | None, list[Volume]], None, None]: |
21 | 25 | client = Client.from_config_file_and_env() |
22 | | - api = InstanceV1API(client) |
23 | | - return api, client.default_project_id, zone |
| 26 | + instanceAPI = InstanceV1API(client, bypass_validation=True) |
| 27 | + blockAPI = BlockV1Alpha1API(client, bypass_validation=True) |
| 28 | + |
| 29 | + created_server: Server | None = None |
| 30 | + created_volumes: list[Volume] = [] |
| 31 | + |
| 32 | + yield instanceAPI, blockAPI, zone, created_server, created_volumes |
| 33 | + |
| 34 | + for volume in created_volumes: |
| 35 | + if created_server: |
| 36 | + instanceAPI.detach_server_volume( |
| 37 | + server_id=created_server.id, volume_id=volume.id |
| 38 | + ) |
| 39 | + blockAPI.wait_for_volume(volume_id=volume.id, zone=zone) |
| 40 | + wait_test_instance_server(instanceAPI, created_server.id, zone) |
| 41 | + blockAPI.delete_volume(volume_id=volume.id) |
| 42 | + |
| 43 | + if created_server: |
| 44 | + instanceAPI.delete_server(zone=zone, server_id=created_server.id) |
24 | 45 |
|
25 | | -@pytest.fixture(scope="module") |
26 | 46 | @scw_vcr.use_cassette |
27 | | -def block_api(): |
28 | | - client = Client.from_config_file_and_env() |
29 | | - api = BlockV1API(client) |
30 | | - return api, client.default_project_id, zone |
| 47 | +def wait_test_instance_server(instanceAPI: InstanceV1API, server_id: str, zone: str) -> Server: |
| 48 | + delay = interval |
| 49 | + for i in range(1, max_retry): |
| 50 | + delay *= i |
| 51 | + s = instanceAPI.get_server(zone=zone, server_id=server_id) |
| 52 | + if s.server.state in ("running", "stopped"): |
| 53 | + return s.server |
| 54 | + time.sleep(delay) |
| 55 | + pytest.fail("Server did not reach 'running' or 'stopped' state in time.") |
31 | 56 |
|
32 | 57 | @scw_vcr.use_cassette |
33 | | -def test_instance_create(instance_api): |
34 | | - api, project_id, zone = instance_api |
| 58 | +def create_test_instance_server(instanceAPI: InstanceV1API, zone: str) -> Server: |
35 | 59 | volumes = { |
36 | 60 | "0": VolumeServerTemplate( |
37 | 61 | volume_type=VolumeVolumeType.L_SSD, |
38 | 62 | size=volume_size, |
39 | 63 | boot=False, |
40 | 64 | ) |
41 | 65 | } |
42 | | - instance = api.create_instance(project_id=project_id, zone=zone, name=server_name, dynamic_ip_required=False, volume=volumes, protected=False, boot_type=BootType.LOCAL, image=image_id) |
43 | | - assert instance.name == server_name |
44 | | - assert instance.project_id == project_id |
45 | | - assert instance.zone == zone |
46 | | - assert instance.id is not None |
| 66 | + server = instanceAPI._create_server( |
| 67 | + commercial_type=commercial_type, |
| 68 | + zone=zone, |
| 69 | + name=server_name, |
| 70 | + dynamic_ip_required=False, |
| 71 | + volumes=volumes, |
| 72 | + protected=False, |
| 73 | + boot_type=BootType.LOCAL, |
| 74 | + image="c00ae53c-1e29-4087-a384-47f3c5c1cd84", |
| 75 | + ) |
| 76 | + wait_test_instance_server(instanceAPI, server.server.id, zone) |
| 77 | + return server.server |
| 78 | + |
| 79 | +@scw_vcr.use_cassette |
| 80 | +def create_test_from_empty_volume(blockAPI: BlockV1Alpha1API, number: int, zone: str) -> list[Volume]: |
| 81 | + volumes: list[Volume] = [] |
| 82 | + for _ in range(number): |
| 83 | + volume = blockAPI.create_volume( |
| 84 | + from_empty=CreateVolumeRequestFromEmpty(size=1_000_000_000), |
| 85 | + ) |
| 86 | + blockAPI.wait_for_volume(volume_id=volume.id, zone=zone) |
| 87 | + volumes.append(volume) |
| 88 | + return volumes |
| 89 | + |
| 90 | +@scw_vcr.use_cassette |
| 91 | +def test_attach_additional_volume(apis): |
| 92 | + instanceAPI, blockAPI, zone, created_server, created_volumes = apis |
| 93 | + |
| 94 | + server = create_test_instance_server(instanceAPI, zone) |
| 95 | + |
| 96 | + additional_volumes = create_test_from_empty_volume(blockAPI, 1, zone) |
| 97 | + created_volumes.extend(additional_volumes) |
| 98 | + |
| 99 | + additional_volume = additional_volumes[0] |
| 100 | + |
| 101 | + assert server.id is not None |
| 102 | + assert server.zone == zone |
| 103 | + |
| 104 | + assert additional_volume.id is not None |
| 105 | + assert additional_volume.size == 1_000_000_000 |
| 106 | + |
| 107 | + instanceAPI.attach_server_volume( |
| 108 | + server_id=server.id, |
| 109 | + volume_id=additional_volume.id, |
| 110 | + volume_type=VolumeVolumeType.SBS_VOLUME, |
| 111 | + ) |
| 112 | + |
| 113 | + blockAPI.wait_for_volume(volume_id=additional_volume.id, zone=zone) |
| 114 | + |
| 115 | + updated_server = instanceAPI.get_server(zone=zone, server_id=server.id) |
| 116 | + attached_volumes = updated_server.server.volumes or {} |
| 117 | + attached_volume_ids = [v.id for v in attached_volumes.values()] |
47 | 118 |
|
| 119 | + assert additional_volume.id in attached_volume_ids |
0 commit comments