def setUp(self):
        RackspaceDatabaseDriver.connectionCls.conn_classes = (
                RackspaceMockHttp, RackspaceMockHttp)
        RackspaceDatabaseDriver.connectionCls.auth_url = \
                'https://auth.api.example.com/v1.1/'

        RackspaceMockHttp.type = None
        self.driver = RackspaceDatabaseDriver(key=RACKSPACE_PARAMS[0],
                                                secret=RACKSPACE_PARAMS[1])
class RackspaceTests(unittest.TestCase):
    def setUp(self):
        RackspaceDatabaseDriver.connectionCls.conn_classes = (
                RackspaceMockHttp, RackspaceMockHttp)
        RackspaceDatabaseDriver.connectionCls.auth_url = \
                'https://auth.api.example.com/v1.1/'

        RackspaceMockHttp.type = None
        self.driver = RackspaceDatabaseDriver(key=RACKSPACE_PARAMS[0],
                                                secret=RACKSPACE_PARAMS[1],
                ex_force_base_url='http://www.todo.com')

    def test_list_flavors(self):
        results = self.driver.list_flavors()
        self.assertEqual(len(results), 4)
        self.assertEqual(results[0].id, 3)
        self.assertEqual(results[0].ram, 2048)
        self.assertEqual(results[0].href,
                ("http://ord.databases.api.rackspacecloud.com" +
                "/v1.0/586067/flavors/3"))

    def test_get_flavor(self):
        href = ("http://ord.databases.api." +
            "rackspacecloud.com/v1.0/586067/flavors/3")
        f = Flavor(3, 'm1.medium', 1, 2048, href)
        result = self.driver.get_flavor(3)
        self.assertEqual(result.id, f.id)
        self.assertEqual(result.name, f.name)
        self.assertEqual(result.vcpus, f.vcpus)
        self.assertEqual(result.ram, f.ram)
        self.assertEqual(result.href, f.href)

    def test_delete_instance(self):
        result = self.driver.delete_instance('68345c52')
        self.assertEqual(result, [])

    def test_list_instances(self):
        result = list(self.driver.list_instances())
        self.assertEqual(len(result), 3)
        self.assertEqual(result[0].id, '81e93520')
        self.assertEqual(result[0].size, None)
        self.assertEqual(result[1].id, '68345c52')
        self.assertEqual(result[1].databases, None)
        self.assertEqual(result[1].rootEnabled, None)
        self.assertEqual(result[1].size, 2)
        self.assertEqual(result[2].id, '12345c52')
        self.assertEqual(result[2].databases, None)
        self.assertEqual(result[2].rootEnabled, None)

    def test_get_instance(self):
        flavorRef = ("http://ord.databases.api." +
            "rackspacecloud.com/v1.0/586067/flavors/1")
        databases = [
                Database(name='nextround', collate='utf8_general_ci',
                    character_set='utf8'),
                Database(name='sampledb', collate='utf8_general_ci',
                    character_set='utf8')
        ]
        i = Instance(flavorRef, size=2, name='a_rack_instance',
                id='68345c52', databases=databases,
                status=InstanceStatus.ACTIVE, rootEnabled=False)
        result = self.driver.get_instance('68345c52')

        self.assertEqual(result.flavorRef, i.flavorRef)
        self.assertEqual(result.name, i.name)
        self.assertEqual(result.status, i.status)
        self.assertEqual(result.id, i.id)
        self.assertEqual(result.rootEnabled, i.rootEnabled)
        self.assertEqual(str(result.databases[0]),
                str(i.databases[0]))
        self.assertEqual(str(result.databases[1]),
                str(i.databases[1]))

    def test_create_instance_with_databases(self):
        flavorRef = ("http://ord.databases.api.rackspacecloud.com" +
        "/v1.0/586067/flavors/1")
        databases = [Database('some_database'),
                Database('another_database', character_set='utf8')]
        i = Instance(flavorRef, size=2,
                name='a_rack_instance', databases=databases)
        result = self.driver.create_instance(i)
        self.assertEqual(result.name, 'a_rack_instance')

    def test_restart_instance(self):
        result = self.driver.restart_instance('123456')
        self.assertEqual(result, [])

    def test_resize_instance_volume(self):
        result = self.driver.resize_instance_volume('1234567', 4)
        self.assertEqual(result, [])

    def test_resize_instance(self):
        flavorRef = ("http://ord.databases.api.rackspacecloud.com" +
        "/v1.0/586067/flavors/1")
        result = self.driver.resize_instance('12345678', flavorRef)
        self.assertEqual(result, [])

    def test_create_databases(self):
        databases = [Database('a_database', character_set='utf8',
                collate='utf8_general_ci'),
                Database('another_database')]
        result = self.driver.create_databases('123456', databases)
        self.assertEqual(result, [])

    def test_create_database(self):
        database = Database('a_database', character_set='cset',
                collate='cset_general_ci')
        result = self.driver.create_database('1234567', database)
        self.assertEqual(result, [])

    def test_list_databases(self):
        databases = [Database('a_database', character_set='utf8',
                collate='utf8_general_ci'),
                Database('another_database')]
        results = self.driver.list_databases('123456')
        self.assertEqual(len(results), 2)
        self.assertEqual(str(results[0]), str(databases[0]))
        self.assertEqual(str(results[1]), str(databases[1]))

    def test_delete_database(self):
        result = self.driver.delete_database('123456', 'adatabase')
        self.assertEqual(result, [])

    def test_create_users(self):
        users = [User('a_user', password='******'),
                User('another_user', password='******')]
        database_lists = [
                [Database('a_database'), Database('another_database')],
            [Database('yet_another_database')]]

        result = self.driver.create_users('123456',
                [
                    (users[0], database_lists[0]),
                    (users[1], database_lists[1])
                ])

        self.assertEqual(result, [])

    def test_create_user(self):
        user = User('a_user', password='******')
        database_list = [Database('a_database'), Database('another_database')]

        result = self.driver.create_user('1234567', user, database_list)
        self.assertEqual(result, [])

    def test_list_users(self):
        results = self.driver.list_users('123456')
        expected = [User('dbuser3'), User('dbuser4'),
                User('testuser'), User('userwith2dbs')]
        self.assertEqual(str(results), str(expected))

    def test_delete_user(self):
        result = self.driver.delete_user('123456', 'auser')
        self.assertEqual(result, [])

    def test_enable_root(self):
        expected = User('root', password='******')
        result = self.driver.enable_root('123456')
        self.assertEqual(str(result), str(expected))

    def test_has_root_enabled(self):
        result = self.driver.has_root_enabled('123456')
        self.assertEqual(result, True)
        result = self.driver.has_root_enabled('1234567')
        self.assertEqual(result, False)