Skip to content

Commit 99de488

Browse files
authored
Merge pull request #394 from linode/migrate-bats-to-pytest
migrate: bats test to pytest
2 parents 9a18154 + 8bbd7ac commit 99de488

30 files changed

+3639
-6
lines changed

tests/__init__.py

Whitespace-only changes.

tests/integration/__init__.py

Whitespace-only changes.

tests/integration/cli/test_help.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from tests.integration.helpers import exec_test_command
2+
3+
4+
def test_help_page_for_non_aliased_actions():
5+
process = exec_test_command(["linode-cli", "linodes", "list", "--help"])
6+
output = process.stdout.decode()
7+
8+
assert "Linodes List" in output
9+
assert (
10+
"API Documentation: https://www.linode.com/docs/api/linode-instances/#linodes-list"
11+
in output
12+
)
13+
assert "You may filter results with:" in output
14+
assert "--tags" in output
15+
16+
17+
def test_help_page_for_aliased_actions():
18+
process = exec_test_command(["linode-cli", "linodes", "ls", "--help"])
19+
output = process.stdout.decode()
20+
21+
assert "Linodes List" in output
22+
assert (
23+
"API Documentation: https://www.linode.com/docs/api/linode-instances/#linodes-list"
24+
in output
25+
)
26+
assert "You may filter results with:" in output
27+
assert "--tags" in output
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import os
2+
3+
from tests.integration.helpers import INVALID_HOST, exec_failing_test_command
4+
5+
6+
def test_cli_command_fails_to_access_invalid_host(monkeypatch):
7+
monkeypatch.setenv("LINODE_CLI_API_HOST", INVALID_HOST)
8+
9+
process = exec_failing_test_command(["linode-cli", "linodes", "ls"])
10+
output = process.stderr.decode()
11+
12+
assert "Max retries exceeded with url: //wrongapi.linode.com" in output
13+
14+
15+
def test_cli_uses_v4beta_when_override_is_set(monkeypatch):
16+
monkeypatch.setenv("LINODE_CLI_API_VERSION", "v4beta")
17+
os.system("linode-cli linodes ls --debug 2>&1 | tee /tmp/output_file.txt")
18+
19+
result = os.popen("cat /tmp/output_file.txt").read()
20+
assert "v4beta" in result
21+
22+
23+
def test_cli_command_fails_to_access_invalid_api_scheme(monkeypatch):
24+
monkeypatch.setenv("LINODE_CLI_API_SCHEME", "ssh")
25+
process = exec_failing_test_command(["linode-cli", "linodes", "ls"])
26+
output = process.stderr.decode()
27+
28+
assert "ssh://" in output

tests/integration/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
from typing import Callable, Optional
1212

1313
import pytest
14-
from helpers import get_random_text
1514

1615
from linodecli import ENV_TOKEN_NAME
16+
from tests.integration.helpers import get_random_text
1717

1818

1919
@pytest.fixture(scope="session")
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
import logging
2+
import re
3+
import time
4+
5+
import pytest
6+
7+
from tests.integration.helpers import (
8+
SUCCESS_STATUS_CODE,
9+
delete_all_domains,
10+
exec_test_command,
11+
)
12+
13+
BASE_CMD = ["linode-cli", "domains"]
14+
15+
16+
@pytest.fixture(scope="session", autouse=True)
17+
def domain_records_setup():
18+
# Create one domain for some tests in this suite
19+
try:
20+
timestamp = str(int(time.time()))
21+
# Create domain
22+
domain_id = (
23+
exec_test_command(
24+
BASE_CMD
25+
+ [
26+
"create",
27+
"--type",
28+
"master",
29+
"--domain",
30+
timestamp + "example.com",
31+
32+
"--text",
33+
"--no-header",
34+
"--format=id",
35+
]
36+
)
37+
.stdout.decode()
38+
.rstrip()
39+
)
40+
41+
# Create record
42+
record_id = (
43+
exec_test_command(
44+
BASE_CMD
45+
+ [
46+
"records-create",
47+
"--protocol=tcp",
48+
"--type=SRV",
49+
"--port=23",
50+
"--priority=4",
51+
"--service=telnet",
52+
"--target=8.8.8.8",
53+
"--weight=4",
54+
"--text",
55+
"--no-header",
56+
"--delimiter=,",
57+
"--format=id",
58+
domain_id,
59+
]
60+
)
61+
.stdout.decode()
62+
.rstrip()
63+
)
64+
65+
except:
66+
logging.exception("Failed creating domain in setup")
67+
68+
yield domain_id, record_id
69+
70+
try:
71+
delete_all_domains()
72+
except:
73+
logging.exception("Failed to delete all domains")
74+
75+
76+
def test_create_a_domain():
77+
timestamp = str(int(time.time()))
78+
79+
# Current domain list
80+
process = exec_test_command(
81+
BASE_CMD + ["list", '--format="id"', "--text", "--no-header"]
82+
)
83+
output_current = process.stdout.decode()
84+
85+
# Create domain
86+
exec_test_command(
87+
BASE_CMD
88+
+ [
89+
"create",
90+
"--type",
91+
"master",
92+
"--domain",
93+
timestamp + "example.com",
94+
95+
"--text",
96+
"--no-header",
97+
]
98+
)
99+
100+
process = exec_test_command(
101+
BASE_CMD + ["list", "--format=id", "--text", "--no-header"]
102+
)
103+
output_after = process.stdout.decode()
104+
105+
# Check if list is bigger than previous list
106+
assert (
107+
len(output_after.splitlines()) > len(output_current.splitlines()),
108+
"the list is not updated with new domain..",
109+
)
110+
111+
112+
def test_create_domain_srv_record(domain_records_setup):
113+
domain_id = domain_records_setup[0]
114+
115+
process = exec_test_command(
116+
BASE_CMD
117+
+ [
118+
"records-create",
119+
"--protocol=tcp",
120+
"--type=SRV",
121+
"--port=23",
122+
"--priority=4",
123+
"--service=telnet",
124+
"--target=8.8.8.8",
125+
"--weight=4",
126+
"--text",
127+
"--no-header",
128+
"--delimiter=,",
129+
domain_id,
130+
]
131+
)
132+
133+
output = process.stdout.decode()
134+
135+
assert (
136+
re.search("[0-9]+,SRV,_telnet._tcp,8.8.8.8,0,4,4", output),
137+
"Output does not match the format",
138+
)
139+
140+
141+
def test_list_srv_record(domain_records_setup):
142+
domain_id = domain_records_setup[0]
143+
process = exec_test_command(
144+
BASE_CMD
145+
+ [
146+
"records-list",
147+
domain_id,
148+
"--text",
149+
"--no-header",
150+
"--delimiter=,",
151+
]
152+
)
153+
output = process.stdout.decode()
154+
155+
assert (
156+
re.search("[0-9]+,SRV,_telnet._tcp,8.8.8.8,0,4,4", output),
157+
"Output does not match the format",
158+
)
159+
160+
161+
def test_view_domain_record(domain_records_setup):
162+
domain_id = domain_records_setup[0]
163+
record_id = domain_records_setup[1]
164+
165+
process = exec_test_command(
166+
BASE_CMD
167+
+ [
168+
"records-view",
169+
domain_id,
170+
record_id,
171+
"--target= 8.8.4.4",
172+
"--text",
173+
"--no-header",
174+
"--delimiter=,",
175+
]
176+
)
177+
output = process.stdout.decode()
178+
179+
assert (
180+
re.search("[0-9]+,SRV,_telnet._tcp,8.8.8.8,0,4,4", output),
181+
"Output does not match the format",
182+
)
183+
184+
185+
def test_update_domain_record(domain_records_setup):
186+
domain_id = domain_records_setup[0]
187+
record_id = domain_records_setup[1]
188+
189+
process = exec_test_command(
190+
BASE_CMD
191+
+ [
192+
"records-update",
193+
domain_id,
194+
record_id,
195+
"--target= 8.8.4.4",
196+
"--text",
197+
"--no-header",
198+
"--delimiter=,",
199+
]
200+
)
201+
output = process.stdout.decode()
202+
203+
assert (
204+
re.search("[0-9]+,SRV,_telnet._tcp,8.8.8.8,0,4,4", output),
205+
"Output does not match the format",
206+
)
207+
208+
209+
def test_delete_a_domain_record(domain_records_setup):
210+
domain_id = domain_records_setup[0]
211+
record_id = domain_records_setup[1]
212+
213+
process = exec_test_command(
214+
BASE_CMD + ["records-delete", domain_id, record_id]
215+
)
216+
217+
# Assert on status code returned from deleting domain
218+
assert process.returncode == SUCCESS_STATUS_CODE
219+
220+
221+
def test_delete_all_domains():
222+
delete_all_domains()
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import logging
2+
import re
3+
import time
4+
5+
import pytest
6+
7+
from tests.integration.helpers import (
8+
delete_all_domains,
9+
delete_tag,
10+
exec_failing_test_command,
11+
exec_test_command,
12+
)
13+
14+
BASE_CMD = ["linode-cli", "domains"]
15+
16+
17+
@pytest.fixture(scope="session", autouse=True)
18+
def domain_tags_setup():
19+
yield "setup"
20+
try:
21+
delete_all_domains()
22+
except:
23+
logging.exception("Failed to delete all domains")
24+
25+
26+
# @pytest.mark.skip(reason="BUG 943")
27+
def test_fail_to_create_master_domain_with_invalid_tags():
28+
timestamp = str(int(time.time()))
29+
bad_tag = "*"
30+
31+
exec_failing_test_command(
32+
BASE_CMD
33+
+ [
34+
"create",
35+
"--type",
36+
"master",
37+
"--domain",
38+
timestamp + "example.com",
39+
"--soa_email=" + timestamp + "[email protected]",
40+
"--text",
41+
"--no-header",
42+
"--format=id",
43+
"--tag",
44+
bad_tag,
45+
]
46+
)
47+
48+
49+
# @pytest.mark.skip(reason="BUG 943")
50+
def test_fail_to_create_slave_domain_with_invalid_tags():
51+
timestamp = str(int(time.time()))
52+
bad_tag = "*"
53+
54+
exec_failing_test_command(
55+
BASE_CMD
56+
+ [
57+
"create",
58+
"--type",
59+
"slave",
60+
"--domain",
61+
timestamp + "example.com",
62+
"--soa_email=" + timestamp + "[email protected]",
63+
"--text",
64+
"--no-header",
65+
"--format=id",
66+
"--tag",
67+
bad_tag,
68+
]
69+
)
70+
71+
72+
# @pytest.mark.skip(reason="BUG 943")
73+
def test_create_master_domain_with_tags():
74+
timestamp = str(int(time.time()))
75+
tag = "foo"
76+
77+
process = exec_test_command(
78+
BASE_CMD
79+
+ [
80+
"create",
81+
"--type",
82+
"master",
83+
"--domain",
84+
timestamp + "-example.com",
85+
"--soa_email=" + timestamp + "[email protected]",
86+
"--text",
87+
"--no-header",
88+
"--delimiter=,",
89+
"--format=id,domain,type,status,tags",
90+
"--tag",
91+
tag,
92+
]
93+
)
94+
output = process.stdout.decode()
95+
assert re.search("[0-9]+,[0-9]+-example.com,master,active," + tag, output)
96+
97+
98+
# @pytest.mark.skip(reason="BUG 943")
99+
def test_delete_domain_and_tag():
100+
# need to check if tag foo is still present while running this test
101+
result = exec_test_command(["linode-cli", "tags", "list"]).stdout.decode()
102+
if "foo" in result:
103+
delete_tag("foo")
104+
delete_all_domains()
105+
else:
106+
delete_all_domains()

0 commit comments

Comments
 (0)