Update tests for new environment handling

Signed-off-by: Joffrey F <joffrey@docker.com>
This commit is contained in:
Joffrey F 2016-03-07 17:18:19 -08:00
parent b9ca5188a2
commit 5831b869e8
8 changed files with 32 additions and 41 deletions

View File

@ -10,7 +10,7 @@
- id: end-of-file-fixer - id: end-of-file-fixer
- id: flake8 - id: flake8
- id: name-tests-test - id: name-tests-test
exclude: 'tests/(integration/testcases\.py)|(helpers\.py)' exclude: 'tests/integration/testcases\.py'
- id: requirements-txt-fixer - id: requirements-txt-fixer
- id: trailing-whitespace - id: trailing-whitespace
- repo: git://github.com/asottile/reorder_python_imports - repo: git://github.com/asottile/reorder_python_imports

View File

@ -15,7 +15,7 @@ from operator import attrgetter
import yaml import yaml
from docker import errors from docker import errors
from ..helpers import clear_environment from .. import mock
from compose.cli.command import get_project from compose.cli.command import get_project
from compose.container import Container from compose.container import Container
from compose.project import OneOffFilter from compose.project import OneOffFilter
@ -1452,7 +1452,7 @@ class CLITestCase(DockerClientTestCase):
self.assertEqual(len(containers), 1) self.assertEqual(len(containers), 1)
self.assertIn("FOO=1", containers[0].get('Config.Env')) self.assertIn("FOO=1", containers[0].get('Config.Env'))
@clear_environment @mock.patch.dict(os.environ)
def test_home_and_env_var_in_volume_path(self): def test_home_and_env_var_in_volume_path(self):
os.environ['VOLUME_NAME'] = 'my-volume' os.environ['VOLUME_NAME'] = 'my-volume'
os.environ['HOME'] = '/tmp/home-dir' os.environ['HOME'] = '/tmp/home-dir'

View File

@ -1,14 +1,9 @@
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
import functools
import os
from . import mock
from compose.config.config import ConfigDetails from compose.config.config import ConfigDetails
from compose.config.config import ConfigFile from compose.config.config import ConfigFile
from compose.config.config import load from compose.config.config import load
from compose.config.environment import Environment
def build_config(contents, **kwargs): def build_config(contents, **kwargs):
@ -19,11 +14,3 @@ def build_config_details(contents, working_dir='working_dir', filename='filename
return ConfigDetails( return ConfigDetails(
working_dir, working_dir,
[ConfigFile(filename, contents)]) [ConfigFile(filename, contents)])
def clear_environment(f):
@functools.wraps(f)
def wrapper(self, *args, **kwargs):
Environment.reset()
with mock.patch.dict(os.environ):
f(self, *args, **kwargs)

View File

@ -12,7 +12,6 @@ from six import StringIO
from six import text_type from six import text_type
from .. import mock from .. import mock
from ..helpers import clear_environment
from .testcases import DockerClientTestCase from .testcases import DockerClientTestCase
from .testcases import get_links from .testcases import get_links
from .testcases import pull_busybox from .testcases import pull_busybox
@ -913,7 +912,7 @@ class ServiceTest(DockerClientTestCase):
}.items(): }.items():
self.assertEqual(env[k], v) self.assertEqual(env[k], v)
@clear_environment @mock.patch.dict(os.environ)
def test_resolve_env(self): def test_resolve_env(self):
os.environ['FILE_DEF'] = 'E1' os.environ['FILE_DEF'] = 'E1'
os.environ['FILE_DEF_EMPTY'] = 'E2' os.environ['FILE_DEF_EMPTY'] = 'E2'

View File

@ -12,6 +12,7 @@ from compose.cli.docker_client import docker_client
from compose.config.config import resolve_environment from compose.config.config import resolve_environment
from compose.config.config import V1 from compose.config.config import V1
from compose.config.config import V2_0 from compose.config.config import V2_0
from compose.config.environment import Environment
from compose.const import API_VERSIONS from compose.const import API_VERSIONS
from compose.const import LABEL_PROJECT from compose.const import LABEL_PROJECT
from compose.progress_stream import stream_output from compose.progress_stream import stream_output
@ -89,7 +90,7 @@ class DockerClientTestCase(unittest.TestCase):
if 'command' not in kwargs: if 'command' not in kwargs:
kwargs['command'] = ["top"] kwargs['command'] = ["top"]
kwargs['environment'] = resolve_environment(kwargs) kwargs['environment'] = resolve_environment(kwargs, Environment(None))
labels = dict(kwargs.setdefault('labels', {})) labels = dict(kwargs.setdefault('labels', {}))
labels['com.docker.compose.test-name'] = self.id() labels['com.docker.compose.test-name'] = self.id()

View File

@ -11,7 +11,6 @@ import pytest
from .. import mock from .. import mock
from .. import unittest from .. import unittest
from ..helpers import build_config from ..helpers import build_config
from ..helpers import clear_environment
from compose.cli.command import get_project from compose.cli.command import get_project
from compose.cli.command import get_project_name from compose.cli.command import get_project_name
from compose.cli.docopt_command import NoSuchCommand from compose.cli.docopt_command import NoSuchCommand
@ -44,7 +43,7 @@ class CLITestCase(unittest.TestCase):
project_name = get_project_name(None, project_name=name) project_name = get_project_name(None, project_name=name)
self.assertEquals('explicitprojectname', project_name) self.assertEquals('explicitprojectname', project_name)
@clear_environment @mock.patch.dict(os.environ)
def test_project_name_from_environment_new_var(self): def test_project_name_from_environment_new_var(self):
name = 'namefromenv' name = 'namefromenv'
os.environ['COMPOSE_PROJECT_NAME'] = name os.environ['COMPOSE_PROJECT_NAME'] = name

View File

@ -17,13 +17,13 @@ from compose.config.config import resolve_build_args
from compose.config.config import resolve_environment from compose.config.config import resolve_environment
from compose.config.config import V1 from compose.config.config import V1
from compose.config.config import V2_0 from compose.config.config import V2_0
from compose.config.environment import Environment
from compose.config.errors import ConfigurationError from compose.config.errors import ConfigurationError
from compose.config.errors import VERSION_EXPLANATION from compose.config.errors import VERSION_EXPLANATION
from compose.config.types import VolumeSpec from compose.config.types import VolumeSpec
from compose.const import IS_WINDOWS_PLATFORM from compose.const import IS_WINDOWS_PLATFORM
from tests import mock from tests import mock
from tests import unittest from tests import unittest
from tests.helpers import clear_environment
DEFAULT_VERSION = V2_0 DEFAULT_VERSION = V2_0
@ -1582,7 +1582,7 @@ class PortsTest(unittest.TestCase):
class InterpolationTest(unittest.TestCase): class InterpolationTest(unittest.TestCase):
@clear_environment @mock.patch.dict(os.environ)
def test_config_file_with_environment_variable(self): def test_config_file_with_environment_variable(self):
os.environ.update( os.environ.update(
IMAGE="busybox", IMAGE="busybox",
@ -1605,7 +1605,7 @@ class InterpolationTest(unittest.TestCase):
} }
]) ])
@clear_environment @mock.patch.dict(os.environ)
def test_unset_variable_produces_warning(self): def test_unset_variable_produces_warning(self):
os.environ.pop('FOO', None) os.environ.pop('FOO', None)
os.environ.pop('BAR', None) os.environ.pop('BAR', None)
@ -1621,7 +1621,7 @@ class InterpolationTest(unittest.TestCase):
None, None,
) )
with mock.patch('compose.config.interpolation.log') as log: with mock.patch('compose.config.environment.log') as log:
config.load(config_details) config.load(config_details)
self.assertEqual(2, log.warn.call_count) self.assertEqual(2, log.warn.call_count)
@ -1629,7 +1629,7 @@ class InterpolationTest(unittest.TestCase):
self.assertIn('BAR', warnings[0]) self.assertIn('BAR', warnings[0])
self.assertIn('FOO', warnings[1]) self.assertIn('FOO', warnings[1])
@clear_environment @mock.patch.dict(os.environ)
def test_invalid_interpolation(self): def test_invalid_interpolation(self):
with self.assertRaises(config.ConfigurationError) as cm: with self.assertRaises(config.ConfigurationError) as cm:
config.load( config.load(
@ -1668,7 +1668,7 @@ class VolumeConfigTest(unittest.TestCase):
d = make_service_dict('foo', {'build': '.', 'volumes': ['/data']}, working_dir='.') d = make_service_dict('foo', {'build': '.', 'volumes': ['/data']}, working_dir='.')
self.assertEqual(d['volumes'], ['/data']) self.assertEqual(d['volumes'], ['/data'])
@clear_environment @mock.patch.dict(os.environ)
def test_volume_binding_with_environment_variable(self): def test_volume_binding_with_environment_variable(self):
os.environ['VOLUME_PATH'] = '/host/path' os.environ['VOLUME_PATH'] = '/host/path'
@ -1682,7 +1682,7 @@ class VolumeConfigTest(unittest.TestCase):
self.assertEqual(d['volumes'], [VolumeSpec.parse('/host/path:/container/path')]) self.assertEqual(d['volumes'], [VolumeSpec.parse('/host/path:/container/path')])
@pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason='posix paths') @pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason='posix paths')
@clear_environment @mock.patch.dict(os.environ)
def test_volume_binding_with_home(self): def test_volume_binding_with_home(self):
os.environ['HOME'] = '/home/user' os.environ['HOME'] = '/home/user'
d = make_service_dict('foo', {'build': '.', 'volumes': ['~:/container/path']}, working_dir='.') d = make_service_dict('foo', {'build': '.', 'volumes': ['~:/container/path']}, working_dir='.')
@ -1740,7 +1740,7 @@ class VolumeConfigTest(unittest.TestCase):
working_dir='c:\\Users\\me\\myproject') working_dir='c:\\Users\\me\\myproject')
self.assertEqual(d['volumes'], ['c:\\Users\\me\\otherproject:/data']) self.assertEqual(d['volumes'], ['c:\\Users\\me\\otherproject:/data'])
@clear_environment @mock.patch.dict(os.environ)
def test_home_directory_with_driver_does_not_expand(self): def test_home_directory_with_driver_does_not_expand(self):
os.environ['NAME'] = 'surprise!' os.environ['NAME'] = 'surprise!'
d = make_service_dict('foo', { d = make_service_dict('foo', {
@ -2026,7 +2026,7 @@ class EnvTest(unittest.TestCase):
def test_parse_environment_empty(self): def test_parse_environment_empty(self):
self.assertEqual(config.parse_environment(None), {}) self.assertEqual(config.parse_environment(None), {})
@clear_environment @mock.patch.dict(os.environ)
def test_resolve_environment(self): def test_resolve_environment(self):
os.environ['FILE_DEF'] = 'E1' os.environ['FILE_DEF'] = 'E1'
os.environ['FILE_DEF_EMPTY'] = 'E2' os.environ['FILE_DEF_EMPTY'] = 'E2'
@ -2042,7 +2042,7 @@ class EnvTest(unittest.TestCase):
}, },
} }
self.assertEqual( self.assertEqual(
resolve_environment(service_dict), resolve_environment(service_dict, Environment(None)),
{'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': None}, {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': None},
) )
@ -2073,13 +2073,15 @@ class EnvTest(unittest.TestCase):
assert 'Couldn\'t find env file' in exc.exconly() assert 'Couldn\'t find env file' in exc.exconly()
assert 'nonexistent.env' in exc.exconly() assert 'nonexistent.env' in exc.exconly()
@clear_environment @mock.patch.dict(os.environ)
def test_resolve_environment_from_env_file_with_empty_values(self): def test_resolve_environment_from_env_file_with_empty_values(self):
os.environ['FILE_DEF'] = 'E1' os.environ['FILE_DEF'] = 'E1'
os.environ['FILE_DEF_EMPTY'] = 'E2' os.environ['FILE_DEF_EMPTY'] = 'E2'
os.environ['ENV_DEF'] = 'E3' os.environ['ENV_DEF'] = 'E3'
self.assertEqual( self.assertEqual(
resolve_environment({'env_file': ['tests/fixtures/env/resolve.env']}), resolve_environment(
{'env_file': ['tests/fixtures/env/resolve.env']}, Environment(None)
),
{ {
'FILE_DEF': u'bär', 'FILE_DEF': u'bär',
'FILE_DEF_EMPTY': '', 'FILE_DEF_EMPTY': '',
@ -2088,7 +2090,7 @@ class EnvTest(unittest.TestCase):
}, },
) )
@clear_environment @mock.patch.dict(os.environ)
def test_resolve_build_args(self): def test_resolve_build_args(self):
os.environ['env_arg'] = 'value2' os.environ['env_arg'] = 'value2'
@ -2102,12 +2104,12 @@ class EnvTest(unittest.TestCase):
} }
} }
self.assertEqual( self.assertEqual(
resolve_build_args(build), resolve_build_args(build, Environment(build['context'])),
{'arg1': 'value1', 'empty_arg': '', 'env_arg': 'value2', 'no_env': None}, {'arg1': 'value1', 'empty_arg': '', 'env_arg': 'value2', 'no_env': None},
) )
@pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason='paths use slash') @pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason='paths use slash')
@clear_environment @mock.patch.dict(os.environ)
def test_resolve_path(self): def test_resolve_path(self):
os.environ['HOSTENV'] = '/tmp' os.environ['HOSTENV'] = '/tmp'
os.environ['CONTAINERENV'] = '/host/tmp' os.environ['CONTAINERENV'] = '/host/tmp'
@ -2394,7 +2396,7 @@ class ExtendsTest(unittest.TestCase):
assert 'net: container' in excinfo.exconly() assert 'net: container' in excinfo.exconly()
assert 'cannot be extended' in excinfo.exconly() assert 'cannot be extended' in excinfo.exconly()
@clear_environment @mock.patch.dict(os.environ)
def test_load_config_runs_interpolation_in_extended_service(self): def test_load_config_runs_interpolation_in_extended_service(self):
os.environ.update(HOSTNAME_VALUE="penguin") os.environ.update(HOSTNAME_VALUE="penguin")
expected_interpolated_value = "host-penguin" expected_interpolated_value = "host-penguin"
@ -2466,7 +2468,7 @@ class ExtendsTest(unittest.TestCase):
}, },
])) ]))
@clear_environment @mock.patch.dict(os.environ)
def test_extends_with_environment_and_env_files(self): def test_extends_with_environment_and_env_files(self):
tmpdir = py.test.ensuretemp('test_extends_with_environment') tmpdir = py.test.ensuretemp('test_extends_with_environment')
self.addCleanup(tmpdir.remove) self.addCleanup(tmpdir.remove)

View File

@ -13,7 +13,6 @@ from compose.config.interpolation import interpolate_environment_variables
@pytest.yield_fixture @pytest.yield_fixture
def mock_env(): def mock_env():
with mock.patch.dict(os.environ): with mock.patch.dict(os.environ):
Environment.reset()
os.environ['USER'] = 'jenny' os.environ['USER'] = 'jenny'
os.environ['FOO'] = 'bar' os.environ['FOO'] = 'bar'
yield yield
@ -44,7 +43,9 @@ def test_interpolate_environment_variables_in_services(mock_env):
} }
} }
} }
assert interpolate_environment_variables(services, 'service') == expected assert interpolate_environment_variables(
services, 'service', Environment(None)
) == expected
def test_interpolate_environment_variables_in_volumes(mock_env): def test_interpolate_environment_variables_in_volumes(mock_env):
@ -68,4 +69,6 @@ def test_interpolate_environment_variables_in_volumes(mock_env):
}, },
'other': {}, 'other': {},
} }
assert interpolate_environment_variables(volumes, 'volume') == expected assert interpolate_environment_variables(
volumes, 'volume', Environment(None)
) == expected