test_pushover.py 18 KB

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