test_pushover.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. from flexmock import flexmock
  2. import borgmatic.hooks.monitor
  3. from borgmatic.hooks import pushover as module
  4. def test_ping_monitor_config_with_minimum_config_fail_state_backup_successfully_send_to_pushover():
  5. # This test should be the minimum working configuration. The "message"
  6. # should be auto populated with the default value which is the state name.
  7. hook_config = {'token': 'ksdjfwoweijfvwoeifvjmwghagy92', 'user': '983hfe0of902lkjfa2amanfgui'}
  8. flexmock(module.logger).should_receive('warning').never()
  9. flexmock(module.requests).should_receive('post').with_args(
  10. 'https://api.pushover.net/1/messages.json',
  11. headers={'Content-type': 'application/x-www-form-urlencoded'},
  12. data={
  13. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  14. 'user': '983hfe0of902lkjfa2amanfgui',
  15. 'message': 'fail',
  16. },
  17. ).and_return(flexmock(ok=True)).once()
  18. module.ping_monitor(
  19. hook_config,
  20. {},
  21. 'config.yaml',
  22. borgmatic.hooks.monitor.State.FAIL,
  23. monitoring_log_level=1,
  24. dry_run=False,
  25. )
  26. def test_ping_monitor_config_with_minimum_config_start_state_backup_not_send_to_pushover_exit_early():
  27. # This test should exit early since the hook config does not specify the
  28. # 'start' state. Only the 'fail' state is enabled by default.
  29. hook_config = {'token': 'ksdjfwoweijfvwoeifvjmwghagy92', 'user': '983hfe0of902lkjfa2amanfgui'}
  30. flexmock(module.logger).should_receive('warning').never()
  31. flexmock(module.requests).should_receive('post').never()
  32. module.ping_monitor(
  33. hook_config,
  34. {},
  35. 'config.yaml',
  36. borgmatic.hooks.monitor.State.START,
  37. monitoring_log_level=1,
  38. dry_run=False,
  39. )
  40. def test_ping_monitor_start_state_backup_default_message_successfully_send_to_pushover():
  41. # This test should send a notification to Pushover on backup start
  42. # since the state has been configured. It should default to sending
  43. # the name of the state as the 'message' since it is not
  44. # explicitly declared in the state config.
  45. hook_config = {
  46. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  47. 'user': '983hfe0of902lkjfa2amanfgui',
  48. 'states': {'start', 'fail', 'finish'},
  49. }
  50. flexmock(module.logger).should_receive('warning').never()
  51. flexmock(module.requests).should_receive('post').with_args(
  52. 'https://api.pushover.net/1/messages.json',
  53. headers={'Content-type': 'application/x-www-form-urlencoded'},
  54. data={
  55. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  56. 'user': '983hfe0of902lkjfa2amanfgui',
  57. 'message': 'start',
  58. },
  59. ).and_return(flexmock(ok=True)).once()
  60. module.ping_monitor(
  61. hook_config,
  62. {},
  63. 'config.yaml',
  64. borgmatic.hooks.monitor.State.START,
  65. monitoring_log_level=1,
  66. dry_run=False,
  67. )
  68. def test_ping_monitor_start_state_backup_custom_message_successfully_send_to_pushover():
  69. # This test should send a notification to Pushover on backup start
  70. # since the state has been configured. It should send a custom
  71. # 'message' since it is explicitly declared in the state config.
  72. hook_config = {
  73. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  74. 'user': '983hfe0of902lkjfa2amanfgui',
  75. 'states': {'start', 'fail', 'finish'},
  76. 'start': {'message': 'custom start message'},
  77. }
  78. flexmock(module.logger).should_receive('warning').never()
  79. flexmock(module.requests).should_receive('post').with_args(
  80. 'https://api.pushover.net/1/messages.json',
  81. headers={'Content-type': 'application/x-www-form-urlencoded'},
  82. data={
  83. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  84. 'user': '983hfe0of902lkjfa2amanfgui',
  85. 'message': 'custom start message',
  86. },
  87. ).and_return(flexmock(ok=True)).once()
  88. module.ping_monitor(
  89. hook_config,
  90. {},
  91. 'config.yaml',
  92. borgmatic.hooks.monitor.State.START,
  93. monitoring_log_level=1,
  94. dry_run=False,
  95. )
  96. def test_ping_monitor_start_state_backup_default_message_with_priority_emergency_declared_no_expiry_or_retry_success():
  97. # This simulates priority level 2 being set but expiry and retry are
  98. # not declared. This should set retry and expiry to their defaults.
  99. hook_config = {
  100. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  101. 'user': '983hfe0of902lkjfa2amanfgui',
  102. 'states': {'start', 'fail', 'finish'},
  103. 'start': {'priority': 2},
  104. }
  105. flexmock(module.logger).should_receive('warning').never()
  106. flexmock(module.requests).should_receive('post').with_args(
  107. 'https://api.pushover.net/1/messages.json',
  108. headers={'Content-type': 'application/x-www-form-urlencoded'},
  109. data={
  110. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  111. 'user': '983hfe0of902lkjfa2amanfgui',
  112. 'message': 'start',
  113. 'priority': 2,
  114. 'retry': 30,
  115. 'expire': 1200,
  116. },
  117. ).and_return(flexmock(ok=True)).once()
  118. module.ping_monitor(
  119. hook_config,
  120. {},
  121. 'config.yaml',
  122. borgmatic.hooks.monitor.State.START,
  123. monitoring_log_level=1,
  124. dry_run=False,
  125. )
  126. def test_ping_monitor_start_state_backup_default_message_with_priority_emergency_declared_with_expire_no_retry_success():
  127. # This simulates priority level 2 and expiry being set but retry is
  128. # not declared. This should set retry to the default.
  129. hook_config = {
  130. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  131. 'user': '983hfe0of902lkjfa2amanfgui',
  132. 'states': {'start', 'fail', 'finish'},
  133. 'start': {'priority': 2, 'expire': 600},
  134. }
  135. flexmock(module.logger).should_receive('warning').never()
  136. flexmock(module.requests).should_receive('post').with_args(
  137. 'https://api.pushover.net/1/messages.json',
  138. headers={'Content-type': 'application/x-www-form-urlencoded'},
  139. data={
  140. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  141. 'user': '983hfe0of902lkjfa2amanfgui',
  142. 'message': 'start',
  143. 'priority': 2,
  144. 'retry': 30,
  145. 'expire': 600,
  146. },
  147. ).and_return(flexmock(ok=True)).once()
  148. module.ping_monitor(
  149. hook_config,
  150. {},
  151. 'config.yaml',
  152. borgmatic.hooks.monitor.State.START,
  153. monitoring_log_level=1,
  154. dry_run=False,
  155. )
  156. def test_ping_monitor_start_state_backup_default_message_with_priority_emergency_declared_no_expire_with_retry_success():
  157. # This simulates priority level 2 and retry being set but expire is
  158. # not declared. This should set expire to the default.
  159. hook_config = {
  160. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  161. 'user': '983hfe0of902lkjfa2amanfgui',
  162. 'states': {'start', 'fail', 'finish'},
  163. 'start': {'priority': 2, 'expire': 30},
  164. }
  165. flexmock(module.logger).should_receive('warning').never()
  166. flexmock(module.requests).should_receive('post').with_args(
  167. 'https://api.pushover.net/1/messages.json',
  168. headers={'Content-type': 'application/x-www-form-urlencoded'},
  169. data={
  170. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  171. 'user': '983hfe0of902lkjfa2amanfgui',
  172. 'message': 'start',
  173. 'priority': 2,
  174. 'retry': 30,
  175. 'expire': 30,
  176. },
  177. ).and_return(flexmock(ok=True)).once()
  178. module.ping_monitor(
  179. hook_config,
  180. {},
  181. 'config.yaml',
  182. borgmatic.hooks.monitor.State.START,
  183. monitoring_log_level=1,
  184. dry_run=False,
  185. )
  186. def test_ping_monitor_start_state_backup_default_message_with_priority_high_declared_expire_and_retry_delared_success():
  187. # This simulates priority level 1, retry and expiry being set. Since expire
  188. # and retry are only used for priority level 2, they should not be included
  189. # in the request sent to Pushover. This test verifies that those are
  190. # stripped from the request.
  191. hook_config = {
  192. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  193. 'user': '983hfe0of902lkjfa2amanfgui',
  194. 'states': {'start', 'fail', 'finish'},
  195. 'start': {'priority': 1, 'expire': 30, 'retry': 30},
  196. }
  197. flexmock(module.logger).should_receive('warning').never()
  198. flexmock(module.requests).should_receive('post').with_args(
  199. 'https://api.pushover.net/1/messages.json',
  200. headers={'Content-type': 'application/x-www-form-urlencoded'},
  201. data={
  202. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  203. 'user': '983hfe0of902lkjfa2amanfgui',
  204. 'message': 'start',
  205. 'priority': 1,
  206. },
  207. ).and_return(flexmock(ok=True)).once()
  208. module.ping_monitor(
  209. hook_config,
  210. {},
  211. 'config.yaml',
  212. borgmatic.hooks.monitor.State.START,
  213. monitoring_log_level=1,
  214. dry_run=False,
  215. )
  216. def test_ping_monitor_start_state_backup_based_on_documentation_advanced_example_success():
  217. # Here is a test of what is provided in the monitor-your-backups.md file
  218. # as an 'advanced example'. This test runs the start state.
  219. hook_config = {
  220. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  221. 'user': '983hfe0of902lkjfa2amanfgui',
  222. 'states': {'start', 'fail', 'finish'},
  223. 'start': {
  224. 'message': 'Backup <b>Started</b>',
  225. 'priority': -2,
  226. 'title': 'Backup Started',
  227. 'html': 1,
  228. 'ttl': 10,
  229. },
  230. 'fail': {
  231. 'message': 'Backup <font color="#ff6961">Failed</font>',
  232. 'priority': 2,
  233. 'expire': 1200,
  234. 'retry': 30,
  235. 'device': 'pixel8',
  236. 'title': 'Backup Failed',
  237. 'html': 1,
  238. 'sound': 'siren',
  239. 'url': 'https://ticketing-system.example.com/login',
  240. 'url_title': 'Login to ticketing system',
  241. },
  242. 'finish': {
  243. 'message': 'Backup <font color="#77dd77">Finished</font>',
  244. 'priority': 0,
  245. 'title': 'Backup Finished',
  246. 'html': 1,
  247. 'ttl': 60,
  248. 'url': 'https://ticketing-system.example.com/login',
  249. 'url_title': 'Login to ticketing system',
  250. },
  251. }
  252. flexmock(module.logger).should_receive('warning').never()
  253. flexmock(module.requests).should_receive('post').with_args(
  254. 'https://api.pushover.net/1/messages.json',
  255. headers={'Content-type': 'application/x-www-form-urlencoded'},
  256. data={
  257. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  258. 'user': '983hfe0of902lkjfa2amanfgui',
  259. 'message': 'Backup <b>Started</b>',
  260. 'priority': -2,
  261. 'title': 'Backup Started',
  262. 'html': 1,
  263. 'ttl': 10,
  264. },
  265. ).and_return(flexmock(ok=True)).once()
  266. module.ping_monitor(
  267. hook_config,
  268. {},
  269. 'config.yaml',
  270. borgmatic.hooks.monitor.State.START,
  271. monitoring_log_level=1,
  272. dry_run=False,
  273. )
  274. def test_ping_monitor_fail_state_backup_based_on_documentation_advanced_example_success():
  275. # Here is a test of what is provided in the monitor-your-backups.md file
  276. # as an 'advanced example'. This test runs the fail state.
  277. hook_config = {
  278. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  279. 'user': '983hfe0of902lkjfa2amanfgui',
  280. 'states': {'start', 'fail', 'finish'},
  281. 'start': {
  282. 'message': 'Backup <b>Started</b>',
  283. 'priority': -2,
  284. 'title': 'Backup Started',
  285. 'html': 1,
  286. 'ttl': 10,
  287. },
  288. 'fail': {
  289. 'message': 'Backup <font color="#ff6961">Failed</font>',
  290. 'priority': 2,
  291. 'expire': 1200,
  292. 'retry': 30,
  293. 'device': 'pixel8',
  294. 'title': 'Backup Failed',
  295. 'html': 1,
  296. 'sound': 'siren',
  297. 'url': 'https://ticketing-system.example.com/login',
  298. 'url_title': 'Login to ticketing system',
  299. },
  300. 'finish': {
  301. 'message': 'Backup <font color="#77dd77">Finished</font>',
  302. 'priority': 0,
  303. 'title': 'Backup Finished',
  304. 'html': 1,
  305. 'ttl': 60,
  306. 'url': 'https://ticketing-system.example.com/login',
  307. 'url_title': 'Login to ticketing system',
  308. },
  309. }
  310. flexmock(module.logger).should_receive('warning').never()
  311. flexmock(module.requests).should_receive('post').with_args(
  312. 'https://api.pushover.net/1/messages.json',
  313. headers={'Content-type': 'application/x-www-form-urlencoded'},
  314. data={
  315. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  316. 'user': '983hfe0of902lkjfa2amanfgui',
  317. 'message': 'Backup <font color="#ff6961">Failed</font>',
  318. 'priority': 2,
  319. 'expire': 1200,
  320. 'retry': 30,
  321. 'device': 'pixel8',
  322. 'title': 'Backup Failed',
  323. 'html': 1,
  324. 'sound': 'siren',
  325. 'url': 'https://ticketing-system.example.com/login',
  326. 'url_title': 'Login to ticketing system',
  327. },
  328. ).and_return(flexmock(ok=True)).once()
  329. module.ping_monitor(
  330. hook_config,
  331. {},
  332. 'config.yaml',
  333. borgmatic.hooks.monitor.State.FAIL,
  334. monitoring_log_level=1,
  335. dry_run=False,
  336. )
  337. def test_ping_monitor_finish_state_backup_based_on_documentation_advanced_example_success():
  338. # Here is a test of what is provided in the monitor-your-backups.md file
  339. # as an 'advanced example'. This test runs the finish state.
  340. hook_config = {
  341. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  342. 'user': '983hfe0of902lkjfa2amanfgui',
  343. 'states': {'start', 'fail', 'finish'},
  344. 'start': {
  345. 'message': 'Backup <b>Started</b>',
  346. 'priority': -2,
  347. 'title': 'Backup Started',
  348. 'html': 1,
  349. 'ttl': 10,
  350. },
  351. 'fail': {
  352. 'message': 'Backup <font color="#ff6961">Failed</font>',
  353. 'priority': 2,
  354. 'expire': 1200,
  355. 'retry': 30,
  356. 'device': 'pixel8',
  357. 'title': 'Backup Failed',
  358. 'html': 1,
  359. 'sound': 'siren',
  360. 'url': 'https://ticketing-system.example.com/login',
  361. 'url_title': 'Login to ticketing system',
  362. },
  363. 'finish': {
  364. 'message': 'Backup <font color="#77dd77">Finished</font>',
  365. 'priority': 0,
  366. 'title': 'Backup Finished',
  367. 'html': 1,
  368. 'ttl': 60,
  369. 'url': 'https://ticketing-system.example.com/login',
  370. 'url_title': 'Login to ticketing system',
  371. },
  372. }
  373. flexmock(module.logger).should_receive('warning').never()
  374. flexmock(module.requests).should_receive('post').with_args(
  375. 'https://api.pushover.net/1/messages.json',
  376. headers={'Content-type': 'application/x-www-form-urlencoded'},
  377. data={
  378. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  379. 'user': '983hfe0of902lkjfa2amanfgui',
  380. 'message': 'Backup <font color="#77dd77">Finished</font>',
  381. 'priority': 0,
  382. 'title': 'Backup Finished',
  383. 'html': 1,
  384. 'ttl': 60,
  385. 'url': 'https://ticketing-system.example.com/login',
  386. 'url_title': 'Login to ticketing system',
  387. },
  388. ).and_return(flexmock(ok=True)).once()
  389. module.ping_monitor(
  390. hook_config,
  391. {},
  392. 'config.yaml',
  393. borgmatic.hooks.monitor.State.FINISH,
  394. monitoring_log_level=1,
  395. dry_run=False,
  396. )