-
Notifications
You must be signed in to change notification settings - Fork 85
test: Refactor unit tests into group and object directories #499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ykim-akamai
merged 4 commits into
linode:dev
from
ykim-akamai:test/refactor_unit_tests_into_group_and_objects
Feb 4, 2025
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| from test.unit.base import ClientBaseCase | ||
|
|
||
| from linode_api4.objects import MySQLDatabase | ||
|
|
||
|
|
||
| class DatabaseTest(ClientBaseCase): | ||
| """ | ||
| Tests methods of the DatabaseGroup class | ||
| """ | ||
|
|
||
| def test_get_types(self): | ||
| """ | ||
| Test that database types are properly handled | ||
| """ | ||
| types = self.client.database.types() | ||
|
|
||
| self.assertEqual(len(types), 1) | ||
| self.assertEqual(types[0].type_class, "nanode") | ||
| self.assertEqual(types[0].id, "g6-nanode-1") | ||
| self.assertEqual(types[0].engines.mysql[0].price.monthly, 20) | ||
|
|
||
| def test_get_engines(self): | ||
| """ | ||
| Test that database engines are properly handled | ||
| """ | ||
| engines = self.client.database.engines() | ||
|
|
||
| self.assertEqual(len(engines), 2) | ||
|
|
||
| self.assertEqual(engines[0].engine, "mysql") | ||
| self.assertEqual(engines[0].id, "mysql/8.0.26") | ||
| self.assertEqual(engines[0].version, "8.0.26") | ||
|
|
||
| self.assertEqual(engines[1].engine, "postgresql") | ||
| self.assertEqual(engines[1].id, "postgresql/10.14") | ||
| self.assertEqual(engines[1].version, "10.14") | ||
|
|
||
| def test_get_databases(self): | ||
| """ | ||
| Test that databases are properly handled | ||
| """ | ||
| dbs = self.client.database.instances() | ||
|
|
||
| self.assertEqual(len(dbs), 1) | ||
| self.assertEqual(dbs[0].allow_list[1], "192.0.1.0/24") | ||
| self.assertEqual(dbs[0].cluster_size, 3) | ||
| self.assertEqual(dbs[0].encrypted, False) | ||
| self.assertEqual(dbs[0].engine, "mysql") | ||
| self.assertEqual( | ||
| dbs[0].hosts.primary, | ||
| "lin-123-456-mysql-mysql-primary.servers.linodedb.net", | ||
| ) | ||
| self.assertEqual( | ||
| dbs[0].hosts.secondary, | ||
| "lin-123-456-mysql-primary-private.servers.linodedb.net", | ||
| ) | ||
| self.assertEqual(dbs[0].id, 123) | ||
| self.assertEqual(dbs[0].region, "us-east") | ||
| self.assertEqual(dbs[0].updates.duration, 3) | ||
| self.assertEqual(dbs[0].version, "8.0.26") | ||
|
|
||
| def test_database_instance(self): | ||
| """ | ||
| Ensures that the .instance attribute properly translates database types | ||
| """ | ||
|
|
||
| dbs = self.client.database.instances() | ||
| db_translated = dbs[0].instance | ||
|
|
||
| self.assertTrue(isinstance(db_translated, MySQLDatabase)) | ||
| self.assertEqual(db_translated.ssl_connection, True) | ||
|
|
||
|
|
||
| class MySQLDatabaseTest(ClientBaseCase): | ||
| """ | ||
| Tests methods of the MySQLDatabase class | ||
| """ | ||
|
|
||
| def test_get_instances(self): | ||
| """ | ||
| Test that database types are properly handled | ||
| """ | ||
| dbs = self.client.database.mysql_instances() | ||
|
|
||
| self.assertEqual(len(dbs), 1) | ||
| self.assertEqual(dbs[0].allow_list[1], "192.0.1.0/24") | ||
| self.assertEqual(dbs[0].cluster_size, 3) | ||
| self.assertEqual(dbs[0].encrypted, False) | ||
| self.assertEqual(dbs[0].engine, "mysql") | ||
| self.assertEqual( | ||
| dbs[0].hosts.primary, | ||
| "lin-123-456-mysql-mysql-primary.servers.linodedb.net", | ||
| ) | ||
| self.assertEqual( | ||
| dbs[0].hosts.secondary, | ||
| "lin-123-456-mysql-primary-private.servers.linodedb.net", | ||
| ) | ||
| self.assertEqual(dbs[0].id, 123) | ||
| self.assertEqual(dbs[0].region, "us-east") | ||
| self.assertEqual(dbs[0].updates.duration, 3) | ||
| self.assertEqual(dbs[0].version, "8.0.26") | ||
|
|
||
| def test_create(self): | ||
| """ | ||
| Test that MySQL databases can be created | ||
| """ | ||
|
|
||
| with self.mock_post("/databases/mysql/instances") as m: | ||
| # We don't care about errors here; we just want to | ||
| # validate the request. | ||
| try: | ||
| self.client.database.mysql_create( | ||
| "cool", | ||
| "us-southeast", | ||
| "mysql/8.0.26", | ||
| "g6-standard-1", | ||
| cluster_size=3, | ||
| ) | ||
| except Exception: | ||
| pass | ||
|
|
||
| self.assertEqual(m.method, "post") | ||
| self.assertEqual(m.call_url, "/databases/mysql/instances") | ||
| self.assertEqual(m.call_data["label"], "cool") | ||
| self.assertEqual(m.call_data["region"], "us-southeast") | ||
| self.assertEqual(m.call_data["engine"], "mysql/8.0.26") | ||
| self.assertEqual(m.call_data["type"], "g6-standard-1") | ||
| self.assertEqual(m.call_data["cluster_size"], 3) | ||
|
|
||
|
|
||
| class PostgreSQLDatabaseTest(ClientBaseCase): | ||
| """ | ||
| Tests methods of the PostgreSQLDatabase class | ||
| """ | ||
|
|
||
| def test_get_instances(self): | ||
| """ | ||
| Test that database types are properly handled | ||
| """ | ||
| dbs = self.client.database.postgresql_instances() | ||
|
|
||
| self.assertEqual(len(dbs), 1) | ||
| self.assertEqual(dbs[0].allow_list[1], "192.0.1.0/24") | ||
| self.assertEqual(dbs[0].cluster_size, 3) | ||
| self.assertEqual(dbs[0].encrypted, False) | ||
| self.assertEqual(dbs[0].engine, "postgresql") | ||
| self.assertEqual( | ||
| dbs[0].hosts.primary, | ||
| "lin-0000-000-pgsql-primary.servers.linodedb.net", | ||
| ) | ||
| self.assertEqual( | ||
| dbs[0].hosts.secondary, | ||
| "lin-0000-000-pgsql-primary-private.servers.linodedb.net", | ||
| ) | ||
| self.assertEqual(dbs[0].id, 123) | ||
| self.assertEqual(dbs[0].region, "us-east") | ||
| self.assertEqual(dbs[0].updates.duration, 3) | ||
| self.assertEqual(dbs[0].version, "13.2") | ||
|
|
||
| def test_create(self): | ||
| """ | ||
| Test that PostgreSQL databases can be created | ||
| """ | ||
|
|
||
| with self.mock_post("/databases/postgresql/instances") as m: | ||
| # We don't care about errors here; we just want to | ||
| # validate the request. | ||
| try: | ||
| self.client.database.postgresql_create( | ||
| "cool", | ||
| "us-southeast", | ||
| "postgresql/13.2", | ||
| "g6-standard-1", | ||
| cluster_size=3, | ||
| ) | ||
| except Exception: | ||
|
|
||
| pass | ||
|
|
||
| self.assertEqual(m.method, "post") | ||
| self.assertEqual(m.call_url, "/databases/postgresql/instances") | ||
| self.assertEqual(m.call_data["label"], "cool") | ||
| self.assertEqual(m.call_data["region"], "us-southeast") | ||
| self.assertEqual(m.call_data["engine"], "postgresql/13.2") | ||
| self.assertEqual(m.call_data["type"], "g6-standard-1") | ||
| self.assertEqual(m.call_data["cluster_size"], 3) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| from test.unit.base import ClientBaseCase | ||
|
|
||
|
|
||
| class ImageTest(ClientBaseCase): | ||
| """ | ||
| Tests methods of the Image class | ||
| """ | ||
|
|
||
| def test_image_create_cloud_init(self): | ||
| """ | ||
| Test that an image can be created successfully with cloud-init. | ||
| """ | ||
|
|
||
| with self.mock_post("images/private/123") as m: | ||
| self.client.images.create( | ||
| "Test Image", | ||
| "us-southeast", | ||
| description="very real image upload.", | ||
| cloud_init=True, | ||
| ) | ||
|
|
||
| self.assertTrue(m.call_data["cloud_init"]) | ||
|
|
||
| def test_image_create_upload_cloud_init(self): | ||
| """ | ||
| Test that an image upload URL can be created successfully with cloud-init. | ||
| """ | ||
|
|
||
| with self.mock_post("images/upload") as m: | ||
| self.client.images.create_upload( | ||
| "Test Image", | ||
| "us-southeast", | ||
| description="very real image upload.", | ||
| cloud_init=True, | ||
| ) | ||
|
|
||
| self.assertTrue(m.call_data["cloud_init"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| from test.unit.base import ClientBaseCase | ||
|
|
||
| from linode_api4 import InstancePlacementGroupAssignment | ||
| from linode_api4.objects import ConfigInterface | ||
|
|
||
|
|
||
| class LinodeTest(ClientBaseCase): | ||
| """ | ||
| Tests methods of the Linode class | ||
| """ | ||
|
|
||
| def test_instance_create_with_user_data(self): | ||
| """ | ||
| Tests that the metadata field is populated on Linode create. | ||
| """ | ||
|
|
||
| with self.mock_post("linode/instances/123") as m: | ||
| self.client.linode.instance_create( | ||
| "g6-nanode-1", | ||
| "us-southeast", | ||
| metadata=self.client.linode.build_instance_metadata( | ||
| user_data="cool" | ||
| ), | ||
| ) | ||
|
|
||
| self.assertEqual( | ||
| m.call_data, | ||
| { | ||
| "region": "us-southeast", | ||
| "type": "g6-nanode-1", | ||
| "metadata": {"user_data": "Y29vbA=="}, | ||
| }, | ||
| ) | ||
|
|
||
| def test_instance_create_with_interfaces(self): | ||
| """ | ||
| Tests that user can pass a list of interfaces on Linode create. | ||
| """ | ||
| interfaces = [ | ||
| {"purpose": "public"}, | ||
| ConfigInterface( | ||
| purpose="vlan", label="cool-vlan", ipam_address="10.0.0.4/32" | ||
| ), | ||
| ] | ||
| with self.mock_post("linode/instances/123") as m: | ||
| self.client.linode.instance_create( | ||
| "us-southeast", | ||
| "g6-nanode-1", | ||
| interfaces=interfaces, | ||
| ) | ||
|
|
||
| self.assertEqual( | ||
| m.call_data["interfaces"], | ||
| [ | ||
| {"purpose": "public"}, | ||
| { | ||
| "purpose": "vlan", | ||
| "label": "cool-vlan", | ||
| "ipam_address": "10.0.0.4/32", | ||
| }, | ||
| ], | ||
| ) | ||
|
|
||
| def test_build_instance_metadata(self): | ||
| """ | ||
| Tests that the metadata field is built correctly. | ||
| """ | ||
| self.assertEqual( | ||
| self.client.linode.build_instance_metadata(user_data="cool"), | ||
| {"user_data": "Y29vbA=="}, | ||
| ) | ||
|
|
||
| self.assertEqual( | ||
| self.client.linode.build_instance_metadata( | ||
| user_data="cool", encode_user_data=False | ||
| ), | ||
| {"user_data": "cool"}, | ||
| ) | ||
|
|
||
| def test_create_with_placement_group(self): | ||
| """ | ||
| Tests that you can create a Linode with a Placement Group | ||
| """ | ||
|
|
||
| with self.mock_post("linode/instances/123") as m: | ||
| self.client.linode.instance_create( | ||
| "g6-nanode-1", | ||
| "eu-west", | ||
| placement_group=InstancePlacementGroupAssignment( | ||
| id=123, | ||
| compliant_only=True, | ||
| ), | ||
| ) | ||
|
|
||
| self.assertEqual( | ||
| m.call_data["placement_group"], {"id": 123, "compliant_only": True} | ||
| ) | ||
|
|
||
|
|
||
| class TypeTest(ClientBaseCase): | ||
| def test_get_types(self): | ||
| """ | ||
| Tests that Linode types can be returned | ||
| """ | ||
| types = self.client.linode.types() | ||
|
|
||
| self.assertEqual(len(types), 5) | ||
| for t in types: | ||
| self.assertTrue(t._populated) | ||
| self.assertIsNotNone(t.id) | ||
| self.assertIsNotNone(t.label) | ||
| self.assertIsNotNone(t.disk) | ||
| self.assertIsNotNone(t.type_class) | ||
| self.assertIsNotNone(t.gpus) | ||
| self.assertIsNone(t.successor) | ||
| self.assertIsNotNone(t.region_prices) | ||
| self.assertIsNotNone(t.addons.backups.region_prices) | ||
| self.assertIsNotNone(t.accelerated_devices) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| from test.unit.base import ClientBaseCase | ||
|
|
||
| from linode_api4.objects import ( | ||
| LKEClusterControlPlaneACLAddressesOptions, | ||
| LKEClusterControlPlaneACLOptions, | ||
| LKEClusterControlPlaneOptions, | ||
| ) | ||
|
|
||
|
|
||
| class LKETest(ClientBaseCase): | ||
| """ | ||
| Tests methods of the LKE class | ||
| """ | ||
|
|
||
| def test_cluster_create_with_acl(self): | ||
| """ | ||
| Tests that an LKE cluster can be created with a control plane ACL configuration. | ||
| """ | ||
|
|
||
| with self.mock_post("lke/clusters") as m: | ||
| self.client.lke.cluster_create( | ||
| "us-mia", | ||
| "test-acl-cluster", | ||
| [self.client.lke.node_pool("g6-nanode-1", 3)], | ||
| "1.29", | ||
| control_plane=LKEClusterControlPlaneOptions( | ||
| acl=LKEClusterControlPlaneACLOptions( | ||
| enabled=True, | ||
| addresses=LKEClusterControlPlaneACLAddressesOptions( | ||
| ipv4=["10.0.0.1/32"], ipv6=["1234::5678"] | ||
| ), | ||
| ) | ||
| ), | ||
| ) | ||
|
|
||
| assert "high_availability" not in m.call_data["control_plane"] | ||
| assert m.call_data["control_plane"]["acl"]["enabled"] | ||
| assert m.call_data["control_plane"]["acl"]["addresses"]["ipv4"] == [ | ||
| "10.0.0.1/32" | ||
| ] | ||
| assert m.call_data["control_plane"]["acl"]["addresses"]["ipv6"] == [ | ||
| "1234::5678" | ||
| ] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.