Skip to content

Commit e87a949

Browse files
test: add vpc tests (#541)
Co-authored-by: Zhiwei Liang <[email protected]>
1 parent b9bba92 commit e87a949

File tree

4 files changed

+394
-0
lines changed

4 files changed

+394
-0
lines changed

tests/integration/conftest.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,31 @@ def nodebalancer_with_default_conf():
374374
res_arr = result.split(",")
375375
nodebalancer_id = res_arr[0]
376376
delete_target_id(target="nodebalancers", id=nodebalancer_id)
377+
378+
379+
def get_regions_with_capabilities(capabilities):
380+
regions = (
381+
exec_test_command(
382+
[
383+
"linode-cli",
384+
"regions",
385+
"ls",
386+
"--text",
387+
"--no-headers",
388+
"--format=id,capabilities",
389+
]
390+
)
391+
.stdout.decode()
392+
.rstrip()
393+
)
394+
395+
regions = regions.split("\n")
396+
397+
regions_with_all_caps = []
398+
399+
for region in regions:
400+
region_name = region.split()[0]
401+
if all(capability in region for capability in capabilities):
402+
regions_with_all_caps.append(region_name)
403+
404+
return regions_with_all_caps

tests/integration/vpc/__init__.py

Whitespace-only changes.

tests/integration/vpc/conftest.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import time
2+
3+
import pytest
4+
5+
from tests.integration.conftest import get_regions_with_capabilities
6+
from tests.integration.helpers import delete_target_id, exec_test_command
7+
8+
9+
@pytest.fixture
10+
def test_vpc_w_subnet():
11+
region = get_regions_with_capabilities(["VPCs"])[0]
12+
13+
vpc_label = str(time.time_ns()) + "label"
14+
15+
subnet_label = str(time.time_ns()) + "label"
16+
17+
vpc_id = (
18+
exec_test_command(
19+
[
20+
"linode-cli",
21+
"vpcs",
22+
"create",
23+
"--label",
24+
vpc_label,
25+
"--region",
26+
region,
27+
"--subnets.ipv4",
28+
"10.0.0.0/24",
29+
"--subnets.label",
30+
subnet_label,
31+
"--no-headers",
32+
"--text",
33+
"--format=id",
34+
]
35+
)
36+
.stdout.decode()
37+
.rstrip()
38+
)
39+
40+
yield vpc_id
41+
42+
delete_target_id(target="vpcs", id=vpc_id)
43+
44+
45+
@pytest.fixture
46+
def test_vpc_wo_subnet():
47+
region = get_regions_with_capabilities(["VPCs"])[0]
48+
49+
label = str(time.time_ns()) + "label"
50+
51+
vpc_id = (
52+
exec_test_command(
53+
[
54+
"linode-cli",
55+
"vpcs",
56+
"create",
57+
"--label",
58+
label,
59+
"--region",
60+
region,
61+
"--no-headers",
62+
"--text",
63+
"--format=id",
64+
]
65+
)
66+
.stdout.decode()
67+
.rstrip()
68+
)
69+
70+
yield vpc_id
71+
72+
delete_target_id(target="vpcs", id=vpc_id)
73+
74+
75+
@pytest.fixture
76+
def test_subnet(test_vpc_wo_subnet):
77+
vpc_id = test_vpc_wo_subnet
78+
subnet_label = str(time.time_ns()) + "label"
79+
res = (
80+
exec_test_command(
81+
[
82+
"linode-cli",
83+
"vpcs",
84+
"subnet-create",
85+
"--label",
86+
subnet_label,
87+
"--ipv4",
88+
"10.0.0.0/24",
89+
vpc_id,
90+
"--text",
91+
"--no-headers",
92+
"--delimiter=,",
93+
]
94+
)
95+
.stdout.decode()
96+
.rstrip()
97+
)
98+
99+
yield res, subnet_label

tests/integration/vpc/test_vpc.py

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
import re
2+
import time
3+
4+
from tests.integration.conftest import get_regions_with_capabilities
5+
from tests.integration.helpers import (
6+
exec_failing_test_command,
7+
exec_test_command,
8+
)
9+
10+
BASE_CMD = ["linode-cli", "vpcs"]
11+
12+
13+
def test_list_vpcs(test_vpc_wo_subnet):
14+
vpc_id = test_vpc_wo_subnet
15+
res = (
16+
exec_test_command(BASE_CMD + ["ls", "--text"]).stdout.decode().rstrip()
17+
)
18+
headers = ["id", "label", "description", "region"]
19+
20+
for header in headers:
21+
assert header in res
22+
assert vpc_id in res
23+
24+
25+
def test_view_vpc(test_vpc_wo_subnet):
26+
vpc_id = test_vpc_wo_subnet
27+
28+
res = (
29+
exec_test_command(BASE_CMD + ["view", vpc_id, "--text", "--no-headers"])
30+
.stdout.decode()
31+
.rstrip()
32+
)
33+
34+
assert vpc_id in res
35+
36+
37+
def test_update_vpc(test_vpc_wo_subnet):
38+
vpc_id = test_vpc_wo_subnet
39+
40+
new_label = str(time.time_ns()) + "label"
41+
42+
updated_label = (
43+
exec_test_command(
44+
BASE_CMD
45+
+ [
46+
"update",
47+
vpc_id,
48+
"--label",
49+
new_label,
50+
"--description",
51+
"new description",
52+
"--text",
53+
"--no-headers",
54+
"--format=label",
55+
]
56+
)
57+
.stdout.decode()
58+
.rstrip()
59+
)
60+
61+
description = (
62+
exec_test_command(
63+
BASE_CMD
64+
+ ["view", vpc_id, "--text", "--no-headers", "--format=description"]
65+
)
66+
.stdout.decode()
67+
.rstrip()
68+
)
69+
70+
assert new_label == updated_label
71+
assert "new description" in description
72+
73+
74+
def test_list_subnets(test_vpc_w_subnet):
75+
vpc_id = test_vpc_w_subnet
76+
77+
res = (
78+
exec_test_command(
79+
BASE_CMD + ["subnets-list", vpc_id, "--text", "--delimiter=,"]
80+
)
81+
.stdout.decode()
82+
.rstrip()
83+
)
84+
85+
lines = res.splitlines()
86+
87+
headers = ["id", "label", "ipv4", "linodes"]
88+
89+
for header in headers:
90+
assert header in lines[0]
91+
92+
for line in lines[1:]:
93+
assert re.match(
94+
r"^(\d+),(\w+),(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d+),", line
95+
), "String format does not match"
96+
97+
98+
def test_view_subnet(test_vpc_wo_subnet, test_subnet):
99+
# note calling test_subnet fixture will add subnet to test_vpc_wo_subnet
100+
res, label = test_subnet
101+
102+
res = res.split(",")
103+
104+
vpc_subnet_id = res[0]
105+
106+
vpc_id = test_vpc_wo_subnet
107+
108+
output = (
109+
exec_test_command(
110+
BASE_CMD + ["subnet-view", vpc_id, vpc_subnet_id, "--text"]
111+
)
112+
.stdout.decode()
113+
.rstrip()
114+
)
115+
116+
headers = ["id", "label", "ipv4", "linodes"]
117+
118+
for header in headers:
119+
assert header in output
120+
assert vpc_subnet_id in output
121+
122+
123+
def test_update_subnet(test_vpc_w_subnet):
124+
vpc_id = test_vpc_w_subnet
125+
126+
new_label = str(time.time_ns()) + "label"
127+
128+
subnet_id = (
129+
exec_test_command(
130+
BASE_CMD
131+
+ ["subnets-list", vpc_id, "--text", "--format=id", "--no-headers"]
132+
)
133+
.stdout.decode()
134+
.rstrip()
135+
)
136+
137+
updated_label = (
138+
exec_test_command(
139+
BASE_CMD
140+
+ [
141+
"subnet-update",
142+
vpc_id,
143+
subnet_id,
144+
"--label",
145+
new_label,
146+
"--text",
147+
"--format=label",
148+
"--no-headers",
149+
]
150+
)
151+
.stdout.decode()
152+
.rstrip()
153+
)
154+
155+
assert new_label == updated_label
156+
157+
158+
def test_fails_to_create_vpc_invalid_label():
159+
invalid_label = "invalid_label"
160+
region = get_regions_with_capabilities(["VPCs"])[0]
161+
162+
res = (
163+
exec_failing_test_command(
164+
BASE_CMD + ["create", "--label", invalid_label, "--region", region]
165+
)
166+
.stderr.decode()
167+
.rstrip()
168+
)
169+
170+
assert "Request failed: 400" in res
171+
assert "Label must include only ASCII" in res
172+
173+
174+
def test_fails_to_create_vpc_duplicate_label(test_vpc_wo_subnet):
175+
vpc_id = test_vpc_wo_subnet
176+
label = (
177+
exec_test_command(
178+
BASE_CMD
179+
+ ["view", vpc_id, "--text", "--no-headers", "--format=label"]
180+
)
181+
.stdout.decode()
182+
.rstrip()
183+
)
184+
region = get_regions_with_capabilities(["VPCs"])[0]
185+
186+
res = (
187+
exec_failing_test_command(
188+
BASE_CMD + ["create", "--label", label, "--region", region]
189+
)
190+
.stderr.decode()
191+
.rstrip()
192+
)
193+
194+
assert "Label must be unique among your VPCs" in res
195+
196+
197+
def test_fails_to_update_vpc_invalid_label(test_vpc_wo_subnet):
198+
vpc_id = test_vpc_wo_subnet
199+
invalid_label = "invalid_label"
200+
201+
res = (
202+
exec_failing_test_command(
203+
BASE_CMD + ["update", vpc_id, "--label", invalid_label]
204+
)
205+
.stderr.decode()
206+
.rstrip()
207+
)
208+
209+
assert "Request failed: 400" in res
210+
assert "Label must include only ASCII" in res
211+
212+
213+
def test_fails_to_create_vpc_subnet_w_invalid_label(test_vpc_wo_subnet):
214+
vpc_id = test_vpc_wo_subnet
215+
invalid_label = "invalid_label"
216+
region = get_regions_with_capabilities(["VPCs"])[0]
217+
218+
res = exec_failing_test_command(
219+
BASE_CMD
220+
+ [
221+
"subnet-create",
222+
"--label",
223+
invalid_label,
224+
"--ipv4",
225+
"10.1.0.0/24",
226+
vpc_id,
227+
]
228+
).stderr.decode()
229+
230+
assert "Request failed: 400" in res
231+
assert "Label must include only ASCII" in res
232+
233+
234+
def test_fails_to_update_vpc_subenet_w_invalid_label(test_vpc_w_subnet):
235+
vpc_id = test_vpc_w_subnet
236+
237+
invalid_label = "invalid_label"
238+
239+
subnet_id = (
240+
exec_test_command(
241+
BASE_CMD
242+
+ ["subnets-list", vpc_id, "--text", "--format=id", "--no-headers"]
243+
)
244+
.stdout.decode()
245+
.rstrip()
246+
)
247+
248+
res = (
249+
exec_failing_test_command(
250+
BASE_CMD
251+
+ [
252+
"subnet-update",
253+
vpc_id,
254+
subnet_id,
255+
"--label",
256+
invalid_label,
257+
"--text",
258+
"--format=label",
259+
"--no-headers",
260+
]
261+
)
262+
.stderr.decode()
263+
.rstrip()
264+
)
265+
266+
assert "Request failed: 400" in res
267+
assert "Label must include only ASCII" in res

0 commit comments

Comments
 (0)