Exemplo n.º 1
0
def setup_handlers(web_app):  # pragma: no cover
    host_pattern = ".*$"

    rucio_config = RucioConfig(config=web_app.settings['config'])
    config = Config(rucio_config)
    rucio_factory = RucioAPIFactory(config=config)

    handler_params = dict(rucio_config=config, rucio=rucio_factory)

    base_url = web_app.settings["base_url"]
    base_path = url_path_join(base_url, 'rucio-jupyterlab')
    handlers = [
        (url_path_join(base_path,
                       'instances'), InstancesHandler, handler_params),
        (url_path_join(base_path, 'auth'), AuthConfigHandler, handler_params),
        (url_path_join(base_path, 'files'), DIDBrowserHandler, handler_params),
        (url_path_join(base_path, 'did'), DIDDetailsHandler, handler_params),
        (url_path_join(base_path,
                       'did-search'), DIDSearchHandler, handler_params),
        (url_path_join(base_path, 'did', 'make-available'),
         DIDMakeAvailableHandler, handler_params),
        (url_path_join(base_path,
                       'file-browser'), FileBrowserHandler, handler_params),
        (url_path_join(base_path,
                       'purge-cache'), PurgeCacheHandler, handler_params)
    ]
    web_app.add_handlers(host_pattern, handlers)
def test_get_handler__wildcard_disabled__should_print_error(mocker, rucio):
    mock_self = MockHandler()

    def mock_get_query_argument(key, default=None):
        args = {
            'namespace': MOCK_ACTIVE_INSTANCE,
            'type': 'all',
            'did': 'scope:name'
        }
        return args.get(key, default)

    mocker.patch.object(mock_self, 'get_query_argument', side_effect=mock_get_query_argument)  # pylint: disable=no-member

    class MockDIDSearchHandler(DIDSearchHandlerImpl):
        @staticmethod
        def search_did(scope, name, search_type='all', limit=100):
            raise WildcardDisallowedException()

    mocker.patch('rucio_jupyterlab.handlers.did_search.DIDSearchHandlerImpl', MockDIDSearchHandler)

    def finish_side_effect(output):
        finish_json = json.loads(output)
        expected = {'error': 'wildcard_disabled'}
        assert finish_json == expected, "Invalid finish response"

    mocker.patch.object(mock_self, 'set_status', return_value=None)
    mocker.patch.object(mock_self, 'finish', side_effect=finish_side_effect)

    rucio_api_factory = RucioAPIFactory(None)
    mocker.patch.object(rucio_api_factory, 'for_instance', return_value=rucio)
    mock_self.rucio = rucio_api_factory

    DIDSearchHandler.get(mock_self)

    mock_self.set_status.assert_called_with(400)  # pylint: disable=no-member
Exemplo n.º 3
0
def test_get_handler(mocker, rucio):
    mock_self = MockHandler()
    mock_active_instance = 'atlas'

    def mock_get_query_argument(key, default=None):
        args = {
            'namespace': mock_active_instance,
            'poll': '0',
            'did': 'scope:name'
        }
        return args.get(key, default)

    mocker.patch.object(mock_self,
                        'get_query_argument',
                        side_effect=mock_get_query_argument)

    rucio_api_factory = RucioAPIFactory(None)
    mocker.patch.object(rucio_api_factory, 'for_instance', return_value=rucio)
    mock_self.rucio = rucio_api_factory

    mock_did_details = [{
        'status': 'OK',
        'did': 'scope:name1',
        'path': '/eos/user/rucio/scope:name1',
        'size': 123
    }, {
        'status': 'OK',
        'did': 'scope:name2',
        'path': '/eos/user/rucio/scope:name2',
        'size': 456
    }]

    class MockReplicaModeHandler(ReplicaModeHandler):
        def get_did_details(self, scope, name, force_fetch=False):
            return mock_did_details

    mocker.patch('rucio_jupyterlab.handlers.did_details.ReplicaModeHandler',
                 MockReplicaModeHandler)

    def finish_side_effect(output):
        finish_json = json.loads(output)
        assert finish_json == mock_did_details, "Invalid finish response"

    mocker.patch.object(mock_self, 'finish', side_effect=finish_side_effect)

    DIDDetailsHandler.get(mock_self)

    calls = [call('namespace'), call('poll', '0'), call('did')]
    mock_self.get_query_argument.assert_has_calls(calls, any_order=True)  # pylint: disable=no-member
    rucio_api_factory.for_instance.assert_called_once_with(
        mock_active_instance)  # pylint: disable=no-member
def test_get_handler(mocker, rucio):
    mock_self = MockHandler()

    def mock_get_query_argument(key, default=None):
        args = {
            'namespace': MOCK_ACTIVE_INSTANCE,
            'poll': '0',
            'did': 'scope:name'
        }
        return args.get(key, default)

    mocker.patch.object(mock_self,
                        'get_query_argument',
                        side_effect=mock_get_query_argument)

    rucio_api_factory = RucioAPIFactory(None)
    mocker.patch.object(rucio_api_factory, 'for_instance', return_value=rucio)
    mock_self.rucio = rucio_api_factory

    mock_attached_files = [
        AttachedFile('scope1:name1', 123456),
        AttachedFile('scope2:name2', 789456),
        AttachedFile('scope3:name3', 1)
    ]

    class MockDIDBrowserHandlerImpl(DIDBrowserHandlerImpl):
        def get_files(self, scope, name, force_fetch=False):
            return [x.__dict__ for x in mock_attached_files]

    mocker.patch('rucio_jupyterlab.handlers.did_browser.DIDBrowserHandlerImpl',
                 MockDIDBrowserHandlerImpl)

    def finish_side_effect(output):
        finish_json = json.loads(output)
        assert finish_json == [x.__dict__ for x in mock_attached_files
                               ], "Invalid finish response"

    mocker.patch.object(mock_self, 'finish', side_effect=finish_side_effect)

    DIDBrowserHandler.get(mock_self)

    calls = [call('namespace'), call('poll', '0'), call('did')]
    mock_self.get_query_argument.assert_has_calls(calls, any_order=True)  # pylint: disable=no-member

    rucio_api_factory.for_instance.assert_called_once_with(
        MOCK_ACTIVE_INSTANCE)  # pylint: disable=no-member
def test_get_handler__inputs_correct__should_not_error(mocker, rucio):
    mock_self = MockHandler()

    def mock_get_query_argument(key, default=None):
        args = {
            'namespace': MOCK_ACTIVE_INSTANCE,
            'type': 'all',
            'did': 'scope:name'
        }
        return args.get(key, default)

    mocker.patch.object(mock_self, 'get_query_argument', side_effect=mock_get_query_argument)

    class MockDIDSearchHandler(DIDSearchHandlerImpl):
        @staticmethod
        def search_did(scope, name, search_type='all', limit=100):
            return [
                {'did': 'scope:name1', 'size': None, 'type': 'container'},
                {'did': 'scope:name2', 'size': None, 'type': 'dataset'},
                {'did': 'scope:name3', 'size': 123, 'type': 'file'}
            ]

    mocker.patch('rucio_jupyterlab.handlers.did_search.DIDSearchHandlerImpl', MockDIDSearchHandler)

    def finish_side_effect(output):
        finish_json = json.loads(output)
        expected = [
            {'did': 'scope:name1', 'size': None, 'type': 'container'},
            {'did': 'scope:name2', 'size': None, 'type': 'dataset'},
            {'did': 'scope:name3', 'size': 123, 'type': 'file'}
        ]
        assert finish_json == expected, "Invalid finish response"

    mocker.patch.object(mock_self, 'finish', side_effect=finish_side_effect)

    rucio_api_factory = RucioAPIFactory(None)
    mocker.patch.object(rucio_api_factory, 'for_instance', return_value=rucio)
    mock_self.rucio = rucio_api_factory

    DIDSearchHandler.get(mock_self)

    calls = [call('did'), call('namespace'), call('type', 'collection')]
    mock_self.get_query_argument.assert_has_calls(calls, any_order=True)  # pylint: disable=no-member
def test_post_handler__mode_download(mocker, rucio):
    """
    This unit test handles the POST handler for the endpoint.
    It checks whether a query argument named 'namespace' is read,
    whether get_json_body is called, and whether set_status is
    NOT called. It also checks whether
    DIDMakeAvailableHandlerImpl.handle_make_available() is called.
    """

    mock_self = MockHandler()
    mocker.patch.object(mock_self,
                        'get_query_argument',
                        return_value=MOCK_ACTIVE_INSTANCE)
    mocker.patch.object(mock_self,
                        'get_json_body',
                        return_value={'did': 'scope:name'})
    mocker.patch.object(mock_self, 'set_status')

    make_available_called = False

    class MockDownloadModeHandler(DownloadModeHandler):
        def make_available(self, scope, name):
            nonlocal make_available_called
            if scope == 'scope' and name == 'name':
                make_available_called = True
            return {}

    mocker.patch(
        'rucio_jupyterlab.handlers.did_make_available.DownloadModeHandler',
        MockDownloadModeHandler)

    rucio_api_factory = RucioAPIFactory(None)
    rucio.instance_config['mode'] = 'download'
    mocker.patch.object(rucio_api_factory, 'for_instance', return_value=rucio)
    mock_self.rucio = rucio_api_factory

    DIDMakeAvailableHandler.post(mock_self)

    mock_self.get_query_argument.assert_called_with('namespace')  # pylint: disable=no-member
    assert make_available_called, "Make available is not called"