Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 192 additions & 0 deletions test/unit/groups/database_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
import logging
from test.unit.base import ClientBaseCase

from linode_api4.objects import MySQLDatabase

logger = logging.getLogger(__name__)


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 as e:
logger.warning(
"An error occurred while validating the request: %s", e
)

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 as e:
logger.warning(
"An error occurred while validating the request: %s", e
)

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)
37 changes: 37 additions & 0 deletions test/unit/groups/image_test.py
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"])
118 changes: 118 additions & 0 deletions test/unit/groups/linode_test.py
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)
43 changes: 43 additions & 0 deletions test/unit/groups/lke_test.py
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"
]
Loading