def test_flatten(self):
        _obj = fakes.FakeClusterNode.create_one()

        flat_data = cluster_node._flatten_cluster_node(_obj)

        data = (
            flat_data['id'],
            flat_data['name'],
            flat_data['flavor'],
            flat_data['private_ip'],
            flat_data['public_ip'],
            flat_data['availability_zone'],
            flat_data['ssh_key'],
            flat_data['status'],
        )

        cmp_data = (
            _obj.metadata.id,
            _obj.metadata.name,
            _obj.spec.flavor,
            _obj.status.private_ip,
            _obj.status.floating_ip,
            _obj.spec.availability_zone,
            _obj.spec.login.get('sshKey', None),
            _obj.status.status,
        )

        self.assertEqual(data, cmp_data)
class TestShowClusterNode(fakes.TestCCE):

    _obj = fakes.FakeClusterNode.create_one()

    columns = ('ID', 'name', 'flavor',
               'private_ip', 'public_ip', 'availability_zone', 'ssh_key',
               'status')

    flat_data = cluster_node._flatten_cluster_node(_obj)

    data = (
        flat_data['id'],
        flat_data['name'],
        flat_data['flavor'],
        flat_data['private_ip'],
        flat_data['public_ip'],
        flat_data['availability_zone'],
        flat_data['ssh_key'],
        flat_data['status'],
    )

    def setUp(self):
        super(TestShowClusterNode, self).setUp()

        self.cmd = cluster_node.ShowCCEClusterNode(self.app, None)

        self.client.find_cluster_node = mock.Mock()

        self.client.find_cluster = mock.Mock(
            return_value=cluster.Cluster(id='cluster_uuid'))

    def test_show(self):
        arglist = [
            'cluster_uuid',
            'node_id'
        ]

        verifylist = [
            ('cluster', 'cluster_uuid'),
            ('node', 'node_id')
        ]

        # Verify cm is triggereg with default parameters
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        # Set the response
        self.client.find_cluster_node.side_effect = [
            self._obj
        ]

        # Trigger the action
        columns, data = self.cmd.take_action(parsed_args)

        self.client.find_cluster_node.assert_called_once_with(
            cluster='cluster_uuid',
            node='node_id')

        self.assertEqual(self.columns, columns)
        self.assertEqual(self.data, data)
class TestListClusterNode(fakes.TestCCE):

    _objs = fakes.FakeClusterNode.create_multiple(3)

    columns = ('ID', 'name', 'private_ip', 'public_ip',
               'status')

    data = []

    for s in _objs:
        flat_data = cluster_node._flatten_cluster_node(s)

        data.append((
            flat_data['id'],
            flat_data['name'],
            flat_data['private_ip'],
            flat_data['public_ip'],
            flat_data['status'],
        ))

    def setUp(self):
        super(TestListClusterNode, self).setUp()

        self.cmd = cluster_node.ListCCEClusterNode(self.app, None)

        self.client.cluster_nodes = mock.Mock()
        self.client.find_cluster = mock.Mock(
            return_value=cluster.Cluster(id='cluster_id'))

    def test_list_default(self):
        arglist = ['cluster_id']

        verifylist = [
            ('cluster', 'cluster_id')
        ]

        # Verify cm is triggereg with default parameters
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        # Set the response
        self.client.cluster_nodes.side_effect = [
            self._objs
        ]

        # Trigger the action
        columns, data = self.cmd.take_action(parsed_args)

        self.client.cluster_nodes.assert_called_once_with(
            'cluster_id'
        )

        self.assertEqual(self.columns, columns)
        self.assertEqual(self.data, list(data))
class TestCreateClusterNode(fakes.TestCCE):

    _obj = fakes.FakeClusterNode.create_one()

    columns = ('ID', 'name', 'flavor',
               'private_ip', 'public_ip', 'availability_zone', 'ssh_key',
               'status')

    flat_data = cluster_node._flatten_cluster_node(_obj)

    data = (
        flat_data['id'],
        flat_data['name'],
        flat_data['flavor'],
        flat_data['private_ip'],
        flat_data['public_ip'],
        flat_data['availability_zone'],
        flat_data['ssh_key'],
        flat_data['status'],
    )

    def setUp(self):
        super(TestCreateClusterNode, self).setUp()

        self.cmd = cluster_node.CreateCCEClusterNode(self.app, None)

        self.client.create_cluster_node = mock.Mock()

        self.client.find_cluster = mock.Mock(
            return_value=cluster.Cluster(id='cluster_id'))

    def test_create(self):
        arglist = [
            'cluster_name',
            '--flavor', 'flvr',
            '--label', 'l1=v1',
            '--label', 'l2=v2',
            '--annotation', 'a1=v1',
            '--annotation', 'a2=v2',
            '--volume', 'sata,30',
            '--root_volume', 'sata,230',
            '--ssh_key', 'key',
            '--availability_zone', 'az',
            '--count', '12'
        ]

        verifylist = [
            ('cluster', 'cluster_name'),
            ('flavor', 'flvr'),
            ('label', ['l1=v1', 'l2=v2']),
            ('annotation', ['a1=v1', 'a2=v2']),
            ('volume', 'sata,30'),
            ('root_volume', 'sata,230'),
            ('ssh_key', 'key'),
            ('availability_zone', 'az'),
            ('count', 12)
        ]

        # Verify cm is triggereg with default parameters
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        # Set the response
        self.client.create_cluster_node.side_effect = [
            self._obj
        ]

        # Trigger the action
        columns, data = self.cmd.take_action(parsed_args)

        self.client.create_cluster_node.assert_called_once_with(
            cluster='cluster_id',
            metadata={
                'annotations': {'a1': 'v1', 'a2': 'v2'},
                'labels': {'l1': 'v1', 'l2': 'v2'}
            },
            spec={
                'flavor': 'flvr',
                'login': {'sshKey': 'key'},
                'az': 'az',
                'count': 12,
                'rootVolume': {'volumetype': 'SATA', 'size': 230},
                'dataVolumes': [{'volumetype': 'SATA', 'size': 30}]
            }
        )

        self.assertEqual(self.columns, columns)
        self.assertEqual(self.data, data)
class TestCreateClusterNode(fakes.TestCCE):

    _obj = fakes.FakeClusterNode.create_one()

    columns = (
        'id',
        'name',
        'private_ip',
        'public_ip',
        'status',
        'flavor',
        'ssh_key',
        'availability_zone',
        'operating_system',
        'root_volume_type',
        'root_volume_size',
        'data_volume_type_1',
        'data_volume_size_1',
        'data_volume_type_2',
        'data_volume_size_2')

    flat_data = cluster_node._flatten_cluster_node(_obj)

    data = (
        flat_data['id'],
        flat_data['name'],
        flat_data['private_ip'],
        flat_data['public_ip'],
        flat_data['status'],
        flat_data['flavor'],
        flat_data['ssh_key'],
        flat_data['availability_zone'],
        flat_data['operating_system'],
        flat_data['root_volume_type'],
        flat_data['root_volume_size'],
        flat_data['data_volume_type_1'],
        flat_data['data_volume_size_1'],
        flat_data['data_volume_type_2'],
        flat_data['data_volume_size_2']
    )

    def setUp(self):
        super(TestCreateClusterNode, self).setUp()

        self.cmd = cluster_node.CreateCCEClusterNode(self.app, None)

        self.app.client_manager.sdk_connection = mock.Mock()

        self.cloud_client = self.app.client_manager.sdk_connection

        self.cloud_client.create_cce_cluster_node = mock.Mock()

        self.client.find_cluster = mock.Mock(
            return_value=cluster.Cluster(id='cluster_id'))

    def test_create(self):
        arglist = [
            'cluster_name',
            '--name', 'node_name',
            '--annotation', 'a1=v1',
            '--annotation', 'a2=v2',
            '--availability-zone', 'az',
            '--bandwidth', '30',
            '--count', '12',
            '--data-volume', 'volumetype=SATA,size=100',
            '--data-volume', 'volumetype=SAS,size=200',
            '--flavor', 'flvr',
            '--label', 'l1=v1',
            '--label', 'l2=v2',
            '--network', 'nw',
            '--root-volume-size', '230',
            '--root-volume-type', 'SATA',
            '--ssh-key', 'key',
        ]

        verifylist = [
            ('cluster', 'cluster_name'),
            ('name', 'node_name'),
            ('annotations', {'a1': 'v1', 'a2': 'v2'}),
            ('availability_zone', 'az'),
            ('bandwidth', 30),
            ('count', 12),
            ('data_volumes', [
                {'volumetype': 'SATA', 'size': '100'},
                {'volumetype': 'SAS', 'size': '200'}]),
            ('flavor', 'flvr'),
            ('labels', {'l1': 'v1', 'l2': 'v2'}),
            ('network', 'nw'),
            ('root_volume_size', 230),
            ('root_volume_type', 'SATA'),
            ('ssh_key', 'key'),
        ]

        # Verify cm is triggered with default parameters
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        # Set the response
        self.cloud_client.create_cce_cluster_node.side_effect = [
            self._obj
        ]

        # Trigger the action
        columns, data = self.cmd.take_action(parsed_args)

        self.cloud_client.create_cce_cluster_node.assert_called_once_with(
            availability_zone='az',
            cluster='cluster_name',
            count=12,
            flavor='flvr',
            network='nw',
            ssh_key='key',
            annotations={'a1': 'v1', 'a2': 'v2'},
            bandwidth=30,
            data_volumes=[
                {'volumetype': 'SATA', 'size': '100'},
                {'volumetype': 'SAS', 'size': '200'}],
            labels={'l1': 'v1', 'l2': 'v2'},
            name='node_name',
            root_volume_size=230,
            root_volume_type='SATA',
            wait=False,
            wait_interval=5,
            wait_timeout=3600
        )

        self.assertEqual(self.columns, columns)
        self.assertEqual(self.data, data)