Exemple #1
0
    def test_should_recover_from_latest_entry(self, mock_request):
        operation = uuid4()

        client = DefaultClientApi(api_url=self.url,
                                  api_token=self.token,
                                  context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.success(
            {'operation': operation})

        query = 'test.*'
        definition = uuid4()
        destination = '/tmp/some/path/03'
        discard_paths = True

        self.assertEqual(
            client.recover_from_latest(definition=definition,
                                       path_query=query,
                                       destination=destination,
                                       discard_paths=discard_paths), {
                                           'successful': True,
                                           'operation': operation
                                       })

        self.assert_valid_request(
            mock=mock_request,
            expected_method='put',
            expected_url='/operations/recover/{}/latest'.format(definition),
            expected_request_params={
                'query': query,
                'destination': destination,
                'keep_structure': not discard_paths
            })
Exemple #2
0
    def test_should_handle_request_failures(self, mock_request):
        client = DefaultClientApi(api_url=self.url,
                                  api_token=self.token,
                                  context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.failure(
            response='test failure')

        with self.assertRaises(Abort):
            client.operations()
    def test_should_handle_unexpected_responses_when_retrieving_init_state(
            self, mock_request):
        client = DefaultInitApi(api_url=self.url,
                                connect_retries=1,
                                backoff_factor=2,
                                context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.empty()

        with self.assertRaises(Abort):
            client.state()
    def test_should_handle_init_state_retrieval_failures(self, mock_request):
        client = DefaultInitApi(api_url=self.url,
                                connect_retries=1,
                                backoff_factor=2,
                                context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.failure(
            response='test failure')

        with self.assertRaises(Abort):
            client.state()
Exemple #5
0
    def test_should_get_current_user(self, mock_request):
        client = DefaultClientApi(api_url=self.url,
                                  api_token=self.token,
                                  context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.success(mock_data.USER)

        self.assertEqual(client.user(), mock_data.USER)

        self.assert_valid_request(mock=mock_request,
                                  expected_method='get',
                                  expected_url='/user')
Exemple #6
0
    def test_should_get_dataset_definitions(self, mock_request):
        client = DefaultClientApi(api_url=self.url,
                                  api_token=self.token,
                                  context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.success(mock_data.DEFINITIONS)

        self.assertEqual(client.dataset_definitions(), mock_data.DEFINITIONS)

        self.assert_valid_request(mock=mock_request,
                                  expected_method='get',
                                  expected_url='/datasets/definitions')
    def test_should_retrieve_init_state(self, mock_request):
        client = DefaultInitApi(api_url=self.url,
                                connect_retries=1,
                                backoff_factor=2,
                                context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.success(
            mock_data.INIT_STATE_PENDING)

        self.assertDictEqual(client.state(), mock_data.INIT_STATE_PENDING)

        mock_request.assert_called_once()
Exemple #8
0
    def test_should_get_active_operations(self, mock_request):
        client = DefaultClientApi(api_url=self.url,
                                  api_token=self.token,
                                  context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.success(
            mock_data.ACTIVE_OPERATIONS)

        self.assertEqual(client.operations(), mock_data.ACTIVE_OPERATIONS)

        self.assert_valid_request(mock=mock_request,
                                  expected_method='get',
                                  expected_url='/operations')
Exemple #9
0
    def test_should_get_backup_rules(self, mock_request):
        client = DefaultClientApi(api_url=self.url,
                                  api_token=self.token,
                                  context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.success(
            mock_data.BACKUP_RULES)

        self.assertEqual(client.backup_rules(), mock_data.BACKUP_RULES)

        self.assert_valid_request(mock=mock_request,
                                  expected_method='get',
                                  expected_url='/operations/backup/rules')
Exemple #10
0
    def test_should_get_public_schedules(self, mock_request):
        client = DefaultClientApi(api_url=self.url,
                                  api_token=self.token,
                                  context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.success(
            mock_data.SCHEDULES_PUBLIC)

        self.assertEqual(client.schedules_public(), mock_data.SCHEDULES_PUBLIC)

        self.assert_valid_request(mock=mock_request,
                                  expected_method='get',
                                  expected_url='/schedules/public')
Exemple #11
0
def create_client_api(config, api_token, insecure) -> ClientApi:
    """
    Creates a new client API with the provided configuration.

    If the default client API cannot be created (API token is missing) then an
    instance of :class:`InactiveClientApi` is returned instead.

    :param config: client configuration
    :param api_token: API token string or None if it is not available
    :param insecure: set to `True` to not verify TLS certificate when making requests to API
    :return: the client API or InactiveClientApi if it is not available
    """

    if api_token:
        api_type = config.get_string('stasis.client.api.type')
        if api_type.lower() != 'http':
            logging.error('Expected [http] API but [{}] found'.format(api_type))
            raise Abort()

        api_config = config.get_config('stasis.client.api.http')

        api_url = '{}://{}:{}'.format(
            'https' if api_config.get_bool('context.enabled') or insecure else 'http',
            api_config.get_string('interface'),
            api_config.get_int('port')
        )

        if api_config.get_bool('context.enabled') and not insecure:
            api_context = CustomHttpsContext(
                certificate_type=api_config.get_string('context.keystore.type'),
                certificate_path=api_config.get_string('context.keystore.path'),
                certificate_password=api_config.get_string('context.keystore.password')
            )
        else:
            api_context = DefaultHttpsContext(verify=not insecure)

        default_client = DefaultClientApi(
            api_url=api_url,
            api_token=api_token,
            context=api_context
        )

        if default_client.is_active():
            api = default_client
        else:
            api = InactiveClientApi()
    else:
        api = InactiveClientApi()

    return api
Exemple #12
0
    def test_should_get_dataset_metadata(self, mock_request):
        client = DefaultClientApi(api_url=self.url,
                                  api_token=self.token,
                                  context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.success(mock_data.METADATA)

        entry = uuid4()
        self.assertEqual(client.dataset_metadata(entry=entry),
                         mock_data.METADATA)

        self.assert_valid_request(
            mock=mock_request,
            expected_method='get',
            expected_url='/datasets/metadata/{}'.format(entry))
Exemple #13
0
    def test_should_handle_failures_when_providing_init_credentials(
            self, mock_request):
        client = DefaultInitApi(api_url=self.url,
                                connect_retries=1,
                                backoff_factor=2,
                                context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.failure(
            response='test failure')

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

        with self.assertRaises(Abort):
            client.provide_credentials(username=username, password=password)
Exemple #14
0
    def test_should_refresh_configured_schedules(self, mock_request):
        client = DefaultClientApi(api_url=self.url,
                                  api_token=self.token,
                                  context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.success()

        self.assertEqual(client.schedules_configured_refresh(), {
            'successful': True,
            'operation': None
        })

        self.assert_valid_request(mock=mock_request,
                                  expected_method='put',
                                  expected_url='/schedules/configured/refresh')
Exemple #15
0
    def test_should_get_dataset_entries_for_definition(self, mock_request):
        client = DefaultClientApi(api_url=self.url,
                                  api_token=self.token,
                                  context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.success(mock_data.ENTRIES)

        definition = uuid4()
        self.assertEqual(
            client.dataset_entries_for_definition(definition=definition),
            mock_data.ENTRIES)

        self.assert_valid_request(
            mock=mock_request,
            expected_method='get',
            expected_url='/datasets/entries/{}'.format(definition))
Exemple #16
0
    def test_should_get_operation_progress(self, mock_request):
        client = DefaultClientApi(api_url=self.url,
                                  api_token=self.token,
                                  context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.success(
            mock_data.OPERATION_PROGRESS[0])

        operation = uuid4()
        self.assertEqual(client.operation_progress(operation),
                         mock_data.OPERATION_PROGRESS[0])

        self.assert_valid_request(
            mock=mock_request,
            expected_method='get',
            expected_url='/operations/{}/progress'.format(operation))
Exemple #17
0
    def test_should_check_if_api_is_active(self, mock_request):
        client = DefaultClientApi(api_url=self.url,
                                  api_token=self.token,
                                  context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.success(mock_data.PING)

        self.assertTrue(client.is_active())

        self.assert_valid_request(mock=mock_request,
                                  expected_method='get',
                                  expected_url='/service/ping')

        mock_request.return_value = MockResponse.failure()

        self.assertFalse(client.is_active())
Exemple #18
0
    def test_should_define_backups(self, mock_request):
        client = DefaultClientApi(api_url=self.url,
                                  api_token=self.token,
                                  context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.success()

        definition_request = {'a': 1, 'b': 2}
        self.assertEqual(client.backup_define(request=definition_request), {
            'successful': True,
            'operation': None
        })

        self.assert_valid_request(mock=mock_request,
                                  expected_method='post',
                                  expected_url='/datasets/definitions',
                                  expected_request_data=definition_request)
Exemple #19
0
    def test_should_stop_an_active_operation(self, mock_request):
        client = DefaultClientApi(api_url=self.url,
                                  api_token=self.token,
                                  context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.empty()

        operation = uuid4()
        self.assertEqual(client.operation_stop(operation), {
            'successful': True,
            'operation': None
        })

        self.assert_valid_request(
            mock=mock_request,
            expected_method='put',
            expected_url='/operations/{}/stop'.format(operation))
Exemple #20
0
    def test_should_send_service_termination_requests(self, mock_request):
        client = DefaultClientApi(api_url=self.url,
                                  api_token=self.token,
                                  context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.success()

        self.assertDictEqual(client.stop(), {
            'successful': True,
            'operation': None
        })

        self.assert_valid_request(mock=mock_request,
                                  expected_method='put',
                                  expected_url='/service/stop')

        mock_request.return_value = MockResponse.failure()

        self.assertDictEqual(client.stop(), {'successful': False})
Exemple #21
0
    def test_should_start_backups(self, mock_request):
        operation = uuid4()

        client = DefaultClientApi(api_url=self.url,
                                  api_token=self.token,
                                  context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.success(
            {'operation': operation})

        definition = uuid4()
        self.assertEqual(client.backup_start(definition=definition), {
            'successful': True,
            'operation': operation
        })

        self.assert_valid_request(
            mock=mock_request,
            expected_method='put',
            expected_url='/operations/backup/{}'.format(definition))
Exemple #22
0
    def test_should_search_dataset_metadata(self, mock_request):
        client = DefaultClientApi(api_url=self.url,
                                  api_token=self.token,
                                  context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.success(
            mock_data.METADATA_SEARCH_RESULTS)

        query = 'test.*'
        until = '2020-02-02T02:02:02'
        self.assertEqual(
            client.dataset_metadata_search(search_query=query, until=until),
            mock_data.METADATA_SEARCH_RESULTS)

        self.assert_valid_request(mock=mock_request,
                                  expected_method='get',
                                  expected_url='/datasets/metadata/search',
                                  expected_request_params={
                                      'query': query,
                                      'until': until
                                  })
Exemple #23
0
    def test_should_provide_init_credentials(self, mock_request):
        client = DefaultInitApi(api_url=self.url,
                                connect_retries=1,
                                backoff_factor=2,
                                context=DefaultHttpsContext(verify=False))
        mock_request.return_value = MockResponse.success()

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

        self.assertDictEqual(
            client.provide_credentials(username=username, password=password),
            {'successful': True})

        mock_request.assert_called_once_with(method='post',
                                             url='{}/init'.format(self.url),
                                             data={
                                                 'username': username,
                                                 'password': password
                                             },
                                             verify=False)
Exemple #24
0
def create_init_api(config, insecure, client_api) -> InitApi:
    """
    Creates a new initialization API with the provided configuration.

    If the client API is reported as active (i.e. initialization is already done) then an
    instance of :class:`InactiveInitApi` is returned instead.

    :param config: client configuration
    :param insecure: set to `True` to not verify TLS certificate when making requests to API
    :param client_api: client API to use for determining API state
    :return: the init API or InactiveInitApi if it is not available
    """
    if not client_api.is_active():
        api_config = config.get_config('stasis.client.api.init')

        api_url = '{}://{}:{}'.format(
            'https' if api_config.get_bool('context.enabled') or insecure else 'http',
            api_config.get_string('interface'),
            api_config.get_int('port')
        )

        if api_config.get_bool('context.enabled') and not insecure:
            api_context = CustomHttpsContext(
                certificate_type=api_config.get_string('context.keystore.type'),
                certificate_path=api_config.get_string('context.keystore.path'),
                certificate_password=api_config.get_string('context.keystore.password')
            )
        else:
            api_context = DefaultHttpsContext(verify=not insecure)

        api = DefaultInitApi(
            api_url=api_url,
            context=api_context,
            connect_retries=10,
            backoff_factor=0.1
        )
    else:
        api = InactiveInitApi()

    return api
Exemple #25
0
    def test_should_follow_operation_progress(self, mock_request):
        client = DefaultClientApi(api_url=self.url,
                                  api_token=self.token,
                                  context=DefaultHttpsContext(verify=False))

        def sse_response():
            for e in mock_data.OPERATION_PROGRESS:
                yield 'data: {}\n\n'.format(json.dumps(e)).encode('utf-8')

        mock_response = MockResponse.success(sse_response())
        mock_request.return_value = mock_response

        operation = uuid4()

        self.assertEqual(list(client.operation_follow(operation)),
                         mock_data.OPERATION_PROGRESS)

        self.assertTrue(mock_response.closed)

        self.assert_valid_streaming_request(
            mock=mock_request,
            expected_method='get',
            expected_url='/operations/{}/follow'.format(operation))