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
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=[])
示例#3
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)