cli_test.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. class CLITestCase(DockerClientTestCase):
  8. def setUp(self):
  9. super(CLITestCase, self).setUp()
  10. self.command = TopLevelCommand()
  11. self.command.base_dir = 'tests/fixtures/simple-figfile'
  12. def tearDown(self):
  13. self.command.project.kill()
  14. self.command.project.remove_stopped()
  15. @patch('sys.stdout', new_callable=StringIO)
  16. def test_ps(self, mock_stdout):
  17. self.command.project.get_service('simple').create_container()
  18. self.command.dispatch(['ps'], None)
  19. self.assertIn('fig_simple_1', mock_stdout.getvalue())
  20. @patch('sys.stdout', new_callable=StringIO)
  21. def test_ps_default_figfile(self, mock_stdout):
  22. self.command.base_dir = 'tests/fixtures/multiple-figfiles'
  23. self.command.dispatch(['up', '-d'], None)
  24. self.command.dispatch(['ps'], None)
  25. output = mock_stdout.getvalue()
  26. self.assertIn('fig_simple_1', output)
  27. self.assertIn('fig_another_1', output)
  28. self.assertNotIn('fig_yetanother_1', output)
  29. @patch('sys.stdout', new_callable=StringIO)
  30. def test_ps_alternate_figfile(self, mock_stdout):
  31. self.command.base_dir = 'tests/fixtures/multiple-figfiles'
  32. self.command.dispatch(['-f', 'fig2.yml', 'up', '-d'], None)
  33. self.command.dispatch(['-f', 'fig2.yml', 'ps'], None)
  34. output = mock_stdout.getvalue()
  35. self.assertNotIn('fig_simple_1', output)
  36. self.assertNotIn('fig_another_1', output)
  37. self.assertIn('fig_yetanother_1', output)
  38. def test_rm(self):
  39. service = self.command.project.get_service('simple')
  40. service.create_container()
  41. service.kill()
  42. self.assertEqual(len(service.containers(stopped=True)), 1)
  43. self.command.dispatch(['rm', '--force'], None)
  44. self.assertEqual(len(service.containers(stopped=True)), 0)
  45. def test_scale(self):
  46. project = self.command.project
  47. self.command.scale({'SERVICE=NUM': ['simple=1']})
  48. self.assertEqual(len(project.get_service('simple').containers()), 1)
  49. self.command.scale({'SERVICE=NUM': ['simple=3', 'another=2']})
  50. self.assertEqual(len(project.get_service('simple').containers()), 3)
  51. self.assertEqual(len(project.get_service('another').containers()), 2)
  52. self.command.scale({'SERVICE=NUM': ['simple=1', 'another=1']})
  53. self.assertEqual(len(project.get_service('simple').containers()), 1)
  54. self.assertEqual(len(project.get_service('another').containers()), 1)
  55. self.command.scale({'SERVICE=NUM': ['simple=1', 'another=1']})
  56. self.assertEqual(len(project.get_service('simple').containers()), 1)
  57. self.assertEqual(len(project.get_service('another').containers()), 1)
  58. self.command.scale({'SERVICE=NUM': ['simple=0', 'another=0']})
  59. self.assertEqual(len(project.get_service('simple').containers()), 0)
  60. self.assertEqual(len(project.get_service('another').containers()), 0)