def test_web_service_integration():
    """
    Test web service integration.
    """
    from accelpy._common import accelize_ws_session
    from accelpy._application import Application
    from random import randint

    # Use dev environment
    request_endpoint = accelize_ws_session._ENDPOINT
    accelize_ws_session._ENDPOINT = 'https://master.devmetering.accelize.com'

    product_id = 'accelize.com/accelpy/ci'
    version = f'{randint(0, 255)}.{randint(0, 255)}.{randint(0, 255)}'
    application = f'{product_id}:{version}'

    definition = dict(application=dict(product_id=product_id,
                                       type='container_service',
                                       version=version),
                      fpga=dict(image='nothing'),
                      package=dict(name='nothing', type='container_image'),
                      accelize_drm=dict(use_service=False))

    try:
        scr_app = Application(definition)

        # Test: push
        scr_app.push()

        # Test: Get
        srv_app = Application.from_id(application)
        assert scr_app._definition == srv_app._definition

        # Test: List
        assert product_id in Application.list()

        # Test: List with prefix
        assert product_id in Application.list('accelize.com/accelpy')

        # Test: List version
        assert version in Application.list_versions(product_id)

        # Test: List version with prefix
        assert version in Application.list_versions(product_id,
                                                    version.split('.', 1)[0])

        # Test: Delete
        srv_app.delete()
        assert version not in Application.list_versions(product_id)

    finally:
        try:
            srv_app.delete()
        except Exception:
            pass
        accelize_ws_session._endpoint = request_endpoint
Beispiel #2
0
def _get_product_ids(prefix):
    """
    Get products IDs from web server.

    Args:
        prefix (str): Application prefix to filter.

    Returns:
        list of str: Product ids.
    """
    from accelpy._application import Application
    return Application.list(prefix)
def test_application():
    """
    Test common Application features.
    """
    from json import loads
    from copy import deepcopy
    import accelpy._application as accelpy_app
    from accelpy._application import Application
    from accelpy.exceptions import RuntimeException

    # Test: Load from dict
    definition = {
        'application': {
            'product_id': 'my_product_id',
            'version': '1.0.0',
            'type': 'container_service'
        },
        'package': [{
            'type': 'container_image',
            'name': 'my_container_image'
        }],
        'fpga': {
            'image': ['my_fpga_image'],
            'count': 1
        },
        'accelize_drm': {
            'use_service': False
        }
    }
    app = Application(definition)

    # Test: __getitem__
    assert app['application']['product_id'] == 'my_product_id'

    # Test: As dict
    assert app.to_dict()['application']['product_id'] == 'my_product_id'

    # Test: Cannot delete local application
    with pytest.raises(RuntimeException):
        app.delete()

    # Mock server
    accelpy_app_accelize_ws_session = accelpy_app.accelize_ws_session

    class Server:
        """Mocked server"""
        @staticmethod
        def request(path, *_, method='get', data=None, **__):
            """Mocked server response"""

            if '/productconfiguration/' in path:
                if method == 'get':
                    srv_def = deepcopy(definition)
                    srv_def['application']['configuration_id'] = 2
                    return dict(results=[srv_def])

                elif method == 'post':
                    assert loads(data) == definition, 'Pushed definition match'
                    return {'application': {'configuration_id': 2}}

                elif method == 'delete':
                    assert path.endswith('/2/')
                    return

            elif ('/productconfigurationlistversion/' in path
                  and method == 'get'):
                return dict(results=['1.0.0'])

            elif ('/productconfigurationlistproduct/' in path
                  and method == 'get'):
                return dict(results=['product'])

            raise ValueError(f'path={path}; method={method}')

    accelpy_app.accelize_ws_session = Server

    # Test basic mocked server flow
    try:
        # Test: push
        Application(definition).push()

        # Test: Get
        assert Application.from_id('app').to_dict() == definition

        # Test: List
        assert Application.list() == ['product']

        # Test: List version
        assert Application.list_versions('product') == ['1.0.0']
        assert Application.list_versions('product', '1') == ['1.0.0']

        # Test: Delete
        Application.from_id('app').delete()

    finally:
        accelpy_app.accelize_ws_session = accelpy_app_accelize_ws_session