@@ -456,6 +456,261 @@ def test_opsgenie_default_alert_routing():
456456 assert alert .get_info ()['recipients' ] == ['devops@test.com' ]
457457
458458
459+ def test_opsgenie_details_with_constant_value ():
460+ rule = {
461+ 'name' : 'Opsgenie Details' ,
462+ 'type' : mock_rule (),
463+ 'opsgenie_account' : 'genies' ,
464+ 'opsgenie_key' : 'ogkey' ,
465+ 'opsgenie_details' : {'Foo' : 'Bar' }
466+ }
467+ match = {
468+ '@timestamp' : '2014-10-31T00:00:00'
469+ }
470+ alert = OpsGenieAlerter (rule )
471+
472+ with mock .patch ('requests.post' ) as mock_post_request :
473+ alert .alert ([match ])
474+
475+ mock_post_request .assert_called_once_with (
476+ 'https://api.opsgenie.com/v2/alerts' ,
477+ headers = {
478+ 'Content-Type' : 'application/json' ,
479+ 'Authorization' : 'GenieKey ogkey'
480+ },
481+ json = mock .ANY ,
482+ proxies = None
483+ )
484+
485+ expected_json = {
486+ 'description' : BasicMatchString (rule , match ).__str__ (),
487+ 'details' : {'Foo' : 'Bar' },
488+ 'message' : 'ElastAlert: Opsgenie Details' ,
489+ 'priority' : None ,
490+ 'source' : 'ElastAlert' ,
491+ 'tags' : ['ElastAlert' , 'Opsgenie Details' ],
492+ 'user' : 'genies'
493+ }
494+ actual_json = mock_post_request .call_args_list [0 ][1 ]['json' ]
495+ assert expected_json == actual_json
496+
497+
498+ def test_opsgenie_details_with_field ():
499+ rule = {
500+ 'name' : 'Opsgenie Details' ,
501+ 'type' : mock_rule (),
502+ 'opsgenie_account' : 'genies' ,
503+ 'opsgenie_key' : 'ogkey' ,
504+ 'opsgenie_details' : {'Foo' : {'field' : 'message' }}
505+ }
506+ match = {
507+ 'message' : 'Bar' ,
508+ '@timestamp' : '2014-10-31T00:00:00'
509+ }
510+ alert = OpsGenieAlerter (rule )
511+
512+ with mock .patch ('requests.post' ) as mock_post_request :
513+ alert .alert ([match ])
514+
515+ mock_post_request .assert_called_once_with (
516+ 'https://api.opsgenie.com/v2/alerts' ,
517+ headers = {
518+ 'Content-Type' : 'application/json' ,
519+ 'Authorization' : 'GenieKey ogkey'
520+ },
521+ json = mock .ANY ,
522+ proxies = None
523+ )
524+
525+ expected_json = {
526+ 'description' : BasicMatchString (rule , match ).__str__ (),
527+ 'details' : {'Foo' : 'Bar' },
528+ 'message' : 'ElastAlert: Opsgenie Details' ,
529+ 'priority' : None ,
530+ 'source' : 'ElastAlert' ,
531+ 'tags' : ['ElastAlert' , 'Opsgenie Details' ],
532+ 'user' : 'genies'
533+ }
534+ actual_json = mock_post_request .call_args_list [0 ][1 ]['json' ]
535+ assert expected_json == actual_json
536+
537+
538+ def test_opsgenie_details_with_nested_field ():
539+ rule = {
540+ 'name' : 'Opsgenie Details' ,
541+ 'type' : mock_rule (),
542+ 'opsgenie_account' : 'genies' ,
543+ 'opsgenie_key' : 'ogkey' ,
544+ 'opsgenie_details' : {'Foo' : {'field' : 'nested.field' }}
545+ }
546+ match = {
547+ 'nested' : {
548+ 'field' : 'Bar'
549+ },
550+ '@timestamp' : '2014-10-31T00:00:00'
551+ }
552+ alert = OpsGenieAlerter (rule )
553+
554+ with mock .patch ('requests.post' ) as mock_post_request :
555+ alert .alert ([match ])
556+
557+ mock_post_request .assert_called_once_with (
558+ 'https://api.opsgenie.com/v2/alerts' ,
559+ headers = {
560+ 'Content-Type' : 'application/json' ,
561+ 'Authorization' : 'GenieKey ogkey'
562+ },
563+ json = mock .ANY ,
564+ proxies = None
565+ )
566+
567+ expected_json = {
568+ 'description' : BasicMatchString (rule , match ).__str__ (),
569+ 'details' : {'Foo' : 'Bar' },
570+ 'message' : 'ElastAlert: Opsgenie Details' ,
571+ 'priority' : None ,
572+ 'source' : 'ElastAlert' ,
573+ 'tags' : ['ElastAlert' , 'Opsgenie Details' ],
574+ 'user' : 'genies'
575+ }
576+ actual_json = mock_post_request .call_args_list [0 ][1 ]['json' ]
577+ assert expected_json == actual_json
578+
579+
580+ def test_opsgenie_details_with_non_string_field ():
581+ rule = {
582+ 'name' : 'Opsgenie Details' ,
583+ 'type' : mock_rule (),
584+ 'opsgenie_account' : 'genies' ,
585+ 'opsgenie_key' : 'ogkey' ,
586+ 'opsgenie_details' : {
587+ 'Age' : {'field' : 'age' },
588+ 'Message' : {'field' : 'message' }
589+ }
590+ }
591+ match = {
592+ 'age' : 10 ,
593+ 'message' : {
594+ 'format' : 'The cow goes %s!' ,
595+ 'arg0' : 'moo'
596+ }
597+ }
598+ alert = OpsGenieAlerter (rule )
599+
600+ with mock .patch ('requests.post' ) as mock_post_request :
601+ alert .alert ([match ])
602+
603+ mock_post_request .assert_called_once_with (
604+ 'https://api.opsgenie.com/v2/alerts' ,
605+ headers = {
606+ 'Content-Type' : 'application/json' ,
607+ 'Authorization' : 'GenieKey ogkey'
608+ },
609+ json = mock .ANY ,
610+ proxies = None
611+ )
612+
613+ expected_json = {
614+ 'description' : BasicMatchString (rule , match ).__str__ (),
615+ 'details' : {
616+ 'Age' : '10' ,
617+ 'Message' : "{'format': 'The cow goes %s!', 'arg0': 'moo'}"
618+ },
619+ 'message' : 'ElastAlert: Opsgenie Details' ,
620+ 'priority' : None ,
621+ 'source' : 'ElastAlert' ,
622+ 'tags' : ['ElastAlert' , 'Opsgenie Details' ],
623+ 'user' : 'genies'
624+ }
625+ actual_json = mock_post_request .call_args_list [0 ][1 ]['json' ]
626+ assert expected_json == actual_json
627+
628+
629+ def test_opsgenie_details_with_missing_field ():
630+ rule = {
631+ 'name' : 'Opsgenie Details' ,
632+ 'type' : mock_rule (),
633+ 'opsgenie_account' : 'genies' ,
634+ 'opsgenie_key' : 'ogkey' ,
635+ 'opsgenie_details' : {
636+ 'Message' : {'field' : 'message' },
637+ 'Missing' : {'field' : 'missing' }
638+ }
639+ }
640+ match = {
641+ 'message' : 'Testing' ,
642+ '@timestamp' : '2014-10-31T00:00:00'
643+ }
644+ alert = OpsGenieAlerter (rule )
645+
646+ with mock .patch ('requests.post' ) as mock_post_request :
647+ alert .alert ([match ])
648+
649+ mock_post_request .assert_called_once_with (
650+ 'https://api.opsgenie.com/v2/alerts' ,
651+ headers = {
652+ 'Content-Type' : 'application/json' ,
653+ 'Authorization' : 'GenieKey ogkey'
654+ },
655+ json = mock .ANY ,
656+ proxies = None
657+ )
658+
659+ expected_json = {
660+ 'description' : BasicMatchString (rule , match ).__str__ (),
661+ 'details' : {'Message' : 'Testing' },
662+ 'message' : 'ElastAlert: Opsgenie Details' ,
663+ 'priority' : None ,
664+ 'source' : 'ElastAlert' ,
665+ 'tags' : ['ElastAlert' , 'Opsgenie Details' ],
666+ 'user' : 'genies'
667+ }
668+ actual_json = mock_post_request .call_args_list [0 ][1 ]['json' ]
669+ assert expected_json == actual_json
670+
671+
672+ def test_opsgenie_details_with_environment_variable_replacement (environ ):
673+ environ .update ({
674+ 'TEST_VAR' : 'Bar'
675+ })
676+ rule = {
677+ 'name' : 'Opsgenie Details' ,
678+ 'type' : mock_rule (),
679+ 'opsgenie_account' : 'genies' ,
680+ 'opsgenie_key' : 'ogkey' ,
681+ 'opsgenie_details' : {'Foo' : '$TEST_VAR' }
682+ }
683+ match = {
684+ '@timestamp' : '2014-10-31T00:00:00'
685+ }
686+ alert = OpsGenieAlerter (rule )
687+
688+ with mock .patch ('requests.post' ) as mock_post_request :
689+ alert .alert ([match ])
690+
691+ mock_post_request .assert_called_once_with (
692+ 'https://api.opsgenie.com/v2/alerts' ,
693+ headers = {
694+ 'Content-Type' : 'application/json' ,
695+ 'Authorization' : 'GenieKey ogkey'
696+ },
697+ json = mock .ANY ,
698+ proxies = None
699+ )
700+
701+ expected_json = {
702+ 'description' : BasicMatchString (rule , match ).__str__ (),
703+ 'details' : {'Foo' : 'Bar' },
704+ 'message' : 'ElastAlert: Opsgenie Details' ,
705+ 'priority' : None ,
706+ 'source' : 'ElastAlert' ,
707+ 'tags' : ['ElastAlert' , 'Opsgenie Details' ],
708+ 'user' : 'genies'
709+ }
710+ actual_json = mock_post_request .call_args_list [0 ][1 ]['json' ]
711+ assert expected_json == actual_json
712+
713+
459714def test_jira ():
460715 description_txt = "Description stuff goes here like a runbook link."
461716 rule = {
0 commit comments