testcases.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. from .. import unittest
  4. from compose.cli.docker_client import docker_client
  5. from compose.config.config import ServiceLoader
  6. from compose.const import LABEL_PROJECT
  7. from compose.progress_stream import stream_output
  8. from compose.service import Service
  9. class DockerClientTestCase(unittest.TestCase):
  10. @classmethod
  11. def setUpClass(cls):
  12. cls.client = docker_client()
  13. def tearDown(self):
  14. for c in self.client.containers(
  15. all=True,
  16. filters={'label': '%s=composetest' % LABEL_PROJECT}):
  17. self.client.kill(c['Id'])
  18. self.client.remove_container(c['Id'])
  19. for i in self.client.images(
  20. filters={'label': 'com.docker.compose.test_image'}):
  21. self.client.remove_image(i)
  22. def create_service(self, name, **kwargs):
  23. if 'image' not in kwargs and 'build' not in kwargs:
  24. kwargs['image'] = 'busybox:latest'
  25. if 'command' not in kwargs:
  26. kwargs['command'] = ["top"]
  27. options = ServiceLoader(working_dir='.').make_service_dict(name, kwargs)
  28. return Service(
  29. project='composetest',
  30. client=self.client,
  31. **options
  32. )
  33. def check_build(self, *args, **kwargs):
  34. kwargs.setdefault('rm', True)
  35. build_output = self.client.build(*args, **kwargs)
  36. stream_output(build_output, open('/dev/null', 'w'))