Exemplo n.º 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()
Exemplo n.º 2
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()
Exemplo n.º 3
0
 def test_db_manager_shared_instance_with_public_shared(self):
     settings.SHARED_SERVER = "20.20.20.20"
     settings.SHARED_SERVER_PUBLIC_HOST = "10.10.10.10"
     settings.SHARED_USER = "******"
     settings.SHARED_PASSWORD = "******"
     instance = Instance(shared=True)
     db = instance.db_manager()
     self.assertEqual("10.10.10.10", db.public_host)
Exemplo n.º 4
0
 def test_db_manager_shared_instance_with_public_shared(self):
     settings.SHARED_SERVER = "20.20.20.20"
     settings.SHARED_SERVER_PUBLIC_HOST = "10.10.10.10"
     settings.SHARED_USER = "******"
     settings.SHARED_PASSWORD = "******"
     instance = Instance(shared=True)
     db = instance.db_manager()
     self.assertEqual("10.10.10.10", db.public_host)
Exemplo n.º 5
0
 def test_is_up_shold_return_true_when_instance_is_running_and_db_is_up(self):
     mocker = Mocker()
     obj = mocker.replace("mysqlapi.api.models.DatabaseManager.is_up")
     obj()
     mocker.result(True)
     mocker.replay()
     instance = Instance(name="foo", state="running")
     manager = DatabaseManager("foo", "127.0.0.1")
     self.assertTrue(instance.is_up(manager))
Exemplo n.º 6
0
 def test_is_up_should_return_false_when_instance_is_not_running(self):
     mocker = Mocker()
     obj = mocker.replace("mysqlapi.api.models.DatabaseManager.is_up")
     obj()
     mocker.result(False)
     mocker.replay()
     instance = Instance(name="foo", state="running")
     manager = DatabaseManager("foo", "127.0.0.1")
     self.assertFalse(instance.is_up(manager))
Exemplo n.º 7
0
 def test_db_manager_dedicated_instance(self):
     instance = Instance(
         host="10.10.10.10",
         shared=False,
     )
     db = instance.db_manager()
     self.assertIsInstance(db, DatabaseManager)
     self.assertEqual(instance.host, db.conn.hostname)
     self.assertEqual("root", db.conn.username)
     self.assertEqual("", db.conn.password)
Exemplo n.º 8
0
 def test_alloc_already_allocated(self):
     pi = ProvisionedInstance(instance=Instance(name="mydb"),
                              host="10.10.10.10",
                              port=3306,
                              admin_user="******",
                              admin_password="******")
     with self.assertRaises(TypeError) as cm:
         pi.alloc(Instance(name="yourdb"))
     exc = cm.exception
     self.assertEqual("This instance is not available", exc.args[0])
Exemplo n.º 9
0
 def test_db_manager_dedicated_instance(self):
     instance = Instance(
         host="10.10.10.10",
         shared=False,
     )
     db = instance.db_manager()
     self.assertIsInstance(db, DatabaseManager)
     self.assertEqual(instance.host, db.conn.hostname)
     self.assertEqual("root", db.conn.username)
     self.assertEqual("", db.conn.password)
Exemplo n.º 10
0
 def test_db_manager_shared_instance(self):
     settings.SHARED_SERVER = "20.20.20.20"
     settings.SHARED_USER = "******"
     settings.SHARED_PASSWORD = "******"
     instance = Instance(shared=True, )
     db = instance.db_manager()
     self.assertIsInstance(db, DatabaseManager)
     self.assertEqual("20.20.20.20", db.conn.hostname)
     self.assertEqual("fsouza", db.conn.username)
     self.assertEqual("123", db.conn.password)
Exemplo n.º 11
0
 def test_db_manager_shared_instance(self):
     settings.SHARED_SERVER = "20.20.20.20"
     settings.SHARED_USER = "******"
     settings.SHARED_PASSWORD = "******"
     instance = Instance(
         shared=True,
     )
     db = instance.db_manager()
     self.assertIsInstance(db, DatabaseManager)
     self.assertEqual("20.20.20.20", db.conn.hostname)
     self.assertEqual("fsouza", db.conn.username)
     self.assertEqual("123", db.conn.password)
Exemplo n.º 12
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()
Exemplo n.º 13
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])
Exemplo n.º 14
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()
Exemplo n.º 15
0
 def test_db_manager_provisioned_instance(self):
     instance = Instance(name="lost")
     pi = ProvisionedInstance.objects.create(host="127.0.0.1",
                                             port=3306,
                                             admin_user="******",
                                             admin_password="")
     self.addCleanup(pi.delete)
     pi.alloc(instance)
     self.addCleanup(pi.dealloc)
     pi.admin_user = "******"
     pi.admin_password = "******"
     pi.save()
     db = instance.db_manager()
     self.assertEqual(instance.host, db.conn.hostname)
     self.assertEqual(instance.port, db.conn.port)
     self.assertEqual(pi.admin_user, db.conn.username)
     self.assertEqual(pi.admin_password, db.conn.password)
Exemplo n.º 16
0
 def test_db_manager_provisioned_instance(self):
     instance = Instance(name="lost")
     pi = ProvisionedInstance.objects.create(host="127.0.0.1",
                                             port=3306,
                                             admin_user="******",
                                             admin_password="")
     self.addCleanup(pi.delete)
     pi.alloc(instance)
     self.addCleanup(pi.dealloc)
     pi.admin_user = "******"
     pi.admin_password = "******"
     pi.save()
     db = instance.db_manager()
     self.assertEqual(instance.host, db.conn.hostname)
     self.assertEqual(instance.port, db.conn.port)
     self.assertEqual(pi.admin_user, db.conn.username)
     self.assertEqual(pi.admin_password, db.conn.password)
Exemplo n.º 17
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()
Exemplo n.º 18
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()
Exemplo n.º 19
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()
Exemplo n.º 20
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()
Exemplo n.º 21
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()
Exemplo n.º 22
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)
Exemplo n.º 23
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()
Exemplo n.º 24
0
 def test_alloc_create_database_failure(self):
     pi = ProvisionedInstance(host="localhost",
                              admin_user="******",
                              admin_password="")
     pi.save()
     self.addCleanup(pi.delete)
     db_manager = mock.Mock()
     db_manager.create_database.side_effect = TypeError("blow up")
     pi._db_manager = db_manager
     instance = Instance(name="hibria")
     with self.assertRaises(DatabaseCreationError):
         pi.alloc(instance)
     self.assertIsNone(instance.pk)
Exemplo n.º 25
0
 def test_manager(self):
     pi = ProvisionedInstance(instance=Instance(name="mydb"),
                              host="10.10.10.10",
                              port=3306,
                              admin_user="******",
                              admin_password="******")
     with mock.patch("mysqlapi.api.models.DatabaseManager") as dm:
         pi._manager()
         dm.assert_called_with(name="mydb",
                               host="10.10.10.10",
                               port=3306,
                               user="******",
                               password="******")
Exemplo n.º 26
0
 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")
Exemplo n.º 27
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()
Exemplo n.º 28
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()
Exemplo n.º 29
0
 def test_dealloc(self):
     pi = ProvisionedInstance(host="localhost",
                              admin_user="******",
                              admin_password="")
     pi.save()
     self.addCleanup(pi.delete)
     db_manager = mock.Mock()
     pi._db_manager = db_manager
     instance = Instance(name="hibria")
     pi.alloc(instance)
     self.addCleanup(instance.delete)
     pi.dealloc()
     self.assertIsNone(pi.instance)
     self.assertEqual("stopped", instance.state)
     db_manager.drop_database.assert_called()
Exemplo n.º 30
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()
Exemplo n.º 31
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()
Exemplo n.º 32
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()
Exemplo n.º 33
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)
Exemplo n.º 34
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()
Exemplo n.º 35
0
 def test_drop_database_from_pool(self):
     instance = Instance(name="presto")
     pi = ProvisionedInstance.objects.create(host="127.0.0.1",
                                             port=3306,
                                             admin_user="******")
     self.addCleanup(pi.delete)
     pi.alloc(instance)
     self.addCleanup(instance.delete)
     view = DropDatabase()
     request = RequestFactory().delete("/presto")
     resp = view.delete(request, "presto")
     self.assertEqual(200, resp.status_code)
     sql = "select SCHEMA_NAME from information_schema.SCHEMATA " +\
           "where SCHEMA_NAME = 'presto'"
     self.cursor.execute(sql)
     row = self.cursor.fetchone()
     self.assertIsNone(row)
     pi = ProvisionedInstance.objects.get(pk=pi.pk)
     self.assertEqual(None, pi.instance)
Exemplo n.º 36
0
 def test_alloc(self):
     pi = ProvisionedInstance(host="localhost",
                              admin_user="******",
                              admin_password="")
     pi.save()
     self.addCleanup(pi.delete)
     db_manager = mock.Mock()
     pi._db_manager = db_manager
     instance = Instance(name="hibria")
     pi.alloc(instance)
     self.addCleanup(instance.delete)
     self.assertIsNotNone(instance.pk)
     self.assertIsNone(instance.ec2_id)
     self.assertFalse(instance.shared)
     self.assertEqual("running", instance.state)
     self.assertEqual("localhost", instance.host)
     self.assertEqual("3306", instance.port)
     self.assertEqual(instance, pi.instance)
     db_manager.create_database.assert_called()
Exemplo n.º 37
0
 def test_is_up_returns_true_when_instance_is_running_and_db_is_up(self):
     with mock.patch("mysqlapi.api.models.DatabaseManager.is_up") as is_up:
         is_up.return_value = True
         instance = Instance(name="foo", state="running")
         self.assertTrue(instance.is_up())
Exemplo n.º 38
0
 def test_is_up_should_return_false_when_instance_is_not_running(self):
     with mock.patch("mysqlapi.api.models.DatabaseManager.is_up") as is_up:
         is_up.return_value = False
         instance = Instance(name="foo", state="running")
         self.assertFalse(instance.is_up())
Exemplo n.º 39
0
 def test_is_up_should_return_false_when_instance_is_not_running(self):
     with mock.patch("mysqlapi.api.models.DatabaseManager.is_up") as is_up:
         is_up.return_value = False
         instance = Instance(name="foo", state="running")
         self.assertFalse(instance.is_up())
Exemplo n.º 40
0
 def test_is_up_returns_true_when_instance_is_running_and_db_is_up(self):
     with mock.patch("mysqlapi.api.models.DatabaseManager.is_up") as is_up:
         is_up.return_value = True
         instance = Instance(name="foo", state="running")
         self.assertTrue(instance.is_up())
Exemplo n.º 41
0
 def test_create_database_invalid_name(self):
     instance = Instance(name="mysql")
     with self.assertRaises(InvalidInstanceName):
         create_database(instance)