test_pushover.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. import pytest
  2. from flexmock import flexmock
  3. import borgmatic.hooks.monitoring.monitor
  4. from borgmatic.hooks.monitoring import pushover as module
  5. def test_ping_monitor_config_with_minimum_config_fail_state_backup_successfully_send_to_pushover():
  6. '''
  7. This test should be the minimum working configuration. The "message"
  8. should be auto populated with the default value which is the state name.
  9. '''
  10. hook_config = {'token': 'ksdjfwoweijfvwoeifvjmwghagy92', 'user': '983hfe0of902lkjfa2amanfgui'}
  11. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  12. 'resolve_credential'
  13. ).replace_with(lambda value, config: value)
  14. flexmock(module.logger).should_receive('warning').never()
  15. flexmock(module.requests).should_receive('post').with_args(
  16. 'https://api.pushover.net/1/messages.json',
  17. headers={'Content-type': 'application/x-www-form-urlencoded'},
  18. data={
  19. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  20. 'user': '983hfe0of902lkjfa2amanfgui',
  21. 'message': 'fail',
  22. },
  23. ).and_return(flexmock(ok=True)).once()
  24. module.ping_monitor(
  25. hook_config,
  26. {},
  27. 'config.yaml',
  28. borgmatic.hooks.monitoring.monitor.State.FAIL,
  29. monitoring_log_level=1,
  30. dry_run=False,
  31. )
  32. def test_ping_monitor_config_with_minimum_config_start_state_backup_not_send_to_pushover_exit_early():
  33. '''
  34. This test should exit early since the hook config does not specify the
  35. 'start' state. Only the 'fail' state is enabled by default.
  36. '''
  37. hook_config = {'token': 'ksdjfwoweijfvwoeifvjmwghagy92', 'user': '983hfe0of902lkjfa2amanfgui'}
  38. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  39. 'resolve_credential'
  40. ).replace_with(lambda value, config: value)
  41. flexmock(module.logger).should_receive('warning').never()
  42. flexmock(module.requests).should_receive('post').never()
  43. module.ping_monitor(
  44. hook_config,
  45. {},
  46. 'config.yaml',
  47. borgmatic.hooks.monitoring.monitor.State.START,
  48. monitoring_log_level=1,
  49. dry_run=False,
  50. )
  51. def test_ping_monitor_start_state_backup_default_message_successfully_send_to_pushover():
  52. '''
  53. This test should send a notification to Pushover on backup start
  54. since the state has been configured. It should default to sending
  55. the name of the state as the 'message' since it is not
  56. explicitly declared in the state config.
  57. '''
  58. hook_config = {
  59. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  60. 'user': '983hfe0of902lkjfa2amanfgui',
  61. 'states': {'start', 'fail', 'finish'},
  62. }
  63. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  64. 'resolve_credential'
  65. ).replace_with(lambda value, config: value)
  66. flexmock(module.logger).should_receive('warning').never()
  67. flexmock(module.requests).should_receive('post').with_args(
  68. 'https://api.pushover.net/1/messages.json',
  69. headers={'Content-type': 'application/x-www-form-urlencoded'},
  70. data={
  71. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  72. 'user': '983hfe0of902lkjfa2amanfgui',
  73. 'message': 'start',
  74. },
  75. ).and_return(flexmock(ok=True)).once()
  76. module.ping_monitor(
  77. hook_config,
  78. {},
  79. 'config.yaml',
  80. borgmatic.hooks.monitoring.monitor.State.START,
  81. monitoring_log_level=1,
  82. dry_run=False,
  83. )
  84. def test_ping_monitor_start_state_backup_custom_message_successfully_send_to_pushover():
  85. '''
  86. This test should send a notification to Pushover on backup start
  87. since the state has been configured. It should send a custom
  88. 'message' since it is explicitly declared in the state config.
  89. '''
  90. hook_config = {
  91. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  92. 'user': '983hfe0of902lkjfa2amanfgui',
  93. 'states': {'start', 'fail', 'finish'},
  94. 'start': {'message': 'custom start message'},
  95. }
  96. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  97. 'resolve_credential'
  98. ).replace_with(lambda value, config: value)
  99. flexmock(module.logger).should_receive('warning').never()
  100. flexmock(module.requests).should_receive('post').with_args(
  101. 'https://api.pushover.net/1/messages.json',
  102. headers={'Content-type': 'application/x-www-form-urlencoded'},
  103. data={
  104. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  105. 'user': '983hfe0of902lkjfa2amanfgui',
  106. 'message': 'custom start message',
  107. },
  108. ).and_return(flexmock(ok=True)).once()
  109. module.ping_monitor(
  110. hook_config,
  111. {},
  112. 'config.yaml',
  113. borgmatic.hooks.monitoring.monitor.State.START,
  114. monitoring_log_level=1,
  115. dry_run=False,
  116. )
  117. def test_ping_monitor_start_state_backup_default_message_with_priority_emergency_uses_expire_and_retry_defaults():
  118. '''
  119. This simulates priority level 2 being set but expiry and retry are
  120. not declared. This should set retry and expiry to their defaults.
  121. '''
  122. hook_config = {
  123. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  124. 'user': '983hfe0of902lkjfa2amanfgui',
  125. 'states': {'start', 'fail', 'finish'},
  126. 'start': {'priority': 2},
  127. }
  128. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  129. 'resolve_credential'
  130. ).replace_with(lambda value, config: value)
  131. flexmock(module.logger).should_receive('warning').never()
  132. flexmock(module.requests).should_receive('post').with_args(
  133. 'https://api.pushover.net/1/messages.json',
  134. headers={'Content-type': 'application/x-www-form-urlencoded'},
  135. data={
  136. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  137. 'user': '983hfe0of902lkjfa2amanfgui',
  138. 'message': 'start',
  139. 'priority': 2,
  140. 'retry': 30,
  141. 'expire': 600,
  142. },
  143. ).and_return(flexmock(ok=True)).once()
  144. module.ping_monitor(
  145. hook_config,
  146. {},
  147. 'config.yaml',
  148. borgmatic.hooks.monitoring.monitor.State.START,
  149. monitoring_log_level=1,
  150. dry_run=False,
  151. )
  152. def test_ping_monitor_start_state_backup_default_message_with_priority_emergency_declared_with_expire_no_retry_success():
  153. '''
  154. This simulates priority level 2 and expiry being set but retry is
  155. not declared. This should set retry to the default.
  156. '''
  157. hook_config = {
  158. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  159. 'user': '983hfe0of902lkjfa2amanfgui',
  160. 'states': {'start', 'fail', 'finish'},
  161. 'start': {'priority': 2, 'expire': 600},
  162. }
  163. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  164. 'resolve_credential'
  165. ).replace_with(lambda value, config: value)
  166. flexmock(module.logger).should_receive('warning').never()
  167. flexmock(module.requests).should_receive('post').with_args(
  168. 'https://api.pushover.net/1/messages.json',
  169. headers={'Content-type': 'application/x-www-form-urlencoded'},
  170. data={
  171. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  172. 'user': '983hfe0of902lkjfa2amanfgui',
  173. 'message': 'start',
  174. 'priority': 2,
  175. 'retry': 30,
  176. 'expire': 600,
  177. },
  178. ).and_return(flexmock(ok=True)).once()
  179. module.ping_monitor(
  180. hook_config,
  181. {},
  182. 'config.yaml',
  183. borgmatic.hooks.monitoring.monitor.State.START,
  184. monitoring_log_level=1,
  185. dry_run=False,
  186. )
  187. def test_ping_monitor_start_state_backup_default_message_with_priority_emergency_declared_no_expire_with_retry_success():
  188. '''
  189. This simulates priority level 2 and retry being set but expire is
  190. not declared. This should set expire to the default.
  191. '''
  192. hook_config = {
  193. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  194. 'user': '983hfe0of902lkjfa2amanfgui',
  195. 'states': {'start', 'fail', 'finish'},
  196. 'start': {'priority': 2, 'retry': 30},
  197. }
  198. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  199. 'resolve_credential'
  200. ).replace_with(lambda value, config: value)
  201. flexmock(module.logger).should_receive('warning').never()
  202. flexmock(module.requests).should_receive('post').with_args(
  203. 'https://api.pushover.net/1/messages.json',
  204. headers={'Content-type': 'application/x-www-form-urlencoded'},
  205. data={
  206. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  207. 'user': '983hfe0of902lkjfa2amanfgui',
  208. 'message': 'start',
  209. 'priority': 2,
  210. 'retry': 30,
  211. 'expire': 600,
  212. },
  213. ).and_return(flexmock(ok=True)).once()
  214. module.ping_monitor(
  215. hook_config,
  216. {},
  217. 'config.yaml',
  218. borgmatic.hooks.monitoring.monitor.State.START,
  219. monitoring_log_level=1,
  220. dry_run=False,
  221. )
  222. def test_ping_monitor_start_state_backup_default_message_with_priority_high_declared_expire_and_retry_raises():
  223. '''
  224. This simulates priority level 1, retry and expiry being set. Since expire
  225. and retry are only used for priority level 2, they should not be included
  226. in the request sent to Pushover. This test verifies that a ValueError is
  227. raised.
  228. '''
  229. hook_config = {
  230. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  231. 'user': '983hfe0of902lkjfa2amanfgui',
  232. 'states': {'start', 'fail', 'finish'},
  233. 'start': {'priority': 1, 'expire': 30, 'retry': 30},
  234. }
  235. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  236. 'resolve_credential'
  237. ).replace_with(lambda value, config: value)
  238. flexmock(module.logger).should_receive('warning').never()
  239. flexmock(module.requests).should_receive('post').never()
  240. with pytest.raises(ValueError):
  241. module.ping_monitor(
  242. hook_config,
  243. {},
  244. 'config.yaml',
  245. borgmatic.hooks.monitoring.monitor.State.START,
  246. monitoring_log_level=1,
  247. dry_run=False,
  248. )
  249. def test_ping_monitor_start_state_backup_based_on_documentation_advanced_example_success():
  250. '''
  251. Here is a test of what is provided in the monitor-your-backups.md file
  252. as an 'advanced example'. This test runs the start state.
  253. '''
  254. hook_config = {
  255. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  256. 'user': '983hfe0of902lkjfa2amanfgui',
  257. 'states': {'start', 'fail', 'finish'},
  258. 'start': {
  259. 'message': 'Backup <b>Started</b>',
  260. 'priority': -2,
  261. 'title': 'Backup Started',
  262. 'html': 1,
  263. 'ttl': 10,
  264. },
  265. 'fail': {
  266. 'message': 'Backup <font color="#ff6961">Failed</font>',
  267. 'priority': 2,
  268. 'expire': 600,
  269. 'retry': 30,
  270. 'device': 'pixel8',
  271. 'title': 'Backup Failed',
  272. 'html': 1,
  273. 'sound': 'siren',
  274. 'url': 'https://ticketing-system.example.com/login',
  275. 'url_title': 'Login to ticketing system',
  276. },
  277. 'finish': {
  278. 'message': 'Backup <font color="#77dd77">Finished</font>',
  279. 'priority': 0,
  280. 'title': 'Backup Finished',
  281. 'html': 1,
  282. 'ttl': 60,
  283. 'url': 'https://ticketing-system.example.com/login',
  284. 'url_title': 'Login to ticketing system',
  285. },
  286. }
  287. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  288. 'resolve_credential'
  289. ).replace_with(lambda value, config: value)
  290. flexmock(module.logger).should_receive('warning').never()
  291. flexmock(module.requests).should_receive('post').with_args(
  292. 'https://api.pushover.net/1/messages.json',
  293. headers={'Content-type': 'application/x-www-form-urlencoded'},
  294. data={
  295. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  296. 'user': '983hfe0of902lkjfa2amanfgui',
  297. 'message': 'Backup <b>Started</b>',
  298. 'priority': -2,
  299. 'title': 'Backup Started',
  300. 'html': 1,
  301. 'ttl': 10,
  302. },
  303. ).and_return(flexmock(ok=True)).once()
  304. module.ping_monitor(
  305. hook_config,
  306. {},
  307. 'config.yaml',
  308. borgmatic.hooks.monitoring.monitor.State.START,
  309. monitoring_log_level=1,
  310. dry_run=False,
  311. )
  312. def test_ping_monitor_fail_state_backup_based_on_documentation_advanced_example_success():
  313. '''
  314. Here is a test of what is provided in the monitor-your-backups.md file
  315. as an 'advanced example'. This test runs the fail state.
  316. '''
  317. hook_config = {
  318. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  319. 'user': '983hfe0of902lkjfa2amanfgui',
  320. 'states': {'start', 'fail', 'finish'},
  321. 'start': {
  322. 'message': 'Backup <b>Started</b>',
  323. 'priority': -2,
  324. 'title': 'Backup Started',
  325. 'html': 1,
  326. 'ttl': 10,
  327. },
  328. 'fail': {
  329. 'message': 'Backup <font color="#ff6961">Failed</font>',
  330. 'priority': 2,
  331. 'expire': 600,
  332. 'retry': 30,
  333. 'device': 'pixel8',
  334. 'title': 'Backup Failed',
  335. 'html': 1,
  336. 'sound': 'siren',
  337. 'url': 'https://ticketing-system.example.com/login',
  338. 'url_title': 'Login to ticketing system',
  339. },
  340. 'finish': {
  341. 'message': 'Backup <font color="#77dd77">Finished</font>',
  342. 'priority': 0,
  343. 'title': 'Backup Finished',
  344. 'html': 1,
  345. 'ttl': 60,
  346. 'url': 'https://ticketing-system.example.com/login',
  347. 'url_title': 'Login to ticketing system',
  348. },
  349. }
  350. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  351. 'resolve_credential'
  352. ).replace_with(lambda value, config: value)
  353. flexmock(module.logger).should_receive('warning').never()
  354. flexmock(module.requests).should_receive('post').with_args(
  355. 'https://api.pushover.net/1/messages.json',
  356. headers={'Content-type': 'application/x-www-form-urlencoded'},
  357. data={
  358. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  359. 'user': '983hfe0of902lkjfa2amanfgui',
  360. 'message': 'Backup <font color="#ff6961">Failed</font>',
  361. 'priority': 2,
  362. 'expire': 600,
  363. 'retry': 30,
  364. 'device': 'pixel8',
  365. 'title': 'Backup Failed',
  366. 'html': 1,
  367. 'sound': 'siren',
  368. 'url': 'https://ticketing-system.example.com/login',
  369. 'url_title': 'Login to ticketing system',
  370. },
  371. ).and_return(flexmock(ok=True)).once()
  372. module.ping_monitor(
  373. hook_config,
  374. {},
  375. 'config.yaml',
  376. borgmatic.hooks.monitoring.monitor.State.FAIL,
  377. monitoring_log_level=1,
  378. dry_run=False,
  379. )
  380. def test_ping_monitor_finish_state_backup_based_on_documentation_advanced_example_success():
  381. '''
  382. Here is a test of what is provided in the monitor-your-backups.md file
  383. as an 'advanced example'. This test runs the finish state.
  384. '''
  385. hook_config = {
  386. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  387. 'user': '983hfe0of902lkjfa2amanfgui',
  388. 'states': {'start', 'fail', 'finish'},
  389. 'start': {
  390. 'message': 'Backup <b>Started</b>',
  391. 'priority': -2,
  392. 'title': 'Backup Started',
  393. 'html': 1,
  394. 'ttl': 10,
  395. },
  396. 'fail': {
  397. 'message': 'Backup <font color="#ff6961">Failed</font>',
  398. 'priority': 2,
  399. 'expire': 600,
  400. 'retry': 30,
  401. 'device': 'pixel8',
  402. 'title': 'Backup Failed',
  403. 'html': 1,
  404. 'sound': 'siren',
  405. 'url': 'https://ticketing-system.example.com/login',
  406. 'url_title': 'Login to ticketing system',
  407. },
  408. 'finish': {
  409. 'message': 'Backup <font color="#77dd77">Finished</font>',
  410. 'priority': 0,
  411. 'title': 'Backup Finished',
  412. 'html': 1,
  413. 'ttl': 60,
  414. 'url': 'https://ticketing-system.example.com/login',
  415. 'url_title': 'Login to ticketing system',
  416. },
  417. }
  418. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  419. 'resolve_credential'
  420. ).replace_with(lambda value, config: value)
  421. flexmock(module.logger).should_receive('warning').never()
  422. flexmock(module.requests).should_receive('post').with_args(
  423. 'https://api.pushover.net/1/messages.json',
  424. headers={'Content-type': 'application/x-www-form-urlencoded'},
  425. data={
  426. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  427. 'user': '983hfe0of902lkjfa2amanfgui',
  428. 'message': 'Backup <font color="#77dd77">Finished</font>',
  429. 'priority': 0,
  430. 'title': 'Backup Finished',
  431. 'html': 1,
  432. 'ttl': 60,
  433. 'url': 'https://ticketing-system.example.com/login',
  434. 'url_title': 'Login to ticketing system',
  435. },
  436. ).and_return(flexmock(ok=True)).once()
  437. module.ping_monitor(
  438. hook_config,
  439. {},
  440. 'config.yaml',
  441. borgmatic.hooks.monitoring.monitor.State.FINISH,
  442. monitoring_log_level=1,
  443. dry_run=False,
  444. )
  445. def test_ping_monitor_config_with_minimum_config_fail_state_backup_successfully_send_to_pushover_dry_run():
  446. '''
  447. This test should be the minimum working configuration. The "message"
  448. should be auto populated with the default value which is the state name.
  449. '''
  450. hook_config = {'token': 'ksdjfwoweijfvwoeifvjmwghagy92', 'user': '983hfe0of902lkjfa2amanfgui'}
  451. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  452. 'resolve_credential'
  453. ).replace_with(lambda value, config: value)
  454. flexmock(module.logger).should_receive('warning').never()
  455. flexmock(module.requests).should_receive('post').and_return(flexmock(ok=True)).never()
  456. module.ping_monitor(
  457. hook_config,
  458. {},
  459. 'config.yaml',
  460. borgmatic.hooks.monitoring.monitor.State.FAIL,
  461. monitoring_log_level=1,
  462. dry_run=True,
  463. )
  464. def test_ping_monitor_config_incorrect_state_exit_early():
  465. '''
  466. This test should exit early since the start state is not declared in the configuration.
  467. '''
  468. hook_config = {
  469. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  470. 'user': '983hfe0of902lkjfa2amanfgui',
  471. }
  472. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  473. 'resolve_credential'
  474. ).replace_with(lambda value, config: value)
  475. flexmock(module.logger).should_receive('warning').never()
  476. flexmock(module.requests).should_receive('post').and_return(flexmock(ok=True)).never()
  477. module.ping_monitor(
  478. hook_config,
  479. {},
  480. 'config.yaml',
  481. borgmatic.hooks.monitoring.monitor.State.START,
  482. monitoring_log_level=1,
  483. dry_run=True,
  484. )
  485. def test_ping_monitor_push_post_error_bails():
  486. '''
  487. This test simulates the Pushover servers not responding with a 200 OK. We
  488. should raise for status and warn then exit.
  489. '''
  490. hook_config = hook_config = {
  491. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  492. 'user': '983hfe0of902lkjfa2amanfgui',
  493. }
  494. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  495. 'resolve_credential'
  496. ).replace_with(lambda value, config: value)
  497. push_response = flexmock(ok=False)
  498. push_response.should_receive('raise_for_status').and_raise(
  499. module.requests.ConnectionError
  500. ).once()
  501. flexmock(module.requests).should_receive('post').with_args(
  502. 'https://api.pushover.net/1/messages.json',
  503. headers={'Content-type': 'application/x-www-form-urlencoded'},
  504. data={
  505. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  506. 'user': '983hfe0of902lkjfa2amanfgui',
  507. 'message': 'fail',
  508. },
  509. ).and_return(push_response).once()
  510. flexmock(module.logger).should_receive('warning').once()
  511. module.ping_monitor(
  512. hook_config,
  513. {},
  514. 'config.yaml',
  515. borgmatic.hooks.monitoring.monitor.State.FAIL,
  516. monitoring_log_level=1,
  517. dry_run=False,
  518. )
  519. def test_ping_monitor_credential_error_bails():
  520. hook_config = hook_config = {
  521. 'token': 'ksdjfwoweijfvwoeifvjmwghagy92',
  522. 'user': '983hfe0of902lkjfa2amanfgui',
  523. }
  524. flexmock(module.borgmatic.hooks.credential.parse).should_receive(
  525. 'resolve_credential'
  526. ).and_raise(ValueError)
  527. flexmock(module.requests).should_receive('post').never()
  528. flexmock(module.logger).should_receive('warning').once()
  529. module.ping_monitor(
  530. hook_config,
  531. {},
  532. 'config.yaml',
  533. borgmatic.hooks.monitoring.monitor.State.FAIL,
  534. monitoring_log_level=1,
  535. dry_run=False,
  536. )