test_cli.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. # systemctl-mqtt - MQTT client triggering & reporting shutdown on systemd-based systems
  2. #
  3. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import typing
  18. import unittest.mock
  19. import pytest
  20. import systemctl_mqtt
  21. import systemctl_mqtt._homeassistant
  22. import systemctl_mqtt._utils
  23. # pylint: disable=protected-access
  24. @pytest.mark.parametrize(
  25. (
  26. "argv",
  27. "expected_mqtt_host",
  28. "expected_mqtt_port",
  29. "expected_username",
  30. "expected_password",
  31. "expected_topic_prefix",
  32. ),
  33. [
  34. (
  35. ["", "--mqtt-host", "mqtt-broker.local"],
  36. "mqtt-broker.local",
  37. 8883,
  38. None,
  39. None,
  40. None,
  41. ),
  42. (
  43. ["", "--mqtt-host", "mqtt-broker.local", "--mqtt-port", "8883"],
  44. "mqtt-broker.local",
  45. 8883,
  46. None,
  47. None,
  48. None,
  49. ),
  50. (
  51. ["", "--mqtt-host", "mqtt-broker.local", "--mqtt-username", "me"],
  52. "mqtt-broker.local",
  53. 8883,
  54. "me",
  55. None,
  56. None,
  57. ),
  58. (
  59. [
  60. "",
  61. "--mqtt-host",
  62. "mqtt-broker.local",
  63. "--mqtt-username",
  64. "me",
  65. "--mqtt-password",
  66. "secret",
  67. ],
  68. "mqtt-broker.local",
  69. 8883,
  70. "me",
  71. "secret",
  72. None,
  73. ),
  74. (
  75. [
  76. "",
  77. "--mqtt-host",
  78. "mqtt-broker.local",
  79. "--mqtt-topic-prefix",
  80. "system/command",
  81. ],
  82. "mqtt-broker.local",
  83. 8883,
  84. None,
  85. None,
  86. "system/command",
  87. ),
  88. ],
  89. )
  90. def test__main(
  91. argv,
  92. expected_mqtt_host,
  93. expected_mqtt_port,
  94. expected_username,
  95. expected_password,
  96. expected_topic_prefix: typing.Optional[str],
  97. ):
  98. # pylint: disable=too-many-arguments
  99. with unittest.mock.patch("systemctl_mqtt._run") as run_mock, unittest.mock.patch(
  100. "sys.argv", argv
  101. ), unittest.mock.patch(
  102. "systemctl_mqtt._utils.get_hostname", return_value="hostname"
  103. ):
  104. # pylint: disable=protected-access
  105. systemctl_mqtt._main()
  106. run_mock.assert_called_once_with(
  107. mqtt_host=expected_mqtt_host,
  108. mqtt_port=expected_mqtt_port,
  109. mqtt_username=expected_username,
  110. mqtt_password=expected_password,
  111. mqtt_topic_prefix=expected_topic_prefix or "systemctl/hostname",
  112. homeassistant_discovery_prefix="homeassistant",
  113. homeassistant_node_id="hostname",
  114. )
  115. @pytest.mark.parametrize(
  116. ("password_file_content", "expected_password",),
  117. [
  118. ("secret", "secret"),
  119. ("secret space", "secret space"),
  120. ("secret ", "secret "),
  121. (" secret ", " secret "),
  122. ("secret\n", "secret"),
  123. ("secret\n\n", "secret\n"),
  124. ("secret\r\n", "secret"),
  125. ("secret\n\r\n", "secret\n"),
  126. ("你好\n", "你好"),
  127. ],
  128. )
  129. def test__main_password_file(tmpdir, password_file_content, expected_password):
  130. mqtt_password_path = tmpdir.join("mqtt-password")
  131. with mqtt_password_path.open("w") as mqtt_password_file:
  132. mqtt_password_file.write(password_file_content)
  133. with unittest.mock.patch("systemctl_mqtt._run") as run_mock, unittest.mock.patch(
  134. "sys.argv",
  135. [
  136. "",
  137. "--mqtt-host",
  138. "localhost",
  139. "--mqtt-username",
  140. "me",
  141. "--mqtt-password-file",
  142. str(mqtt_password_path),
  143. ],
  144. ), unittest.mock.patch(
  145. "systemctl_mqtt._utils.get_hostname", return_value="hostname"
  146. ):
  147. # pylint: disable=protected-access
  148. systemctl_mqtt._main()
  149. run_mock.assert_called_once_with(
  150. mqtt_host="localhost",
  151. mqtt_port=8883,
  152. mqtt_username="me",
  153. mqtt_password=expected_password,
  154. mqtt_topic_prefix="systemctl/hostname",
  155. homeassistant_discovery_prefix="homeassistant",
  156. homeassistant_node_id="hostname",
  157. )
  158. def test__main_password_file_collision(capsys):
  159. with unittest.mock.patch(
  160. "sys.argv",
  161. [
  162. "",
  163. "--mqtt-host",
  164. "localhost",
  165. "--mqtt-username",
  166. "me",
  167. "--mqtt-password",
  168. "secret",
  169. "--mqtt-password-file",
  170. "/var/lib/secrets/mqtt/password",
  171. ],
  172. ):
  173. with pytest.raises(SystemExit):
  174. # pylint: disable=protected-access
  175. systemctl_mqtt._main()
  176. out, err = capsys.readouterr()
  177. assert not out
  178. assert (
  179. "argument --mqtt-password-file: not allowed with argument --mqtt-password\n"
  180. in err
  181. )
  182. @pytest.mark.parametrize(
  183. ("args", "discovery_prefix"),
  184. [
  185. ([], "homeassistant"),
  186. (["--homeassistant-discovery-prefix", "home/assistant"], "home/assistant"),
  187. ],
  188. )
  189. def test__main_homeassistant_discovery_prefix(args, discovery_prefix):
  190. with unittest.mock.patch("systemctl_mqtt._run") as run_mock, unittest.mock.patch(
  191. "sys.argv", ["", "--mqtt-host", "mqtt-broker.local"] + args
  192. ):
  193. systemctl_mqtt._main()
  194. assert run_mock.call_count == 1
  195. assert run_mock.call_args[1]["homeassistant_discovery_prefix"] == discovery_prefix
  196. @pytest.mark.parametrize(
  197. ("args", "node_id"),
  198. [([], "fallback"), (["--homeassistant-node-id", "raspberrypi"], "raspberrypi"),],
  199. )
  200. def test__main_homeassistant_node_id(args, node_id):
  201. with unittest.mock.patch("systemctl_mqtt._run") as run_mock, unittest.mock.patch(
  202. "sys.argv", ["", "--mqtt-host", "mqtt-broker.local"] + args
  203. ), unittest.mock.patch(
  204. "systemctl_mqtt._utils.get_hostname", return_value="fallback",
  205. ):
  206. systemctl_mqtt._main()
  207. assert run_mock.call_count == 1
  208. assert run_mock.call_args[1]["homeassistant_node_id"] == node_id
  209. @pytest.mark.parametrize(
  210. "args", [["--homeassistant-node-id", "no pe"], ["--homeassistant-node-id", ""]],
  211. )
  212. def test__main_homeassistant_node_id_invalid(args):
  213. with unittest.mock.patch(
  214. "sys.argv", ["", "--mqtt-host", "mqtt-broker.local"] + args
  215. ):
  216. with pytest.raises(ValueError):
  217. systemctl_mqtt._main()