test_cli.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import typing
  2. import unittest.mock
  3. import pytest
  4. import systemctl_mqtt
  5. @pytest.mark.parametrize(
  6. (
  7. "argv",
  8. "expected_mqtt_host",
  9. "expected_mqtt_port",
  10. "expected_username",
  11. "expected_password",
  12. "expected_topic_prefix",
  13. ),
  14. [
  15. (
  16. ["", "--mqtt-host", "mqtt-broker.local"],
  17. "mqtt-broker.local",
  18. 1883,
  19. None,
  20. None,
  21. None,
  22. ),
  23. (
  24. ["", "--mqtt-host", "mqtt-broker.local", "--mqtt-port", "8883"],
  25. "mqtt-broker.local",
  26. 8883,
  27. None,
  28. None,
  29. None,
  30. ),
  31. (
  32. ["", "--mqtt-host", "mqtt-broker.local", "--mqtt-username", "me"],
  33. "mqtt-broker.local",
  34. 1883,
  35. "me",
  36. None,
  37. None,
  38. ),
  39. (
  40. [
  41. "",
  42. "--mqtt-host",
  43. "mqtt-broker.local",
  44. "--mqtt-username",
  45. "me",
  46. "--mqtt-password",
  47. "secret",
  48. ],
  49. "mqtt-broker.local",
  50. 1883,
  51. "me",
  52. "secret",
  53. None,
  54. ),
  55. (
  56. [
  57. "",
  58. "--mqtt-host",
  59. "mqtt-broker.local",
  60. "--mqtt-topic-prefix",
  61. "system/command",
  62. ],
  63. "mqtt-broker.local",
  64. 1883,
  65. None,
  66. None,
  67. "system/command",
  68. ),
  69. ],
  70. )
  71. def test__main(
  72. argv,
  73. expected_mqtt_host,
  74. expected_mqtt_port,
  75. expected_username,
  76. expected_password,
  77. expected_topic_prefix: typing.Optional[str],
  78. ):
  79. # pylint: disable=too-many-arguments
  80. with unittest.mock.patch("systemctl_mqtt._run") as run_mock, unittest.mock.patch(
  81. "sys.argv", argv
  82. ), unittest.mock.patch("systemctl_mqtt._get_hostname", return_value="hostname"):
  83. # pylint: disable=protected-access
  84. systemctl_mqtt._main()
  85. run_mock.assert_called_once_with(
  86. mqtt_host=expected_mqtt_host,
  87. mqtt_port=expected_mqtt_port,
  88. mqtt_username=expected_username,
  89. mqtt_password=expected_password,
  90. mqtt_topic_prefix=expected_topic_prefix or "systemctl/hostname",
  91. )
  92. @pytest.mark.parametrize(
  93. ("password_file_content", "expected_password",),
  94. [
  95. ("secret", "secret"),
  96. ("secret space", "secret space"),
  97. ("secret ", "secret "),
  98. (" secret ", " secret "),
  99. ("secret\n", "secret"),
  100. ("secret\n\n", "secret\n"),
  101. ("secret\r\n", "secret"),
  102. ("secret\n\r\n", "secret\n"),
  103. ("你好\n", "你好"),
  104. ],
  105. )
  106. def test__main_password_file(tmpdir, password_file_content, expected_password):
  107. mqtt_password_path = tmpdir.join("mqtt-password")
  108. with mqtt_password_path.open("w") as mqtt_password_file:
  109. mqtt_password_file.write(password_file_content)
  110. with unittest.mock.patch("systemctl_mqtt._run") as run_mock, unittest.mock.patch(
  111. "sys.argv",
  112. [
  113. "",
  114. "--mqtt-host",
  115. "localhost",
  116. "--mqtt-username",
  117. "me",
  118. "--mqtt-password-file",
  119. str(mqtt_password_path),
  120. ],
  121. ), unittest.mock.patch("systemctl_mqtt._get_hostname", return_value="hostname"):
  122. # pylint: disable=protected-access
  123. systemctl_mqtt._main()
  124. run_mock.assert_called_once_with(
  125. mqtt_host="localhost",
  126. mqtt_port=1883,
  127. mqtt_username="me",
  128. mqtt_password=expected_password,
  129. mqtt_topic_prefix="systemctl/hostname",
  130. )
  131. def test__main_password_file_collision(capsys):
  132. with unittest.mock.patch(
  133. "sys.argv",
  134. [
  135. "",
  136. "--mqtt-host",
  137. "localhost",
  138. "--mqtt-username",
  139. "me",
  140. "--mqtt-password",
  141. "secret",
  142. "--mqtt-password-file",
  143. "/var/lib/secrets/mqtt/password",
  144. ],
  145. ):
  146. with pytest.raises(SystemExit):
  147. # pylint: disable=protected-access
  148. systemctl_mqtt._main()
  149. out, err = capsys.readouterr()
  150. assert not out
  151. assert (
  152. "argument --mqtt-password-file: not allowed with argument --mqtt-password\n"
  153. in err
  154. )
  155. @pytest.mark.parametrize("hostname", ["test"])
  156. def test__get_hostname(hostname):
  157. with unittest.mock.patch("socket.gethostname", return_value=hostname):
  158. # pylint: disable=protected-access
  159. assert systemctl_mqtt._get_hostname() == hostname