示例#1
0
async def test_expose_services(patch, app, async_mock):
    a = Expose(service='foo', service_expose_name='foo', http_path='foo')
    b = Expose(service='foo', service_expose_name='foo', http_path='foo')
    patch.object(app.app_config, 'get_expose_config', return_value=[a, b])
    patch.object(App, '_expose_service', new=async_mock())

    await app.expose_services()

    assert app._expose_service.mock.call_count == 2
示例#2
0
async def test_expose_service(patch, app, async_mock, no_config, no_http_path):
    if no_http_path and no_config:
        # THey're mutually exclusive.
        return

    app.services = {
        'foo': {
            'configuration': {
                'expose': {
                    'my_expose_name': {
                        'http': {
                            'path': '/expose_path',
                            'port': 1993
                        }
                    }
                }
            }
        }
    }

    if no_config:
        app.services['foo']['configuration']['expose']['my_expose_name'] = None

    if no_http_path:
        app.services['foo']['configuration']['expose']['my_expose_name'][
            'http']['path'] = None

    patch.object(Containers, 'expose_service', new=async_mock())

    e = Expose(service='foo',
               service_expose_name='my_expose_name',
               http_path='/expose_external_path')

    if no_config or no_http_path:
        with pytest.raises(StoryscriptError):
            await app._expose_service(e)
    else:
        await app._expose_service(e)
        Containers.expose_service.mock.assert_called_with(app, e)
async def test_expose_service(app, patch, async_mock):
    container_name = 'container_name'
    patch.object(Containers, 'get_container_name', return_value=container_name)

    patch.object(Containers, 'create_and_start', new=async_mock())
    patch.object(Kubernetes, 'create_ingress', new=async_mock())

    e = Expose(service='service',
               service_expose_name='expose_name',
               http_path='expose_path')

    ingress_name = Containers.hash_ingress_name(e)
    hostname = f'{app.app_dns}--{Containers.get_simple_name(e.service)}'

    await Containers.expose_service(app, e)

    Containers.create_and_start.mock.assert_called_with(
        app, None, e.service, container_name)

    Kubernetes.create_ingress.mock.assert_called_with(ingress_name,
                                                      app,
                                                      e,
                                                      container_name,
                                                      hostname=hostname)
示例#4
0
async def test_create_ingress(patch, app, async_mock, resource_exists,
                              k8s_api_returned_2xx):
    if resource_exists and not k8s_api_returned_2xx:
        # Invalid combination, since if the ing resource exists already,
        # no additional call to the k8s API is made.
        return

    app.app_id = 'my_app_id'
    app.config.INGRESS_GLOBAL_STATIC_IP_NAME = 'ip-static-name-global'
    ingress_name = 'my_ingress_name'
    hostname = 'my_ingress_hostname'
    container_name = 'my_container_name'
    expose = Expose(service='service',
                    service_expose_name='expose_name',
                    http_path='expose_path')

    http_conf = {'path': '/my_app', 'port': 6000}

    app.services = {
        expose.service: {
            ServiceConstants.config: {
                KEY_EXPOSE: {
                    expose.service_expose_name: {
                        'http': http_conf
                    }
                }
            }
        }
    }

    app.config.APP_DOMAIN = 'foo.com'

    patch.object(Kubernetes,
                 '_does_resource_exist',
                 new=async_mock(return_value=resource_exists))

    expected_payload = {
        'apiVersion': 'extensions/v1beta1',
        'kind': 'Ingress',
        'metadata': {
            'name': ingress_name,
            'annotations': {
                'kubernetes.io/ingress.class': 'nginx',
                'kubernetes.io/ingress.global-static-ip-name':
                app.config.INGRESS_GLOBAL_STATIC_IP_NAME,
                'ingress.kubernetes.io/rewrite-target': expose.http_path,
                'nginx.ingress.kubernetes.io/proxy-body-size': '1m',
                'nginx.ingress.kubernetes.io/proxy-read-timeout': '120'
            }
        },
        'spec': {
            'tls': [{
                'hosts': [f'{hostname}.'
                          f'{app.config.APP_DOMAIN}']
            }],
            'rules': [{
                'host': f'{hostname}.{app.config.APP_DOMAIN}',
                'http': {
                    'paths': [{
                        'path': http_conf['path'],
                        'backend': {
                            'serviceName': container_name,
                            'servicePort': http_conf['port']
                        }
                    }]
                }
            }]
        }
    }

    patch.object(Kubernetes, 'make_k8s_call', new=async_mock(return_value=314))
    patch.object(Kubernetes, 'is_2xx', return_value=k8s_api_returned_2xx)

    if k8s_api_returned_2xx:
        await Kubernetes.create_ingress(ingress_name, app, expose,
                                        container_name, hostname)
    else:
        with pytest.raises(K8sError):
            await Kubernetes.create_ingress(ingress_name, app, expose,
                                            container_name, hostname)
        return

    if resource_exists:
        Kubernetes.make_k8s_call.mock.assert_not_called()
    else:
        prefix = Kubernetes._get_api_path_prefix('ingresses')
        prefix = f'{prefix}/{app.app_id}/ingresses'
        Kubernetes.make_k8s_call.mock.assert_called_with(
            app.config, app.logger, prefix, payload=expected_payload)

        Kubernetes.is_2xx.assert_called_with(314)
def test_hash_ingress_name():
    e = Expose(service='service',
               service_expose_name='expose_name',
               http_path='expose_path')
    ret = Containers.hash_ingress_name(e)
    assert ret == 'exposename-0cf994f170f9d213bb814f74baca87ea149f7536'