cli_test.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_up(self):
  39. self.command.dispatch(['up', '-d'], None)
  40. service = self.command.project.get_service('simple')
  41. another = self.command.project.get_service('another')
  42. self.assertEqual(len(service.containers()), 1)
  43. self.assertEqual(len(another.containers()), 0)
  44. def test_up_with_links(self):
  45. self.command.base_dir = 'tests/fixtures/links-figfile'
  46. self.command.dispatch([str('up'), str('-d'), str('web')], None)
  47. web = self.command.project.get_service('web')
  48. db = self.command.project.get_service('db')
  49. console = self.command.project.get_service('console')
  50. self.assertEqual(len(web.containers()), 1)
  51. self.assertEqual(len(db.containers()), 1)
  52. self.assertEqual(len(console.containers()), 0)
  53. def test_up_with_no_links(self):
  54. self.command.base_dir = 'tests/fixtures/links-figfile'
  55. self.command.dispatch([str('up'), str('-d'), str('--no-links'), str('web')], None)
  56. web = self.command.project.get_service('web')
  57. db = self.command.project.get_service('db')
  58. console = self.command.project.get_service('console')
  59. self.assertEqual(len(web.containers()), 1)
  60. self.assertEqual(len(db.containers()), 0)
  61. self.assertEqual(len(console.containers()), 0)
  62. def test_rm(self):
  63. service = self.command.project.get_service('simple')
  64. service.create_container()
  65. service.kill()
  66. self.assertEqual(len(service.containers(stopped=True)), 1)
  67. self.command.dispatch(['rm', '--force'], None)
  68. self.assertEqual(len(service.containers(stopped=True)), 0)
  69. def test_scale(self):
  70. project = self.command.project
  71. self.command.scale({'SERVICE=NUM': ['simple=1']})
  72. self.assertEqual(len(project.get_service('simple').containers()), 1)
  73. self.command.scale({'SERVICE=NUM': ['simple=3', 'another=2']})
  74. self.assertEqual(len(project.get_service('simple').containers()), 3)
  75. self.assertEqual(len(project.get_service('another').containers()), 2)
  76. self.command.scale({'SERVICE=NUM': ['simple=1', 'another=1']})
  77. self.assertEqual(len(project.get_service('simple').containers()), 1)
  78. self.assertEqual(len(project.get_service('another').containers()), 1)
  79. self.command.scale({'SERVICE=NUM': ['simple=1', 'another=1']})
  80. self.assertEqual(len(project.get_service('simple').containers()), 1)
  81. self.assertEqual(len(project.get_service('another').containers()), 1)
  82. self.command.scale({'SERVICE=NUM': ['simple=0', 'another=0']})
  83. self.assertEqual(len(project.get_service('simple').containers()), 0)
  84. self.assertEqual(len(project.get_service('another').containers()), 0)