cli_test.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. self.assertIn(cache_indicator, output)
  49. mock_stdout.truncate(0)
  50. self.command.dispatch(['build', '--no-cache', 'simple'], None)
  51. self.assertNotIn(cache_indicator, output)
  52. def test_up(self):
  53. self.command.dispatch(['up', '-d'], None)
  54. service = self.command.project.get_service('simple')
  55. another = self.command.project.get_service('another')
  56. self.assertEqual(len(service.containers()), 1)
  57. self.assertEqual(len(another.containers()), 1)
  58. def test_up_with_links(self):
  59. self.command.base_dir = 'tests/fixtures/links-figfile'
  60. self.command.dispatch(['up', '-d', 'web'], None)
  61. web = self.command.project.get_service('web')
  62. db = self.command.project.get_service('db')
  63. console = self.command.project.get_service('console')
  64. self.assertEqual(len(web.containers()), 1)
  65. self.assertEqual(len(db.containers()), 1)
  66. self.assertEqual(len(console.containers()), 0)
  67. def test_up_with_no_deps(self):
  68. self.command.base_dir = 'tests/fixtures/links-figfile'
  69. self.command.dispatch(['up', '-d', '--no-deps', 'web'], None)
  70. web = self.command.project.get_service('web')
  71. db = self.command.project.get_service('db')
  72. console = self.command.project.get_service('console')
  73. self.assertEqual(len(web.containers()), 1)
  74. self.assertEqual(len(db.containers()), 0)
  75. self.assertEqual(len(console.containers()), 0)
  76. def test_up_with_recreate(self):
  77. self.command.dispatch(['up', '-d'], None)
  78. service = self.command.project.get_service('simple')
  79. self.assertEqual(len(service.containers()), 1)
  80. old_ids = [c.id for c in service.containers()]
  81. self.command.dispatch(['up', '-d'], None)
  82. self.assertEqual(len(service.containers()), 1)
  83. new_ids = [c.id for c in service.containers()]
  84. self.assertNotEqual(old_ids, new_ids)
  85. def test_up_with_keep_old(self):
  86. self.command.dispatch(['up', '-d'], None)
  87. service = self.command.project.get_service('simple')
  88. self.assertEqual(len(service.containers()), 1)
  89. old_ids = [c.id for c in service.containers()]
  90. self.command.dispatch(['up', '-d', '--no-recreate'], None)
  91. self.assertEqual(len(service.containers()), 1)
  92. new_ids = [c.id for c in service.containers()]
  93. self.assertEqual(old_ids, new_ids)
  94. @patch('dockerpty.start')
  95. def test_run_service_without_links(self, mock_stdout):
  96. self.command.base_dir = 'tests/fixtures/links-figfile'
  97. self.command.dispatch(['run', 'console', '/bin/true'], None)
  98. self.assertEqual(len(self.command.project.containers()), 0)
  99. @patch('dockerpty.start')
  100. def test_run_service_with_links(self, __):
  101. self.command.base_dir = 'tests/fixtures/links-figfile'
  102. self.command.dispatch(['run', 'web', '/bin/true'], None)
  103. db = self.command.project.get_service('db')
  104. console = self.command.project.get_service('console')
  105. self.assertEqual(len(db.containers()), 1)
  106. self.assertEqual(len(console.containers()), 0)
  107. @patch('dockerpty.start')
  108. def test_run_with_no_deps(self, __):
  109. self.command.base_dir = 'tests/fixtures/links-figfile'
  110. self.command.dispatch(['run', '--no-deps', 'web', '/bin/true'], None)
  111. db = self.command.project.get_service('db')
  112. self.assertEqual(len(db.containers()), 0)
  113. @patch('dockerpty.start')
  114. def test_run_does_not_recreate_linked_containers(self, __):
  115. self.command.base_dir = 'tests/fixtures/links-figfile'
  116. self.command.dispatch(['up', '-d', 'db'], None)
  117. db = self.command.project.get_service('db')
  118. self.assertEqual(len(db.containers()), 1)
  119. old_ids = [c.id for c in db.containers()]
  120. self.command.dispatch(['run', 'web', '/bin/true'], None)
  121. self.assertEqual(len(db.containers()), 1)
  122. new_ids = [c.id for c in db.containers()]
  123. self.assertEqual(old_ids, new_ids)
  124. @patch('dockerpty.start')
  125. def test_run_without_command(self, __):
  126. self.command.base_dir = 'tests/fixtures/commands-figfile'
  127. self.client.build('tests/fixtures/simple-dockerfile', tag='figtest_test')
  128. for c in self.command.project.containers(stopped=True, one_off=True):
  129. c.remove()
  130. self.command.dispatch(['run', 'implicit'], None)
  131. service = self.command.project.get_service('implicit')
  132. containers = service.containers(stopped=True, one_off=True)
  133. self.assertEqual(
  134. [c.human_readable_command for c in containers],
  135. [u'/bin/sh -c echo "success"'],
  136. )
  137. self.command.dispatch(['run', 'explicit'], None)
  138. service = self.command.project.get_service('explicit')
  139. containers = service.containers(stopped=True, one_off=True)
  140. self.assertEqual(
  141. [c.human_readable_command for c in containers],
  142. [u'/bin/true'],
  143. )
  144. def test_rm(self):
  145. service = self.command.project.get_service('simple')
  146. service.create_container()
  147. service.kill()
  148. self.assertEqual(len(service.containers(stopped=True)), 1)
  149. self.command.dispatch(['rm', '--force'], None)
  150. self.assertEqual(len(service.containers(stopped=True)), 0)
  151. def test_scale(self):
  152. project = self.command.project
  153. self.command.scale({'SERVICE=NUM': ['simple=1']})
  154. self.assertEqual(len(project.get_service('simple').containers()), 1)
  155. self.command.scale({'SERVICE=NUM': ['simple=3', 'another=2']})
  156. self.assertEqual(len(project.get_service('simple').containers()), 3)
  157. self.assertEqual(len(project.get_service('another').containers()), 2)
  158. self.command.scale({'SERVICE=NUM': ['simple=1', 'another=1']})
  159. self.assertEqual(len(project.get_service('simple').containers()), 1)
  160. self.assertEqual(len(project.get_service('another').containers()), 1)
  161. self.command.scale({'SERVICE=NUM': ['simple=1', 'another=1']})
  162. self.assertEqual(len(project.get_service('simple').containers()), 1)
  163. self.assertEqual(len(project.get_service('another').containers()), 1)
  164. self.command.scale({'SERVICE=NUM': ['simple=0', 'another=0']})
  165. self.assertEqual(len(project.get_service('simple').containers()), 0)
  166. self.assertEqual(len(project.get_service('another').containers()), 0)