Skip to content

Commit 4d3c836

Browse files
committed
refactor unit test into group and object directories
1 parent 8e5134e commit 4d3c836

15 files changed

+610
-501
lines changed

test/unit/groups/database_test.py

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