cli_test.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from __future__ import unicode_literals
  2. from __future__ import absolute_import
  3. from .testcases import DockerClientTestCase
  4. from mock import patch
  5. from six import StringIO
  6. from fig.cli.main import TopLevelCommand
  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. def test_yaml_filename_check(self):
  16. self.command.base_dir = 'tests/fixtures/longer-filename-figfile'
  17. project = self.command.project
  18. self.assertTrue( project.get_service('definedinyamlnotyml'), "Service: definedinyamlnotyml should have been loaded from .yaml file" )
  19. def test_help(self):
  20. self.assertRaises(SystemExit, lambda: self.command.dispatch(['-h'], None))
  21. @patch('sys.stdout', new_callable=StringIO)
  22. def test_ps(self, mock_stdout):
  23. self.command.project.get_service('simple').create_container()
  24. self.command.dispatch(['ps'], None)
  25. self.assertIn('fig_simple_1', mock_stdout.getvalue())
  26. @patch('sys.stdout', new_callable=StringIO)
  27. def test_default_figfile(self, mock_stdout):
  28. self.command.base_dir = 'tests/fixtures/multiple-figfiles'
  29. self.command.dispatch(['up', '-d'], None)
  30. self.command.dispatch(['ps'], None)
  31. output = mock_stdout.getvalue()
  32. self.assertIn('fig_simple_1', output)
  33. self.assertIn('fig_another_1', output)
  34. self.assertNotIn('fig_yetanother_1', output)
  35. @patch('sys.stdout', new_callable=StringIO)
  36. def test_alternate_figfile(self, mock_stdout):
  37. self.command.base_dir = 'tests/fixtures/multiple-figfiles'
  38. self.command.dispatch(['-f', 'fig2.yml', 'up', '-d'], None)
  39. self.command.dispatch(['-f', 'fig2.yml', 'ps'], None)
  40. output = mock_stdout.getvalue()
  41. self.assertNotIn('fig_simple_1', output)
  42. self.assertNotIn('fig_another_1', output)
  43. self.assertIn('fig_yetanother_1', output)
  44. def test_scale(self):
  45. project = self.command.project
  46. self.command.scale({'SERVICE=NUM': ['simple=1']})
  47. self.assertEqual(len(project.get_service('simple').containers()), 1)
  48. self.command.scale({'SERVICE=NUM': ['simple=3', 'another=2']})
  49. self.assertEqual(len(project.get_service('simple').containers()), 3)
  50. self.assertEqual(len(project.get_service('another').containers()), 2)
  51. self.command.scale({'SERVICE=NUM': ['simple=1', 'another=1']})
  52. self.assertEqual(len(project.get_service('simple').containers()), 1)
  53. self.assertEqual(len(project.get_service('another').containers()), 1)
  54. self.command.scale({'SERVICE=NUM': ['simple=1', 'another=1']})
  55. self.assertEqual(len(project.get_service('simple').containers()), 1)
  56. self.assertEqual(len(project.get_service('another').containers()), 1)
  57. self.command.scale({'SERVICE=NUM': ['simple=0', 'another=0']})
  58. self.assertEqual(len(project.get_service('simple').containers()), 0)
  59. self.assertEqual(len(project.get_service('another').containers()), 0)