cli_test.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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('simplefigfile_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('multiplefigfiles_simple_1', output)
  30. self.assertIn('multiplefigfiles_another_1', output)
  31. self.assertNotIn('multiplefigfiles_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('multiplefigfiles_simple_1', output)
  39. self.assertNotIn('multiplefigfiles_another_1', output)
  40. self.assertIn('multiplefigfiles_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()), 1)
  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_deps(self):
  57. self.command.base_dir = 'tests/fixtures/links-figfile'
  58. self.command.dispatch(['up', '-d', '--no-deps', '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('dockerpty.start')
  84. def test_run_service_without_links(self, mock_stdout):
  85. self.command.base_dir = 'tests/fixtures/links-figfile'
  86. self.command.dispatch(['run', 'console', '/bin/true'], None)
  87. self.assertEqual(len(self.command.project.containers()), 0)
  88. @patch('dockerpty.start')
  89. def test_run_service_with_links(self, mock_stdout):
  90. self.command.base_dir = 'tests/fixtures/links-figfile'
  91. self.command.dispatch(['run', 'web', '/bin/true'], None)
  92. db = self.command.project.get_service('db')
  93. console = self.command.project.get_service('console')
  94. self.assertEqual(len(db.containers()), 1)
  95. self.assertEqual(len(console.containers()), 0)
  96. @patch('dockerpty.start')
  97. def test_run_with_no_deps(self, mock_stdout):
  98. mock_stdout.fileno = lambda: 1
  99. self.command.base_dir = 'tests/fixtures/links-figfile'
  100. self.command.dispatch(['run', '--no-deps', 'web', '/bin/true'], None)
  101. db = self.command.project.get_service('db')
  102. self.assertEqual(len(db.containers()), 0)
  103. @patch('dockerpty.start')
  104. def test_run_does_not_recreate_linked_containers(self, mock_stdout):
  105. mock_stdout.fileno = lambda: 1
  106. self.command.base_dir = 'tests/fixtures/links-figfile'
  107. self.command.dispatch(['up', '-d', 'db'], None)
  108. db = self.command.project.get_service('db')
  109. self.assertEqual(len(db.containers()), 1)
  110. old_ids = [c.id for c in db.containers()]
  111. self.command.dispatch(['run', 'web', '/bin/true'], None)
  112. self.assertEqual(len(db.containers()), 1)
  113. new_ids = [c.id for c in db.containers()]
  114. self.assertEqual(old_ids, new_ids)
  115. def test_rm(self):
  116. service = self.command.project.get_service('simple')
  117. service.create_container()
  118. service.kill()
  119. self.assertEqual(len(service.containers(stopped=True)), 1)
  120. self.command.dispatch(['rm', '--force'], None)
  121. self.assertEqual(len(service.containers(stopped=True)), 0)
  122. def test_scale(self):
  123. project = self.command.project
  124. self.command.scale({'SERVICE=NUM': ['simple=1']})
  125. self.assertEqual(len(project.get_service('simple').containers()), 1)
  126. self.command.scale({'SERVICE=NUM': ['simple=3', 'another=2']})
  127. self.assertEqual(len(project.get_service('simple').containers()), 3)
  128. self.assertEqual(len(project.get_service('another').containers()), 2)
  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=1', 'another=1']})
  133. self.assertEqual(len(project.get_service('simple').containers()), 1)
  134. self.assertEqual(len(project.get_service('another').containers()), 1)
  135. self.command.scale({'SERVICE=NUM': ['simple=0', 'another=0']})
  136. self.assertEqual(len(project.get_service('simple').containers()), 0)
  137. self.assertEqual(len(project.get_service('another').containers()), 0)