cli_test.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. from __future__ import unicode_literals
  2. from __future__ import absolute_import
  3. from .testcases import DockerClientTestCase
  4. from mock import patch
  5. from fig.cli.main import TopLevelCommand
  6. from fig.packages.six import StringIO
  7. import sys
  8. class CLITestCase(DockerClientTestCase):
  9. def setUp(self):
  10. super(CLITestCase, self).setUp()
  11. self.old_sys_exit = sys.exit
  12. sys.exit = lambda code=0: None
  13. self.command = TopLevelCommand()
  14. self.command.base_dir = 'tests/fixtures/simple-figfile'
  15. def tearDown(self):
  16. sys.exit = self.old_sys_exit
  17. self.command.project.kill()
  18. self.command.project.remove_stopped()
  19. @patch('sys.stdout', new_callable=StringIO)
  20. def test_ps(self, mock_stdout):
  21. self.command.project.get_service('simple').create_container()
  22. self.command.dispatch(['ps'], None)
  23. self.assertIn('fig_simple_1', mock_stdout.getvalue())
  24. @patch('sys.stdout', new_callable=StringIO)
  25. def test_ps_default_figfile(self, mock_stdout):
  26. self.command.base_dir = 'tests/fixtures/multiple-figfiles'
  27. self.command.dispatch(['up', '-d'], None)
  28. self.command.dispatch(['ps'], None)
  29. output = mock_stdout.getvalue()
  30. self.assertIn('fig_simple_1', output)
  31. self.assertIn('fig_another_1', output)
  32. self.assertNotIn('fig_yetanother_1', output)
  33. @patch('sys.stdout', new_callable=StringIO)
  34. def test_ps_alternate_figfile(self, mock_stdout):
  35. self.command.base_dir = 'tests/fixtures/multiple-figfiles'
  36. self.command.dispatch(['-f', 'fig2.yml', 'up', '-d'], None)
  37. self.command.dispatch(['-f', 'fig2.yml', 'ps'], None)
  38. output = mock_stdout.getvalue()
  39. self.assertNotIn('fig_simple_1', output)
  40. self.assertNotIn('fig_another_1', output)
  41. self.assertIn('fig_yetanother_1', output)
  42. def test_up(self):
  43. self.command.dispatch(['up', '-d'], None)
  44. service = self.command.project.get_service('simple')
  45. another = self.command.project.get_service('another')
  46. self.assertEqual(len(service.containers()), 1)
  47. self.assertEqual(len(another.containers()), 0)
  48. def test_up_with_links(self):
  49. self.command.base_dir = 'tests/fixtures/links-figfile'
  50. self.command.dispatch([str('up'), str('-d'), str('web')], None)
  51. web = self.command.project.get_service('web')
  52. db = self.command.project.get_service('db')
  53. console = self.command.project.get_service('console')
  54. self.assertEqual(len(web.containers()), 1)
  55. self.assertEqual(len(db.containers()), 1)
  56. self.assertEqual(len(console.containers()), 0)
  57. def test_up_with_no_links(self):
  58. self.command.base_dir = 'tests/fixtures/links-figfile'
  59. self.command.dispatch([str('up'), str('-d'), str('--no-links'), str('web')], None)
  60. web = self.command.project.get_service('web')
  61. db = self.command.project.get_service('db')
  62. console = self.command.project.get_service('console')
  63. self.assertEqual(len(web.containers()), 1)
  64. self.assertEqual(len(db.containers()), 0)
  65. self.assertEqual(len(console.containers()), 0)
  66. @patch('sys.stdout', new_callable=StringIO)
  67. def test_run_with_links(self, mock_stdout):
  68. mock_stdout.fileno = lambda: 1
  69. self.command.base_dir = 'tests/fixtures/links-figfile'
  70. self.command.dispatch([str('run'), str('web'), str('/bin/true')], None)
  71. db = self.command.project.get_service('db')
  72. console = self.command.project.get_service('console')
  73. self.assertEqual(len(db.containers()), 1)
  74. self.assertEqual(len(console.containers()), 0)
  75. @patch('sys.stdout', new_callable=StringIO)
  76. def test_run_with_no_links(self, mock_stdout):
  77. mock_stdout.fileno = lambda: 1
  78. self.command.base_dir = 'tests/fixtures/links-figfile'
  79. self.command.dispatch([str('run'), str('--no-links'), str('web'), str('/bin/true')], None)
  80. db = self.command.project.get_service('db')
  81. self.assertEqual(len(db.containers()), 0)
  82. def test_rm(self):
  83. service = self.command.project.get_service('simple')
  84. service.create_container()
  85. service.kill()
  86. self.assertEqual(len(service.containers(stopped=True)), 1)
  87. self.command.dispatch(['rm', '--force'], None)
  88. self.assertEqual(len(service.containers(stopped=True)), 0)
  89. def test_scale(self):
  90. project = self.command.project
  91. self.command.scale({'SERVICE=NUM': ['simple=1']})
  92. self.assertEqual(len(project.get_service('simple').containers()), 1)
  93. self.command.scale({'SERVICE=NUM': ['simple=3', 'another=2']})
  94. self.assertEqual(len(project.get_service('simple').containers()), 3)
  95. self.assertEqual(len(project.get_service('another').containers()), 2)
  96. self.command.scale({'SERVICE=NUM': ['simple=1', 'another=1']})
  97. self.assertEqual(len(project.get_service('simple').containers()), 1)
  98. self.assertEqual(len(project.get_service('another').containers()), 1)
  99. self.command.scale({'SERVICE=NUM': ['simple=1', 'another=1']})
  100. self.assertEqual(len(project.get_service('simple').containers()), 1)
  101. self.assertEqual(len(project.get_service('another').containers()), 1)
  102. self.command.scale({'SERVICE=NUM': ['simple=0', 'another=0']})
  103. self.assertEqual(len(project.get_service('simple').containers()), 0)
  104. self.assertEqual(len(project.get_service('another').containers()), 0)