From fcf75dfd60f3d717f967a07f18ff186205c374e9 Mon Sep 17 00:00:00 2001 From: Erik Olof Gunnar Andersson Date: Mon, 22 May 2017 15:05:34 -0700 Subject: [PATCH] Lazy load all configuration options This is a follow up to Change #444040. Configuration options should always be lazy loaded, this is to prevent changes in import order from interfering with the configuration variable being instantiated properly. Change-Id: I3040108128ba56746ed4d5d688b8b8d33da753fc Closes-Bug: #1694538 --- trove/common/glance_remote.py | 3 +- trove/common/remote.py | 28 +- .../experimental/cassandra/guestagent.py | 20 +- .../experimental/cassandra/taskmanager.py | 1 - .../experimental/galera_common/guestagent.py | 6 +- .../experimental/mongodb/guestagent.py | 23 +- .../experimental/mongodb/taskmanager.py | 1 - .../cluster/experimental/redis/guestagent.py | 12 +- .../cluster/experimental/redis/taskmanager.py | 1 - .../experimental/vertica/guestagent.py | 6 +- .../experimental/vertica/taskmanager.py | 1 - trove/common/trove_remote.py | 3 +- trove/db/__init__.py | 4 +- trove/guestagent/api.py | 121 ++++---- trove/guestagent/models.py | 4 +- trove/taskmanager/models.py | 29 +- trove/tests/fakes/guestagent.py | 2 +- .../unittests/taskmanager/test_models.py | 263 +++++++++--------- trove/tests/unittests/trove_testtools.py | 3 + 19 files changed, 271 insertions(+), 260 deletions(-) diff --git a/trove/common/glance_remote.py b/trove/common/glance_remote.py index d2f9fdd98b..ea0cfdd03e 100644 --- a/trove/common/glance_remote.py +++ b/trove/common/glance_remote.py @@ -51,4 +51,5 @@ def glance_client(context, region_name=None): session=session) -create_glance_client = import_class(CONF.remote_glance_client) +def create_glance_client(*arg, **kwargs): + return import_class(CONF.remote_glance_client)(*arg, **kwargs) diff --git a/trove/common/remote.py b/trove/common/remote.py index 53625e3d09..4809f99d58 100644 --- a/trove/common/remote.py +++ b/trove/common/remote.py @@ -172,9 +172,25 @@ def neutron_client(context, region_name=None): return client -create_dns_client = import_class(CONF.remote_dns_client) -create_guest_client = import_class(CONF.remote_guest_client) -create_nova_client = import_class(CONF.remote_nova_client) -create_swift_client = import_class(CONF.remote_swift_client) -create_cinder_client = import_class(CONF.remote_cinder_client) -create_neutron_client = import_class(CONF.remote_neutron_client) +def create_dns_client(*arg, **kwargs): + return import_class(CONF.remote_dns_client)(*arg, **kwargs) + + +def create_guest_client(*arg, **kwargs): + return import_class(CONF.remote_guest_client)(*arg, **kwargs) + + +def create_nova_client(*arg, **kwargs): + return import_class(CONF.remote_nova_client)(*arg, **kwargs) + + +def create_swift_client(*arg, **kwargs): + return import_class(CONF.remote_swift_client)(*arg, **kwargs) + + +def create_cinder_client(*arg, **kwargs): + return import_class(CONF.remote_cinder_client)(*arg, **kwargs) + + +def create_neutron_client(*arg, **kwargs): + return import_class(CONF.remote_neutron_client)(*arg, **kwargs) diff --git a/trove/common/strategies/cluster/experimental/cassandra/guestagent.py b/trove/common/strategies/cluster/experimental/cassandra/guestagent.py index 79a73b35a7..1f30771708 100644 --- a/trove/common/strategies/cluster/experimental/cassandra/guestagent.py +++ b/trove/common/strategies/cluster/experimental/cassandra/guestagent.py @@ -45,28 +45,28 @@ class CassandraGuestAgentAPI(guest_api.API): LOG.debug("Retrieving the data center for node: %s", self.id) version = guest_api.API.API_BASE_VERSION - return self._call("get_data_center", guest_api.AGENT_LOW_TIMEOUT, + return self._call("get_data_center", self.agent_low_timeout, version=version) def get_rack(self): LOG.debug("Retrieving the rack for node: %s", self.id) version = guest_api.API.API_BASE_VERSION - return self._call("get_rack", guest_api.AGENT_LOW_TIMEOUT, + return self._call("get_rack", self.agent_low_timeout, version=version) def set_seeds(self, seeds): LOG.debug("Configuring the gossip seeds for node: %s", self.id) version = guest_api.API.API_BASE_VERSION - return self._call("set_seeds", guest_api.AGENT_LOW_TIMEOUT, + return self._call("set_seeds", self.agent_low_timeout, version=version, seeds=seeds) def get_seeds(self): LOG.debug("Retrieving the gossip seeds for node: %s", self.id) version = guest_api.API.API_BASE_VERSION - return self._call("get_seeds", guest_api.AGENT_LOW_TIMEOUT, + return self._call("get_seeds", self.agent_low_timeout, version=version) def set_auto_bootstrap(self, enabled): @@ -74,7 +74,7 @@ class CassandraGuestAgentAPI(guest_api.API): "for node: %(id)s", {'enabled': enabled, 'id': self.id}) version = guest_api.API.API_BASE_VERSION - return self._call("set_auto_bootstrap", guest_api.AGENT_LOW_TIMEOUT, + return self._call("set_auto_bootstrap", self.agent_low_timeout, version=version, enabled=enabled) def cluster_complete(self): @@ -82,14 +82,14 @@ class CassandraGuestAgentAPI(guest_api.API): self.id) version = guest_api.API.API_BASE_VERSION - return self._call("cluster_complete", guest_api.AGENT_HIGH_TIMEOUT, + return self._call("cluster_complete", self.agent_high_timeout, version=version) def node_cleanup_begin(self): LOG.debug("Signaling the node to prepare for cleanup: %s", self.id) version = guest_api.API.API_BASE_VERSION - return self._call("node_cleanup_begin", guest_api.AGENT_LOW_TIMEOUT, + return self._call("node_cleanup_begin", self.agent_low_timeout, version=version) def node_cleanup(self): @@ -109,14 +109,14 @@ class CassandraGuestAgentAPI(guest_api.API): version = guest_api.API.API_BASE_VERSION return self._call( - "cluster_secure", guest_api.AGENT_HIGH_TIMEOUT, + "cluster_secure", self.agent_high_timeout, version=version, password=password) def get_admin_credentials(self): LOG.debug("Retrieving the admin credentials from node: %s", self.id) version = guest_api.API.API_BASE_VERSION - return self._call("get_admin_credentials", guest_api.AGENT_LOW_TIMEOUT, + return self._call("get_admin_credentials", self.agent_low_timeout, version=version) def store_admin_credentials(self, admin_credentials): @@ -124,6 +124,6 @@ class CassandraGuestAgentAPI(guest_api.API): version = guest_api.API.API_BASE_VERSION return self._call("store_admin_credentials", - guest_api.AGENT_LOW_TIMEOUT, + self.agent_low_timeout, version=version, admin_credentials=admin_credentials) diff --git a/trove/common/strategies/cluster/experimental/cassandra/taskmanager.py b/trove/common/strategies/cluster/experimental/cassandra/taskmanager.py index 0d1b3a1250..a641e837dd 100644 --- a/trove/common/strategies/cluster/experimental/cassandra/taskmanager.py +++ b/trove/common/strategies/cluster/experimental/cassandra/taskmanager.py @@ -28,7 +28,6 @@ import trove.taskmanager.models as task_models LOG = logging.getLogger(__name__) CONF = cfg.CONF -USAGE_SLEEP_TIME = CONF.usage_sleep_time # seconds. class CassandraTaskManagerStrategy(base.BaseTaskManagerStrategy): diff --git a/trove/common/strategies/cluster/experimental/galera_common/guestagent.py b/trove/common/strategies/cluster/experimental/galera_common/guestagent.py index 1161c38d36..f79c74b31e 100644 --- a/trove/common/strategies/cluster/experimental/galera_common/guestagent.py +++ b/trove/common/strategies/cluster/experimental/galera_common/guestagent.py @@ -67,7 +67,7 @@ class GaleraCommonGuestAgentAPI(guest_api.API): LOG.debug("Notifying cluster install completion.") version = guest_api.API.API_BASE_VERSION - return self._call("cluster_complete", guest_api.AGENT_HIGH_TIMEOUT, + return self._call("cluster_complete", self.agent_high_timeout, version=version) def get_cluster_context(self): @@ -75,7 +75,7 @@ class GaleraCommonGuestAgentAPI(guest_api.API): LOG.debug("Getting the cluster context.") version = guest_api.API.API_BASE_VERSION - return self._call("get_cluster_context", guest_api.AGENT_HIGH_TIMEOUT, + return self._call("get_cluster_context", self.agent_high_timeout, version=version) def write_cluster_configuration_overrides(self, cluster_configuration): @@ -84,6 +84,6 @@ class GaleraCommonGuestAgentAPI(guest_api.API): version = guest_api.API.API_BASE_VERSION self._call("write_cluster_configuration_overrides", - guest_api.AGENT_HIGH_TIMEOUT, + self.agent_high_timeout, version=version, cluster_configuration=cluster_configuration) diff --git a/trove/common/strategies/cluster/experimental/mongodb/guestagent.py b/trove/common/strategies/cluster/experimental/mongodb/guestagent.py index 6a7b1ff102..af33f33bec 100644 --- a/trove/common/strategies/cluster/experimental/mongodb/guestagent.py +++ b/trove/common/strategies/cluster/experimental/mongodb/guestagent.py @@ -22,7 +22,6 @@ from trove.guestagent import api as guest_api LOG = logging.getLogger(__name__) CONF = cfg.CONF -ADD_MEMBERS_TIMEOUT = CONF.mongodb.add_members_timeout class MongoDbGuestAgentStrategy(base.BaseGuestAgentStrategy): @@ -52,7 +51,7 @@ class MongoDbGuestAgentAPI(guest_api.API): 'id': self.id}) version = guest_api.API.API_BASE_VERSION - return self._call("add_shard", guest_api.AGENT_HIGH_TIMEOUT, + return self._call("add_shard", self.agent_high_timeout, version=version, replica_set_name=replica_set_name, replica_set_member=replica_set_member) @@ -62,7 +61,7 @@ class MongoDbGuestAgentAPI(guest_api.API): 'members': members, 'id': self.id}) version = guest_api.API.API_BASE_VERSION - return self._call("add_members", ADD_MEMBERS_TIMEOUT, + return self._call("add_members", CONF.mongodb.add_members_timeout, version=version, members=members) def add_config_servers(self, config_servers): @@ -71,7 +70,7 @@ class MongoDbGuestAgentAPI(guest_api.API): 'id': self.id}) version = guest_api.API.API_BASE_VERSION - return self._call("add_config_servers", guest_api.AGENT_HIGH_TIMEOUT, + return self._call("add_config_servers", self.agent_high_timeout, version=version, config_servers=config_servers) @@ -79,28 +78,28 @@ class MongoDbGuestAgentAPI(guest_api.API): LOG.debug("Notify regarding cluster install completion") version = guest_api.API.API_BASE_VERSION - return self._call("cluster_complete", guest_api.AGENT_HIGH_TIMEOUT, + return self._call("cluster_complete", self.agent_high_timeout, version=version) def get_key(self): LOG.debug("Requesting cluster key from guest") version = guest_api.API.API_BASE_VERSION - return self._call("get_key", guest_api.AGENT_LOW_TIMEOUT, + return self._call("get_key", self.agent_low_timeout, version=version) def prep_primary(self): LOG.debug("Preparing member to be primary member.") version = guest_api.API.API_BASE_VERSION - return self._call("prep_primary", guest_api.AGENT_HIGH_TIMEOUT, + return self._call("prep_primary", self.agent_high_timeout, version=version) def create_admin_user(self, password): LOG.debug("Creating admin user") version = guest_api.API.API_BASE_VERSION - return self._call("create_admin_user", guest_api.AGENT_HIGH_TIMEOUT, + return self._call("create_admin_user", self.agent_high_timeout, version=version, password=password) def store_admin_password(self, password): @@ -108,7 +107,7 @@ class MongoDbGuestAgentAPI(guest_api.API): version = guest_api.API.API_BASE_VERSION return self._call("store_admin_password", - guest_api.AGENT_LOW_TIMEOUT, + self.agent_low_timeout, version=version, password=password) @@ -117,7 +116,7 @@ class MongoDbGuestAgentAPI(guest_api.API): version = guest_api.API.API_BASE_VERSION return self._call("get_replica_set_name", - guest_api.AGENT_HIGH_TIMEOUT, + self.agent_high_timeout, version=version) def get_admin_password(self): @@ -125,7 +124,7 @@ class MongoDbGuestAgentAPI(guest_api.API): version = guest_api.API.API_BASE_VERSION return self._call("get_admin_password", - guest_api.AGENT_LOW_TIMEOUT, + self.agent_low_timeout, version=version) def is_shard_active(self, replica_set_name): @@ -133,6 +132,6 @@ class MongoDbGuestAgentAPI(guest_api.API): version = guest_api.API.API_BASE_VERSION return self._call("is_shard_active", - guest_api.AGENT_HIGH_TIMEOUT, + self.agent_high_timeout, version=version, replica_set_name=replica_set_name) diff --git a/trove/common/strategies/cluster/experimental/mongodb/taskmanager.py b/trove/common/strategies/cluster/experimental/mongodb/taskmanager.py index e41e30ef50..86b7529d6e 100644 --- a/trove/common/strategies/cluster/experimental/mongodb/taskmanager.py +++ b/trove/common/strategies/cluster/experimental/mongodb/taskmanager.py @@ -31,7 +31,6 @@ import trove.taskmanager.models as task_models LOG = logging.getLogger(__name__) CONF = cfg.CONF -USAGE_SLEEP_TIME = CONF.usage_sleep_time # seconds. class MongoDbTaskManagerStrategy(base.BaseTaskManagerStrategy): diff --git a/trove/common/strategies/cluster/experimental/redis/guestagent.py b/trove/common/strategies/cluster/experimental/redis/guestagent.py index 1f8f2c4d39..f06519a4b9 100644 --- a/trove/common/strategies/cluster/experimental/redis/guestagent.py +++ b/trove/common/strategies/cluster/experimental/redis/guestagent.py @@ -44,7 +44,7 @@ class RedisGuestAgentAPI(guest_api.API): version = guest_api.API.API_BASE_VERSION return self._call("get_node_ip", - guest_api.AGENT_HIGH_TIMEOUT, + self.agent_high_timeout, version=version) def get_node_id_for_removal(self): @@ -52,21 +52,21 @@ class RedisGuestAgentAPI(guest_api.API): version = guest_api.API.API_BASE_VERSION return self._call("get_node_id_for_removal", - guest_api.AGENT_HIGH_TIMEOUT, + self.agent_high_timeout, version=version) def remove_nodes(self, node_ids): LOG.debug("Removing nodes from cluster.") version = guest_api.API.API_BASE_VERSION - return self._call("remove_nodes", guest_api.AGENT_HIGH_TIMEOUT, + return self._call("remove_nodes", self.agent_high_timeout, version=version, node_ids=node_ids) def cluster_meet(self, ip, port): LOG.debug("Joining node to cluster.") version = guest_api.API.API_BASE_VERSION - return self._call("cluster_meet", guest_api.AGENT_HIGH_TIMEOUT, + return self._call("cluster_meet", self.agent_high_timeout, version=version, ip=ip, port=port) def cluster_addslots(self, first_slot, last_slot): @@ -74,7 +74,7 @@ class RedisGuestAgentAPI(guest_api.API): version = guest_api.API.API_BASE_VERSION return self._call("cluster_addslots", - guest_api.AGENT_HIGH_TIMEOUT, + self.agent_high_timeout, version=version, first_slot=first_slot, last_slot=last_slot) @@ -82,5 +82,5 @@ class RedisGuestAgentAPI(guest_api.API): LOG.debug("Notifying cluster install completion.") version = guest_api.API.API_BASE_VERSION - return self._call("cluster_complete", guest_api.AGENT_HIGH_TIMEOUT, + return self._call("cluster_complete", self.agent_high_timeout, version=version) diff --git a/trove/common/strategies/cluster/experimental/redis/taskmanager.py b/trove/common/strategies/cluster/experimental/redis/taskmanager.py index 6d4c576524..ffea4232b8 100644 --- a/trove/common/strategies/cluster/experimental/redis/taskmanager.py +++ b/trove/common/strategies/cluster/experimental/redis/taskmanager.py @@ -26,7 +26,6 @@ import trove.taskmanager.models as task_models LOG = logging.getLogger(__name__) CONF = cfg.CONF -USAGE_SLEEP_TIME = CONF.usage_sleep_time # seconds. class RedisTaskManagerStrategy(base.BaseTaskManagerStrategy): diff --git a/trove/common/strategies/cluster/experimental/vertica/guestagent.py b/trove/common/strategies/cluster/experimental/vertica/guestagent.py index 3459977602..76a2614a5e 100644 --- a/trove/common/strategies/cluster/experimental/vertica/guestagent.py +++ b/trove/common/strategies/cluster/experimental/vertica/guestagent.py @@ -45,7 +45,7 @@ class VerticaGuestAgentAPI(guest_api.API): LOG.debug("Getting public keys for user: %s.", user) version = guest_api.API.API_BASE_VERSION - return self._call("get_public_keys", guest_api.AGENT_HIGH_TIMEOUT, + return self._call("get_public_keys", self.agent_high_timeout, version=version, user=user) def authorize_public_keys(self, user, public_keys): @@ -53,7 +53,7 @@ class VerticaGuestAgentAPI(guest_api.API): version = guest_api.API.API_BASE_VERSION return self._call("authorize_public_keys", - guest_api.AGENT_HIGH_TIMEOUT, + self.agent_high_timeout, version=version, user=user, public_keys=public_keys) @@ -89,5 +89,5 @@ class VerticaGuestAgentAPI(guest_api.API): LOG.debug("Notifying cluster install completion.") version = guest_api.API.API_BASE_VERSION - return self._call("cluster_complete", guest_api.AGENT_HIGH_TIMEOUT, + return self._call("cluster_complete", self.agent_high_timeout, version=version) diff --git a/trove/common/strategies/cluster/experimental/vertica/taskmanager.py b/trove/common/strategies/cluster/experimental/vertica/taskmanager.py index 63d9d003aa..250fcd1c2a 100644 --- a/trove/common/strategies/cluster/experimental/vertica/taskmanager.py +++ b/trove/common/strategies/cluster/experimental/vertica/taskmanager.py @@ -27,7 +27,6 @@ import trove.taskmanager.models as task_models LOG = logging.getLogger(__name__) CONF = cfg.CONF -USAGE_SLEEP_TIME = CONF.usage_sleep_time # seconds. class VerticaTaskManagerStrategy(base.BaseTaskManagerStrategy): diff --git a/trove/common/trove_remote.py b/trove/common/trove_remote.py index f6904589a8..ac472a2424 100644 --- a/trove/common/trove_remote.py +++ b/trove/common/trove_remote.py @@ -51,4 +51,5 @@ def trove_client(context, region_name=None): return client -create_trove_client = import_class(CONF.remote_trove_client) +def create_trove_client(*arg, **kwargs): + return import_class(CONF.remote_trove_client)(*arg, **kwargs) diff --git a/trove/db/__init__.py b/trove/db/__init__.py index b8f27b3f23..fc25eceeac 100644 --- a/trove/db/__init__.py +++ b/trove/db/__init__.py @@ -20,11 +20,9 @@ from trove.common import utils CONF = cfg.CONF -db_api_opt = CONF.db_api_implementation - def get_db_api(): - return utils.import_module(db_api_opt) + return utils.import_module(CONF.db_api_implementation) class Query(object): diff --git a/trove/guestagent/api.py b/trove/guestagent/api.py index b70643851e..c9942a7568 100644 --- a/trove/guestagent/api.py +++ b/trove/guestagent/api.py @@ -30,9 +30,6 @@ from trove import rpc CONF = cfg.CONF LOG = logging.getLogger(__name__) -AGENT_LOW_TIMEOUT = CONF.agent_call_low_timeout -AGENT_HIGH_TIMEOUT = CONF.agent_call_high_timeout -AGENT_SNAPSHOT_TIMEOUT = CONF.agent_replication_snapshot_timeout class API(object): @@ -67,6 +64,10 @@ class API(object): self.id = id super(API, self).__init__() + self.agent_low_timeout = CONF.agent_call_low_timeout + self.agent_high_timeout = CONF.agent_call_high_timeout + self.agent_snapshot_timeout = CONF.agent_replication_snapshot_timeout + version_cap = self.VERSION_ALIASES.get( CONF.upgrade_levels.guestagent, CONF.upgrade_levels.guestagent) self.target = messaging.Target(topic=self._get_routing_key(), @@ -149,7 +150,7 @@ class API(object): version = self.API_BASE_VERSION return self._call("get_user", - AGENT_LOW_TIMEOUT, version=version, + self.agent_low_timeout, version=version, username=username, hostname=hostname) def list_access(self, username, hostname): @@ -159,7 +160,7 @@ class API(object): version = self.API_BASE_VERSION return self._call("list_access", - AGENT_LOW_TIMEOUT, version=version, + self.agent_low_timeout, version=version, username=username, hostname=hostname) def grant_access(self, username, hostname, databases): @@ -171,7 +172,7 @@ class API(object): version = self.API_BASE_VERSION return self._call("grant_access", - AGENT_LOW_TIMEOUT, version=version, + self.agent_low_timeout, version=version, username=username, hostname=hostname, databases=databases) @@ -184,7 +185,7 @@ class API(object): version = self.API_BASE_VERSION return self._call("revoke_access", - AGENT_LOW_TIMEOUT, version=version, + self.agent_low_timeout, version=version, username=username, hostname=hostname, database=database) @@ -193,8 +194,8 @@ class API(object): LOG.debug("Listing Users for instance %s.", self.id) version = self.API_BASE_VERSION - return self._call("list_users", AGENT_HIGH_TIMEOUT, - version=version, + return self._call("list_users", + self.agent_high_timeout, version=version, limit=limit, marker=marker, include_marker=include_marker) @@ -221,7 +222,7 @@ class API(object): LOG.debug("Listing databases for instance %s.", self.id) version = self.API_BASE_VERSION - return self._call("list_databases", AGENT_LOW_TIMEOUT, + return self._call("list_databases", self.agent_low_timeout, version=version, limit=limit, marker=marker, include_marker=include_marker) @@ -243,7 +244,7 @@ class API(object): LOG.debug("Enable root user for instance %s.", self.id) version = self.API_BASE_VERSION - return self._call("enable_root", AGENT_HIGH_TIMEOUT, + return self._call("enable_root", self.agent_high_timeout, version=version) def enable_root_with_password(self, root_password=None): @@ -253,7 +254,8 @@ class API(object): LOG.debug("Enable root user for instance %s.", self.id) version = self.API_BASE_VERSION - return self._call("enable_root_with_password", AGENT_HIGH_TIMEOUT, + return self._call("enable_root_with_password", + self.agent_high_timeout, version=version, root_password=root_password) def disable_root(self): @@ -263,7 +265,7 @@ class API(object): LOG.debug("Disable root user for instance %s.", self.id) version = self.API_BASE_VERSION - return self._call("disable_root", AGENT_LOW_TIMEOUT, + return self._call("disable_root", self.agent_low_timeout, version=version) def is_root_enabled(self): @@ -273,7 +275,7 @@ class API(object): LOG.debug("Check root access for instance %s.", self.id) version = self.API_BASE_VERSION - return self._call("is_root_enabled", AGENT_LOW_TIMEOUT, + return self._call("is_root_enabled", self.agent_low_timeout, version=version) def get_hwinfo(self): @@ -281,7 +283,7 @@ class API(object): LOG.debug("Check hwinfo on instance %s.", self.id) version = self.API_BASE_VERSION - return self._call("get_hwinfo", AGENT_LOW_TIMEOUT, + return self._call("get_hwinfo", self.agent_low_timeout, version=version) def get_diagnostics(self): @@ -289,15 +291,16 @@ class API(object): LOG.debug("Check diagnostics on instance %s.", self.id) version = self.API_BASE_VERSION - return self._call("get_diagnostics", AGENT_LOW_TIMEOUT, - version=version) + return self._call("get_diagnostics", + self.agent_low_timeout, version=version) def rpc_ping(self): """Make a synchronous RPC call to check if we can ping the instance.""" LOG.debug("Check RPC ping on instance %s.", self.id) version = self.API_BASE_VERSION - return self._call("rpc_ping", AGENT_LOW_TIMEOUT, version=version) + return self._call("rpc_ping", + self.agent_low_timeout, version=version) def prepare(self, memory_mb, packages, databases, users, device_path='/dev/vdb', mount_point='/mnt/volume', @@ -352,8 +355,8 @@ class API(object): LOG.debug("Sending the call to prepare the guest for upgrade.") version = self.API_BASE_VERSION - return self._call("pre_upgrade", AGENT_HIGH_TIMEOUT, - version=version) + return self._call("pre_upgrade", + self.agent_high_timeout, version=version) def post_upgrade(self, upgrade_info): """Recover the guest after upgrading the guest's image.""" @@ -364,7 +367,8 @@ class API(object): CONF.upgrade_levels.guestagent, CONF.upgrade_levels.guestagent) self.client = self.get_client(self.target, version_cap) - self._call("post_upgrade", AGENT_HIGH_TIMEOUT, version=version, + self._call("post_upgrade", + self.agent_high_timeout, version=version, upgrade_info=upgrade_info) def restart(self): @@ -373,15 +377,16 @@ class API(object): "on the Guest.") version = self.API_BASE_VERSION - self._call("restart", AGENT_HIGH_TIMEOUT, version=version) + self._call("restart", self.agent_high_timeout, version=version) def start_db_with_conf_changes(self, config_contents): """Start the database server.""" LOG.debug("Sending the call to start the database process on " - "the Guest with a timeout of %s.", AGENT_HIGH_TIMEOUT) + "the Guest with a timeout of %s.", + self.agent_high_timeout) version = self.API_BASE_VERSION - self._call("start_db_with_conf_changes", AGENT_HIGH_TIMEOUT, + self._call("start_db_with_conf_changes", self.agent_high_timeout, version=version, config_contents=config_contents) def reset_configuration(self, configuration): @@ -389,10 +394,11 @@ class API(object): the config file to a new flavor. """ LOG.debug("Sending the call to change the database conf file on the " - "Guest with a timeout of %s.", AGENT_HIGH_TIMEOUT) + "Guest with a timeout of %s.", + self.agent_high_timeout) version = self.API_BASE_VERSION - self._call("reset_configuration", AGENT_HIGH_TIMEOUT, + self._call("reset_configuration", self.agent_high_timeout, version=version, configuration=configuration) def stop_db(self, do_not_start_on_reboot=False): @@ -401,7 +407,8 @@ class API(object): "on the Guest.") version = self.API_BASE_VERSION - self._call("stop_db", AGENT_HIGH_TIMEOUT, version=version, + self._call("stop_db", self.agent_high_timeout, + version=version, do_not_start_on_reboot=do_not_start_on_reboot) def upgrade(self, instance_version, location, metadata=None): @@ -419,7 +426,7 @@ class API(object): LOG.debug("Check Volume Info on instance %s.", self.id) version = self.API_BASE_VERSION - return self._call("get_filesystem_stats", AGENT_LOW_TIMEOUT, + return self._call("get_filesystem_stats", self.agent_low_timeout, version=version, fs_path=None) def update_guest(self): @@ -427,7 +434,8 @@ class API(object): LOG.debug("Updating guest agent on instance %s.", self.id) version = self.API_BASE_VERSION - self._call("update_guest", AGENT_HIGH_TIMEOUT, version=version) + self._call("update_guest", + self.agent_high_timeout, version=version) def create_backup(self, backup_info): """Make async call to create a full backup of this instance.""" @@ -445,7 +453,8 @@ class API(object): 'mount': mount_point, 'id': self.id}) version = self.API_BASE_VERSION - self._call("mount_volume", AGENT_LOW_TIMEOUT, version=version, + self._call("mount_volume", + self.agent_low_timeout, version=version, device_path=device_path, mount_point=mount_point) def unmount_volume(self, device_path=None, mount_point=None): @@ -454,7 +463,8 @@ class API(object): 'device': device_path, 'id': self.id}) version = self.API_BASE_VERSION - self._call("unmount_volume", AGENT_LOW_TIMEOUT, version=version, + self._call("unmount_volume", + self.agent_low_timeout, version=version, device_path=device_path, mount_point=mount_point) def resize_fs(self, device_path=None, mount_point=None): @@ -463,7 +473,8 @@ class API(object): 'device': device_path, 'id': self.id}) version = self.API_BASE_VERSION - self._call("resize_fs", AGENT_HIGH_TIMEOUT, version=version, + self._call("resize_fs", + self.agent_high_timeout, version=version, device_path=device_path, mount_point=mount_point) def update_overrides(self, overrides, remove=False): @@ -472,7 +483,7 @@ class API(object): "%(id)s.", {'overrides': overrides, 'id': self.id}) version = self.API_BASE_VERSION - self._call("update_overrides", AGENT_HIGH_TIMEOUT, + self._call("update_overrides", self.agent_high_timeout, version=version, overrides=overrides, remove=remove) def apply_overrides(self, overrides): @@ -480,7 +491,7 @@ class API(object): "%(id)s.", {'overrides': overrides, 'id': self.id}) version = self.API_BASE_VERSION - self._call("apply_overrides", AGENT_HIGH_TIMEOUT, + self._call("apply_overrides", self.agent_high_timeout, version=version, overrides=overrides) def backup_required_for_replication(self): @@ -488,7 +499,7 @@ class API(object): version = self.API_BASE_VERSION return self._call("backup_required_for_replication", - AGENT_LOW_TIMEOUT, + self.agent_low_timeout, version=version) def get_replication_snapshot(self, snapshot_info=None, @@ -496,7 +507,8 @@ class API(object): LOG.debug("Retrieving replication snapshot from instance %s.", self.id) version = self.API_BASE_VERSION - return self._call("get_replication_snapshot", AGENT_SNAPSHOT_TIMEOUT, + return self._call("get_replication_snapshot", + self.agent_snapshot_timeout, version=version, snapshot_info=snapshot_info, replica_source_config=replica_source_config) @@ -512,7 +524,7 @@ class API(object): LOG.debug("Detaching replica %s from its replication source.", self.id) version = self.API_BASE_VERSION - return self._call("detach_replica", AGENT_HIGH_TIMEOUT, + return self._call("detach_replica", self.agent_high_timeout, version=version, for_failover=for_failover) def get_replica_context(self): @@ -520,27 +532,29 @@ class API(object): version = self.API_BASE_VERSION return self._call("get_replica_context", - AGENT_HIGH_TIMEOUT, version=version) + self.agent_high_timeout, version=version) def attach_replica(self, replica_info, slave_config): LOG.debug("Attaching replica %s.", replica_info) version = self.API_BASE_VERSION - self._call("attach_replica", AGENT_HIGH_TIMEOUT, version=version, + self._call("attach_replica", + self.agent_high_timeout, version=version, replica_info=replica_info, slave_config=slave_config) def make_read_only(self, read_only): LOG.debug("Executing make_read_only(%s)", read_only) version = self.API_BASE_VERSION - self._call("make_read_only", AGENT_HIGH_TIMEOUT, version=version, + self._call("make_read_only", + self.agent_high_timeout, version=version, read_only=read_only) def enable_as_master(self, replica_source_config): LOG.debug("Executing enable_as_master") version = self.API_BASE_VERSION - self._call("enable_as_master", AGENT_HIGH_TIMEOUT, + self._call("enable_as_master", self.agent_high_timeout, version=version, replica_source_config=replica_source_config) @@ -550,48 +564,49 @@ class API(object): version = self.API_BASE_VERSION return self._call("get_txn_count", - AGENT_HIGH_TIMEOUT, version=version) + self.agent_high_timeout, version=version) def get_last_txn(self): LOG.debug("Executing get_last_txn.") version = self.API_BASE_VERSION return self._call("get_last_txn", - AGENT_HIGH_TIMEOUT, version=version) + self.agent_high_timeout, version=version) def get_latest_txn_id(self): LOG.debug("Executing get_latest_txn_id.") version = self.API_BASE_VERSION return self._call("get_latest_txn_id", - AGENT_HIGH_TIMEOUT, version=version) + self.agent_high_timeout, version=version) def wait_for_txn(self, txn): LOG.debug("Executing wait_for_txn.") version = self.API_BASE_VERSION - self._call("wait_for_txn", AGENT_HIGH_TIMEOUT, version=version, - txn=txn) + self._call("wait_for_txn", + self.agent_high_timeout, version=version, txn=txn) def cleanup_source_on_replica_detach(self, replica_info): LOG.debug("Cleaning up master %s on detach of replica.", self.id) version = self.API_BASE_VERSION - self._call("cleanup_source_on_replica_detach", AGENT_HIGH_TIMEOUT, + self._call("cleanup_source_on_replica_detach", + self.agent_high_timeout, version=version, replica_info=replica_info) def demote_replication_master(self): LOG.debug("Demoting instance %s to non-master.", self.id) version = self.API_BASE_VERSION - self._call("demote_replication_master", AGENT_HIGH_TIMEOUT, + self._call("demote_replication_master", self.agent_high_timeout, version=version) def guest_log_list(self): LOG.debug("Retrieving guest log list for %s.", self.id) version = self.API_BASE_VERSION - result = self._call("guest_log_list", AGENT_HIGH_TIMEOUT, + result = self._call("guest_log_list", self.agent_high_timeout, version=version) LOG.debug("guest_log_list returns %s", result) return result @@ -600,7 +615,7 @@ class API(object): LOG.debug("Processing guest log '%s' for %s.", log_name, self.id) version = self.API_BASE_VERSION - return self._call("guest_log_action", AGENT_HIGH_TIMEOUT, + return self._call("guest_log_action", self.agent_high_timeout, version=version, log_name=log_name, enable=enable, disable=disable, publish=publish, discard=discard) @@ -610,7 +625,7 @@ class API(object): self.id, include_contents) version = self.API_BASE_VERSION - result = self._call("module_list", AGENT_HIGH_TIMEOUT, + result = self._call("module_list", self.agent_high_timeout, version=version, include_contents=include_contents) return result @@ -619,12 +634,12 @@ class API(object): LOG.debug("Applying modules to %s.", self.id) version = self.API_BASE_VERSION - return self._call("module_apply", AGENT_HIGH_TIMEOUT, + return self._call("module_apply", self.agent_high_timeout, version=version, modules=modules) def module_remove(self, module): LOG.debug("Removing modules from %s.", self.id) version = self.API_BASE_VERSION - return self._call("module_remove", AGENT_HIGH_TIMEOUT, + return self._call("module_remove", self.agent_high_timeout, version=version, module=module) diff --git a/trove/guestagent/models.py b/trove/guestagent/models.py index cdf64651bb..4e4a981d01 100644 --- a/trove/guestagent/models.py +++ b/trove/guestagent/models.py @@ -29,8 +29,6 @@ LOG = logging.getLogger(__name__) CONF = cfg.CONF -AGENT_HEARTBEAT = CONF.agent_heartbeat_time - def persisted_models(): return {'agent_heartbeats': AgentHeartBeat} @@ -91,4 +89,4 @@ class AgentHeartBeat(dbmodels.DatabaseModelBase): @staticmethod def is_active(agent): return (datetime.now() - agent.updated_at < - timedelta(seconds=AGENT_HEARTBEAT)) + timedelta(seconds=CONF.agent_heartbeat_time)) diff --git a/trove/taskmanager/models.py b/trove/taskmanager/models.py index 892c833e03..354992f2a9 100755 --- a/trove/taskmanager/models.py +++ b/trove/taskmanager/models.py @@ -81,13 +81,6 @@ from trove import rpc LOG = logging.getLogger(__name__) CONF = cfg.CONF -VOLUME_TIME_OUT = CONF.volume_time_out # seconds. -DNS_TIME_OUT = CONF.dns_time_out # seconds. -RESIZE_TIME_OUT = CONF.resize_time_out # seconds. -REVERT_TIME_OUT = CONF.revert_time_out # seconds. -USAGE_SLEEP_TIME = CONF.usage_sleep_time # seconds. - -use_nova_server_volume = CONF.use_nova_server_volume class NotifyMixin(object): @@ -276,7 +269,7 @@ class ClusterTasks(Cluster): try: utils.poll_until(lambda: instance_ids, lambda ids: _all_have_status(ids), - sleep_time=USAGE_SLEEP_TIME, + sleep_time=CONF.usage_sleep_time, time_out=CONF.usage_timeout) except PollTimeOut: LOG.exception(_("Timed out while waiting for all instances " @@ -431,7 +424,7 @@ class FreshInstanceTasks(FreshInstance, NotifyMixin, ConfigurationMixin): error_details = '' try: utils.poll_until(self._service_is_active, - sleep_time=USAGE_SLEEP_TIME, + sleep_time=CONF.usage_sleep_time, time_out=timeout) LOG.info(_("Created instance %s successfully."), self.id) TroveInstanceCreate(instance=self, @@ -452,7 +445,7 @@ class FreshInstanceTasks(FreshInstance, NotifyMixin, ConfigurationMixin): if error_message: inst_models.save_instance_fault( self.id, error_message, error_details, - skip_delta=USAGE_SLEEP_TIME + 1) + skip_delta=CONF.usage_sleep_time + 1) def create_instance(self, flavor, image_id, databases, users, datastore_manager, packages, volume_size, @@ -480,7 +473,7 @@ class FreshInstanceTasks(FreshInstance, NotifyMixin, ConfigurationMixin): files = self.get_injected_files(datastore_manager) cinder_volume_type = volume_type or CONF.cinder_volume_type - if use_nova_server_volume: + if CONF.use_nova_server_volume: volume_info = self._create_server_volume( flavor['id'], image_id, @@ -854,7 +847,7 @@ class FreshInstanceTasks(FreshInstance, NotifyMixin, ConfigurationMixin): lambda: volume_client.volumes.get(volume_ref.id), lambda v_ref: v_ref.status in ['available', 'error'], sleep_time=2, - time_out=VOLUME_TIME_OUT) + time_out=CONF.volume_time_out) v_ref = volume_client.volumes.get(volume_ref.id) if v_ref.status in ['error']: @@ -965,7 +958,7 @@ class FreshInstanceTasks(FreshInstance, NotifyMixin, ConfigurationMixin): raise TroveError(status=server.status) utils.poll_until(get_server, ip_is_available, - sleep_time=1, time_out=DNS_TIME_OUT) + sleep_time=1, time_out=CONF.dns_time_out) server = self.nova_client.servers.get( self.db_info.compute_instance_id) self.db_info.addresses = server.addresses @@ -1807,7 +1800,7 @@ class ResizeActionBase(object): utils.poll_until( self._guest_is_awake, sleep_time=2, - time_out=RESIZE_TIME_OUT) + time_out=CONF.resize_time_out) def _assert_nova_status_is_ok(self): # Make sure Nova thinks things went well. @@ -1825,7 +1818,7 @@ class ResizeActionBase(object): utils.poll_until( self._datastore_is_online, sleep_time=2, - time_out=RESIZE_TIME_OUT) + time_out=CONF.resize_time_out) def _assert_datastore_is_offline(self): # Tell the guest to turn off MySQL, and ensure the status becomes @@ -1834,7 +1827,7 @@ class ResizeActionBase(object): utils.poll_until( self._datastore_is_offline, sleep_time=2, - time_out=RESIZE_TIME_OUT) + time_out=CONF.resize_time_out) def _assert_processes_are_ok(self): """Checks the procs; if anything is wrong, reverts the operation.""" @@ -1931,7 +1924,7 @@ class ResizeActionBase(object): utils.poll_until( update_server_info, sleep_time=2, - time_out=RESIZE_TIME_OUT) + time_out=CONF.resize_time_out) def _wait_for_revert_nova_action(self): # Wait for the server to return to ACTIVE after revert. @@ -1942,7 +1935,7 @@ class ResizeActionBase(object): utils.poll_until( update_server_info, sleep_time=2, - time_out=REVERT_TIME_OUT) + time_out=CONF.revert_time_out) class ResizeAction(ResizeActionBase): diff --git a/trove/tests/fakes/guestagent.py b/trove/tests/fakes/guestagent.py index dca1f0ad71..fbfb6e401f 100644 --- a/trove/tests/fakes/guestagent.py +++ b/trove/tests/fakes/guestagent.py @@ -380,5 +380,5 @@ def get_or_create(id): return DB[id] -def fake_create_guest_client(context, id): +def fake_create_guest_client(context, id, manager=None): return get_or_create(id) diff --git a/trove/tests/unittests/taskmanager/test_models.py b/trove/tests/unittests/taskmanager/test_models.py index 81b28e02f2..47d8cd9726 100644 --- a/trove/tests/unittests/taskmanager/test_models.py +++ b/trove/tests/unittests/taskmanager/test_models.py @@ -22,6 +22,7 @@ from mock import Mock, MagicMock, patch, PropertyMock, call from novaclient import exceptions as nova_exceptions import novaclient.v2.flavors import novaclient.v2.servers +from oslo_config import cfg from swiftclient.client import ClientException from testtools.matchers import Equals, Is @@ -173,10 +174,10 @@ class fake_DBInstance(object): return self.deleted -class FreshInstanceTasksTest(trove_testtools.TestCase): +class BaseFreshInstanceTasksTest(trove_testtools.TestCase): def setUp(self): - super(FreshInstanceTasksTest, self).setUp() + super(BaseFreshInstanceTasksTest, self).setUp() mock_instance = patch('trove.instance.models.FreshInstance') mock_instance.start() self.addCleanup(mock_instance.stop) @@ -219,31 +220,23 @@ class FreshInstanceTasksTest(trove_testtools.TestCase): 'create_sec_group_rule') self.tm_sgr_create_sgr_mock = self.tm_sgr_create_sgr_patch.start() self.addCleanup(self.tm_sgr_create_sgr_patch.stop) - self.task_models_conf_patch = patch('trove.taskmanager.models.CONF') - self.task_models_conf_mock = self.task_models_conf_patch.start() - self.addCleanup(self.task_models_conf_patch.stop) - self.inst_models_conf_patch = patch('trove.instance.models.CONF') - self.inst_models_conf_mock = self.inst_models_conf_patch.start() - self.addCleanup(self.inst_models_conf_patch.stop) def tearDown(self): - super(FreshInstanceTasksTest, self).tearDown() + super(BaseFreshInstanceTasksTest, self).tearDown() os.remove(self.cloudinit) os.remove(self.guestconfig) InstanceServiceStatus.find_by = self.orig_ISS_find_by DBInstance.find_by = self.orig_DBI_find_by + +class FreshInstanceTasksTest(BaseFreshInstanceTasksTest): + def test_create_instance_userdata(self): cloudinit_location = os.path.dirname(self.cloudinit) datastore_manager = os.path.splitext(os.path.basename(self. cloudinit))[0] - def fake_conf_getter(*args, **kwargs): - if args[0] == 'cloudinit_location': - return cloudinit_location - else: - return '' - self.task_models_conf_mock.get.side_effect = fake_conf_getter + cfg.CONF.set_override('cloudinit_location', cloudinit_location) server = self.freshinstancetasks._create_server( None, None, None, datastore_manager, None, None, None) @@ -251,17 +244,10 @@ class FreshInstanceTasksTest(trove_testtools.TestCase): @patch.object(DBInstance, 'get_by') def test_create_instance_guestconfig(self, patch_get_by): - def fake_conf_getter(*args, **kwargs): - if args[0] == 'guest_config': - return self.guestconfig - if args[0] == 'guest_info': - return 'guest_info.conf' - if args[0] == 'injected_config_location': - return '/etc/trove/conf.d' - else: - return '' + cfg.CONF.set_override('guest_config', self.guestconfig) + cfg.CONF.set_override('guest_info', 'guest_info.conf') + cfg.CONF.set_override('injected_config_location', '/etc/trove/conf.d') - self.inst_models_conf_mock.get.side_effect = fake_conf_getter # execute files = self.freshinstancetasks.get_injected_files("test") # verify @@ -275,17 +261,10 @@ class FreshInstanceTasksTest(trove_testtools.TestCase): @patch.object(DBInstance, 'get_by') def test_create_instance_guestconfig_compat(self, patch_get_by): - def fake_conf_getter(*args, **kwargs): - if args[0] == 'guest_config': - return self.guestconfig - if args[0] == 'guest_info': - return '/etc/guest_info' - if args[0] == 'injected_config_location': - return '/etc' - else: - return '' + cfg.CONF.set_override('guest_config', self.guestconfig) + cfg.CONF.set_override('guest_info', '/etc/guest_info') + cfg.CONF.set_override('injected_config_location', '/etc') - self.inst_models_conf_mock.get.side_effect = fake_conf_getter # execute files = self.freshinstancetasks.get_injected_files("test") # verify @@ -298,7 +277,6 @@ class FreshInstanceTasksTest(trove_testtools.TestCase): files['/etc/trove-guestagent.conf']) def test_create_instance_with_az_kwarg(self): - self.task_models_conf_mock.get.return_value = '' # execute server = self.freshinstancetasks._create_server( None, None, None, None, None, availability_zone='nova', nics=None) @@ -306,7 +284,6 @@ class FreshInstanceTasksTest(trove_testtools.TestCase): self.assertIsNotNone(server) def test_create_instance_with_az(self): - self.task_models_conf_mock.get.return_value = '' # execute server = self.freshinstancetasks._create_server( None, None, None, None, None, 'nova', None) @@ -314,7 +291,6 @@ class FreshInstanceTasksTest(trove_testtools.TestCase): self.assertIsNotNone(server) def test_create_instance_with_az_none(self): - self.task_models_conf_mock.get.return_value = '' # execute server = self.freshinstancetasks._create_server( None, None, None, None, None, None, None) @@ -328,13 +304,125 @@ class FreshInstanceTasksTest(trove_testtools.TestCase): @patch('trove.taskmanager.models.LOG') def test_update_status_of_instance_failure( self, mock_logging, dbi_find_by_mock, iss_find_by_mock): - self.task_models_conf_mock.get.return_value = '' self.freshinstancetasks.update_statuses_on_time_out() self.assertEqual(ServiceStatuses.FAILED_TIMEOUT_GUESTAGENT, fake_InstanceServiceStatus.find_by().get_status()) self.assertEqual(InstanceTasks.BUILDING_ERROR_TIMEOUT_GA, fake_DBInstance.find_by().get_task_status()) + @patch.object(BaseInstance, 'update_db') + @patch.object(backup_models.Backup, 'get_by_id') + @patch.object(taskmanager_models.FreshInstanceTasks, 'report_root_enabled') + @patch.object(taskmanager_models.FreshInstanceTasks, 'get_injected_files') + @patch.object(taskmanager_models.FreshInstanceTasks, '_create_secgroup') + @patch.object(taskmanager_models.FreshInstanceTasks, '_build_volume_info') + @patch.object(taskmanager_models.FreshInstanceTasks, '_create_server') + @patch.object(taskmanager_models.FreshInstanceTasks, '_guest_prepare') + @patch.object(template, 'SingleInstanceConfigTemplate') + @patch.object(taskmanager_models.FreshInstanceTasks, '_create_dns_entry', + side_effect=TroveError) + @patch('trove.taskmanager.models.LOG') + def test_error_create_dns_entry_create_instance(self, *args): + mock_flavor = {'id': 6, 'ram': 512, 'name': 'big_flavor'} + self.assertRaisesRegexp( + TroveError, + 'Error creating DNS entry for instance', + self.freshinstancetasks.create_instance, mock_flavor, + 'mysql-image-id', None, None, 'mysql', 'mysql-server', + 2, Mock(), None, 'root_password', None, Mock(), None, None, None, + None, None) + + @patch.object(BaseInstance, 'update_db') + @patch.object(taskmanager_models.FreshInstanceTasks, '_create_dns_entry') + @patch.object(taskmanager_models.FreshInstanceTasks, 'get_injected_files') + @patch.object(taskmanager_models.FreshInstanceTasks, '_create_server') + @patch.object(taskmanager_models.FreshInstanceTasks, '_create_secgroup') + @patch.object(taskmanager_models.FreshInstanceTasks, '_build_volume_info') + @patch.object(taskmanager_models.FreshInstanceTasks, '_guest_prepare') + @patch.object(template, 'SingleInstanceConfigTemplate') + def test_create_instance(self, + mock_single_instance_template, + mock_guest_prepare, + mock_build_volume_info, + mock_create_secgroup, + mock_create_server, + mock_get_injected_files, + *args): + + cfg.CONF.set_override('tcp_ports', ['3306', '3301-3307'], + group='mysql') + + mock_flavor = {'id': 8, 'ram': 768, 'name': 'bigger_flavor'} + config_content = {'config_contents': 'some junk'} + mock_single_instance_template.return_value.config_contents = ( + config_content) + overrides = Mock() + self.freshinstancetasks.create_instance(mock_flavor, 'mysql-image-id', + None, None, 'mysql', + 'mysql-server', 2, + None, None, None, None, + overrides, None, None, + 'volume_type', None, + {'group': 'sg-id'}) + mock_create_secgroup.assert_called_with('mysql') + mock_build_volume_info.assert_called_with('mysql', volume_size=2, + volume_type='volume_type') + mock_guest_prepare.assert_called_with( + 768, mock_build_volume_info(), 'mysql-server', None, None, None, + config_content, None, overrides, None, None, None) + mock_create_server.assert_called_with( + 8, 'mysql-image-id', mock_create_secgroup(), + 'mysql', mock_build_volume_info()['block_device'], None, + None, mock_get_injected_files(), {'group': 'sg-id'}) + + @patch.object(trove.guestagent.api.API, 'attach_replication_slave') + @patch.object(rpc, 'get_client') + @patch.object(DBInstance, 'get_by') + def test_attach_replication_slave(self, mock_get_by, mock_get_client, + mock_attach_replication_slave): + mock_flavor = {'id': 8, 'ram': 768, 'name': 'bigger_flavor'} + snapshot = {'replication_strategy': 'MysqlGTIDReplication', + 'master': {'id': 'master-id'}} + config_content = {'config_contents': 'some junk'} + replica_config = MagicMock() + replica_config.config_contents = config_content + with patch.object(taskmanager_models.FreshInstanceTasks, + '_render_replica_config', + return_value=replica_config): + self.freshinstancetasks.attach_replication_slave(snapshot, + mock_flavor) + mock_attach_replication_slave.assert_called_with(snapshot, + config_content) + + @patch.object(BaseInstance, 'update_db') + @patch.object(rpc, 'get_client') + @patch.object(taskmanager_models.FreshInstanceTasks, + '_render_replica_config') + @patch.object(trove.guestagent.api.API, 'attach_replication_slave', + side_effect=GuestError) + @patch('trove.taskmanager.models.LOG') + @patch.object(DBInstance, 'get_by') + def test_error_attach_replication_slave(self, *args): + mock_flavor = {'id': 8, 'ram': 768, 'name': 'bigger_flavor'} + snapshot = {'replication_strategy': 'MysqlGTIDReplication', + 'master': {'id': 'master-id'}} + self.assertRaisesRegexp( + TroveError, 'Error attaching instance', + self.freshinstancetasks.attach_replication_slave, + snapshot, mock_flavor) + + +class InstanceSecurityGroupRuleTests(BaseFreshInstanceTasksTest): + + def setUp(self): + super(InstanceSecurityGroupRuleTests, self).setUp() + self.task_models_conf_patch = patch('trove.taskmanager.models.CONF') + self.task_models_conf_mock = self.task_models_conf_patch.start() + self.addCleanup(self.task_models_conf_patch.stop) + self.inst_models_conf_patch = patch('trove.instance.models.CONF') + self.inst_models_conf_mock = self.inst_models_conf_patch.start() + self.addCleanup(self.inst_models_conf_patch.stop) + def test_create_sg_rules_success(self): datastore_manager = 'mysql' self.task_models_conf_mock.get = Mock(return_value=FakeOptGroup()) @@ -394,103 +482,6 @@ class FreshInstanceTasksTest(trove_testtools.TestCase): 'mysql-image-id', None, None, 'mysql', 'mysql-server', 2, None, None, None, None, Mock(), None, None, None, None, None) - @patch.object(BaseInstance, 'update_db') - @patch.object(backup_models.Backup, 'get_by_id') - @patch.object(taskmanager_models.FreshInstanceTasks, 'report_root_enabled') - @patch.object(taskmanager_models.FreshInstanceTasks, 'get_injected_files') - @patch.object(taskmanager_models.FreshInstanceTasks, '_create_secgroup') - @patch.object(taskmanager_models.FreshInstanceTasks, '_build_volume_info') - @patch.object(taskmanager_models.FreshInstanceTasks, '_create_server') - @patch.object(taskmanager_models.FreshInstanceTasks, '_guest_prepare') - @patch.object(template, 'SingleInstanceConfigTemplate') - @patch.object(taskmanager_models.FreshInstanceTasks, '_create_dns_entry', - side_effect=TroveError) - @patch('trove.taskmanager.models.LOG') - def test_error_create_dns_entry_create_instance(self, *args): - mock_flavor = {'id': 6, 'ram': 512, 'name': 'big_flavor'} - self.assertRaisesRegexp( - TroveError, - 'Error creating DNS entry for instance', - self.freshinstancetasks.create_instance, mock_flavor, - 'mysql-image-id', None, None, 'mysql', 'mysql-server', - 2, Mock(), None, 'root_password', None, Mock(), None, None, None, - None, None) - - @patch.object(BaseInstance, 'update_db') - @patch.object(taskmanager_models.FreshInstanceTasks, '_create_dns_entry') - @patch.object(taskmanager_models.FreshInstanceTasks, 'get_injected_files') - @patch.object(taskmanager_models.FreshInstanceTasks, '_create_server') - @patch.object(taskmanager_models.FreshInstanceTasks, '_create_secgroup') - @patch.object(taskmanager_models.FreshInstanceTasks, '_build_volume_info') - @patch.object(taskmanager_models.FreshInstanceTasks, '_guest_prepare') - @patch.object(template, 'SingleInstanceConfigTemplate') - def test_create_instance(self, - mock_single_instance_template, - mock_guest_prepare, - mock_build_volume_info, - mock_create_secgroup, - mock_create_server, - mock_get_injected_files, - *args): - mock_flavor = {'id': 8, 'ram': 768, 'name': 'bigger_flavor'} - config_content = {'config_contents': 'some junk'} - mock_single_instance_template.return_value.config_contents = ( - config_content) - overrides = Mock() - self.freshinstancetasks.create_instance(mock_flavor, 'mysql-image-id', - None, None, 'mysql', - 'mysql-server', 2, - None, None, None, None, - overrides, None, None, - 'volume_type', None, - {'group': 'sg-id'}) - mock_create_secgroup.assert_called_with('mysql') - mock_build_volume_info.assert_called_with('mysql', volume_size=2, - volume_type='volume_type') - mock_guest_prepare.assert_called_with( - 768, mock_build_volume_info(), 'mysql-server', None, None, None, - config_content, None, overrides, None, None, None) - mock_create_server.assert_called_with( - 8, 'mysql-image-id', mock_create_secgroup(), - 'mysql', mock_build_volume_info()['block_device'], None, - None, mock_get_injected_files(), {'group': 'sg-id'}) - - @patch.object(trove.guestagent.api.API, 'attach_replication_slave') - @patch.object(rpc, 'get_client') - @patch.object(DBInstance, 'get_by') - def test_attach_replication_slave(self, mock_get_by, mock_get_client, - mock_attach_replication_slave): - mock_flavor = {'id': 8, 'ram': 768, 'name': 'bigger_flavor'} - snapshot = {'replication_strategy': 'MysqlGTIDReplication', - 'master': {'id': 'master-id'}} - config_content = {'config_contents': 'some junk'} - replica_config = MagicMock() - replica_config.config_contents = config_content - with patch.object(taskmanager_models.FreshInstanceTasks, - '_render_replica_config', - return_value=replica_config): - self.freshinstancetasks.attach_replication_slave(snapshot, - mock_flavor) - mock_attach_replication_slave.assert_called_with(snapshot, - config_content) - - @patch.object(BaseInstance, 'update_db') - @patch.object(rpc, 'get_client') - @patch.object(taskmanager_models.FreshInstanceTasks, - '_render_replica_config') - @patch.object(trove.guestagent.api.API, 'attach_replication_slave', - side_effect=GuestError) - @patch('trove.taskmanager.models.LOG') - @patch.object(DBInstance, 'get_by') - def test_error_attach_replication_slave(self, *args): - mock_flavor = {'id': 8, 'ram': 768, 'name': 'bigger_flavor'} - snapshot = {'replication_strategy': 'MysqlGTIDReplication', - 'master': {'id': 'master-id'}} - self.assertRaisesRegexp( - TroveError, 'Error attaching instance', - self.freshinstancetasks.attach_replication_slave, - snapshot, mock_flavor) - class ResizeVolumeTest(trove_testtools.TestCase): diff --git a/trove/tests/unittests/trove_testtools.py b/trove/tests/unittests/trove_testtools.py index 36413b1e50..4a71a579fe 100644 --- a/trove/tests/unittests/trove_testtools.py +++ b/trove/tests/unittests/trove_testtools.py @@ -97,6 +97,9 @@ class TestCase(testtools.TestCase): "references from a previous test case.") super(TestCase, self).setUp() + + self.addCleanup(cfg.CONF.reset) + root_logger.DefaultRootHandler.set_info(self.id()) # Default manager used by all unittsest unless explicitly overridden.