예제 #1
0
    def test_get_from_multiple_managers(self, _):
        # get_resource tries all the managers in a cluster
        with mock.patch('cloudify.utils.get_manager_file_server_url',
                        return_value=['http://server1', 'http://server2']):

            # can't connect to any managers - thats NonRecoverable
            with mock.patch('requests.get',
                            side_effect=[
                                requests.ConnectionError(),
                                requests.ConnectionError(),
                            ]) as mock_get:
                pytest.raises(exceptions.NonRecoverableError,
                              get_resource_from_manager, 'resource.txt')
            assert len(mock_get.mock_calls) == 2

            # can't connect to the first, but second is OK
            with mock.patch('requests.get',
                            side_effect=[
                                requests.ConnectionError(),
                                mock.Mock(ok=True, content='content'),
                            ]) as mock_get:
                response = get_resource_from_manager('resource.txt')
            assert response == 'content'
            assert len(mock_get.mock_calls) == 2

            # first is OK already, second not tried
            with mock.patch('requests.get',
                            side_effect=[
                                mock.Mock(ok=True, content='content'),
                            ]) as mock_get:
                response = get_resource_from_manager('resource.txt')
            assert response == 'content'
            assert len(mock_get.mock_calls) == 1

            # first is 404, but second is OK. First must've not replicated
            # the files yet.
            with mock.patch('requests.get',
                            side_effect=[
                                mock.Mock(ok=False,
                                          status_code=404,
                                          reason='Not found'),
                                mock.Mock(ok=True, content='content'),
                            ]) as mock_get:
                response = get_resource_from_manager('resource.txt')
            assert response == 'content'
            assert len(mock_get.mock_calls) == 2

            # both are 404 - the exception is reraised
            with mock.patch('requests.get',
                            side_effect=[
                                mock.Mock(ok=False,
                                          status_code=404,
                                          reason='Not found'),
                                mock.Mock(ok=False,
                                          status_code=404,
                                          reason='Not found'),
                            ]) as mock_get:
                pytest.raises(exceptions.HttpException,
                              get_resource_from_manager, 'resource.txt')
            assert len(mock_get.mock_calls) == 2
예제 #2
0
    for trigger_type_name in triggers_to_remove:
        del policy_triggers[trigger_type_name]


def _process_source(source):
    split = source.split('://')
    schema = split[0]
    the_rest = ''.join(split[1:])

    try:
        if schema in ['http', 'https']:
            return requests.get(source).text
        elif schema == 'file' and the_rest:
            with open(the_rest) as f:
                return f.read()
    except IOError, e:
        raise NonRecoverableError('Failed processing source: {} ({})'.format(
            source, e.message))

    try:
        # try downloading blueprint resource
        return ctx.get_resource(source)
    except HttpException:
        pass
    try:
        # try downloading cloudify resource
        return get_resource_from_manager(source)
    except HttpException:
        pass
    raise NonRecoverableError('Failed processing source: {}'.format(source))
        del policy_triggers[trigger_type_name]


def _process_source(source):
    split = source.split('://')
    schema = split[0]
    the_rest = ''.join(split[1:])

    try:
        if schema in ['http', 'https']:
            return requests.get(source).text
        elif schema == 'file' and the_rest:
            with open(the_rest) as f:
                return f.read()
    except IOError, e:
        raise NonRecoverableError('Failed processing source: {} ({})'
                                  .format(source, e.message))

    try:
        # try downloading blueprint resource
        return ctx.get_resource(source)
    except HttpException:
        pass
    try:
        # try downloading cloudify resource
        return get_resource_from_manager(source)
    except HttpException:
        pass
    raise NonRecoverableError('Failed processing source: {}'
                              .format(source))