cli_test.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. def test_up_with_recreate(self):
  67. self.command.dispatch(['up', '-d'], None)
  68. service = self.command.project.get_service('simple')
  69. self.assertEqual(len(service.containers()), 1)
  70. old_ids = [c.id for c in service.containers()]
  71. self.command.dispatch(['up', '-d'], None)
  72. self.assertEqual(len(service.containers()), 1)
  73. new_ids = [c.id for c in service.containers()]
  74. self.assertNotEqual(old_ids, new_ids)
  75. def test_up_with_keep_old(self):
  76. self.command.dispatch(['up', '-d'], None)
  77. service = self.command.project.get_service('simple')
  78. self.assertEqual(len(service.containers()), 1)
  79. old_ids = [c.id for c in service.containers()]
  80. self.command.dispatch([str('up'), str('-d'), str('--keep-old')], None)
  81. self.assertEqual(len(service.containers()), 1)
  82. new_ids = [c.id for c in service.containers()]
  83. self.assertEqual(old_ids, new_ids)
  84. @patch('sys.stdout', new_callable=StringIO)
  85. def test_run_with_links(self, mock_stdout):
  86. mock_stdout.fileno = lambda: 1
  87. self.command.base_dir = 'tests/fixtures/links-figfile'
  88. self.command.dispatch([str('run'), str('web'), str('/bin/true')], None)
  89. db = self.command.project.get_service('db')
  90. console = self.command.project.get_service('console')
  91. self.assertEqual(len(db.containers()), 1)
  92. self.assertEqual(len(console.containers()), 0)
  93. @patch('sys.stdout', new_callable=StringIO)
  94. def test_run_with_no_links(self, mock_stdout):
  95. mock_stdout.fileno = lambda: 1
  96. self.command.base_dir = 'tests/fixtures/links-figfile'
  97. self.command.dispatch([str('run'), str('--no-links'), str('web'), str('/bin/true')], None)
  98. db = self.command.project.get_service('db')
  99. self.assertEqual(len(db.containers()), 0)
  100. @patch('sys.stdout', new_callable=StringIO)
  101. def test_run_does_not_recreate_linked_containers(self, mock_stdout):
  102. mock_stdout.fileno = lambda: 1
  103. self.command.base_dir = 'tests/fixtures/links-figfile'
  104. self.command.dispatch([str('up'), str('-d'), str('db')], None)
  105. db = self.command.project.get_service('db')
  106. self.assertEqual(len(db.containers()), 1)
  107. old_ids = [c.id for c in db.containers()]
  108. self.command.dispatch([str('run'), str('web'), str('/bin/true')], None)
  109. self.assertEqual(len(db.containers()), 1)
  110. new_ids = [c.id for c in db.containers()]
  111. self.assertEqual(old_ids, new_ids)
  112. def test_rm(self):
  113. service = self.command.project.get_service('simple')
  114. service.create_container()
  115. service.kill()
  116. self.assertEqual(len(service.containers(stopped=True)), 1)
  117. self.command.dispatch(['rm', '--force'], None)
  118. self.assertEqual(len(service.containers(stopped=True)), 0)
  119. def test_scale(self):
  120. project = self.command.project
  121. self.command.scale({'SERVICE=NUM': ['simple=1']})
  122. self.assertEqual(len(project.get_service('simple').containers()), 1)
  123. self.command.scale({'SERVICE=NUM': ['simple=3', 'another=2']})
  124. self.assertEqual(len(project.get_service('simple').containers()), 3)
  125. self.assertEqual(len(project.get_service('another').containers()), 2)
  126. self.command.scale({'SERVICE=NUM': ['simple=1', 'another=1']})
  127. self.assertEqual(len(project.get_service('simple').containers()), 1)
  128. self.assertEqual(len(project.get_service('another').containers()), 1)
  129. self.command.scale({'SERVICE=NUM': ['simple=1', 'another=1']})
  130. self.assertEqual(len(project.get_service('simple').containers()), 1)
  131. self.assertEqual(len(project.get_service('another').containers()), 1)
  132. self.command.scale({'SERVICE=NUM': ['simple=0', 'another=0']})
  133. self.assertEqual(len(project.get_service('simple').containers()), 0)
  134. self.assertEqual(len(project.get_service('another').containers()), 0)