Example #1
0
 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_raises_exception_when_instance_fail_to_boot(self):
     instance = Instance(name="seven_cities")
     ec2_client = mocks.FakeEC2Client()
     ec2_client.run = lambda instance: False
     with self.assertRaises(DatabaseCreationException) as e:
         create_database(instance, ec2_client)
     self.assertEqual(u"Failed to create EC2 instance.", e.exception[1])
Example #3
0
 def test_create_database_raises_exception_when_instance_fail_to_boot(self):
     instance = Instance(name="seven_cities")
     ec2_client = mocks.FakeEC2Client()
     ec2_client.run = lambda instance: False
     with self.assertRaises(DatabaseCreationError) as e:
         create_database(instance, ec2_client)
     self.assertEqual(u"Failed to create EC2 instance.", e.exception[1])
 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()
Example #5
0
 def post(self, request):
     if not "name" in request.POST:
         return HttpResponse("App name is missing", status=500)
     name = request.POST.get("name", None)
     if not name:
         return HttpResponse("App name is empty", status=500)
     instance = Instance(name=name)
     try:
         create_database(instance, self._client)
     except Exception, e:
         return HttpResponse(e[1], status=500)
Example #6
0
 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_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()
Example #8
0
 def post(self, request):
     if not "name" in request.POST:
         return HttpResponse("App name is missing", status=500)
     name = request.POST.get("name")
     if not name:
         return HttpResponse("App name is empty", status=500)
     instance = Instance(name=canonicalize_db_name(name))
     try:
         create_database(instance, self._client)
     except Exception as e:
         return HttpResponse(e.args[-1], status=500)
     return HttpResponse("", status=201)
 def test_create_database_when_database_already_exist(self):
     settings.SHARED_SERVER = "127.0.0.1"
     instance = Instance(
         name="caravan",
         ec2_id="i-89",
     )
     create_database(instance)
     Instance.objects.filter(name=instance.name)[0].delete()
     try:
         with self.assertRaises(InstanceAlreadyExists):
             create_database(instance)
     finally:
         self.cursor.execute("DROP DATABASE IF EXISTS caravan")
 def test_create_database_when_database_already_exist(self):
     settings.SHARED_SERVER = "127.0.0.1"
     instance = Instance(
         name="caravan",
         ec2_id="i-89",
     )
     create_database(instance)
     Instance.objects.filter(name=instance.name)[0].delete()
     try:
         with self.assertRaises(InstanceAlreadyExists):
             create_database(instance)
     finally:
         self.cursor.execute("DROP DATABASE IF EXISTS caravan")
 def test_create_database_provisioned_none_left(self):
     settings.USE_POOL = True
     instance = Instance.objects.create(name="mydb")
     self.addCleanup(instance.delete)
     pi = ProvisionedInstance.objects.create(instance=instance,
                                             host="127.0.0.1",
                                             port=3306,
                                             admin_user="******")
     self.addCleanup(pi.delete)
     instance = Instance(name="hello_world")
     with self.assertRaises(DatabaseCreationError) as cm:
         create_database(instance)
     exc = cm.exception
     self.assertEqual((instance, "No free instances available in the pool"),
                      exc.args)
Example #12
0
 def test_create_database_provisioned_none_left(self):
     settings.USE_POOL = True
     instance = Instance.objects.create(name="mydb")
     self.addCleanup(instance.delete)
     pi = ProvisionedInstance.objects.create(instance=instance,
                                             host="127.0.0.1",
                                             port=3306,
                                             admin_user="******")
     self.addCleanup(pi.delete)
     instance = Instance(name="hello_world")
     with self.assertRaises(DatabaseCreationError) as cm:
         create_database(instance)
     exc = cm.exception
     self.assertEqual((instance, "No free instances available in the pool"),
                      exc.args)
Example #13
0
 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()
Example #14
0
 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_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()
Example #17
0
 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()
Example #19
0
 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_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()
Example #22
0
 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()
Example #23
0
 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()
Example #24
0
 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()
 def test_create_database_invalid_name(self):
     instance = Instance(name="mysql")
     with self.assertRaises(InvalidInstanceName):
         create_database(instance)
Example #26
0
 def test_create_database_invalid_name(self):
     instance = Instance(name="mysql")
     with self.assertRaises(InvalidInstanceName):
         create_database(instance)