def test_create_database_terminates_the_instance_when_cant_create_db(self): exc_msg = u"I've failed to create your database, sorry! :(" module = "mysqlapi.api.models.DatabaseManager.create_database" with mock.patch(module) as c_database: c_database.side_effect = Exception(exc_msg) instance = Instance( ec2_id="i-00009", name="home", host="unknown.host", state="running", ) ec2_client = mocks.FakeEC2Client() try: t = start_creator(DatabaseManager, ec2_client) create_database(instance, ec2_client) t.stop() self.assertIn("unauthorize instance home", ec2_client.actions) self.assertIn("terminate instance home", ec2_client.actions) index_unauthorize = ec2_client.actions.index( "unauthorize instance home" ) index_terminate = ec2_client.actions.index( "terminate instance home" ) msg = "Should unauthorize before terminate." assert index_unauthorize < index_terminate, msg self.assertIsNotNone(instance.pk) self.assertEqual("error", instance.state) self.assertEqual(exc_msg, instance.reason) finally: instance.delete()
def test_create_database_terminates_the_instance_when_cant_create_db(self): exc_msg = u"I've failed to create your database, sorry! :(" module = "mysqlapi.api.models.DatabaseManager.create_database" with mock.patch(module) as c_database: c_database.side_effect = Exception(exc_msg) instance = Instance( ec2_id="i-00009", name="home", host="unknown.host", state="running", ) ec2_client = mocks.FakeEC2Client() try: t = start_creator(DatabaseManager, ec2_client) create_database(instance, ec2_client) t.stop() self.assertIn("unauthorize instance home", ec2_client.actions) self.assertIn("terminate instance home", ec2_client.actions) index_unauthorize = ec2_client.actions.index( "unauthorize instance home") index_terminate = ec2_client.actions.index( "terminate instance home") msg = "Should unauthorize before terminate." assert index_unauthorize < index_terminate, msg self.assertIsNotNone(instance.pk) self.assertEqual("error", instance.state) self.assertEqual(exc_msg, instance.reason) finally: instance.delete()
def test_create_database_when_instance_already_exist(self): settings.SHARED_SERVER = "127.0.0.1" instance = Instance( name="caravanx", ec2_id="i-89", ) instance.save() try: with self.assertRaises(InstanceAlreadyExists): create_database(instance) finally: instance.delete()
def test_create_database_canonicalizes_name(self): settings.SHARED_SERVER = "127.0.0.1" instance = Instance(name="invalid-db-name", ec2_id="i-681") canonical_name = canonicalize_db_name(instance.name) try: create_database(instance) sql = "select SCHEMA_NAME from information_schema.SCHEMATA " +\ "where SCHEMA_NAME = '{0}'" self.cursor.execute(sql.format(canonical_name)) row = self.cursor.fetchone() self.assertIsNotNone(row) self.assertEqual(canonical_name, row[0]) self.assertIsNotNone(instance.pk) finally: sql = "DROP DATABASE IF EXISTS {0}" self.cursor.execute(sql.format(canonical_name)) instance.delete()
def test_create_database_shared(self): settings.SHARED_SERVER = "127.0.0.1" instance = Instance(name="water", ec2_id="i-681") try: create_database(instance) sql = "select SCHEMA_NAME from information_schema.SCHEMATA " +\ "where SCHEMA_NAME = 'water'" self.cursor.execute(sql) row = self.cursor.fetchone() self.assertIsNotNone(row) self.assertEqual("water", row[0]) self.assertIsNotNone(instance.pk) self.assertTrue(instance.shared) self.assertEqual("running", instance.state) self.assertIsNone(instance.ec2_id) finally: self.cursor.execute("DROP DATABASE IF EXISTS water") instance.delete()
def test_create_database_function_start_thread_that_creates_the_database_once_the_instance_changes_it_state(self): instance = Instance( ec2_id="i-00009", name="der_trommler", host="127.0.0.1", state="running", ) ec2_client = mocks.MultipleFailureEC2Client(times=2) try: t = create_database(instance, ec2_client) t.join() self.cursor.execute("select SCHEMA_NAME from information_schema.SCHEMATA where SCHEMA_NAME = 'der_trommler'") row = self.cursor.fetchone() self.assertIsNotNone(row) self.assertEqual("der_trommler", row[0]) self.assertIsNotNone(instance.pk) finally: self.cursor.execute("DROP DATABASE IF EXISTS der_trommler") instance.delete()
def test_create_database_canonicalizes_name(self): settings.SHARED_SERVER = "127.0.0.1" instance = Instance( name="invalid-db-name", ec2_id="i-681" ) canonical_name = canonicalize_db_name(instance.name) try: create_database(instance) sql = "select SCHEMA_NAME from information_schema.SCHEMATA " +\ "where SCHEMA_NAME = '{0}'" self.cursor.execute(sql.format(canonical_name)) row = self.cursor.fetchone() self.assertIsNotNone(row) self.assertEqual(canonical_name, row[0]) self.assertIsNotNone(instance.pk) finally: sql = "DROP DATABASE IF EXISTS {0}" self.cursor.execute(sql.format(canonical_name)) instance.delete()
def test_create_database_terminates_the_instance_when_cant_authorize(self): instance = Instance( ec2_id="i-00009", name="home", host="unknown.host", state="running", ) ec2_client = mocks.FakeEC2Client() ec2_client.authorize = lambda *args, **kwargs: False try: t = start_creator(DatabaseManager, ec2_client) create_database(instance, ec2_client) t.stop() self.assertIn("terminate instance home", ec2_client.actions) self.assertIsNotNone(instance.pk) self.assertEqual("error", instance.state) reason = "Failed to authorize access to the instance." self.assertEqual(reason, instance.reason) finally: instance.delete()
def test_create_database_shared(self): settings.SHARED_SERVER = "127.0.0.1" instance = Instance( name="water", ec2_id="i-681" ) try: create_database(instance) sql = "select SCHEMA_NAME from information_schema.SCHEMATA " +\ "where SCHEMA_NAME = 'water'" self.cursor.execute(sql) row = self.cursor.fetchone() self.assertIsNotNone(row) self.assertEqual("water", row[0]) self.assertIsNotNone(instance.pk) self.assertTrue(instance.shared) self.assertEqual("running", instance.state) self.assertIsNone(instance.ec2_id) finally: self.cursor.execute("DROP DATABASE IF EXISTS water") instance.delete()
def test_create_database_sends_the_instance_to_the_queue(self): instance = Instance( ec2_id="i-00009", name="der_trommler", host="127.0.0.1", state="running", ) ec2_client = mocks.MultipleFailureEC2Client(times=0) try: t = start_creator(DatabaseManager, ec2_client) create_database(instance, ec2_client) t.stop() sql = "select SCHEMA_NAME from information_schema.SCHEMATA " +\ "where SCHEMA_NAME = 'der_trommler'" self.cursor.execute(sql) row = self.cursor.fetchone() self.assertIsNotNone(row) self.assertEqual("der_trommler", row[0]) self.assertIsNotNone(instance.pk) finally: self.cursor.execute("DROP DATABASE IF EXISTS der_trommler") instance.delete()
def test_create_database_provisioned(self): settings.USE_POOL = True pi = ProvisionedInstance.objects.create(host="127.0.0.1", port=3306, admin_user="******") self.addCleanup(pi.delete) instance = Instance(name="hello_world") try: create_database(instance) sql = "select SCHEMA_NAME from information_schema.SCHEMATA " +\ "where SCHEMA_NAME = 'hello_world'" self.cursor.execute(sql) row = self.cursor.fetchone() self.assertIsNotNone(row) self.assertEqual("hello_world", row[0]) self.assertIsNotNone(instance.pk) self.assertEqual("127.0.0.1", instance.host) self.assertEqual("3306", instance.port) self.assertEqual("running", instance.state) self.assertFalse(instance.shared) self.assertIsNone(instance.ec2_id) finally: self.cursor.execute("DROP DATABASE IF EXISTS hello_world") instance.delete()
def test_create_database_terminates_the_instance_if_it_fails_to_create_the_database_and_save_instance_with_error_state(self): exc_msg = u"I've failed to create your database, sorry! :(" mocker = Mocker() c_database = mocker.replace("mysqlapi.api.models.DatabaseManager.create_database") c_database() mocker.throw(Exception(exc_msg)) mocker.replay() instance = Instance( ec2_id="i-00009", name="home", host="unknown.host", state="running", ) ec2_client = mocks.FakeEC2Client() try: t = create_database(instance, ec2_client) t.join() self.assertIn("terminate instance home", ec2_client.actions) self.assertIsNotNone(instance.pk) self.assertEqual("error", instance.state) self.assertEqual(exc_msg, instance.reason) finally: instance.delete() mocker.verify()