Skip to content

Commit d3de874

Browse files
authored
test: Refactor unit tests into group and object directories (#499)
* refactor unit test into group and object directories * address codeQL warnings * fix import
1 parent f2b4732 commit d3de874

15 files changed

+627
-308
lines changed

test/unit/groups/database_test.py

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
import logging
2+
from test.unit.base import ClientBaseCase
3+
4+
from linode_api4.objects import MySQLDatabase
5+
6+
logger = logging.getLogger(__name__)
7+
8+
9+
class DatabaseTest(ClientBaseCase):
10+
"""
11+
Tests methods of the DatabaseGroup class
12+
"""
13+
14+
def test_get_types(self):
15+
"""
16+
Test that database types are properly handled
17+
"""
18+
types = self.client.database.types()
19+
20+
self.assertEqual(len(types), 1)
21+
self.assertEqual(types[0].type_class, "nanode")
22+
self.assertEqual(types[0].id, "g6-nanode-1")
23+
self.assertEqual(types[0].engines.mysql[0].price.monthly, 20)
24+
25+
def test_get_engines(self):
26+
"""
27+
Test that database engines are properly handled
28+
"""
29+
engines = self.client.database.engines()
30+
31+
self.assertEqual(len(engines), 2)
32+
33+
self.assertEqual(engines[0].engine, "mysql")
34+
self.assertEqual(engines[0].id, "mysql/8.0.26")
35+
self.assertEqual(engines[0].version, "8.0.26")
36+
37+
self.assertEqual(engines[1].engine, "postgresql")
38+
self.assertEqual(engines[1].id, "postgresql/10.14")
39+
self.assertEqual(engines[1].version, "10.14")
40+
41+
def test_get_databases(self):
42+
"""
43+
Test that databases are properly handled
44+
"""
45+
dbs = self.client.database.instances()
46+
47+
self.assertEqual(len(dbs), 1)
48+
self.assertEqual(dbs[0].allow_list[1], "192.0.1.0/24")
49+
self.assertEqual(dbs[0].cluster_size, 3)
50+
self.assertEqual(dbs[0].encrypted, False)
51+
self.assertEqual(dbs[0].engine, "mysql")
52+
self.assertEqual(
53+
dbs[0].hosts.primary,
54+
"lin-123-456-mysql-mysql-primary.servers.linodedb.net",
55+
)
56+
self.assertEqual(
57+
dbs[0].hosts.secondary,
58+
"lin-123-456-mysql-primary-private.servers.linodedb.net",
59+
)
60+
self.assertEqual(dbs[0].id, 123)
61+
self.assertEqual(dbs[0].region, "us-east")
62+
self.assertEqual(dbs[0].updates.duration, 3)
63+
self.assertEqual(dbs[0].version, "8.0.26")
64+
65+
def test_database_instance(self):
66+
"""
67+
Ensures that the .instance attribute properly translates database types
68+
"""
69+
70+
dbs = self.client.database.instances()
71+
db_translated = dbs[0].instance
72+
73+
self.assertTrue(isinstance(db_translated, MySQLDatabase))
74+
self.assertEqual(db_translated.ssl_connection, True)
75+
76+
77+
class MySQLDatabaseTest(ClientBaseCase):
78+
"""
79+
Tests methods of the MySQLDatabase class
80+
"""
81+
82+
def test_get_instances(self):
83+
"""
84+
Test that database types are properly handled
85+
"""
86+
dbs = self.client.database.mysql_instances()
87+
88+
self.assertEqual(len(dbs), 1)
89+
self.assertEqual(dbs[0].allow_list[1], "192.0.1.0/24")
90+
self.assertEqual(dbs[0].cluster_size, 3)
91+
self.assertEqual(dbs[0].encrypted, False)
92+
self.assertEqual(dbs[0].engine, "mysql")
93+
self.assertEqual(
94+
dbs[0].hosts.primary,
95+
"lin-123-456-mysql-mysql-primary.servers.linodedb.net",
96+
)
97+
self.assertEqual(
98+
dbs[0].hosts.secondary,
99+
"lin-123-456-mysql-primary-private.servers.linodedb.net",
100+
)
101+
self.assertEqual(dbs[0].id, 123)
102+
self.assertEqual(dbs[0].region, "us-east")
103+
self.assertEqual(dbs[0].updates.duration, 3)
104+
self.assertEqual(dbs[0].version, "8.0.26")
105+
106+
def test_create(self):
107+
"""
108+
Test that MySQL databases can be created
109+
"""
110+
111+
with self.mock_post("/databases/mysql/instances") as m:
112+
# We don't care about errors here; we just want to
113+
# validate the request.
114+
try:
115+
self.client.database.mysql_create(
116+
"cool",
117+
"us-southeast",
118+
"mysql/8.0.26",
119+
"g6-standard-1",
120+
cluster_size=3,
121+
)
122+
except Exception as e:
123+
logger.warning(
124+
"An error occurred while validating the request: %s", e
125+
)
126+
127+
self.assertEqual(m.method, "post")
128+
self.assertEqual(m.call_url, "/databases/mysql/instances")
129+
self.assertEqual(m.call_data["label"], "cool")
130+
self.assertEqual(m.call_data["region"], "us-southeast")
131+
self.assertEqual(m.call_data["engine"], "mysql/8.0.26")
132+
self.assertEqual(m.call_data["type"], "g6-standard-1")
133+
self.assertEqual(m.call_data["cluster_size"], 3)
134+
135+
136+
class PostgreSQLDatabaseTest(ClientBaseCase):
137+
"""
138+
Tests methods of the PostgreSQLDatabase class
139+
"""
140+
141+
def test_get_instances(self):
142+
"""
143+
Test that database types are properly handled
144+
"""
145+
dbs = self.client.database.postgresql_instances()
146+
147+
self.assertEqual(len(dbs), 1)
148+
self.assertEqual(dbs[0].allow_list[1], "192.0.1.0/24")
149+
self.assertEqual(dbs[0].cluster_size, 3)
150+
self.assertEqual(dbs[0].encrypted, False)
151+
self.assertEqual(dbs[0].engine, "postgresql")
152+
self.assertEqual(
153+
dbs[0].hosts.primary,
154+
"lin-0000-000-pgsql-primary.servers.linodedb.net",
155+
)
156+
self.assertEqual(
157+
dbs[0].hosts.secondary,
158+
"lin-0000-000-pgsql-primary-private.servers.linodedb.net",
159+
)
160+
self.assertEqual(dbs[0].id, 123)
161+
self.assertEqual(dbs[0].region, "us-east")
162+
self.assertEqual(dbs[0].updates.duration, 3)
163+
self.assertEqual(dbs[0].version, "13.2")
164+
165+
def test_create(self):
166+
"""
167+
Test that PostgreSQL databases can be created
168+
"""
169+
170+
with self.mock_post("/databases/postgresql/instances") as m:
171+
# We don't care about errors here; we just want to
172+
# validate the request.
173+
try:
174+
self.client.database.postgresql_create(
175+
"cool",
176+
"us-southeast",
177+
"postgresql/13.2",
178+
"g6-standard-1",
179+
cluster_size=3,
180+
)
181+
except Exception as e:
182+
logger.warning(
183+
"An error occurred while validating the request: %s", e
184+
)
185+
186+
self.assertEqual(m.method, "post")
187+
self.assertEqual(m.call_url, "/databases/postgresql/instances")
188+
self.assertEqual(m.call_data["label"], "cool")
189+
self.assertEqual(m.call_data["region"], "us-southeast")
190+
self.assertEqual(m.call_data["engine"], "postgresql/13.2")
191+
self.assertEqual(m.call_data["type"], "g6-standard-1")
192+
self.assertEqual(m.call_data["cluster_size"], 3)

test/unit/groups/image_test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from test.unit.base import ClientBaseCase
2+
3+
4+
class ImageTest(ClientBaseCase):
5+
"""
6+
Tests methods of the Image class
7+
"""
8+
9+
def test_image_create_cloud_init(self):
10+
"""
11+
Test that an image can be created successfully with cloud-init.
12+
"""
13+
14+
with self.mock_post("images/private/123") as m:
15+
self.client.images.create(
16+
"Test Image",
17+
"us-southeast",
18+
description="very real image upload.",
19+
cloud_init=True,
20+
)
21+
22+
self.assertTrue(m.call_data["cloud_init"])
23+
24+
def test_image_create_upload_cloud_init(self):
25+
"""
26+
Test that an image upload URL can be created successfully with cloud-init.
27+
"""
28+
29+
with self.mock_post("images/upload") as m:
30+
self.client.images.create_upload(
31+
"Test Image",
32+
"us-southeast",
33+
description="very real image upload.",
34+
cloud_init=True,
35+
)
36+
37+
self.assertTrue(m.call_data["cloud_init"])

test/unit/groups/linode_test.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
from test.unit.base import ClientBaseCase
2+
3+
from linode_api4 import InstancePlacementGroupAssignment
4+
from linode_api4.objects import ConfigInterface
5+
6+
7+
class LinodeTest(ClientBaseCase):
8+
"""
9+
Tests methods of the Linode class
10+
"""
11+
12+
def test_instance_create_with_user_data(self):
13+
"""
14+
Tests that the metadata field is populated on Linode create.
15+
"""
16+
17+
with self.mock_post("linode/instances/123") as m:
18+
self.client.linode.instance_create(
19+
"g6-nanode-1",
20+
"us-southeast",
21+
metadata=self.client.linode.build_instance_metadata(
22+
user_data="cool"
23+
),
24+
)
25+
26+
self.assertEqual(
27+
m.call_data,
28+
{
29+
"region": "us-southeast",
30+
"type": "g6-nanode-1",
31+
"metadata": {"user_data": "Y29vbA=="},
32+
},
33+
)
34+
35+
def test_instance_create_with_interfaces(self):
36+
"""
37+
Tests that user can pass a list of interfaces on Linode create.
38+
"""
39+
interfaces = [
40+
{"purpose": "public"},
41+
ConfigInterface(
42+
purpose="vlan", label="cool-vlan", ipam_address="10.0.0.4/32"
43+
),
44+
]
45+
with self.mock_post("linode/instances/123") as m:
46+
self.client.linode.instance_create(
47+
"us-southeast",
48+
"g6-nanode-1",
49+
interfaces=interfaces,
50+
)
51+
52+
self.assertEqual(
53+
m.call_data["interfaces"],
54+
[
55+
{"purpose": "public"},
56+
{
57+
"purpose": "vlan",
58+
"label": "cool-vlan",
59+
"ipam_address": "10.0.0.4/32",
60+
},
61+
],
62+
)
63+
64+
def test_build_instance_metadata(self):
65+
"""
66+
Tests that the metadata field is built correctly.
67+
"""
68+
self.assertEqual(
69+
self.client.linode.build_instance_metadata(user_data="cool"),
70+
{"user_data": "Y29vbA=="},
71+
)
72+
73+
self.assertEqual(
74+
self.client.linode.build_instance_metadata(
75+
user_data="cool", encode_user_data=False
76+
),
77+
{"user_data": "cool"},
78+
)
79+
80+
def test_create_with_placement_group(self):
81+
"""
82+
Tests that you can create a Linode with a Placement Group
83+
"""
84+
85+
with self.mock_post("linode/instances/123") as m:
86+
self.client.linode.instance_create(
87+
"g6-nanode-1",
88+
"eu-west",
89+
placement_group=InstancePlacementGroupAssignment(
90+
id=123,
91+
compliant_only=True,
92+
),
93+
)
94+
95+
self.assertEqual(
96+
m.call_data["placement_group"], {"id": 123, "compliant_only": True}
97+
)
98+
99+
100+
class TypeTest(ClientBaseCase):
101+
def test_get_types(self):
102+
"""
103+
Tests that Linode types can be returned
104+
"""
105+
types = self.client.linode.types()
106+
107+
self.assertEqual(len(types), 5)
108+
for t in types:
109+
self.assertTrue(t._populated)
110+
self.assertIsNotNone(t.id)
111+
self.assertIsNotNone(t.label)
112+
self.assertIsNotNone(t.disk)
113+
self.assertIsNotNone(t.type_class)
114+
self.assertIsNotNone(t.gpus)
115+
self.assertIsNone(t.successor)
116+
self.assertIsNotNone(t.region_prices)
117+
self.assertIsNotNone(t.addons.backups.region_prices)
118+
self.assertIsNotNone(t.accelerated_devices)

test/unit/groups/lke_test.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from test.unit.base import ClientBaseCase
2+
3+
from linode_api4.objects import (
4+
LKEClusterControlPlaneACLAddressesOptions,
5+
LKEClusterControlPlaneACLOptions,
6+
LKEClusterControlPlaneOptions,
7+
)
8+
9+
10+
class LKETest(ClientBaseCase):
11+
"""
12+
Tests methods of the LKE class
13+
"""
14+
15+
def test_cluster_create_with_acl(self):
16+
"""
17+
Tests that an LKE cluster can be created with a control plane ACL configuration.
18+
"""
19+
20+
with self.mock_post("lke/clusters") as m:
21+
self.client.lke.cluster_create(
22+
"us-mia",
23+
"test-acl-cluster",
24+
[self.client.lke.node_pool("g6-nanode-1", 3)],
25+
"1.29",
26+
control_plane=LKEClusterControlPlaneOptions(
27+
acl=LKEClusterControlPlaneACLOptions(
28+
enabled=True,
29+
addresses=LKEClusterControlPlaneACLAddressesOptions(
30+
ipv4=["10.0.0.1/32"], ipv6=["1234::5678"]
31+
),
32+
)
33+
),
34+
)
35+
36+
assert "high_availability" not in m.call_data["control_plane"]
37+
assert m.call_data["control_plane"]["acl"]["enabled"]
38+
assert m.call_data["control_plane"]["acl"]["addresses"]["ipv4"] == [
39+
"10.0.0.1/32"
40+
]
41+
assert m.call_data["control_plane"]["acl"]["addresses"]["ipv6"] == [
42+
"1234::5678"
43+
]

0 commit comments

Comments
 (0)