cli_test.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. from __future__ import absolute_import
  2. from .testcases import DockerClientTestCase
  3. from mock import patch
  4. from fig.cli.main import TopLevelCommand
  5. from fig.packages.six import StringIO
  6. import sys
  7. class CLITestCase(DockerClientTestCase):
  8. def setUp(self):
  9. super(CLITestCase, self).setUp()
  10. self.old_sys_exit = sys.exit
  11. sys.exit = lambda code=0: None
  12. self.command = TopLevelCommand()
  13. self.command.base_dir = 'tests/fixtures/simple-figfile'
  14. def tearDown(self):
  15. sys.exit = self.old_sys_exit
  16. self.command.project.kill()
  17. self.command.project.remove_stopped()
  18. @patch('sys.stdout', new_callable=StringIO)
  19. def test_ps(self, mock_stdout):
  20. self.command.project.get_service('simple').create_container()
  21. self.command.dispatch(['ps'], None)
  22. self.assertIn('simplefigfile_simple_1', mock_stdout.getvalue())
  23. @patch('sys.stdout', new_callable=StringIO)
  24. def test_ps_default_figfile(self, mock_stdout):
  25. self.command.base_dir = 'tests/fixtures/multiple-figfiles'
  26. self.command.dispatch(['up', '-d'], None)
  27. self.command.dispatch(['ps'], None)
  28. output = mock_stdout.getvalue()
  29. self.assertIn('multiplefigfiles_simple_1', output)
  30. self.assertIn('multiplefigfiles_another_1', output)
  31. self.assertNotIn('multiplefigfiles_yetanother_1', output)
  32. @patch('sys.stdout', new_callable=StringIO)
  33. def test_ps_alternate_figfile(self, mock_stdout):
  34. self.command.base_dir = 'tests/fixtures/multiple-figfiles'
  35. self.command.dispatch(['-f', 'fig2.yml', 'up', '-d'], None)
  36. self.command.dispatch(['-f', 'fig2.yml', 'ps'], None)
  37. output = mock_stdout.getvalue()
  38. self.assertNotIn('multiplefigfiles_simple_1', output)
  39. self.assertNotIn('multiplefigfiles_another_1', output)
  40. self.assertIn('multiplefigfiles_yetanother_1', output)
  41. @patch('sys.stdout', new_callable=StringIO)
  42. def test_build_no_cache(self, mock_stdout):
  43. self.command.base_dir = 'tests/fixtures/simple-dockerfile'
  44. self.command.dispatch(['build', 'simple'], None)
  45. mock_stdout.truncate(0)
  46. cache_indicator = 'Using cache'
  47. self.command.dispatch(['build', 'simple'], None)
  48. output = mock_stdout.getvalue()
  49. self.assertIn(cache_indicator, output)
  50. mock_stdout.truncate(0)
  51. self.command.dispatch(['build', '--no-cache', 'simple'], None)
  52. output = mock_stdout.getvalue()
  53. self.assertNotIn(cache_indicator, output)
  54. def test_up(self):
  55. self.command.dispatch(['up', '-d'], None)
  56. service = self.command.project.get_service('simple')
  57. another = self.command.project.get_service('another')
  58. self.assertEqual(len(service.containers()), 1)
  59. self.assertEqual(len(another.containers()), 1)
  60. def test_up_with_links(self):
  61. self.command.base_dir = 'tests/fixtures/links-figfile'
  62. self.command.dispatch(['up', '-d', 'web'], None)
  63. web = self.command.project.get_service('web')
  64. db = self.command.project.get_service('db')
  65. console = self.command.project.get_service('console')
  66. self.assertEqual(len(web.containers()), 1)
  67. self.assertEqual(len(db.containers()), 1)
  68. self.assertEqual(len(console.containers()), 0)
  69. def test_up_with_no_deps(self):
  70. self.command.base_dir = 'tests/fixtures/links-figfile'
  71. self.command.dispatch(['up', '-d', '--no-deps', 'web'], None)
  72. web = self.command.project.get_service('web')
  73. db = self.command.project.get_service('db')
  74. console = self.command.project.get_service('console')
  75. self.assertEqual(len(web.containers()), 1)
  76. self.assertEqual(len(db.containers()), 0)
  77. self.assertEqual(len(console.containers()), 0)
  78. def test_up_with_recreate(self):
  79. self.command.dispatch(['up', '-d'], None)
  80. service = self.command.project.get_service('simple')
  81. self.assertEqual(len(service.containers()), 1)
  82. old_ids = [c.id for c in service.containers()]
  83. self.command.dispatch(['up', '-d'], None)
  84. self.assertEqual(len(service.containers()), 1)
  85. new_ids = [c.id for c in service.containers()]
  86. self.assertNotEqual(old_ids, new_ids)
  87. def test_up_with_keep_old(self):
  88. self.command.dispatch(['up', '-d'], None)
  89. service = self.command.project.get_service('simple')
  90. self.assertEqual(len(service.containers()), 1)
  91. old_ids = [c.id for c in service.containers()]
  92. self.command.dispatch(['up', '-d', '--no-recreate'], None)
  93. self.assertEqual(len(service.containers()), 1)
  94. new_ids = [c.id for c in service.containers()]
  95. self.assertEqual(old_ids, new_ids)
  96. @patch('dockerpty.start')
  97. def test_run_service_without_links(self, mock_stdout):
  98. self.command.base_dir = 'tests/fixtures/links-figfile'
  99. self.command.dispatch(['run', 'console', '/bin/true'], None)
  100. self.assertEqual(len(self.command.project.containers()), 0)
  101. @patch('dockerpty.start')
  102. def test_run_service_with_links(self, __):
  103. self.command.base_dir = 'tests/fixtures/links-figfile'
  104. self.command.dispatch(['run', 'web', '/bin/true'], None)
  105. db = self.command.project.get_service('db')
  106. console = self.command.project.get_service('console')
  107. self.assertEqual(len(db.containers()), 1)
  108. self.assertEqual(len(console.containers()), 0)
  109. @patch('dockerpty.start')
  110. def test_run_with_no_deps(self, __):
  111. self.command.base_dir = 'tests/fixtures/links-figfile'
  112. self.command.dispatch(['run', '--no-deps', 'web', '/bin/true'], None)
  113. db = self.command.project.get_service('db')
  114. self.assertEqual(len(db.containers()), 0)
  115. @patch('dockerpty.start')
  116. def test_run_does_not_recreate_linked_containers(self, __):
  117. self.command.base_dir = 'tests/fixtures/links-figfile'
  118. self.command.dispatch(['up', '-d', 'db'], None)
  119. db = self.command.project.get_service('db')
  120. self.assertEqual(len(db.containers()), 1)
  121. old_ids = [c.id for c in db.containers()]
  122. self.command.dispatch(['run', 'web', '/bin/true'], None)
  123. self.assertEqual(len(db.containers()), 1)
  124. new_ids = [c.id for c in db.containers()]
  125. self.assertEqual(old_ids, new_ids)
  126. @patch('dockerpty.start')
  127. def test_run_without_command(self, __):
  128. self.command.base_dir = 'tests/fixtures/commands-figfile'
  129. self.client.build('tests/fixtures/simple-dockerfile', tag='figtest_test')
  130. for c in self.command.project.containers(stopped=True, one_off=True):
  131. c.remove()
  132. self.command.dispatch(['run', 'implicit'], None)
  133. service = self.command.project.get_service('implicit')
  134. containers = service.containers(stopped=True, one_off=True)
  135. self.assertEqual(
  136. [c.human_readable_command for c in containers],
  137. [u'/bin/sh -c echo "success"'],
  138. )
  139. self.command.dispatch(['run', 'explicit'], None)
  140. service = self.command.project.get_service('explicit')
  141. containers = service.containers(stopped=True, one_off=True)
  142. self.assertEqual(
  143. [c.human_readable_command for c in containers],
  144. [u'/bin/true'],
  145. )
  146. def test_rm(self):
  147. service = self.command.project.get_service('simple')
  148. service.create_container()
  149. service.kill()
  150. self.assertEqual(len(service.containers(stopped=True)), 1)
  151. self.command.dispatch(['rm', '--force'], None)
  152. self.assertEqual(len(service.containers(stopped=True)), 0)
  153. def test_scale(self):
  154. project = self.command.project
  155. self.command.scale({'SERVICE=NUM': ['simple=1']})
  156. self.assertEqual(len(project.get_service('simple').containers()), 1)
  157. self.command.scale({'SERVICE=NUM': ['simple=3', 'another=2']})
  158. self.assertEqual(len(project.get_service('simple').containers()), 3)
  159. self.assertEqual(len(project.get_service('another').containers()), 2)
  160. self.command.scale({'SERVICE=NUM': ['simple=1', 'another=1']})
  161. self.assertEqual(len(project.get_service('simple').containers()), 1)
  162. self.assertEqual(len(project.get_service('another').containers()), 1)
  163. self.command.scale({'SERVICE=NUM': ['simple=1', 'another=1']})
  164. self.assertEqual(len(project.get_service('simple').containers()), 1)
  165. self.assertEqual(len(project.get_service('another').containers()), 1)
  166. self.command.scale({'SERVICE=NUM': ['simple=0', 'another=0']})
  167. self.assertEqual(len(project.get_service('simple').containers()), 0)
  168. self.assertEqual(len(project.get_service('another').containers()), 0)