class TestSetSnapshotConfiguration(fakes.TestCss):

    _cluster = fakes.FakeCluster.create_one()
    _data = fakes.FakeSnapshotPolicy.create_one()

    columns = COLUMNS

    data = fakes.gen_data(_data, columns)

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

        self.cmd = snapshot.SetSnapshotConfiguration(self.app, None)

        self.client.find_cluster = mock.Mock(return_value=self._cluster)
        self.client.get_snapshot_policy = mock.Mock(return_value=self._data)

    def test_setconfiguration_auto(self):
        arglist = [self._cluster.name, '--auto']
        verifylist = [('cluster', self._cluster.name), ('auto', True)]
        # Verify cm is triggereg with default parameters
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        # Trigger the action
        columns, data = self.cmd.take_action(parsed_args)
        self.client.set_snapshot_configuration.assert_called_with(
            self._cluster, True)

        self.assertEqual(self.columns, columns)
        self.assertEqual(self.data, data)
class TestRestoreSnapshot(fakes.TestCss):

    _cluster = fakes.FakeCluster.create_one()

    columns = ('id', 'name', 'type', 'version', 'endpoint', 'disk_encryption',
               'cmk_id', 'error', 'instance', 'instance_count', 'node_count',
               'is_disk_encrypted', 'is_https_enabled', 'progress', 'actions',
               'router_id', 'subnet_id', 'security_group_id', 'status',
               'created_at', 'updated_at')

    data = fakes.gen_data(_cluster, columns)

    default_timeout = 15

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

        self.cmd = snapshot.RestoreSnapshot(self.app, None)

        self.client.find_cluster = mock.Mock(return_value=self._cluster)
        self.client.restore_snapshot = mock.Mock(return_value=None)
        self.client.wait_for_cluster = mock.Mock(return_value=True)
        self.client.get_cluster = mock.Mock(return_value=self._cluster)

    def test_restore(self):
        arglist = [
            self._cluster.name, 'snapshot-uuid', '--target-cluster',
            self._cluster.name, '--indices', '1', '--rename-replacement', '2',
            '--rename-pattern', '3', '--wait'
        ]
        verifylist = [
            ('cluster', self._cluster.name),
            ('snapshotId', 'snapshot-uuid'),
            ('target_cluster', self._cluster.name),
            ('indices', '1'),
            ('rename_replacement', '2'),
            ('rename_pattern', '3'),
            ('wait', True),
        ]
        # Verify cm is triggereg with default parameters
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        # Trigger the action
        columns, data = self.cmd.take_action(parsed_args)
        attrs = {
            'targetCluster': self._cluster.id,
            'indices': '1',
            'renameReplacement': '2',
            'renamePattern': '3',
        }
        self.client.restore_snapshot.assert_called_with(
            self._cluster, 'snapshot-uuid', **attrs)
        self.client.wait_for_cluster.assert_called_with(
            self._cluster.id, self.default_timeout)
        self.assertEqual(self.columns, columns)
class TestSetSnapshotPolicy(fakes.TestCss):

    _cluster = fakes.FakeCluster.create_one()
    _data = fakes.FakeSnapshotPolicy.create_one()

    columns = COLUMNS

    data = fakes.gen_data(_data, columns)

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

        self.cmd = snapshot.SetSnapshotPolicy(self.app, None)

        self.client.find_cluster = mock.Mock(return_value=self._cluster)
        self.client.set_snapshot_policy = mock.Mock(return_value=None)
        self.client.get_snapshot_policy = mock.Mock(return_value=self._data)

    def test_setpolicy(self):
        arglist = [
            self._cluster.name, '--name-prefix', '1', '--keep-days', '2',
            '--period', '3', '--disable', '--delete-auto'
        ]
        verifylist = [
            ('cluster', self._cluster.name),
            ('name_prefix', '1'),
            ('keep_days', 2),
            ('period', '3'),
            ('disable', True),
            ('delete_auto', True),
        ]
        # Verify cm is triggereg with default parameters
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        # Trigger the action
        columns, data = self.cmd.take_action(parsed_args)
        attrs = {
            "prefix": "1",
            "keepday": 2,
            "period": "3",
            "enable": 'false',
            "deleteAuto": 'true',
        }
        self.client.set_snapshot_policy.assert_called_with(
            self._cluster, **attrs)
        self.client.get_snapshot_policy.assert_called_with(self._cluster)

        self.assertEqual(self.columns, columns)
class TestExtendCluster(fakes.TestCss):

    _data = fakes.FakeCluster.create_one()

    columns = COLUMNS

    data = fakes.gen_data(_data, columns)

    default_timeout = 15

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

        self.cmd = cluster.ExtendCluster(self.app, None)

        self.client.find_cluster = mock.Mock(return_value=self._data)
        self.client.extend_cluster = mock.Mock(return_value=None)
        self.client.get_cluster = mock.Mock(return_value=self._data)
        self.client.wait_for_cluster = mock.Mock(return_value=True)

    def test_extend(self):
        arglist = [
            self._data.name,
            '--add-nodes',
            '2',
            '--wait',
        ]

        verifylist = [
            ('cluster', self._data.name),
            ('add_nodes', 2),
            ('wait', True),
        ]

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

        # Trigger the action
        columns, data = self.cmd.take_action(parsed_args)
        self.client.find_cluster.assert_called_with(self._data.name)
        self.client.extend_cluster.assert_called_with(self._data, 2)
        self.client.wait_for_cluster.assert_called_with(
            self._data.id, self.default_timeout)
        self.client.get_cluster.assert_called_with(self._data.id)

        self.assertEqual(self.columns, columns)
class TestCreateSnapshot(fakes.TestCss):

    _cluster = fakes.FakeCluster.create_one()
    _data = fakes.FakeSnapshot.create_one()

    columns = ('id', 'name')

    data = fakes.gen_data(_data, columns)

    default_timeout = 15

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

        self.cmd = snapshot.CreateSnapshot(self.app, None)

        self.client.find_cluster = mock.Mock(return_value=self._cluster)
        self.client.create_snapshot = mock.Mock(return_value=self._data)
        self.client.wait_for_cluster = mock.Mock(return_value=True)

    def test_create(self):
        arglist = [
            self._cluster.name, 'test-snapshot', '--indices', '1',
            '--description', '2', '--wait'
        ]
        verifylist = [
            ('cluster', self._cluster.name),
            ('name', 'test-snapshot'),
            ('indices', '1'),
            ('description', '2'),
            ('wait', True),
        ]
        # Verify cm is triggereg with default parameters
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        # Trigger the action
        columns, data = self.cmd.take_action(parsed_args)
        attrs = {
            'name': 'test-snapshot',
            'indices': '1',
            'description': '2',
        }
        self.client.create_snapshot.assert_called_with(self._cluster, **attrs)
        self.client.wait_for_cluster.assert_called_with(
            self._cluster.id, self.default_timeout)
        self.assertEqual(self.columns, columns)
class TestCreateCluster(fakes.TestCss):

    _data = fakes.FakeCluster.create_one()

    columns = COLUMNS

    data = fakes.gen_data(_data, columns)

    default_timeout = 15

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

        self.cmd = cluster.CreateCluster(self.app, None)

        self.client.create_cluster = mock.Mock(return_value=self._data)
        self.client.get_cluster = mock.Mock(return_value=self._data)
        self.client.wait_for_cluster = mock.Mock(return_value=True)

    def test_create(self):
        arglist = [
            'test-css', '--flavor', 'css-flavor', '--router-id', 'router-uuid',
            '--network-id', 'network-uuid', '--security-group-id', 'sg-uuid',
            '--count', '2', '--size', '60', '--volume-type', 'COMMON',
            '--https-enable', '--admin-pwd', 'testtest', '--backup-policy',
            'period=00:00 GMT+08:00,keepday=7,prefix=snapshot', '--tag',
            'key=key1,value=value1', '--tag', 'key=key2,value=value2', '--wait'
        ]
        verifylist = [
            ('name', 'test-css'),
            ('flavor', 'css-flavor'),
            ('router_id', 'router-uuid'),
            ('network_id', 'network-uuid'),
            ('security_group_id', 'sg-uuid'),
            ('count', 2),
            ('volume_size', 60),
            ('volume_type', 'COMMON'),
            ('https_enable', True),
            ('admin_pwd', 'testtest'),
            ('backup_policy', [{
                'period': '00:00 GMT+08:00',
                'keepday': '7',
                'prefix': 'snapshot'
            }]),
            ('tags', [{
                'key': 'key1',
                'value': 'value1'
            }, {
                'key': 'key2',
                'value': 'value2'
            }]),
            ('wait', True),
        ]
        # Verify cm is triggereg with default parameters
        parsed_args = self.check_parser(self.cmd, arglist, verifylist)

        # Trigger the action
        columns, data = self.cmd.take_action(parsed_args)
        attrs = {
            'name':
            'test-css',
            'instanceNum':
            2,
            'instance': {
                'flavorRef': 'css-flavor',
                'volume': {
                    'volume_type': 'COMMON',
                    'size': 60
                },
                'nics': {
                    'vpcId': 'router-uuid',
                    'netId': 'network-uuid',
                    'securityGroupId': 'sg-uuid'
                }
            },
            'httpsEnable':
            'true',
            'authorityEnable':
            True,
            'adminPwd':
            'testtest',
            'backupStrategy': {
                'period': '00:00 GMT+08:00',
                'keepday': 7,
                'prefix': 'snapshot'
            },
            'tags': [{
                'key': 'key1',
                'value': 'value1'
            }, {
                'key': 'key2',
                'value': 'value2'
            }]
        }
        self.client.create_cluster.assert_called_with(**attrs)
        self.client.wait_for_cluster.assert_called_with(
            self._data.id, self.default_timeout)
        self.client.get_cluster.assert_called_with(self._data.id)
        self.assertEqual(self.columns, columns)
class TestShowCluster(fakes.TestCss):

    _data = fakes.FakeCluster.create_one()

    columns = COLUMNS

    data = fakes.gen_data(_data, columns)

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

        self.cmd = cluster.ShowCluster(self.app, None)

        self.client.find_cluster = mock.Mock(return_value=self._data)

    def test_show_no_options(self):
        arglist = []
        verifylist = []

        # Testing that a call without the required argument will fail and
        # throw a "ParserExecption"
        self.assertRaises(tests_utils.ParserException, self.check_parser,
                          self.cmd, arglist, verifylist)

    def test_show(self):
        arglist = [
            self._data.id,
        ]

        verifylist = [
            ('cluster', self._data.id),
        ]

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

        # Trigger the action
        columns, data = self.cmd.take_action(parsed_args)
        self.client.find_cluster.assert_called_with(self._data.id)

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

    def test_show_non_existent(self):
        arglist = [
            'unexist_css_cluster',
        ]

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

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

        find_mock_result = exceptions.CommandError('Resource Not Found')
        self.client.find_cluster = (mock.Mock(side_effect=find_mock_result))

        # Trigger the action
        try:
            self.cmd.take_action(parsed_args)
        except Exception as e:
            self.assertEqual('Resource Not Found', str(e))
        self.client.find_cluster.assert_called_with('unexist_css_cluster')