def test_interpolate_environment_external_resource_convert_types(mock_env): entry = { 'resource1': { 'external': '${TRUE}', } } expected = { 'resource1': { 'external': True, } } value = interpolate_environment_variables(VERSION, entry, 'network', mock_env) assert value == expected value = interpolate_environment_variables(VERSION, entry, 'volume', mock_env) assert value == expected value = interpolate_environment_variables(VERSION, entry, 'secret', mock_env) assert value == expected value = interpolate_environment_variables(VERSION, entry, 'config', mock_env) assert value == expected
def test_interpolate_environment_variables_in_services(mock_env): services = { 'servivea': { 'image': 'example:${USER}', 'entrypoint': '/bin/bash', 'volumes': ['$FOO:/target'], 'read_only': '${RO}', 'cpu_shares': '${CPU_SHARES}', 'logging': { 'driver': '${FOO}', 'options': { 'user': '******', } } } } expected = { 'servivea': { 'image': 'example:jenny', 'entrypoint': '/bin/bash', 'volumes': ['bar:/target'], 'read_only': False, 'cpu_shares': 512, 'logging': { 'driver': 'bar', 'options': { 'user': '******', } } } } assert interpolate_environment_variables(services, 'service', load_jsonschema('service', V2_0)) == expected
def test_interpolate_environment_variables_in_services(mock_env): services = { 'servicea': { 'image': 'example:${USER}', 'volumes': ['$FOO:/target'], 'logging': { 'driver': '${FOO}', 'options': { 'user': '******', } } } } expected = { 'servicea': { 'image': 'example:jenny', 'volumes': ['bar:/target'], 'logging': { 'driver': 'bar', 'options': { 'user': '******', } } } } value = interpolate_environment_variables(V2_0, services, 'service', mock_env) assert value == expected
def test_interpolate_environment_variables_in_services(mock_env): services = { 'servicea': { 'image': 'example:${USER}', 'volumes': ['$FOO:/target'], 'logging': { 'driver': '${FOO}', 'options': { 'user': '******', } } } } expected = { 'servicea': { 'image': 'example:jenny', 'volumes': ['bar:/target'], 'logging': { 'driver': 'bar', 'options': { 'user': '******', } } } } assert interpolate_environment_variables( services, 'service', Environment.from_env_file(None) ) == expected
def test_interpolate_environment_variables_in_services(mock_env): services = { 'servicea': { 'image': 'example:${USER}', 'volumes': ['$FOO:/target'], 'logging': { 'driver': '${FOO}', 'options': { 'user': '******', } } } } expected = { 'servicea': { 'image': 'example:jenny', 'volumes': ['bar:/target'], 'logging': { 'driver': 'bar', 'options': { 'user': '******', } } } } value = interpolate_environment_variables(V2_0, services, 'service', mock_env) assert value == expected
def test_interpolate_service_name_uses_dot(mock_env): entry = { 'service.1': { 'image': 'busybox', 'ulimits': { 'nproc': '${POSINT}', 'nofile': { 'soft': '${POSINT}', 'hard': '${DEFAULT:-40000}' }, }, } } expected = { 'service.1': { 'image': 'busybox', 'ulimits': { 'nproc': 50, 'nofile': { 'soft': 50, 'hard': 40000 }, }, } } value = interpolate_environment_variables(V3_4, entry, 'service', mock_env) assert value == expected
def test_interpolate_service_name_uses_dot(mock_env): entry = { 'service.1': { 'image': 'busybox', 'ulimits': { 'nproc': '${POSINT}', 'nofile': { 'soft': '${POSINT}', 'hard': '${DEFAULT:-40000}' }, }, } } expected = { 'service.1': { 'image': 'busybox', 'ulimits': { 'nproc': 50, 'nofile': { 'soft': 50, 'hard': 40000 }, }, } } value = interpolate_environment_variables(VERSION, entry, 'service', mock_env) assert value == expected
def test_interpolate_environment_external_resource_convert_types(mock_env): entry = { 'resource1': { 'external': '${TRUE}', } } expected = { 'resource1': { 'external': True, } } value = interpolate_environment_variables(V3_4, entry, 'network', mock_env) assert value == expected value = interpolate_environment_variables(V3_4, entry, 'volume', mock_env) assert value == expected value = interpolate_environment_variables(V3_4, entry, 'secret', mock_env) assert value == expected value = interpolate_environment_variables(V3_4, entry, 'config', mock_env) assert value == expected
def test_interpolate_environment_variables_array_numbers_in_services(mock_env): services = { 'servivea': { 'expose': '${PORTS}', 'ports': [8080, '${SINGLE_PORT}'] } } expected = { 'servivea': { 'expose': [8080, 8089], 'ports': [8080, 8787] } } assert interpolate_environment_variables(services, 'service', load_jsonschema('service', V2_0)) == expected
def test_interpolate_environment_variables_array_element_in_services(mock_env): services = { 'servivea': { 'image': 'example:${USER}', 'volumes': ['/tmp/bar:/bar', '${VOLUMES_ITEM}'], 'cpu_shares': '${CPU_SHARES}', } } expected = { 'servivea': { 'image': 'example:jenny', 'volumes': ['/tmp/bar:/bar', '/tmp/foo:/foo'], 'cpu_shares': 512, } } assert interpolate_environment_variables(services, 'service', load_jsonschema('service', V2_0)) == expected
def test_interpolate_environment_network_convert_types(mock_env): entry = { 'network1': { 'external': '${FALSE}', 'attachable': '${TRUE}', 'internal': '${DEFAULT:-false}' } } expected = { 'network1': { 'external': False, 'attachable': True, 'internal': False, } } value = interpolate_environment_variables(V3_4, entry, 'network', mock_env) assert value == expected
def test_interpolate_environment_network_convert_types(mock_env): entry = { 'network1': { 'external': '${FALSE}', 'attachable': '${TRUE}', 'internal': '${DEFAULT:-false}' } } expected = { 'network1': { 'external': False, 'attachable': True, 'internal': False, } } value = interpolate_environment_variables(V3_4, entry, 'network', mock_env) assert value == expected
def test_interpolate_environment_variables_in_volumes(mock_env): volumes = { 'data': { 'driver': '$FOO', 'driver_opts': { 'max': 2, 'user': '******' } }, 'other': None, } expected = { 'data': { 'driver': 'bar', 'driver_opts': { 'max': 2, 'user': '******' } }, 'other': {}, } assert interpolate_environment_variables(volumes, 'volume') == expected
def test_interpolate_environment_variables_in_secrets(mock_env): secrets = { 'secretservice': { 'file': '$FOO', 'labels': { 'max': 2, 'user': '******' } }, 'other': None, } expected = { 'secretservice': { 'file': 'bar', 'labels': { 'max': '2', 'user': '******' } }, 'other': {}, } value = interpolate_environment_variables(V3_4, secrets, 'secret', mock_env) assert value == expected
def test_interpolate_environment_services_convert_types_invalid(mock_env): entry = {'service1': {'privileged': '${POSINT}'}} with pytest.raises(ConfigurationError) as exc: interpolate_environment_variables(V2_3, entry, 'service', mock_env) assert 'Error while attempting to convert service.service1.privileged to '\ 'appropriate type: "50" is not a valid boolean value' in exc.exconly() entry = {'service1': {'cpus': '${TRUE}'}} with pytest.raises(ConfigurationError) as exc: interpolate_environment_variables(V2_3, entry, 'service', mock_env) assert 'Error while attempting to convert service.service1.cpus to '\ 'appropriate type: "True" is not a valid float' in exc.exconly() entry = {'service1': {'ulimits': {'nproc': '${FLOAT}'}}} with pytest.raises(ConfigurationError) as exc: interpolate_environment_variables(V2_3, entry, 'service', mock_env) assert 'Error while attempting to convert service.service1.ulimits.nproc to '\ 'appropriate type: "0.145" is not a valid integer' in exc.exconly()
def test_interpolate_environment_services_convert_types_v3(mock_env): entry = { 'service1': { 'healthcheck': { 'retries': '${POSINT:-3}', 'disable': '${FALSE}', 'command': 'true' }, 'ulimits': { 'nproc': '${POSINT}', 'nofile': { 'soft': '${POSINT}', 'hard': '${DEFAULT:-40000}' }, }, 'privileged': '${TRUE}', 'read_only': '${DEFAULT:-no}', 'tty': '${DEFAULT:-N}', 'stdin_open': '${DEFAULT-on}', 'deploy': { 'update_config': { 'parallelism': '${DEFAULT:-2}', 'max_failure_ratio': '${FLOAT}', }, 'restart_policy': { 'max_attempts': '$POSINT', }, 'replicas': '${DEFAULT-3}' }, 'ports': [{'target': '${POSINT}', 'published': '${DEFAULT:-5000}'}], 'configs': [{'mode': '${MODE}', 'source': 'config1'}], 'secrets': [{'mode': '${MODE}', 'source': 'secret1'}], } } expected = { 'service1': { 'healthcheck': { 'retries': 50, 'disable': False, 'command': 'true' }, 'ulimits': { 'nproc': 50, 'nofile': { 'soft': 50, 'hard': 40000 }, }, 'privileged': True, 'read_only': False, 'tty': False, 'stdin_open': True, 'deploy': { 'update_config': { 'parallelism': 2, 'max_failure_ratio': 0.145, }, 'restart_policy': { 'max_attempts': 50, }, 'replicas': 3 }, 'ports': [{'target': 50, 'published': 5000}], 'configs': [{'mode': 0o600, 'source': 'config1'}], 'secrets': [{'mode': 0o600, 'source': 'secret1'}], } } value = interpolate_environment_variables(V3_4, entry, 'service', mock_env) assert value == expected
def test_interpolate_environment_services_convert_types_v3(mock_env): entry = { 'service1': { 'healthcheck': { 'retries': '${POSINT:-3}', 'disable': '${FALSE}', 'command': 'true' }, 'ulimits': { 'nproc': '${POSINT}', 'nofile': { 'soft': '${POSINT}', 'hard': '${DEFAULT:-40000}' }, }, 'privileged': '${TRUE}', 'read_only': '${DEFAULT:-no}', 'tty': '${DEFAULT:-N}', 'stdin_open': '${DEFAULT-on}', 'deploy': { 'update_config': { 'parallelism': '${DEFAULT:-2}', 'max_failure_ratio': '${FLOAT}', }, 'restart_policy': { 'max_attempts': '$POSINT', }, 'replicas': '${DEFAULT-3}' }, 'ports': [{ 'target': '${POSINT}', 'published': '${DEFAULT:-5000}' }], 'configs': [{ 'mode': '${MODE}', 'source': 'config1' }], 'secrets': [{ 'mode': '${MODE}', 'source': 'secret1' }], } } expected = { 'service1': { 'healthcheck': { 'retries': 50, 'disable': False, 'command': 'true' }, 'ulimits': { 'nproc': 50, 'nofile': { 'soft': 50, 'hard': 40000 }, }, 'privileged': True, 'read_only': False, 'tty': False, 'stdin_open': True, 'deploy': { 'update_config': { 'parallelism': 2, 'max_failure_ratio': 0.145, }, 'restart_policy': { 'max_attempts': 50, }, 'replicas': 3 }, 'ports': [{ 'target': 50, 'published': 5000 }], 'configs': [{ 'mode': 0o600, 'source': 'config1' }], 'secrets': [{ 'mode': 0o600, 'source': 'secret1' }], } } value = interpolate_environment_variables(V3_4, entry, 'service', mock_env) assert value == expected
def test_interpolate_environment_services_convert_types_v2(mock_env): entry = { 'service1': { 'blkio_config': { 'weight': '${POSINT}', 'weight_device': [{ 'file': '/dev/sda1', 'weight': '${POSINT}' }] }, 'cpus': '${FLOAT}', 'cpu_count': '$POSINT', 'healthcheck': { 'retries': '${POSINT:-3}', 'disable': '${FALSE}', 'command': 'true' }, 'mem_swappiness': '${DEFAULT:-127}', 'oom_score_adj': '${NEGINT}', 'scale': '${POSINT}', 'ulimits': { 'nproc': '${POSINT}', 'nofile': { 'soft': '${POSINT}', 'hard': '${DEFAULT:-40000}' }, }, 'privileged': '${TRUE}', 'read_only': '${DEFAULT:-no}', 'tty': '${DEFAULT:-N}', 'stdin_open': '${DEFAULT-on}', } } expected = { 'service1': { 'blkio_config': { 'weight': 50, 'weight_device': [{ 'file': '/dev/sda1', 'weight': 50 }] }, 'cpus': 0.145, 'cpu_count': 50, 'healthcheck': { 'retries': 50, 'disable': False, 'command': 'true' }, 'mem_swappiness': 127, 'oom_score_adj': -200, 'scale': 50, 'ulimits': { 'nproc': 50, 'nofile': { 'soft': 50, 'hard': 40000 }, }, 'privileged': True, 'read_only': False, 'tty': False, 'stdin_open': True, } } value = interpolate_environment_variables(V2_3, entry, 'service', mock_env) assert value == expected
def test_interpolate_environment_services_convert_types_v2(mock_env): entry = { 'service1': { 'blkio_config': { 'weight': '${POSINT}', 'weight_device': [{'file': '/dev/sda1', 'weight': '${POSINT}'}] }, 'cpus': '${FLOAT}', 'cpu_count': '$POSINT', 'healthcheck': { 'retries': '${POSINT:-3}', 'disable': '${FALSE}', 'command': 'true' }, 'mem_swappiness': '${DEFAULT:-127}', 'oom_score_adj': '${NEGINT}', 'scale': '${POSINT}', 'ulimits': { 'nproc': '${POSINT}', 'nofile': { 'soft': '${POSINT}', 'hard': '${DEFAULT:-40000}' }, }, 'privileged': '${TRUE}', 'read_only': '${DEFAULT:-no}', 'tty': '${DEFAULT:-N}', 'stdin_open': '${DEFAULT-on}', 'volumes': [ {'type': 'tmpfs', 'target': '/target', 'tmpfs': {'size': '$BYTES'}} ] } } expected = { 'service1': { 'blkio_config': { 'weight': 50, 'weight_device': [{'file': '/dev/sda1', 'weight': 50}] }, 'cpus': 0.145, 'cpu_count': 50, 'healthcheck': { 'retries': 50, 'disable': False, 'command': 'true' }, 'mem_swappiness': 127, 'oom_score_adj': -200, 'scale': 50, 'ulimits': { 'nproc': 50, 'nofile': { 'soft': 50, 'hard': 40000 }, }, 'privileged': True, 'read_only': False, 'tty': False, 'stdin_open': True, 'volumes': [ {'type': 'tmpfs', 'target': '/target', 'tmpfs': {'size': 536870912}} ] } } value = interpolate_environment_variables(V2_3, entry, 'service', mock_env) assert value == expected