cli_test.py 8.0 KB

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