Use stdlib modules instead of deprecated pytest fixtures

Signed-off-by: Lumír Balhar <lbalhar@redhat.com>
This commit is contained in:
Lumir Balhar 2019-08-27 12:26:01 +02:00
parent 60458c8ae7
commit 73cc89c15f
4 changed files with 196 additions and 173 deletions

View File

@ -8,7 +8,6 @@ import random
import shutil import shutil
import tempfile import tempfile
import py
import pytest import pytest
from docker.errors import APIError from docker.errors import APIError
from docker.errors import NotFound from docker.errors import NotFound
@ -16,6 +15,7 @@ from docker.errors import NotFound
from .. import mock from .. import mock
from ..helpers import build_config as load_config from ..helpers import build_config as load_config
from ..helpers import BUSYBOX_IMAGE_WITH_TAG from ..helpers import BUSYBOX_IMAGE_WITH_TAG
from ..helpers import cd
from ..helpers import create_host_file from ..helpers import create_host_file
from .testcases import DockerClientTestCase from .testcases import DockerClientTestCase
from .testcases import SWARM_SKIP_CONTAINERS_ALL from .testcases import SWARM_SKIP_CONTAINERS_ALL
@ -1329,9 +1329,9 @@ class ProjectTest(DockerClientTestCase):
}) })
details = config.ConfigDetails('.', [base_file, override_file]) details = config.ConfigDetails('.', [base_file, override_file])
tmpdir = py.test.ensuretemp('logging_test') tmpdir = tempfile.mkdtemp('logging_test')
self.addCleanup(tmpdir.remove) self.addCleanup(shutil.rmtree, tmpdir)
with tmpdir.as_cwd(): with cd(tmpdir):
config_data = config.load(details) config_data = config.load(details)
project = Project.from_config( project = Project.from_config(
name='composetest', config_data=config_data, client=self.client name='composetest', config_data=config_data, client=self.client

View File

@ -6,8 +6,10 @@ from __future__ import absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
import copy import copy
import os
import shutil
import tempfile
import py
from docker.errors import ImageNotFound from docker.errors import ImageNotFound
from ..helpers import BUSYBOX_IMAGE_WITH_TAG from ..helpers import BUSYBOX_IMAGE_WITH_TAG
@ -426,29 +428,32 @@ class ServiceStateTest(DockerClientTestCase):
@no_cluster('Can not guarantee the build will be run on the same node the service is deployed') @no_cluster('Can not guarantee the build will be run on the same node the service is deployed')
def test_trigger_recreate_with_build(self): def test_trigger_recreate_with_build(self):
context = py.test.ensuretemp('test_trigger_recreate_with_build') context = tempfile.mkdtemp('test_trigger_recreate_with_build')
self.addCleanup(context.remove) self.addCleanup(shutil.rmtree, context)
base_image = "FROM busybox\nLABEL com.docker.compose.test_image=true\n" base_image = "FROM busybox\nLABEL com.docker.compose.test_image=true\n"
dockerfile = context.join('Dockerfile') dockerfile = os.path.join(context, 'Dockerfile')
dockerfile.write(base_image) with open(dockerfile, mode="w") as dockerfile_fh:
dockerfile_fh.write(base_image)
web = self.create_service('web', build={'context': str(context)}) web = self.create_service('web', build={'context': str(context)})
container = web.create_container() container = web.create_container()
dockerfile.write(base_image + 'CMD echo hello world\n') with open(dockerfile, mode="w") as dockerfile_fh:
dockerfile_fh.write(base_image + 'CMD echo hello world\n')
web.build() web.build()
web = self.create_service('web', build={'context': str(context)}) web = self.create_service('web', build={'context': str(context)})
assert ('recreate', [container]) == web.convergence_plan() assert ('recreate', [container]) == web.convergence_plan()
def test_image_changed_to_build(self): def test_image_changed_to_build(self):
context = py.test.ensuretemp('test_image_changed_to_build') context = tempfile.mkdtemp('test_image_changed_to_build')
self.addCleanup(context.remove) self.addCleanup(shutil.rmtree, context)
context.join('Dockerfile').write(""" with open(os.path.join(context, 'Dockerfile'), mode="w") as dockerfile:
FROM busybox dockerfile.write("""
LABEL com.docker.compose.test_image=true FROM busybox
""") LABEL com.docker.compose.test_image=true
""")
web = self.create_service('web', image='busybox') web = self.create_service('web', image='busybox')
container = web.create_container() container = web.create_container()

View File

@ -10,7 +10,6 @@ import tempfile
from operator import itemgetter from operator import itemgetter
from random import shuffle from random import shuffle
import py
import pytest import pytest
import yaml import yaml
from ddt import data from ddt import data
@ -18,6 +17,7 @@ from ddt import ddt
from ...helpers import build_config_details from ...helpers import build_config_details
from ...helpers import BUSYBOX_IMAGE_WITH_TAG from ...helpers import BUSYBOX_IMAGE_WITH_TAG
from ...helpers import cd
from compose.config import config from compose.config import config
from compose.config import types from compose.config import types
from compose.config.config import ConfigFile from compose.config.config import ConfigFile
@ -780,13 +780,14 @@ class ConfigTest(unittest.TestCase):
}) })
details = config.ConfigDetails('.', [base_file, override_file]) details = config.ConfigDetails('.', [base_file, override_file])
tmpdir = py.test.ensuretemp('config_test') tmpdir = tempfile.mkdtemp('config_test')
self.addCleanup(tmpdir.remove) self.addCleanup(shutil.rmtree, tmpdir)
tmpdir.join('common.yml').write(""" with open(os.path.join(tmpdir, 'common.yml'), mode="w") as common_fh:
base: common_fh.write("""
labels: ['label=one'] base:
""") labels: ['label=one']
with tmpdir.as_cwd(): """)
with cd(tmpdir):
service_dicts = config.load(details).services service_dicts = config.load(details).services
expected = [ expected = [
@ -815,19 +816,20 @@ class ConfigTest(unittest.TestCase):
} }
) )
tmpdir = pytest.ensuretemp('config_test') tmpdir = tempfile.mkdtemp('config_test')
self.addCleanup(tmpdir.remove) self.addCleanup(shutil.rmtree, tmpdir)
tmpdir.join('base.yml').write(""" with open(os.path.join(tmpdir, 'base.yml'), mode="w") as base_fh:
version: '2.2' base_fh.write("""
services: version: '2.2'
base: services:
image: base base:
web: image: base
extends: base web:
""") extends: base
""")
details = config.ConfigDetails('.', [main_file]) details = config.ConfigDetails('.', [main_file])
with tmpdir.as_cwd(): with cd(tmpdir):
service_dicts = config.load(details).services service_dicts = config.load(details).services
assert service_dicts[0] == { assert service_dicts[0] == {
'name': 'prodweb', 'name': 'prodweb',
@ -1765,22 +1767,23 @@ class ConfigTest(unittest.TestCase):
assert services[0]['environment']['SPRING_JPA_HIBERNATE_DDL-AUTO'] == 'none' assert services[0]['environment']['SPRING_JPA_HIBERNATE_DDL-AUTO'] == 'none'
def test_load_yaml_with_yaml_error(self): def test_load_yaml_with_yaml_error(self):
tmpdir = py.test.ensuretemp('invalid_yaml_test') tmpdir = tempfile.mkdtemp('invalid_yaml_test')
self.addCleanup(tmpdir.remove) self.addCleanup(shutil.rmtree, tmpdir)
invalid_yaml_file = tmpdir.join('docker-compose.yml') invalid_yaml_file = os.path.join(tmpdir, 'docker-compose.yml')
invalid_yaml_file.write(""" with open(invalid_yaml_file, mode="w") as invalid_yaml_file_fh:
web: invalid_yaml_file_fh.write("""
this is bogus: ok: what web:
""") this is bogus: ok: what
""")
with pytest.raises(ConfigurationError) as exc: with pytest.raises(ConfigurationError) as exc:
config.load_yaml(str(invalid_yaml_file)) config.load_yaml(str(invalid_yaml_file))
assert 'line 3, column 32' in exc.exconly() assert 'line 3, column 36' in exc.exconly()
def test_load_yaml_with_bom(self): def test_load_yaml_with_bom(self):
tmpdir = py.test.ensuretemp('bom_yaml') tmpdir = tempfile.mkdtemp('bom_yaml')
self.addCleanup(tmpdir.remove) self.addCleanup(shutil.rmtree, tmpdir)
bom_yaml = tmpdir.join('docker-compose.yml') bom_yaml = os.path.join(tmpdir, 'docker-compose.yml')
with codecs.open(str(bom_yaml), 'w', encoding='utf-8') as f: with codecs.open(str(bom_yaml), 'w', encoding='utf-8') as f:
f.write('''\ufeff f.write('''\ufeff
version: '2.3' version: '2.3'
@ -4724,43 +4727,48 @@ class ExtendsTest(unittest.TestCase):
@mock.patch.dict(os.environ) @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 = tempfile.mkdtemp('test_extends_with_environment')
self.addCleanup(tmpdir.remove) self.addCleanup(shutil.rmtree, tmpdir)
commondir = tmpdir.mkdir('common') commondir = os.path.join(tmpdir, 'common')
commondir.join('base.yml').write(""" os.mkdir(commondir)
app: with open(os.path.join(commondir, 'base.yml'), mode="w") as base_fh:
image: 'example/app' base_fh.write("""
env_file: app:
- 'envs' image: 'example/app'
environment: env_file:
- SECRET - 'envs'
- TEST_ONE=common environment:
- TEST_TWO=common - SECRET
""") - TEST_ONE=common
tmpdir.join('docker-compose.yml').write(""" - TEST_TWO=common
ext: """)
extends: with open(os.path.join(tmpdir, 'docker-compose.yml'), mode="w") as docker_compose_fh:
file: common/base.yml docker_compose_fh.write("""
service: app ext:
env_file: extends:
- 'envs' file: common/base.yml
environment: service: app
- THING env_file:
- TEST_ONE=top - 'envs'
""") environment:
commondir.join('envs').write(""" - THING
COMMON_ENV_FILE - TEST_ONE=top
TEST_ONE=common-env-file """)
TEST_TWO=common-env-file with open(os.path.join(commondir, 'envs'), mode="w") as envs_fh:
TEST_THREE=common-env-file envs_fh.write("""
TEST_FOUR=common-env-file COMMON_ENV_FILE
""") TEST_ONE=common-env-file
tmpdir.join('envs').write(""" TEST_TWO=common-env-file
TOP_ENV_FILE TEST_THREE=common-env-file
TEST_ONE=top-env-file TEST_FOUR=common-env-file
TEST_TWO=top-env-file """)
TEST_THREE=top-env-file with open(os.path.join(tmpdir, 'envs'), mode="w") as envs_fh:
""") envs_fh.write("""
TOP_ENV_FILE
TEST_ONE=top-env-file
TEST_TWO=top-env-file
TEST_THREE=top-env-file
""")
expected = [ expected = [
{ {
@ -4783,72 +4791,77 @@ class ExtendsTest(unittest.TestCase):
os.environ['THING'] = 'thing' os.environ['THING'] = 'thing'
os.environ['COMMON_ENV_FILE'] = 'secret' os.environ['COMMON_ENV_FILE'] = 'secret'
os.environ['TOP_ENV_FILE'] = 'secret' os.environ['TOP_ENV_FILE'] = 'secret'
config = load_from_filename(str(tmpdir.join('docker-compose.yml'))) config = load_from_filename(str(os.path.join(tmpdir, 'docker-compose.yml')))
assert config == expected assert config == expected
def test_extends_with_mixed_versions_is_error(self): def test_extends_with_mixed_versions_is_error(self):
tmpdir = py.test.ensuretemp('test_extends_with_mixed_version') tmpdir = tempfile.mkdtemp('test_extends_with_mixed_version')
self.addCleanup(tmpdir.remove) self.addCleanup(shutil.rmtree, tmpdir)
tmpdir.join('docker-compose.yml').write(""" with open(os.path.join(tmpdir, 'docker-compose.yml'), mode="w") as docker_compose_fh:
version: "2" docker_compose_fh.write("""
services: version: "2"
web: services:
extends: web:
file: base.yml extends:
service: base file: base.yml
image: busybox service: base
""") image: busybox
tmpdir.join('base.yml').write(""" """)
base: with open(os.path.join(tmpdir, 'base.yml'), mode="w") as base_fh:
volumes: ['/foo'] base_fh.write("""
ports: ['3000:3000']
""")
with pytest.raises(ConfigurationError) as exc:
load_from_filename(str(tmpdir.join('docker-compose.yml')))
assert 'Version mismatch' in exc.exconly()
def test_extends_with_defined_version_passes(self):
tmpdir = py.test.ensuretemp('test_extends_with_defined_version')
self.addCleanup(tmpdir.remove)
tmpdir.join('docker-compose.yml').write("""
version: "2"
services:
web:
extends:
file: base.yml
service: base
image: busybox
""")
tmpdir.join('base.yml').write("""
version: "2"
services:
base: base:
volumes: ['/foo'] volumes: ['/foo']
ports: ['3000:3000'] ports: ['3000:3000']
command: top """)
""")
service = load_from_filename(str(tmpdir.join('docker-compose.yml'))) with pytest.raises(ConfigurationError) as exc:
load_from_filename(str(os.path.join(tmpdir, 'docker-compose.yml')))
assert 'Version mismatch' in exc.exconly()
def test_extends_with_defined_version_passes(self):
tmpdir = tempfile.mkdtemp('test_extends_with_defined_version')
self.addCleanup(shutil.rmtree, tmpdir)
with open(os.path.join(tmpdir, 'docker-compose.yml'), mode="w") as docker_compose_fh:
docker_compose_fh.write("""
version: "2"
services:
web:
extends:
file: base.yml
service: base
image: busybox
""")
with open(os.path.join(tmpdir, 'base.yml'), mode="w") as base_fh:
base_fh.write("""
version: "2"
services:
base:
volumes: ['/foo']
ports: ['3000:3000']
command: top
""")
service = load_from_filename(str(os.path.join(tmpdir, 'docker-compose.yml')))
assert service[0]['command'] == "top" assert service[0]['command'] == "top"
def test_extends_with_depends_on(self): def test_extends_with_depends_on(self):
tmpdir = py.test.ensuretemp('test_extends_with_depends_on') tmpdir = tempfile.mkdtemp('test_extends_with_depends_on')
self.addCleanup(tmpdir.remove) self.addCleanup(shutil.rmtree, tmpdir)
tmpdir.join('docker-compose.yml').write(""" with open(os.path.join(tmpdir, 'docker-compose.yml'), mode="w") as docker_compose_fh:
version: "2" docker_compose_fh.write("""
services: version: "2"
base: services:
image: example base:
web: image: example
extends: base web:
image: busybox extends: base
depends_on: ['other'] image: busybox
other: depends_on: ['other']
image: example other:
""") image: example
services = load_from_filename(str(tmpdir.join('docker-compose.yml'))) """)
services = load_from_filename(str(os.path.join(tmpdir, 'docker-compose.yml')))
assert service_sort(services)[2]['depends_on'] == { assert service_sort(services)[2]['depends_on'] == {
'other': {'condition': 'service_started'} 'other': {'condition': 'service_started'}
} }
@ -4867,45 +4880,47 @@ class ExtendsTest(unittest.TestCase):
}] }]
def test_extends_with_ports(self): def test_extends_with_ports(self):
tmpdir = py.test.ensuretemp('test_extends_with_ports') tmpdir = tempfile.mkdtemp('test_extends_with_ports')
self.addCleanup(tmpdir.remove) self.addCleanup(shutil.rmtree, tmpdir)
tmpdir.join('docker-compose.yml').write(""" with open(os.path.join(tmpdir, 'docker-compose.yml'), mode="w") as docker_compose_fh:
version: '2' docker_compose_fh.write("""
version: '2'
services: services:
a: a:
image: nginx image: nginx
ports: ports:
- 80 - 80
b: b:
extends: extends:
service: a service: a
""") """)
services = load_from_filename(str(tmpdir.join('docker-compose.yml'))) services = load_from_filename(str(os.path.join(tmpdir, 'docker-compose.yml')))
assert len(services) == 2 assert len(services) == 2
for svc in services: for svc in services:
assert svc['ports'] == [types.ServicePort('80', None, None, None, None)] assert svc['ports'] == [types.ServicePort('80', None, None, None, None)]
def test_extends_with_security_opt(self): def test_extends_with_security_opt(self):
tmpdir = py.test.ensuretemp('test_extends_with_ports') tmpdir = tempfile.mkdtemp('test_extends_with_ports')
self.addCleanup(tmpdir.remove) self.addCleanup(shutil.rmtree, tmpdir)
tmpdir.join('docker-compose.yml').write(""" with open(os.path.join(tmpdir, 'docker-compose.yml'), mode="w") as docker_compose_fh:
version: '2' docker_compose_fh.write("""
version: '2'
services: services:
a: a:
image: nginx image: nginx
security_opt: security_opt:
- apparmor:unconfined - apparmor:unconfined
- seccomp:unconfined - seccomp:unconfined
b: b:
extends: extends:
service: a service: a
""") """)
services = load_from_filename(str(tmpdir.join('docker-compose.yml'))) services = load_from_filename(str(os.path.join(tmpdir, 'docker-compose.yml')))
assert len(services) == 2 assert len(services) == 2
for svc in services: for svc in services:
assert types.SecurityOpt.parse('apparmor:unconfined') in svc['security_opt'] assert types.SecurityOpt.parse('apparmor:unconfined') in svc['security_opt']

View File

@ -4,6 +4,9 @@ from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
import codecs import codecs
import os
import shutil
import tempfile
import pytest import pytest
@ -46,19 +49,19 @@ class EnvironmentTest(unittest.TestCase):
assert env.get_boolean('UNDEFINED') is False assert env.get_boolean('UNDEFINED') is False
def test_env_vars_from_file_bom(self): def test_env_vars_from_file_bom(self):
tmpdir = pytest.ensuretemp('env_file') tmpdir = tempfile.mkdtemp('env_file')
self.addCleanup(tmpdir.remove) self.addCleanup(shutil.rmtree, tmpdir)
with codecs.open('{}/bom.env'.format(str(tmpdir)), 'w', encoding='utf-8') as f: with codecs.open('{}/bom.env'.format(str(tmpdir)), 'w', encoding='utf-8') as f:
f.write('\ufeffPARK_BOM=박봄\n') f.write('\ufeffPARK_BOM=박봄\n')
assert env_vars_from_file(str(tmpdir.join('bom.env'))) == { assert env_vars_from_file(str(os.path.join(tmpdir, 'bom.env'))) == {
'PARK_BOM': '박봄' 'PARK_BOM': '박봄'
} }
def test_env_vars_from_file_whitespace(self): def test_env_vars_from_file_whitespace(self):
tmpdir = pytest.ensuretemp('env_file') tmpdir = tempfile.mkdtemp('env_file')
self.addCleanup(tmpdir.remove) self.addCleanup(shutil.rmtree, tmpdir)
with codecs.open('{}/whitespace.env'.format(str(tmpdir)), 'w', encoding='utf-8') as f: with codecs.open('{}/whitespace.env'.format(str(tmpdir)), 'w', encoding='utf-8') as f:
f.write('WHITESPACE =yes\n') f.write('WHITESPACE =yes\n')
with pytest.raises(ConfigurationError) as exc: with pytest.raises(ConfigurationError) as exc:
env_vars_from_file(str(tmpdir.join('whitespace.env'))) env_vars_from_file(str(os.path.join(tmpdir, 'whitespace.env')))
assert 'environment variable' in exc.exconly() assert 'environment variable' in exc.exconly()