Beispiel #1
0
    def test_should_start_background_service(self, mock_sleep, mock_popen, mock_process_iter):
        context = Context()
        context.api = InactiveClientApi()
        context.init = MockInitApi(state_responses=[mock_data.INIT_STATE_PENDING, mock_data.INIT_STATE_SUCCESSFUL])
        context.rendering = JsonWriter()
        context.service_binary = 'test-name'
        context.service_main_class = 'test.name.Main'

        mock_process_iter.return_value = []
        mock_popen.return_value = None
        mock_sleep.return_value = None

        username = '******'
        password = '******'

        runner = Runner(cli)
        result = runner.invoke(
            args=['start', '--username', username, '--password', password],
            obj=context
        )

        self.assertEqual(result.exit_code, 0, result.output)
        self.assertDictEqual(json.loads(result.output), {'successful': True})
        mock_popen.assert_called_once()
        mock_sleep.assert_called_once()
        self.assertEqual(context.init.stats['state'], 2)
        self.assertEqual(context.init.stats['provide_credentials'], 1)
Beispiel #2
0
    def test_should_create_filtering_option(self):
        @click.command()
        @click.pass_context
        @with_filtering
        def test(ctx):
            spec = {'test_field': int, 'other_field': str, 'third_field': bool}

            entries = [{
                'test_field': 50,
                'other_field': 'some-test-value',
                'third_field': False
            }, {
                'test_field': 40,
                'other_field': 'some-test-value',
                'third_field': False
            }, {
                'test_field': 40,
                'other_field': 'some-value',
                'third_field': True
            }]

            click.echo('size={}'.format(
                len(list(ctx.obj.filtering.apply(entries, spec)))))

        runner = Runner().with_command(test)
        result = runner.invoke(args=['test', '--filter', 'test_field > 40'])

        self.assertEqual(result.exit_code, 0)
        self.assertEqual(result.output.strip(), 'size=1')
Beispiel #3
0
    def test_should_fail_define_backups_when_invalid_duration_specified(self):
        context = Context()
        context.api = MockClientApi()
        context.rendering = JsonWriter()

        runner = Runner(cli)
        result = runner.invoke(args=[
            'define',
            '--info',
            'test',
            '--redundant-copies',
            5,
            '--existing-versions-policy',
            'at-most',
            '--existing-versions-duration',
            'invalid',
            '--removed-versions-policy',
            'all',
            '--removed-versions-duration',
            '5 hours',
        ],
                               obj=context)

        self.assertEqual(result.exit_code, 2, result.output)
        self.assertIn('expected valid duration format', result.output)
Beispiel #4
0
    def test_should_define_backups(self, mock_prompt):
        context = Context()
        context.api = MockClientApi()
        context.rendering = JsonWriter()

        mock_prompt.return_value = 5

        runner = Runner(cli)
        result = runner.invoke(args=[
            'define',
            '--info',
            'test',
            '--redundant-copies',
            5,
            '--existing-versions-policy',
            'at-most',
            '--existing-versions-duration',
            '30 days',
            '--removed-versions-policy',
            'all',
            '--removed-versions-duration',
            '5 hours',
        ],
                               obj=context)

        self.assertEqual(result.exit_code, 0, result.output)

        self.assertTrue(json.loads(result.output))
        self.assertEqual(context.api.stats['device'], 1)
        self.assertEqual(context.api.stats['backup_define'], 1)
Beispiel #5
0
    def test_should_poll_init_state_when_starting_background_service(self, mock_sleep, mock_popen, mock_process_iter):
        expected_retries = 10

        state_responses = [mock_data.INIT_STATE_PENDING] * (expected_retries + 1)

        context = Context()
        context.api = InactiveClientApi()
        context.init = MockInitApi(state_responses=state_responses)
        context.rendering = JsonWriter()
        context.service_binary = 'test-name'
        context.service_main_class = 'test.name.Main'

        mock_process_iter.return_value = []
        mock_popen.return_value = None
        mock_sleep.return_value = None

        username = '******'
        password = '******'

        runner = Runner(cli)
        result = runner.invoke(
            args=['start', '--username', username, '--password', password],
            obj=context
        )

        self.assertEqual(result.exit_code, 0, result.output)
        self.assertDictEqual(
            json.loads(result.output),
            {'successful': False, 'failure': 'Initialization did not complete; last state received was [pending]'}
        )
        mock_popen.assert_called_once()
        self.assertEqual(mock_sleep.call_count, expected_retries)
        self.assertEqual(context.init.stats['state'], expected_retries + 1)
        self.assertEqual(context.init.stats['provide_credentials'], 1)
Beispiel #6
0
    def test_should_follow_operations(self):
        context = Context()
        context.api = MockClientApi()
        context.rendering = JsonWriter()

        runner = Runner(cli)
        result = runner.invoke(args=['follow', str(uuid4())], obj=context)

        self.assertEqual(result.exit_code, 0, result.output)
        self.assertEqual(context.api.stats['operation_follow'], 1)
Beispiel #7
0
    def test_should_refresh_configured_schedules(self):
        context = Context()
        context.api = MockClientApi()
        context.rendering = JsonWriter()

        runner = Runner(cli)
        result = runner.invoke(args=['refresh'], obj=context)

        self.assertEqual(result.exit_code, 0, result.output)
        self.assertTrue(json.loads(result.output))
        self.assertEqual(context.api.stats['schedules_configured_refresh'], 1)
Beispiel #8
0
    def test_should_show_current_device(self):
        context = Context()
        context.api = MockClientApi()
        context.rendering = JsonWriter()

        runner = Runner(cli)
        result = runner.invoke(args=['status', 'device'], obj=context)

        self.assertEqual(result.exit_code, 0, result.output)
        self.assertTrue(json.loads(result.output))
        self.assertEqual(context.api.stats['device'], 1)
Beispiel #9
0
    def test_should_start_backups(self):
        context = Context()
        context.api = MockClientApi()
        context.rendering = JsonWriter()

        runner = Runner(cli)
        result = runner.invoke(args=['start', str(uuid4())], obj=context)

        self.assertEqual(result.exit_code, 0, result.output)
        self.assertTrue(json.loads(result.output))
        self.assertEqual(context.api.stats['backup_start'], 1)
Beispiel #10
0
    def test_should_start_backups_and_follow_their_progress(self):
        context = Context()
        context.api = MockClientApi()
        context.rendering = JsonWriter()

        runner = Runner(cli)
        result = runner.invoke(args=['start',
                                     str(uuid4()), '--follow'],
                               obj=context)

        self.assertEqual(result.exit_code, 0, result.output)
        self.assertEqual(context.api.stats['backup_start'], 1)
Beispiel #11
0
    def test_should_support_skipping_confirmation_when_stopping_background_service(self):
        context = Context()
        context.api = MockClientApi()
        context.rendering = JsonWriter()
        context.service_binary = 'test-name'
        context.service_main_class = 'test.name.Main'

        runner = Runner(cli)
        result = runner.invoke(args=['stop', '--confirm'], obj=context)

        self.assertEqual(result.exit_code, 0, result.output)
        self.assertDictEqual(json.loads(result.output), {'successful': True})
        self.assertEqual(context.api.stats['stop'], 1)
Beispiel #12
0
    def test_should_stop_operations(self, mock_confirm):
        context = Context()
        context.api = MockClientApi()
        context.rendering = JsonWriter()

        mock_confirm.return_value = None

        runner = Runner(cli)
        result = runner.invoke(args=['stop', str(uuid4())], obj=context)

        self.assertEqual(result.exit_code, 0, result.output)
        self.assertTrue(json.loads(result.output))
        self.assertEqual(context.api.stats['operation_stop'], 1)
Beispiel #13
0
    def test_should_show_entries(self):
        context = Context()
        context.api = MockClientApi()
        context.rendering = JsonWriter()

        runner = Runner(cli)
        result = runner.invoke(args=['show', 'entries'], obj=context)

        self.assertEqual(result.exit_code, 0, result.output)
        self.assertTrue(json.loads(result.output))
        self.assertEqual(context.api.stats['dataset_entries'], 1)
        self.assertEqual(context.api.stats['dataset_entries_for_definition'],
                         0)
Beispiel #14
0
    def test_should_search_metadata(self):
        context = Context()
        context.api = MockClientApi()
        context.rendering = JsonWriter()

        runner = Runner(cli)
        result = runner.invoke(
            args=['search', 'test.*', '-u', '2020-02-02 02:02:02'],
            obj=context)

        self.assertEqual(result.exit_code, 0, result.output)
        self.assertTrue(json.loads(result.output))
        self.assertEqual(context.api.stats['dataset_metadata_search'], 1)
Beispiel #15
0
    def test_should_stop_background_service(self, mock_confirm):
        context = Context()
        context.api = MockClientApi()
        context.rendering = JsonWriter()
        context.service_binary = 'test-name'
        context.service_main_class = 'test.name.Main'

        mock_confirm.return_value = None

        runner = Runner(cli)
        result = runner.invoke(args=['stop'], obj=context)

        self.assertEqual(result.exit_code, 0, result.output)
        self.assertDictEqual(json.loads(result.output), {'successful': True})
        self.assertEqual(context.api.stats['stop'], 1)
Beispiel #16
0
    def test_should_fail_if_ordering_specified_without_sorting(self):
        @click.command()
        @with_sorting
        def test():
            click.echo('unexpected result')

        @click.group()
        def cli():
            pass

        cli.add_command(test)

        runner = Runner(cli)
        result = runner.invoke(args=['test', '--ordering', 'asc'])

        self.assertEqual(result.exit_code, 2)
        self.assertTrue('Specifying "--ordering" without "--order-by" is not supported' in result.output.strip())
Beispiel #17
0
    def test_should_not_stop_background_service_when_not_running(self, mock_process_iter, mock_confirm):
        context = Context()
        context.api = InactiveClientApi()
        context.rendering = JsonWriter()
        context.service_binary = 'test-name'
        context.service_main_class = 'test.name.Main'

        mock_process_iter.return_value = []
        mock_confirm.return_value = None

        runner = Runner(cli)
        result = runner.invoke(args=['stop'], obj=context)

        self.assertEqual(result.exit_code, 0, result.output)
        self.assertDictEqual(
            json.loads(result.output),
            {'successful': False, 'failure': 'Background service is not active'}
        )
Beispiel #18
0
    def test_should_stop_background_service_processes(self, mock_process_iter, mock_confirm):
        context = Context()
        context.api = InactiveClientApi()
        context.rendering = JsonWriter()
        context.service_binary = 'test-name'
        context.service_main_class = 'test.name.Main'

        process = MockProcess()

        mock_process_iter.return_value = [process]
        mock_confirm.return_value = None

        runner = Runner(cli)
        result = runner.invoke(args=['stop'], obj=context)

        self.assertEqual(result.exit_code, 0, result.output)
        self.assertDictEqual(json.loads(result.output), {'successful': True})
        self.assertEqual(process.kill_count, 1)
Beispiel #19
0
    def test_should_not_start_background_service_when_already_running(self):
        context = Context()
        context.api = MockClientApi()
        context.init = InactiveInitApi()
        context.rendering = JsonWriter()
        context.service_binary = 'test-name'
        context.service_main_class = 'test.name.Main'

        username = '******'
        password = '******'

        runner = Runner(cli)
        result = runner.invoke(args=['start', '--username', username, '--password', password], obj=context)

        self.assertEqual(result.exit_code, 0, result.output)
        self.assertDictEqual(
            json.loads(result.output),
            {'successful': False, 'failure': 'Background service is already active'}
        )
Beispiel #20
0
    def test_should_fail_starting_background_service_if_init_already_failed(
            self,
            mock_sleep,
            mock_popen,
            mock_process_iter
    ):
        state_responses = [
            mock_data.INIT_STATE_FAILED,
        ]

        context = Context()
        context.api = InactiveClientApi()
        context.init = MockInitApi(state_responses=state_responses)
        context.rendering = JsonWriter()
        context.service_binary = 'test-name'
        context.service_main_class = 'test.name.Main'

        mock_process_iter.return_value = []
        mock_popen.return_value = None
        mock_sleep.return_value = None

        username = '******'
        password = '******'

        runner = Runner(cli)
        result = runner.invoke(
            args=['start', '--username', username, '--password', password],
            obj=context
        )

        self.assertEqual(result.exit_code, 0, result.output)
        self.assertDictEqual(
            json.loads(result.output),
            {'successful': False, 'failure': 'No or invalid credentials provided'}
        )
        mock_popen.assert_called_once()
        self.assertEqual(mock_sleep.call_count, 0)
        self.assertEqual(context.init.stats['state'], 1)
        self.assertEqual(context.init.stats['provide_credentials'], 0)
Beispiel #21
0
    def test_should_not_start_background_service_when_process_already_running(self, mock_process_iter):
        context = Context()
        context.api = InactiveClientApi()
        context.init = MockInitApi()
        context.rendering = JsonWriter()
        context.service_binary = 'test-name'
        context.service_main_class = 'test.name.Main'

        process = MockProcess()

        mock_process_iter.return_value = [process]
        username = '******'
        password = '******'

        runner = Runner(cli)
        result = runner.invoke(args=['start', '--username', username, '--password', password], obj=context)

        self.assertEqual(result.exit_code, 0, result.output)
        self.assertDictEqual(
            json.loads(result.output),
            {'successful': False, 'failure': 'Unexpected background service process(es) found'}
        )
Beispiel #22
0
    def test_should_show_metadata(self):
        context = Context()
        context.api = MockClientApi()
        context.rendering = JsonWriter()

        runner = Runner(cli)
        result_changes = runner.invoke(
            args=['show', 'metadata',
                  str(uuid4()), 'changes'], obj=context)
        result_crates = runner.invoke(
            args=['show', 'metadata',
                  str(uuid4()), 'crates'], obj=context)
        result_filesystem = runner.invoke(
            args=['show', 'metadata', str(uuid4()), 'fs'], obj=context)

        self.assertEqual(result_changes.exit_code, 0, result_changes.output)
        self.assertTrue(json.loads(result_changes.output))
        self.assertEqual(result_crates.exit_code, 0, result_crates.output)
        self.assertTrue(json.loads(result_crates.output))
        self.assertEqual(result_filesystem.exit_code, 0,
                         result_filesystem.output)
        self.assertTrue(json.loads(result_filesystem.output))

        self.assertEqual(context.api.stats['dataset_metadata'], 3)
Beispiel #23
0
    def test_should_show_backup_rules(self):
        context = Context()
        context.api = MockClientApi()
        context.rendering = JsonWriter()

        runner = Runner(cli)
        result_matched_included = runner.invoke(
            args=['show', 'rules', 'included'], obj=context)
        result_matched_excluded = runner.invoke(
            args=['show', 'rules', 'excluded'], obj=context)
        result_unmatched = runner.invoke(args=['show', 'rules', 'unmatched'],
                                         obj=context)

        self.assertEqual(result_matched_included.exit_code, 0,
                         result_matched_included.output)
        self.assertTrue(json.loads(result_matched_included.output))
        self.assertEqual(result_matched_excluded.exit_code, 0,
                         result_matched_excluded.output)
        self.assertTrue(json.loads(result_matched_excluded.output))
        self.assertEqual(result_unmatched.exit_code, 0,
                         result_unmatched.output)
        self.assertTrue(json.loads(result_unmatched.output))

        self.assertEqual(context.api.stats['backup_rules'], 3)
Beispiel #24
0
    def test_should_create_sorting_option(self):
        @click.command()
        @click.pass_context
        @with_sorting
        def test(ctx):
            spec = {'id': int, 'test_field': int, 'other_field': str}

            entries = [
                {'id': 0, 'test_field': 40, 'other_field': 'some-test-value'},
                {'id': 1, 'test_field': 50, 'other_field': 'some-test-value'},
                {'id': 2, 'test_field': 30, 'other_field': 'some-value'}
            ]

            click.echo(
                'entries={}'.format(','.join(map(lambda e: str(e['id']), ctx.obj.sorting.apply(entries, spec=spec))))
            )

        @click.group()
        def cli():
            pass

        cli.add_command(test)

        runner = Runner(cli)
        result_default = runner.invoke(args=['test', '--order-by', 'test_field'])
        result_asc = runner.invoke(args=['test', '--order-by', 'test_field', '--ordering', 'asc'])
        result_desc = runner.invoke(args=['test', '--order-by', 'test_field', '--ordering', 'desc'])

        self.assertEqual(result_default.exit_code, 0)
        self.assertEqual(result_default.output.strip(), 'entries=1,0,2')

        self.assertEqual(result_asc.exit_code, 0)
        self.assertEqual(result_asc.output.strip(), 'entries=2,0,1')

        self.assertEqual(result_desc.exit_code, 0)
        self.assertEqual(result_desc.output.strip(), 'entries=1,0,2')