cli_test.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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('fig_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('fig_simple_1', output)
  30. self.assertIn('fig_another_1', output)
  31. self.assertNotIn('fig_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('fig_simple_1', output)
  39. self.assertNotIn('fig_another_1', output)
  40. self.assertIn('fig_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()), 0)
  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_links(self):
  57. self.command.base_dir = 'tests/fixtures/links-figfile'
  58. self.command.dispatch(['up', '-d', '--only', '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('sys.stdout', new_callable=StringIO)
  84. def test_run_with_links(self, mock_stdout):
  85. mock_stdout.fileno = lambda: 1
  86. self.command.base_dir = 'tests/fixtures/links-figfile'
  87. self.command.dispatch(['run', 'web', '/bin/true'], None)
  88. db = self.command.project.get_service('db')
  89. console = self.command.project.get_service('console')
  90. self.assertEqual(len(db.containers()), 1)
  91. self.assertEqual(len(console.containers()), 0)
  92. @patch('sys.stdout', new_callable=StringIO)
  93. def test_run_with_no_links(self, mock_stdout):
  94. mock_stdout.fileno = lambda: 1
  95. self.command.base_dir = 'tests/fixtures/links-figfile'
  96. self.command.dispatch(['run', '--only', 'web', '/bin/true'], None)
  97. db = self.command.project.get_service('db')
  98. self.assertEqual(len(db.containers()), 0)
  99. @patch('sys.stdout', new_callable=StringIO)
  100. def test_run_does_not_recreate_linked_containers(self, mock_stdout):
  101. mock_stdout.fileno = lambda: 1
  102. self.command.base_dir = 'tests/fixtures/links-figfile'
  103. self.command.dispatch(['up', '-d', 'db'], None)
  104. db = self.command.project.get_service('db')
  105. self.assertEqual(len(db.containers()), 1)
  106. old_ids = [c.id for c in db.containers()]
  107. self.command.dispatch(['run', 'web', '/bin/true'], None)
  108. self.assertEqual(len(db.containers()), 1)
  109. new_ids = [c.id for c in db.containers()]
  110. self.assertEqual(old_ids, new_ids)
  111. def test_rm(self):
  112. service = self.command.project.get_service('simple')
  113. service.create_container()
  114. service.kill()
  115. self.assertEqual(len(service.containers(stopped=True)), 1)
  116. self.command.dispatch(['rm', '--force'], None)
  117. self.assertEqual(len(service.containers(stopped=True)), 0)
  118. def test_scale(self):
  119. project = self.command.project
  120. self.command.scale({'SERVICE=NUM': ['simple=1']})
  121. self.assertEqual(len(project.get_service('simple').containers()), 1)
  122. self.command.scale({'SERVICE=NUM': ['simple=3', 'another=2']})
  123. self.assertEqual(len(project.get_service('simple').containers()), 3)
  124. self.assertEqual(len(project.get_service('another').containers()), 2)
  125. self.command.scale({'SERVICE=NUM': ['simple=1', 'another=1']})
  126. self.assertEqual(len(project.get_service('simple').containers()), 1)
  127. self.assertEqual(len(project.get_service('another').containers()), 1)
  128. self.command.scale({'SERVICE=NUM': ['simple=1', 'another=1']})
  129. self.assertEqual(len(project.get_service('simple').containers()), 1)
  130. self.assertEqual(len(project.get_service('another').containers()), 1)
  131. self.command.scale({'SERVICE=NUM': ['simple=0', 'another=0']})
  132. self.assertEqual(len(project.get_service('simple').containers()), 0)
  133. self.assertEqual(len(project.get_service('another').containers()), 0)