Skip to content

Commit 3f348cb

Browse files
committed
add tests to close issues
1 parent 0eccb0a commit 3f348cb

File tree

5 files changed

+139
-22
lines changed

5 files changed

+139
-22
lines changed

scaleway/scaleway/instance/v1/custom_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
max_retry = 10
1313
interval = 0.01
1414

15+
1516
class InstanceUtilsV1API(InstanceV1API):
1617
"""
1718
This API extends InstanceV1API by adding utility methods for managing Instance resources,

scaleway/scaleway/instance/v1/tests/test_instance_sdk.py

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33

44
import pytest
55

6+
from scaleway.instance.v1 import IpType
67
from scaleway.instance.v1.custom_api import InstanceUtilsV1API
78
from scaleway_async.instance.v1 import AttachServerVolumeRequestVolumeType
89
from scaleway_core.client import Client
9-
from scaleway.instance.v1.types import VolumeVolumeType, BootType, Server, VolumeServerTemplate
10+
from scaleway.instance.v1.types import (
11+
VolumeVolumeType,
12+
BootType,
13+
Server,
14+
VolumeServerTemplate,
15+
)
1016
from scaleway.block.v1alpha1 import BlockV1Alpha1API
1117
from scaleway.block.v1alpha1.types import Volume, CreateVolumeRequestFromEmpty
1218
from vcr_config import scw_vcr
@@ -18,6 +24,9 @@
1824
commercial_type = "DEV1-S"
1925
zone = "fr-par-1"
2026

27+
# mypy: ignore-errors
28+
29+
2130
@pytest.fixture(scope="module")
2231
@scw_vcr.use_cassette
2332
def instance_block_api() -> tuple[InstanceUtilsV1API, BlockV1Alpha1API]:
@@ -26,10 +35,12 @@ def instance_block_api() -> tuple[InstanceUtilsV1API, BlockV1Alpha1API]:
2635
block_api = BlockV1Alpha1API(client)
2736
return instance_api, block_api
2837

38+
2939
@pytest.fixture(scope="module")
3040
@scw_vcr.use_cassette
31-
def instance_volume(instance_block_api: tuple[InstanceUtilsV1API, BlockV1Alpha1API]) -> Generator[
32-
tuple[Server | None, list[Volume]], None, None]:
41+
def instance_volume(
42+
instance_block_api: tuple[InstanceUtilsV1API, BlockV1Alpha1API],
43+
) -> Generator[tuple[Server | None, list[Volume]], None, None]:
3344
instance_api, block_api = instance_block_api
3445
volumes_list: list[Volume] = []
3546
volumes = {
@@ -52,40 +63,59 @@ def instance_volume(instance_block_api: tuple[InstanceUtilsV1API, BlockV1Alpha1A
5263
instance_api.wait_test_instance_server(instance.server.id, zone=zone)
5364
yield instance.server, volumes_list
5465
for volume in volumes_list:
55-
instance_api.detach_server_volume(server_id=instance.server.id, volume_id=volume.id, zone=zone)
66+
instance_api.detach_server_volume(
67+
server_id=instance.server.id, volume_id=volume.id, zone=zone
68+
)
5669
block_api.wait_for_volume(volume_id=volume.id, zone=zone)
5770
block_api.delete_volume(volume_id=volume.id, zone=zone)
5871
instance_api.delete_server(server_id=instance.server.id, zone=zone)
5972

73+
6074
@scw_vcr.use_cassette
61-
def test_attach_additionnal_volume(instance_block_api: tuple[InstanceUtilsV1API, BlockV1Alpha1API], instance_volume: tuple[Server, list[Volume]]) -> None:
75+
def test_attach_additionnal_volume(
76+
instance_block_api: tuple[InstanceUtilsV1API, BlockV1Alpha1API],
77+
instance_volume: tuple[Server, list[Volume]],
78+
) -> None:
6279
instance_api, block_api = instance_block_api
6380
server, volumes_list = instance_volume
6481
additional_volume = block_api.create_volume(
6582
from_empty=CreateVolumeRequestFromEmpty(size=volume_size),
6683
)
67-
updated_volume = block_api.wait_for_volume(volume_id=additional_volume.id, zone=zone)
84+
updated_volume = block_api.wait_for_volume(
85+
volume_id=additional_volume.id, zone=zone
86+
)
6887
volumes_list.append(updated_volume)
6988
assert updated_volume.size == volume_size
7089
assert updated_volume.id is not None
7190

72-
instance_api.attach_server_volume(server_id=server.id, volume_id=additional_volume.id, zone=zone, volume_type=AttachServerVolumeRequestVolumeType.SBS_VOLUME)
91+
instance_api.attach_server_volume(
92+
server_id=server.id,
93+
volume_id=additional_volume.id,
94+
zone=zone,
95+
volume_type=AttachServerVolumeRequestVolumeType.SBS_VOLUME,
96+
)
7397
instance_api.wait_test_instance_server(server_id=server.id, zone=zone)
7498
block_api.wait_for_volume(volume_id=additional_volume.id, zone=zone)
7599

76100
server_details = instance_api.get_server(server_id=server.id, zone=zone).server
77101
attached_volume_ids = [v.id for v in server_details.volumes.values()]
78102
assert additional_volume.id in attached_volume_ids
79103

104+
80105
@scw_vcr.use_cassette
81-
def test_list_server(instance_block_api: tuple[InstanceUtilsV1API, BlockV1Alpha1API]) -> None:
106+
def test_list_server(
107+
instance_block_api: tuple[InstanceUtilsV1API, BlockV1Alpha1API],
108+
) -> None:
82109
instance_api, block_api = instance_block_api
83110
servers = instance_api.list_servers(zone=zone)
84-
assert len(servers.servers) == 1
111+
assert len(servers.servers) >= 1
85112
assert servers.servers[0].name == server_name
86113

114+
87115
@scw_vcr.use_cassette
88-
def test_create_new_server(instance_block_api: tuple[InstanceUtilsV1API, BlockV1Alpha1API]) -> None:
116+
def test_create_new_server(
117+
instance_block_api: tuple[InstanceUtilsV1API, BlockV1Alpha1API],
118+
) -> None:
89119
instance_api, block_api = instance_block_api
90120
server = instance_api._create_server(
91121
commercial_type=commercial_type,
@@ -99,10 +129,16 @@ def test_create_new_server(instance_block_api: tuple[InstanceUtilsV1API, BlockV1
99129
assert server.server.id is not None
100130
assert server.server.name == server_name
101131

102-
servers = instance_api.list_servers(zone=zone)
103-
assert len(servers.servers) == 2
132+
servers = instance_api.list_servers_all(zone=zone)
133+
assert len(servers) >= 2
104134

105135
instance_api.delete_server(server_id=server.server.id, zone=zone)
106136

107137

108-
138+
@scw_vcr.use_cassette
139+
def test_create_ip(
140+
instance_block_api: tuple[InstanceUtilsV1API, BlockV1Alpha1API],
141+
) -> None:
142+
instance_api, block_api = instance_block_api
143+
ip = instance_api.create_ip(type_=IpType.ROUTED_IPV6).ip
144+
assert ip.id is not None
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from typing import Any, Generator
2+
3+
import pytest
4+
5+
from scaleway.k8s.v1 import K8SV1API, CNI
6+
from scaleway.vpc.v2 import VpcV2API, PrivateNetwork
7+
from scaleway_core.api import ScalewayException
8+
from scaleway_core.client import Client
9+
from vcr_config import scw_vcr
10+
11+
# mypy: ignore-errors
12+
13+
14+
@pytest.fixture(scope="module")
15+
@scw_vcr.use_cassette
16+
def k8s_api() -> K8SV1API:
17+
client = Client.from_config_file_and_env()
18+
k8s_api = K8SV1API(client)
19+
return k8s_api
20+
21+
22+
@pytest.fixture(scope="module")
23+
@scw_vcr.use_cassette
24+
def private_network() -> Generator[PrivateNetwork, Any, None]:
25+
client = Client.from_config_file_and_env()
26+
vpc_api = VpcV2API(client)
27+
vpc = vpc_api.create_vpc(enable_routing=False)
28+
pn = vpc_api.create_private_network(
29+
vpc_id=vpc.id, default_route_propagation_enabled=True
30+
)
31+
yield pn
32+
vpc_api.delete_vpc(vpc_id=vpc.id)
33+
34+
35+
def safe_wait_for_cluster(k8s_api: K8SV1API, cluster_id: str):
36+
try:
37+
return k8s_api.wait_for_cluster(cluster_id=cluster_id)
38+
except ScalewayException as e:
39+
err_type = getattr(e, "type", None) or getattr(e, "error", None)
40+
if err_type == "not_found" or "not found" in str(e).lower():
41+
return None
42+
raise
43+
44+
45+
@scw_vcr.use_cassette
46+
def test_k8s_cluster_list(k8s_api: K8SV1API, private_network: PrivateNetwork) -> None:
47+
cluster = k8s_api.create_cluster(
48+
type_="kapsule",
49+
version="1.32.7",
50+
cni=CNI.CILIUM,
51+
description="",
52+
private_network_id=private_network.id,
53+
)
54+
assert cluster.id is not None
55+
56+
clusters = k8s_api.list_clusters()
57+
assert clusters.total_count >= 1
58+
59+
k8s_api.delete_cluster(cluster_id=cluster.id, with_additional_resources=True)
60+
cluster = safe_wait_for_cluster(k8s_api=k8s_api, cluster_id=cluster.id)
61+
assert cluster is None

scaleway/scaleway/vpc/v2/tests/test_vpc_sdk.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
import pytest
44

5-
from scaleway.vpc.v2 import VpcV2API, VPC, PrivateNetwork, ListVPCsResponse, ListPrivateNetworksResponse
5+
from scaleway.vpc.v2 import (
6+
VpcV2API,
7+
VPC,
8+
PrivateNetwork,
9+
ListVPCsResponse,
10+
ListPrivateNetworksResponse,
11+
)
612
from scaleway_core.api import ScalewayException
713
from scaleway_core.client import Client
814
from scaleway_core.utils import random_name
@@ -15,12 +21,13 @@
1521

1622
# mypy: ignore-errors
1723

24+
1825
@pytest.fixture(scope="module")
1926
@scw_vcr.use_cassette
2027
def vpc_api() -> tuple[VpcV2API, str | None, str | None]:
21-
client = Client.from_config_file_and_env()
22-
api = VpcV2API(client)
23-
return api, client.default_project_id, region
28+
client = Client.from_config_file_and_env()
29+
api = VpcV2API(client)
30+
return api, client.default_project_id, region
2431

2532

2633
@pytest.fixture(scope="module")
@@ -39,7 +46,9 @@ def vpc(vpc_api: tuple[VpcV2API, str, str]) -> Generator[VPC, None, None]:
3946

4047
@pytest.fixture
4148
@scw_vcr.use_cassette
42-
def private_networks_to_cleanup(vpc_api: tuple[VpcV2API, str, str]) -> Generator[list[PrivateNetwork], None, None]:
49+
def private_networks_to_cleanup(
50+
vpc_api: tuple[VpcV2API, str, str],
51+
) -> Generator[list[PrivateNetwork], None, None]:
4352
api, _, _ = vpc_api
4453
items: list[PrivateNetwork] = []
4554
yield items
@@ -75,7 +84,11 @@ def test_vpc_list(vpc_api: tuple[VpcV2API, str, str]) -> None:
7584

7685

7786
@scw_vcr.use_cassette
78-
def test_private_network_create(vpc_api: tuple[VpcV2API, str, str], vpc: VPC , private_networks_to_cleanup: list[PrivateNetwork]) -> None:
87+
def test_private_network_create(
88+
vpc_api: tuple[VpcV2API, str, str],
89+
vpc: VPC,
90+
private_networks_to_cleanup: list[PrivateNetwork],
91+
) -> None:
7992
api, project_id, _ = vpc_api
8093
for i in range(created_pn_count):
8194
pn: PrivateNetwork = api.create_private_network(

scaleway/vcr_config.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@
44
import vcr
55
import inspect
66

7-
PYTHON_UPDATE_CASSETTE = os.getenv("PYTHON_UPDATE_CASSETTE", "false").lower() in ("1", "true", "yes")
7+
PYTHON_UPDATE_CASSETTE = os.getenv("PYTHON_UPDATE_CASSETTE", "false").lower() in (
8+
"1",
9+
"true",
10+
"yes",
11+
)
12+
813

914
def func_path(function):
1015
path = Path(Path(inspect.getfile(function)).parent, "cassettes")
1116
Path.mkdir(path, exist_ok=True)
1217
filename = function.__name__ + ".cassette.yaml"
1318
return Path(path, filename)
1419

20+
1521
scw_vcr = vcr.VCR(
1622
record_mode="all" if PYTHON_UPDATE_CASSETTE else "none",
1723
filter_headers=["x-auth-token"],
18-
func_path_generator=func_path
19-
)
24+
func_path_generator=func_path,
25+
)

0 commit comments

Comments
 (0)