test_launcher_base.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. """
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. Unit Tests for base launcher-wrapper: all are sanity code-path tests, since no interprocess actions should be taken
  6. """
  7. import os
  8. import unittest.mock as mock
  9. import pytest
  10. import ly_test_tools.launchers
  11. import ly_test_tools.launchers.launcher_helper
  12. import ly_test_tools._internal.managers.artifact_manager
  13. pytestmark = pytest.mark.SUITE_smoke
  14. class TestBaseLauncher:
  15. def test_Construct_TestDoubles_BaseLauncherCreated(self):
  16. mock_workspace = mock.MagicMock()
  17. mock_project_log_path = 'c:/mock_project/log/'
  18. mock_workspace.paths.project_log.return_value = mock_project_log_path
  19. under_test = ly_test_tools.launchers.Launcher(mock_workspace, ["some_args"])
  20. assert isinstance(under_test, ly_test_tools.launchers.Launcher)
  21. return under_test
  22. def test_Construct_StringArgs_TypeError(self):
  23. mock_workspace = mock.MagicMock()
  24. with pytest.raises(TypeError):
  25. ly_test_tools.launchers.Launcher(mock_workspace, "bad_args")
  26. def test_BinaryPath_Unimplemented_NotImplementedError(self):
  27. launcher = self.test_Construct_TestDoubles_BaseLauncherCreated()
  28. with pytest.raises(NotImplementedError):
  29. launcher.binary_path()
  30. def test_EnsureStopped_IsAliveUnimplemented_NotImplementedError(self):
  31. launcher = self.test_Construct_TestDoubles_BaseLauncherCreated()
  32. with pytest.raises(NotImplementedError):
  33. launcher.ensure_stopped()
  34. def test_IsAlive_Unimplemented_NotImplementedError(self):
  35. launcher = self.test_Construct_TestDoubles_BaseLauncherCreated()
  36. with pytest.raises(NotImplementedError):
  37. launcher.is_alive()
  38. def test_Kill_Unimplemented_NotImplementedError(self):
  39. launcher = self.test_Construct_TestDoubles_BaseLauncherCreated()
  40. with pytest.raises(NotImplementedError):
  41. launcher.stop()
  42. def test_Launch_Unimplemented_NotImplementedError(self):
  43. launcher = self.test_Construct_TestDoubles_BaseLauncherCreated()
  44. with pytest.raises(NotImplementedError):
  45. launcher.launch()
  46. @mock.patch('os.listdir', mock.MagicMock())
  47. @mock.patch('ly_test_tools.environment.waiter.wait_for', mock.MagicMock(return_value=True))
  48. def test_Start_UnimplementedLauncher_NotImplementedError(self):
  49. launcher = self.test_Construct_TestDoubles_BaseLauncherCreated()
  50. with pytest.raises(NotImplementedError):
  51. launcher.start()
  52. @mock.patch('ly_test_tools.launchers.platforms.base.Launcher.launch')
  53. @mock.patch('ly_test_tools.launchers.platforms.base.Launcher.setup')
  54. def test_Start_MockImplementedLauncher_SetupLaunch(self, mock_setup, mock_launch):
  55. launcher = self.test_Construct_TestDoubles_BaseLauncherCreated()
  56. launcher.start()
  57. mock_setup.assert_called_once()
  58. mock_launch.assert_called_once()
  59. @mock.patch('ly_test_tools.launchers.platforms.base.Launcher.launch')
  60. @mock.patch('ly_test_tools.launchers.platforms.base.Launcher.setup')
  61. @mock.patch('ly_test_tools.launchers.platforms.base.Launcher.stop')
  62. def test_WithStart_MockImplementedLauncher_SetupLaunchStop(self, mock_stop, mock_setup, mock_launch):
  63. launcher = self.test_Construct_TestDoubles_BaseLauncherCreated()
  64. with launcher.start():
  65. pass
  66. mock_stop.assert_called_once()
  67. mock_setup.assert_called_once()
  68. mock_launch.assert_called_once()
  69. @mock.patch('ly_test_tools.launchers.platforms.base.Launcher.setup')
  70. @mock.patch('ly_test_tools.launchers.platforms.base.Launcher.stop')
  71. def test_WithStart_ErrorDuringStart_StopNotCalled(self, mock_stop, mock_setup):
  72. mock_setup.side_effect = BufferError
  73. launcher = self.test_Construct_TestDoubles_BaseLauncherCreated()
  74. with pytest.raises(BufferError):
  75. with launcher.start():
  76. pass
  77. mock_stop.assert_not_called()
  78. mock_setup.assert_called_once()
  79. @mock.patch('ly_test_tools.launchers.platforms.base.Launcher.launch', mock.MagicMock)
  80. @mock.patch('ly_test_tools.launchers.platforms.base.Launcher.setup', mock.MagicMock)
  81. @mock.patch('ly_test_tools.launchers.platforms.base.Launcher.stop')
  82. def test_WithStart_ErrorAfterStart_StopCalled(self, mock_stop):
  83. launcher = self.test_Construct_TestDoubles_BaseLauncherCreated()
  84. with pytest.raises(BufferError):
  85. with launcher.start():
  86. raise BufferError
  87. mock_stop.assert_called_once()
  88. def test_Stop_UnimplementedLauncher_NotImplementedError(self):
  89. launcher = self.test_Construct_TestDoubles_BaseLauncherCreated()
  90. with pytest.raises(NotImplementedError):
  91. launcher.stop()
  92. @mock.patch('ly_test_tools.launchers.platforms.base.Launcher._kill')
  93. @mock.patch('ly_test_tools.launchers.platforms.base.Launcher.ensure_stopped')
  94. @mock.patch('ly_test_tools.launchers.platforms.base.Launcher.teardown')
  95. def test_Stop_MockImplementedLauncher_KillTeardown(self, mock_teardown, mock_ensure, mock_kill):
  96. launcher = self.test_Construct_TestDoubles_BaseLauncherCreated()
  97. launcher.stop()
  98. mock_kill.assert_called_once()
  99. mock_teardown.assert_called_once()
  100. mock_ensure.assert_called_once()
  101. @mock.patch('os.listdir', mock.MagicMock())
  102. @mock.patch('ly_test_tools.environment.waiter.wait_for', mock.MagicMock(return_value=True))
  103. def test_Setup_TestDoublesNoAPOpen_StartAssetProcessor(self):
  104. mock_workspace = mock.MagicMock()
  105. mock_start_ap = mock.MagicMock()
  106. mock_workspace.asset_processor.start = mock_start_ap
  107. mock_project_log_path = 'c:/mock_project/log/'
  108. mock_workspace.paths.project_log.return_value = mock_project_log_path
  109. under_test = ly_test_tools.launchers.Launcher(mock_workspace, ["some_args"])
  110. under_test.setup()
  111. under_test.workspace.asset_processor.start.assert_called_once()
  112. @mock.patch('ly_test_tools.launchers.platforms.base.Launcher.save_project_log_files', mock.MagicMock())
  113. def test_Teardown_TestDoubles_StopAppAndStopAssetProcessor(self):
  114. mock_workspace = mock.MagicMock()
  115. mock_stop_ap = mock.MagicMock()
  116. mock_workspace.asset_processor.stop = mock_stop_ap
  117. under_test = ly_test_tools.launchers.Launcher(mock_workspace, ["some_args"])
  118. under_test.teardown()
  119. mock_stop_ap.assert_called_once()
  120. @mock.patch('os.path.exists', mock.MagicMock(return_value=True))
  121. @mock.patch('ly_test_tools._internal.managers.artifact_manager.ArtifactManager.save_artifact')
  122. @mock.patch('os.listdir')
  123. def test_SaveProjectLogFiles_LogFilesExist_SavesOnlyLogs(self, mock_listdir, under_test):
  124. mock_log = 'foo.log'
  125. mock_txt = 'foo.txt'
  126. mock_args = ['foo']
  127. mock_project_log_path = 'c:/mock_project/log/'
  128. assert_path = os.path.join(mock_project_log_path, mock_log)
  129. mock_listdir.return_value = [mock_log, mock_txt]
  130. mock_workspace = mock.MagicMock()
  131. mock_artifact_manager = ly_test_tools._internal.managers.artifact_manager.ArtifactManager(mock.MagicMock())
  132. mock_workspace.artifact_manager = mock_artifact_manager
  133. mock_workspace.paths.project_log.return_value = mock_project_log_path
  134. mock_launcher = ly_test_tools.launchers.Launcher(mock_workspace, mock_args)
  135. mock_launcher.save_project_log_files()
  136. under_test.assert_called_once_with(assert_path, amount=100)
  137. @mock.patch('os.path.exists', mock.MagicMock(return_value=True))
  138. @mock.patch('ly_test_tools._internal.managers.artifact_manager.ArtifactManager.save_artifact')
  139. @mock.patch('os.listdir')
  140. def test_SaveProjectLogFiles_DmpFilesExist_SavesOnlyDmps(self, mock_listdir, under_test):
  141. mock_dmp = 'foo.dmp'
  142. mock_txt = 'foo.txt'
  143. mock_args = ['foo']
  144. mock_project_log_path = 'c:/mock_project/log/'
  145. assert_path = os.path.join(mock_project_log_path, mock_dmp)
  146. mock_listdir.return_value = [mock_dmp, mock_txt]
  147. mock_workspace = mock.MagicMock()
  148. mock_artifact_manager = ly_test_tools._internal.managers.artifact_manager.ArtifactManager(mock.MagicMock())
  149. mock_workspace.artifact_manager = mock_artifact_manager
  150. mock_workspace.paths.project_log.return_value = mock_project_log_path
  151. mock_launcher = ly_test_tools.launchers.Launcher(mock_workspace, mock_args)
  152. mock_launcher.save_project_log_files()
  153. under_test.assert_called_once_with(assert_path, amount=100)
  154. class TestLauncherBuilder(object):
  155. """
  156. Fixture builder/helper in launchers.launcher
  157. """
  158. def test_CreateLauncher_DummyWorkspace_DefaultLauncher(self):
  159. dummy_workspace = mock.MagicMock()
  160. under_test = ly_test_tools.launchers.launcher_helper.create_game_launcher(
  161. dummy_workspace, ly_test_tools.HOST_OS_EDITOR)
  162. assert isinstance(under_test, ly_test_tools.launchers.Launcher)
  163. def test_CreateDedicateLauncher_DummyWorkspace_DefaultLauncher(self):
  164. dummy_workspace = mock.MagicMock()
  165. under_test = ly_test_tools.launchers.launcher_helper.create_server_launcher(
  166. dummy_workspace, ly_test_tools.HOST_OS_DEDICATED_SERVER)
  167. assert isinstance(under_test, ly_test_tools.launchers.Launcher)
  168. @mock.patch('os.path.exists', mock.MagicMock(return_value=True))
  169. def test_CreateEditor_DummyWorkspace_DefaultLauncher(self):
  170. dummy_workspace = mock.MagicMock()
  171. dummy_workspace.paths.build_directory.return_value = 'dummy'
  172. under_test = ly_test_tools.launchers.launcher_helper.create_editor(
  173. dummy_workspace, ly_test_tools.HOST_OS_EDITOR)
  174. assert isinstance(under_test, ly_test_tools.launchers.Launcher)
  175. def test_CreateEditor_InvalidPlatform_ValidLauncherStillReturned(self):
  176. dummy_workspace = mock.MagicMock()
  177. dummy_workspace.paths.build_directory.return_value = 'dummy'
  178. under_test = ly_test_tools.launchers.launcher_helper.create_editor(
  179. dummy_workspace, 'does not exist')
  180. assert isinstance(under_test, ly_test_tools.launchers.Launcher)
  181. def test_CreateAtomTools_DummyWorkspace_DefaultLauncher(self):
  182. dummy_workspace = mock.MagicMock()
  183. dummy_workspace.paths.build_directory.return_value = 'dummy'
  184. dummy_app_file_name = 'dummy_app'
  185. under_test = ly_test_tools.launchers.launcher_helper.create_atom_tools_launcher(
  186. dummy_workspace, dummy_app_file_name)
  187. assert isinstance(under_test, ly_test_tools.launchers.Launcher)
  188. assert under_test.app_file_name == dummy_app_file_name