test_pushover.py 16 KB

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