def test_flatten(self):
        obj = fakes.FakePolicy.create_one()

        flat_data = policy._flatten_policy(obj)

        data = (flat_data['id'], flat_data['name'], flat_data['enabled'],
                flat_data['operation_type'],
                flat_data['retention_duration_days'], flat_data['max_backups'],
                flat_data['year_backups'], flat_data['day_backups'],
                flat_data['week_backups'], flat_data['month_backups'],
                flat_data['timezone'], flat_data['start_time'])

        od = obj.operation_definition
        cmp_data = (
            obj.id,
            obj.name,
            obj.enabled,
            obj.operation_type,
            od.retention_duration_days,
            od.max_backups,
            od.year_backups,
            od.day_backups,
            od.week_backups,
            od.month_backups,
            od.timezone,
            obj.trigger.properties.start_time,
        )

        self.assertEqual(data, cmp_data)
class TestListPolicy(fakes.TestCBR):

    objects = fakes.FakePolicy.create_multiple(3)

    columns = ('ID', 'name', 'operation_type', 'start_time', 'enabled')

    data = []

    for s in objects:
        flat_data = policy._flatten_policy(s)
        data.append(
            (flat_data['id'], flat_data['name'], flat_data['operation_type'],
             flat_data['start_time'], flat_data['enabled']))

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

        self.cmd = policy.ListPolicies(self.app, None)

        self.client.policies = mock.Mock()
        self.client.api_mock = self.client.policies

    def test_default(self):
        arglist = ['--vault-id', 'vault_id', '--operation-type', 'backup']

        verifylist = [('vault_id', 'vault_id'), ('operation_type', 'backup')]

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

        # Set the response
        self.client.api_mock.side_effect = [self.objects]

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

        self.client.api_mock.assert_called_once_with(
            vault_id='vault_id',
            operation_type='backup',
        )

        self.assertEqual(self.columns, columns)
        self.assertEqual(self.data, list(data))
class TestUpdatePolicy(fakes.TestCBR):

    object = fakes.FakePolicy.create_one()

    columns = (
        'ID',
        'name',
        'operation_type',
        'start_time',
        'enabled',
        'retention_duration_days',
        'max_backups',
        'day_backups',
        'week_backups',
        'month_backups',
        'year_backups',
        'timezone',
    )

    flat_data = policy._flatten_policy(object)

    data = (
        flat_data['id'],
        flat_data['name'],
        flat_data['operation_type'],
        flat_data['start_time'],
        flat_data['enabled'],
        flat_data['retention_duration_days'],
        flat_data['max_backups'],
        flat_data['day_backups'],
        flat_data['week_backups'],
        flat_data['month_backups'],
        flat_data['year_backups'],
        flat_data['timezone'],
    )

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

        self.cmd = policy.UpdatePolicy(self.app, None)
        self.app.client_manager.sdk_connection = mock.Mock()

        self.client.update_policy = mock.Mock()

    def test_default(self):
        arglist = [
            'policy_id', '--name', 'pol1', '--enable', '--pattern',
            'pattern_1', '--pattern', 'pattern_2', '--day-backups', '1',
            '--week-backups', '2', '--month-backups', '3', '--year-backups',
            '4', '--timezone', 'tz', '--max-backups', '10',
            '--retention-duration-days', '9'
        ]
        verifylist = [
            ('policy', 'policy_id'),
            ('name', 'pol1'),
            ('enable', True),
            ('patterns', ['pattern_1', 'pattern_2']),
            ('day_backups', 1),
            ('week_backups', 2),
            ('month_backups', 3),
            ('year_backups', 4),
            ('timezone', 'tz'),
            ('max_backups', 10),
            ('retention_duration_days', 9),
        ]

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

        # Set the response
        self.client.update_policy.side_effect = [self.object]

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

        self.client.find_policy.assert_called_with(name_or_id='policy_id',
                                                   ignore_missing=False)

        self.client.update_policy.assert_called_once_with(
            policy=mock.ANY,
            name='pol1',
            enabled=True,
            trigger={'properties': {
                'pattern': ['pattern_1', 'pattern_2']
            }},
            operation_definition={
                'day_backups': 1,
                'week_backups': 2,
                'month_backups': 3,
                'year_backups': 4,
                'max_backups': 10,
                'retention_duration_days': 9,
                'timezone': 'tz'
            })

        self.data, self.columns = policy._add_vaults_to_policy_obj(
            self.object, self.data, self.columns)

        self.data, self.columns = policy._add_scheduling_patterns(
            self.object, self.data, self.columns)

        self.assertEqual(self.columns, columns)
        self.assertEqual(self.data, data)
class TestShowPolicy(fakes.TestCBR):

    object = fakes.FakePolicy.create_one()

    columns = (
        'ID',
        'name',
        'operation_type',
        'start_time',
        'enabled',
        'retention_duration_days',
        'max_backups',
        'day_backups',
        'week_backups',
        'month_backups',
        'year_backups',
        'timezone',
    )

    flat_data = policy._flatten_policy(object)

    data = (
        flat_data['id'],
        flat_data['name'],
        flat_data['operation_type'],
        flat_data['start_time'],
        flat_data['enabled'],
        flat_data['retention_duration_days'],
        flat_data['max_backups'],
        flat_data['day_backups'],
        flat_data['week_backups'],
        flat_data['month_backups'],
        flat_data['year_backups'],
        flat_data['timezone'],
    )

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

        self.cmd = policy.ShowPolicy(self.app, None)

        self.client.find_policy = mock.Mock()

    def test_default(self):
        arglist = ['policy']
        verifylist = []

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

        # Set the response
        self.client.find_policy.side_effect = [self.object]

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

        self.client.find_policy.assert_called_once_with(
            name_or_id='policy',
            ignore_missing=False,
        )

        self.data, self.columns = policy._add_vaults_to_policy_obj(
            self.object, self.data, self.columns)

        self.data, self.columns = policy._add_scheduling_patterns(
            self.object, self.data, self.columns)

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