Esempio n. 1
0
def test_get_container_name(patch, story, line, reusable):
    patch.object(Containers, 'is_service_reusable', return_value=reusable)
    story.app.app_id = 'my_app'
    ret = Containers.get_container_name(story, line, 'alpine')
    if reusable:
        assert ret == 'asyncy--my_app-alpine-1'
    else:
        h = Containers.hash_story_line(story, line)
        assert ret == f'asyncy--{story.app.app_id}-{h}-1'
Esempio n. 2
0
def test_get_container_name(patch, story, line, reusable):
    patch.object(Containers, 'is_service_reusable', return_value=reusable)
    story.app.app_id = 'my_app'
    ret = Containers.get_container_name(story, line, 'alpine')
    if reusable:
        assert ret == f'alpine-{Containers.hash_service_name("alpine")}'
    else:
        h = Containers.hash_service_name_and_story_line(story, line, 'alpine')
        assert ret == f'alpine-{h}'
def test_service_name(patch, story):
    story.app.version = 'v2'
    patch.object(hashlib, 'sha1')
    ret = Containers.hash_service_name(story.app, 'alpine')

    hashlib.sha1.assert_called_with(f'alpine-v2'.encode('utf-8'))
    assert ret == hashlib.sha1().hexdigest()
Esempio n. 4
0
def test_format_volume_name_not_reusable(patch, story, line):
    patch.object(Containers, 'is_service_reusable', return_value=False)
    patch.object(Containers,
                 'hash_service_name_and_story_line',
                 return_value='hash')
    assert Containers.format_volume_name(story, line, 'asyncy--alpine-1') == \
        'asyncy--alpine-1-hash'
Esempio n. 5
0
def test_hash_story_line(patch, story):
    patch.object(hashlib, 'sha1')
    story.name = 'story_name'
    ret = Containers.hash_story_line(story, {'ln': '1'})

    hashlib.sha1.assert_called_with('story_name-1'.encode('utf-8'))
    assert ret == hashlib.sha1().hexdigest()
Esempio n. 6
0
def test_containers_format_command(story):
    """
    Ensures a simple resolve can be performed
    """
    story_text = 'alpine echo msg:"foo"\n'
    story.context = {}
    story.app.services = {
        'alpine': {
            ServiceConstants.config: {
                'actions': {
                    'echo': {
                        'arguments': {
                            'msg': {
                                'type': 'string'
                            }
                        }
                    }
                }
            }
        }
    }

    story.tree = storyscript.Api.loads(story_text).result()['tree']
    assert Containers.format_command(story, story.line('1'), 'alpine',
                                     'echo') == ['echo', '{"msg":"foo"}']
Esempio n. 7
0
def test_service_name_and_story_line(patch, story):
    patch.object(hashlib, 'sha1')
    story.name = 'story_name'
    ret = Containers.hash_service_name_and_story_line(story, {'ln': '1'},
                                                      'alpine')

    hashlib.sha1.assert_called_with(f'alpine-{story.name}-1'.encode('utf-8'))
    assert ret == hashlib.sha1().hexdigest()
def test_format_command_no_format(logger, app, echo_service, echo_line):
    story = Story.story(app, logger, 'echo.story')
    app.services = echo_service

    config = app.services['alpine'][ServiceConstants.config]
    config['actions']['echo']['format'] = None

    cmd = Containers.format_command(story, echo_line, 'alpine', 'echo')
    assert ['echo', '{"msg":"foo"}'] == cmd
def test_format_command_no_args(logger, app, echo_service, echo_line):
    story = Story.story(app, logger, 'echo.story')
    app.services = echo_service

    echo_service['alpine'][
        ServiceConstants.config]['actions']['echo']['arguments'] = None

    cmd = Containers.format_command(story, echo_line, 'alpine', 'echo')
    assert ['echo'] == cmd
Esempio n. 10
0
def test_format_command_with_format(patch, logger, app, echo_service,
                                    echo_line):
    story = Story.story(app, logger, 'echo.story')
    patch.object(story, 'argument_by_name', return_value='asyncy')
    app.services = echo_service

    config = app.services['alpine'][ServiceConstants.config]
    config['actions']['echo']['format'] = 'echo {msg}'

    cmd = Containers.format_command(story, echo_line, 'alpine', 'echo')
    assert ['echo', 'asyncy'] == cmd
Esempio n. 11
0
def test_get_containerconfig_name(app):
    app.version = 'v1'
    config = ContainerConfig(
        name='name_with_special_!!!_characters',
        data={'auths': {
            'registry_url': {
                'auth': 'base64_string'
            }
        }})
    r = Containers.get_containerconfig_name(app, config.name)
    assert r == 'namewithspecialchara-95b9733c79792f385564973c20be433f6f6832e9'
Esempio n. 12
0
def test_is_service_reusable(story):
    story.app.services = {
        'alpine': {
            'configuration': {
                'actions': {
                    'echo': {
                        'run': 'foo'
                    }
                }
            }
        }
    }

    line = {LineConstants.service: 'alpine', LineConstants.command: 'echo'}

    assert Containers.is_service_reusable(story.app, line) is False
    story.app.services['alpine']['configuration']['actions']['echo'][
        'run'] = None

    assert Containers.is_service_reusable(story.app, line) is True
Esempio n. 13
0
def test_hash_volume_name(patch, story, line, reusable):
    line['ln'] = '1'
    patch.object(Containers, 'is_service_reusable', return_value=reusable)
    name = 'my_volume'
    service = 'foo'
    key = name + '-' + service
    if not reusable:
        key = f'{key}-{line["ln"]}'

    expected = f'myvolume-' + hashlib.sha1(key.encode('utf-8')).hexdigest()
    assert Containers.hash_volume_name(story.app, line, service, name) == \
        expected
Esempio n. 14
0
def test_containers_format_command_no_arguments(story):
    story_text = 'alpine echo\n'
    story.context = {}
    story.app.services = {
        'alpine': {
            ServiceConstants.config: {
                'actions': {
                    'echo': {}
                }
            }
        }
    }
    story.tree = storyscript.Api.loads(story_text).result()['tree']
    assert Containers.format_command(story, story.line('1'), 'alpine',
                                     'echo') == ['echo']
Esempio n. 15
0
def test_containers_format_command_no_arguments(story):
    story_text = 'alpine echo\n'
    story.context = {}
    story.app.services = {
        'alpine': {
            ServiceConstants.config: {
                'actions': {
                    'echo': {}
                }
            }
        }
    }
    story.tree = Compiler.compile(Parser().parse(story_text))['tree']
    assert Containers.format_command(
        story, story.line('1'), 'alpine', 'echo'
    ) == ['echo']
Esempio n. 16
0
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)
Esempio n. 17
0
def test_service_name(patch):
    patch.object(hashlib, 'sha1')
    ret = Containers.hash_service_name('alpine')

    hashlib.sha1.assert_called_with(f'alpine'.encode('utf-8'))
    assert ret == hashlib.sha1().hexdigest()
Esempio n. 18
0
async def test_start_no_command(patch, story, async_mock,
                                run_command, with_volumes):
    line = {
        LineConstants.service: 'alpine',
        LineConstants.command: 'echo',
        'ln': '1'
    }

    patch.object(Kubernetes, 'create_pod', new=async_mock())

    story.app.services = {
        'alpine': {
            ServiceConstants.config: {
                'actions': {
                    'echo': {
                    }
                },
                'volumes': {
                    'db': {
                        'persist': True,
                        'target': '/db'
                    },
                    'tmp': {
                        'persist': False,
                        'target': '/tmp'
                    }
                }
            }
        }
    }

    if not with_volumes:
        del story.app.services['alpine'][ServiceConstants.config]['volumes']

    if run_command is not None:
        story.app.services['alpine'][ServiceConstants.config]['actions'][
            'echo'] = {'run': {'command': run_command}}

    story.app.environment = {
        'alpine': {
            'alpine_only': True
        },
        'global': 'yes'
    }

    patch.object(Containers, 'get_container_name',
                 return_value='asyncy-alpine')

    expected_volumes = []
    if with_volumes:
        hash_db = Containers.hash_volume_name(story, line, 'alpine', 'db')
        hash_tmp = Containers.hash_volume_name(story, line, 'alpine', 'tmp')
        expected_volumes = [
            Volume(persist=True, name=hash_db, mount_path='/db'),
            Volume(persist=False, name=hash_tmp, mount_path='/tmp'),
        ]

    await Containers.start(story, line)

    Kubernetes.create_pod.mock.assert_called_with(
        story=story, line=line, image='alpine', container_name='asyncy-alpine',
        start_command=run_command or ['tail', '-f', '/dev/null'],
        shutdown_command=None,
        env={'alpine_only': True, 'global': 'yes'},
        volumes=expected_volumes)
Esempio n. 19
0
def test_get_registry_url_official(image):
    ret = Containers.get_registry_url(image)
    assert ret == 'https://index.docker.io/v1/'
Esempio n. 20
0
def test_format_volume_name(patch, story, line):
    patch.object(Containers, 'is_service_reusable', return_value=True)
    assert Containers.format_volume_name(story, line, 'asyncy--alpine-1') == \
        'asyncy--alpine-1'
Esempio n. 21
0
def test_get_registry_url_custom():
    image = 'cloud.canister.io:5000/repository/image'
    ret = Containers.get_registry_url(image)
    assert ret == 'cloud.canister.io:5000'
Esempio n. 22
0
def test_format_command(logger, app, echo_service, echo_line):
    story = Story.story(app, logger, 'echo.story')
    app.services = echo_service

    cmd = Containers.format_command(story, echo_line, 'alpine', 'echo')
    assert ['echo', '{"msg":"foo"}'] == cmd
Esempio n. 23
0
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'
Esempio n. 24
0
def test_format_command_no_spec(logger, app, echo_line):
    story = Story.story(app, logger, 'echo.story')
    app.services = {}
    with pytest.raises(ContainerSpecNotRegisteredError):
        Containers.format_command(story, echo_line, 'alpine', 'echo')
Esempio n. 25
0
async def test_start(patch, story, async_mock, missing_required_var,
                     run_command, with_volumes):
    line = {
        LineConstants.service: 'alpine',
        LineConstants.command: 'echo',
        'ln': '1'
    }

    patch.object(Kubernetes, 'create_pod', new=async_mock())

    story.app.services = {
        'alpine': {
            ServiceConstants.config: {
                'actions': {
                    'echo': {}
                },
                'volumes': {
                    'db': {
                        'persist': True,
                        'target': '/db'
                    },
                    'tmp': {
                        'persist': False,
                        'target': '/tmp'
                    }
                },
                'environment': {
                    'param_1': {
                        'required': True
                    },
                    'alpine_only': {}
                }
            }
        }
    }

    if not with_volumes:
        del story.app.services['alpine'][ServiceConstants.config]['volumes']

    if run_command is not None:
        story.app.services['alpine'][
            ServiceConstants.config]['actions']['echo'] = {
                'run': {
                    'command': run_command
                }
            }

    story.app.environment = {
        'alpine': {
            'alpine_only': True,
            'param_1': 'hello_world'
        },
        'global': 'yes'
    }

    if missing_required_var:
        story.app.environment['alpine']['param_1'] = None

    patch.object(Containers,
                 'get_container_name',
                 return_value='asyncy-alpine')

    patch.object(Database, 'get_container_configs', return_value=[])

    expected_volumes = []
    if with_volumes:
        hash_db = Containers.hash_volume_name(story.app, line, 'alpine', 'db')
        hash_tmp = Containers.hash_volume_name(story.app, line, 'alpine',
                                               'tmp')
        expected_volumes = [
            Volume(persist=True, name=hash_db, mount_path='/db'),
            Volume(persist=False, name=hash_tmp, mount_path='/tmp'),
        ]

    if missing_required_var:
        with pytest.raises(EnvironmentVariableNotFound):
            await Containers.start(story, line)
        return
    else:
        await Containers.start(story, line)

    Kubernetes.create_pod.mock.assert_called_with(
        app=story.app,
        service='alpine',
        image='alpine',
        container_name='asyncy-alpine',
        start_command=run_command or ['tail', '-f', '/dev/null'],
        shutdown_command=None,
        env={
            'alpine_only': True,
            'param_1': 'hello_world'
        },
        volumes=expected_volumes,
        container_configs=[])