test_cli.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import pathlib
  2. import subprocess
  3. from unittest.mock import patch
  4. import pytest
  5. import yaml
  6. # pylint: disable=protected-access
  7. import tooncher._cli
  8. def test_cli_help():
  9. proc_info = subprocess.run(
  10. ["tooncher", "--help"],
  11. check=True,
  12. stdout=subprocess.PIPE,
  13. stderr=subprocess.PIPE,
  14. )
  15. assert b"optional arguments:" in proc_info.stdout
  16. assert not proc_info.stderr
  17. @patch("tooncher._cli.run")
  18. @patch("os.environ", {})
  19. def test_engine_path_arg(run_mock):
  20. with patch("sys.argv", ["", "--engine-path", "/opt/ttr/TTREngine", "username"]):
  21. tooncher._cli.main()
  22. run_mock.assert_called_once()
  23. args, kwargs = run_mock.call_args
  24. assert not args
  25. assert kwargs["engine_path"] == "/opt/ttr/TTREngine"
  26. @patch("tooncher._cli.run")
  27. @patch("os.environ", {"TOONCHER_ENGINE_PATH": "/opt/ttr/TTREnvine"})
  28. def test_engine_path_env(run_mock):
  29. with patch("sys.argv", ["", "username"]):
  30. tooncher._cli.main()
  31. run_mock.assert_called_once()
  32. args, kwargs = run_mock.call_args
  33. assert not args
  34. assert kwargs["engine_path"] == "/opt/ttr/TTREnvine"
  35. @patch("tooncher._cli.run")
  36. @patch("os.environ", {"TOONCHER_ENGINE_PATH": "/opt/ttr/TTREnvine"})
  37. def test_engine_path_arg_env(run_mock):
  38. with patch("sys.argv", ["", "--engine-path", "/opt/ttr/TTREngine", "username"]):
  39. tooncher._cli.main()
  40. run_mock.assert_called_once()
  41. args, kwargs = run_mock.call_args
  42. assert not args
  43. assert kwargs["engine_path"] == "/opt/ttr/TTREngine"
  44. @patch("tooncher.launch")
  45. @patch("os.environ", {})
  46. def test_engine_path_config(launch_mock, tmpdir):
  47. config_file = tmpdir.join("config")
  48. config_file.write(
  49. yaml.safe_dump(
  50. {
  51. "engine_path": "/opt/conf/TTR",
  52. "accounts": [{"username": "someone", "password": "secret"}],
  53. }
  54. )
  55. )
  56. with patch("sys.argv", ["", "--config", config_file.strpath, "someone"]):
  57. tooncher._cli.main()
  58. launch_mock.assert_called_once()
  59. args, kwargs = launch_mock.call_args
  60. assert not args
  61. assert kwargs["engine_path"] == pathlib.Path("/opt/conf/TTR")
  62. @patch("tooncher.launch")
  63. @patch("os.environ", {"TOONCHER_ENGINE_PATH": "/opt/ttr/TTREnvine"})
  64. def test_engine_path_env_config(launch_mock, tmpdir):
  65. config_file = tmpdir.join("config")
  66. config_file.write(
  67. yaml.safe_dump(
  68. {
  69. "engine_path": "/opt/conf/TTR",
  70. "accounts": [{"username": "someone", "password": "secret"}],
  71. }
  72. )
  73. )
  74. with patch("sys.argv", ["", "--config", config_file.strpath, "someone"]):
  75. tooncher._cli.main()
  76. launch_mock.assert_called_once()
  77. args, kwargs = launch_mock.call_args
  78. assert not args
  79. assert kwargs["engine_path"] == pathlib.Path("/opt/ttr/TTREnvine")
  80. @patch("tooncher.launch")
  81. def test_account(launch_mock, tmpdir):
  82. config_file = tmpdir.join("config")
  83. config_file.write(
  84. yaml.safe_dump(
  85. {
  86. "engine_path": "/opt/conf/TTR",
  87. "accounts": [
  88. {"username": "someone", "password": "secret"},
  89. {"username": "toon", "password": "town"},
  90. ],
  91. }
  92. )
  93. )
  94. with patch("sys.argv", ["", "--config", config_file.strpath, "toon"]):
  95. tooncher._cli.main()
  96. launch_mock.assert_called_once_with(
  97. engine_path=pathlib.Path("/opt/conf/TTR"),
  98. username="toon",
  99. password="town",
  100. validate_ssl_certs=True,
  101. cpu_limit_percent=None,
  102. )
  103. def test_account_duplicate_username(tmpdir):
  104. config_file = tmpdir.join("config")
  105. config_file.write(
  106. yaml.safe_dump(
  107. {
  108. "engine_path": "/opt/conf/TTR",
  109. "accounts": [
  110. {"username": "someone", "password": "secret"},
  111. {"username": "toon", "password": "town"},
  112. {"username": "toon", "password": "town2"},
  113. ],
  114. }
  115. )
  116. )
  117. with patch("sys.argv", ["", "--config", config_file.strpath, "toon"]):
  118. with pytest.raises(ValueError, match=r"multiple .* username"):
  119. tooncher._cli.main()
  120. def test_account_unknown_username(tmpdir):
  121. config_file = tmpdir.join("config")
  122. config_file.write(
  123. yaml.safe_dump(
  124. {
  125. "engine_path": "/opt/conf/TTR",
  126. "accounts": [
  127. {"username": "someone", "password": "secret"},
  128. {"username": "toon", "password": "town"},
  129. ],
  130. }
  131. )
  132. )
  133. with patch("sys.argv", ["", "--config", config_file.strpath, "player"]):
  134. with pytest.raises(ValueError, match=r"not found"):
  135. tooncher._cli.main()