1
0

testcases.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. from docker import errors
  4. from .. import unittest
  5. from compose.cli.docker_client import docker_client
  6. from compose.config.config import ServiceLoader
  7. from compose.const import LABEL_PROJECT
  8. from compose.progress_stream import stream_output
  9. from compose.service import Service
  10. def pull_busybox(client):
  11. try:
  12. client.inspect_image('busybox:latest')
  13. except errors.APIError:
  14. client.pull('busybox:latest', stream=False)
  15. class DockerClientTestCase(unittest.TestCase):
  16. @classmethod
  17. def setUpClass(cls):
  18. cls.client = docker_client()
  19. def tearDown(self):
  20. for c in self.client.containers(
  21. all=True,
  22. filters={'label': '%s=composetest' % LABEL_PROJECT}):
  23. self.client.kill(c['Id'])
  24. self.client.remove_container(c['Id'])
  25. for i in self.client.images(
  26. filters={'label': 'com.docker.compose.test_image'}):
  27. self.client.remove_image(i)
  28. def create_service(self, name, **kwargs):
  29. if 'image' not in kwargs and 'build' not in kwargs:
  30. kwargs['image'] = 'busybox:latest'
  31. if 'command' not in kwargs:
  32. kwargs['command'] = ["top"]
  33. links = kwargs.get('links', None)
  34. volumes_from = kwargs.get('volumes_from', None)
  35. net = kwargs.get('net', None)
  36. workaround_options = ['links', 'volumes_from', 'net']
  37. for key in workaround_options:
  38. try:
  39. del kwargs[key]
  40. except KeyError:
  41. pass
  42. options = ServiceLoader(working_dir='.', filename=None, service_name=name, service_dict=kwargs).make_service_dict()
  43. labels = options.setdefault('labels', {})
  44. labels['com.docker.compose.test-name'] = self.id()
  45. if links:
  46. options['links'] = links
  47. if volumes_from:
  48. options['volumes_from'] = volumes_from
  49. if net:
  50. options['net'] = net
  51. return Service(
  52. project='composetest',
  53. client=self.client,
  54. **options
  55. )
  56. def check_build(self, *args, **kwargs):
  57. kwargs.setdefault('rm', True)
  58. build_output = self.client.build(*args, **kwargs)
  59. stream_output(build_output, open('/dev/null', 'w'))