class SlicehostTest(unittest.TestCase, TestCaseMixin):

    def setUp(self):

        Slicehost.connectionCls.conn_classes = (None, SlicehostMockHttp)
        SlicehostMockHttp.type = None
        self.driver = Slicehost(*SLICEHOST_PARAMS)

    def test_list_nodes(self):
        ret = self.driver.list_nodes()
        self.assertEqual(len(ret), 1)
        node = ret[0]
        self.assertTrue('174.143.212.229' in node.public_ips)
        self.assertTrue('10.176.164.199' in node.private_ips)
        self.assertEqual(node.state, NodeState.PENDING)

        SlicehostMockHttp.type = 'UNAUTHORIZED'
        try:
            ret = self.driver.list_nodes()
        except InvalidCredsError:
            e = sys.exc_info()[1]
            self.assertEqual(e.value, 'HTTP Basic: Access denied.')
        else:
            self.fail('test should have thrown')

    def test_list_sizes(self):
        ret = self.driver.list_sizes()
        self.assertEqual(len(ret), 7)
        size = ret[0]
        self.assertEqual(size.name, '256 slice')

    def test_list_images(self):
        ret = self.driver.list_images()
        self.assertEqual(len(ret), 11)
        image = ret[0]
        self.assertEqual(image.name, 'CentOS 5.2')
        self.assertEqual(image.id, '2')

    def test_reboot_node(self):
        node = Node(id=1, name=None, state=None, public_ips=None, private_ips=None,
                    driver=self.driver)

        ret = node.reboot()
        self.assertTrue(ret is True)

        ret = self.driver.reboot_node(node)
        self.assertTrue(ret is True)

        SlicehostMockHttp.type = 'FORBIDDEN'
        try:
            ret = self.driver.reboot_node(node)
        except Exception:
            e = sys.exc_info()[1]
            self.assertEqual(e.args[0], 'Permission denied')
        else:
            self.fail('test should have thrown')

    def test_destroy_node(self):
        node = Node(id=1, name=None, state=None, public_ips=None, private_ips=None,
                    driver=self.driver)

        ret = node.destroy()
        self.assertTrue(ret is True)

        ret = self.driver.destroy_node(node)
        self.assertTrue(ret is True)

    def test_create_node(self):
        image = NodeImage(id=11, name='ubuntu 8.10', driver=self.driver)
        size = NodeSize(1, '256 slice', None, None, None, None, driver=self.driver)
        node = self.driver.create_node(name='slicetest', image=image, size=size)
        self.assertEqual(node.name, 'slicetest')
        self.assertEqual(node.extra.get('password'), 'fooadfa1231')
class SlicehostTest(unittest.TestCase, TestCaseMixin):
    def setUp(self):

        Slicehost.connectionCls.conn_classes = (None, SlicehostMockHttp)
        SlicehostMockHttp.type = None
        self.driver = Slicehost(*SLICEHOST_PARAMS)

    def test_list_nodes(self):
        ret = self.driver.list_nodes()
        self.assertEqual(len(ret), 1)
        node = ret[0]
        self.assertTrue('174.143.212.229' in node.public_ips)
        self.assertTrue('10.176.164.199' in node.private_ips)
        self.assertEqual(node.state, NodeState.PENDING)

        SlicehostMockHttp.type = 'UNAUTHORIZED'
        try:
            ret = self.driver.list_nodes()
        except InvalidCredsError:
            e = sys.exc_info()[1]
            self.assertEqual(e.value, 'HTTP Basic: Access denied.')
        else:
            self.fail('test should have thrown')

    def test_list_sizes(self):
        ret = self.driver.list_sizes()
        self.assertEqual(len(ret), 7)
        size = ret[0]
        self.assertEqual(size.name, '256 slice')

    def test_list_images(self):
        ret = self.driver.list_images()
        self.assertEqual(len(ret), 11)
        image = ret[0]
        self.assertEqual(image.name, 'CentOS 5.2')
        self.assertEqual(image.id, '2')

    def test_reboot_node(self):
        node = Node(id=1,
                    name=None,
                    state=None,
                    public_ips=None,
                    private_ips=None,
                    driver=self.driver)

        ret = node.reboot()
        self.assertTrue(ret is True)

        ret = self.driver.reboot_node(node)
        self.assertTrue(ret is True)

        SlicehostMockHttp.type = 'FORBIDDEN'
        try:
            ret = self.driver.reboot_node(node)
        except Exception:
            e = sys.exc_info()[1]
            self.assertEqual(e.args[0], 'Permission denied')
        else:
            self.fail('test should have thrown')

    def test_destroy_node(self):
        node = Node(id=1,
                    name=None,
                    state=None,
                    public_ips=None,
                    private_ips=None,
                    driver=self.driver)

        ret = node.destroy()
        self.assertTrue(ret is True)

        ret = self.driver.destroy_node(node)
        self.assertTrue(ret is True)

    def test_create_node(self):
        image = NodeImage(id=11, name='ubuntu 8.10', driver=self.driver)
        size = NodeSize(1,
                        '256 slice',
                        None,
                        None,
                        None,
                        None,
                        driver=self.driver)
        node = self.driver.create_node(name='slicetest',
                                       image=image,
                                       size=size)
        self.assertEqual(node.name, 'slicetest')
        self.assertEqual(node.extra.get('password'), 'fooadfa1231')