Skip to content

Commit 1e3673f

Browse files
authored
Merge pull request #13 from kaggis/test-emails
ARGO-996 Add ability to generate rules using a group of test emails
2 parents 94d5da4 + 729b017 commit 1e3673f

File tree

6 files changed

+134
-9
lines changed

6 files changed

+134
-9
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ To run argo-alert publisher:
106106
To run argo-alert mail rule generator:
107107

108108
$ argo-alert-rulegen -c /path/to/argo-alert.conf
109+
110+
Using test-emails
111+
-----------------
112+
For testing purposes if you want to distribute notifications using a list
113+
of your own email aliases (before using client contact emails) issue:
114+
115+
$ argo-alert-rulegen -c /path/to/argo-alert.conf --test-emails test01@email.foo,test02@email.foo,test03@email.foo
116+
117+
The rule list will be generated using the configured client sources but each client
118+
email will be replaced (using round-robin method) by a test-email (as defined in the cli arg list)
109119

110120
Run tests
111121
---------

argoalert/argoalert.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def start_listening(environment, kafka_endpoints, kafka_topic,
105105
read_and_send(message, environment, alerta_endpoint, alerta_token)
106106

107107

108-
def gocdb_to_contacts(gocdb_xml, use_notif_flag):
108+
def gocdb_to_contacts(gocdb_xml, use_notif_flag, test_emails):
109109
"""Transform gocdb xml schema info on generic contacts json information
110110
111111
Args:
@@ -118,7 +118,9 @@ def gocdb_to_contacts(gocdb_xml, use_notif_flag):
118118
xmldoc = parseString(gocdb_xml)
119119
contacts = []
120120
clist = xmldoc.getElementsByTagName("CONTACT_EMAIL")
121-
for indx, item in enumerate(clist):
121+
122+
indx = 0
123+
for item in clist:
122124

123125
# By default accept all contacts
124126
notify_val = 'Y'
@@ -143,7 +145,12 @@ def gocdb_to_contacts(gocdb_xml, use_notif_flag):
143145
continue
144146

145147
c["name"] = name_tags[0].firstChild.nodeValue
146-
c["email"] = item.firstChild.nodeValue
148+
149+
if test_emails is None:
150+
c["email"] = item.firstChild.nodeValue
151+
else:
152+
c["email"] = test_emails[indx % len(test_emails)]
153+
indx = indx + 1
147154

148155
contacts.append(c)
149156

bin/argo-alert-rulegen

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,23 @@ def main(args=None):
2626

2727
logging.basicConfig(level=log_level)
2828

29+
test_emails = None
30+
if args.test_emails is not None:
31+
test_emails = args.test_emails.split(',')
32+
2933
# Get site data from gocdb
3034
top_url = gocdb_api + top_req
3135
gocdb_site_xml = argoalert.get_gocdb(top_url, ca_bundle, hostcert, hostkey, verify)
3236

3337
# Convert top-level gocdb xml data to contacts object
34-
top_contacts = argoalert.gocdb_to_contacts(gocdb_site_xml, use_notif_flag)
38+
top_contacts = argoalert.gocdb_to_contacts(gocdb_site_xml, use_notif_flag, test_emails)
3539

3640
# Get sub level contact data from gocdb
3741
sub_url = gocdb_api + sub_req
3842
gocdb_service_xml = argoalert.get_gocdb(sub_url, ca_bundle, hostcert, hostkey, verify)
3943

4044
# Convert sub-level site gocdb xml data to contacts object
41-
sub_contacts = argoalert.gocdb_to_contacts(gocdb_service_xml, use_notif_flag)
45+
sub_contacts = argoalert.gocdb_to_contacts(gocdb_service_xml, use_notif_flag, test_emails)
4246

4347
# merge site and service contacts
4448
contacts = top_contacts + sub_contacts
@@ -57,6 +61,8 @@ if __name__ == "__main__":
5761
arg_parser.add_argument(
5862
"-c", "--config", help="config", dest="config", metavar="string",
5963
required="TRUE")
64+
arg_parser.add_argument(
65+
"-t", "--test-emails", help="test-emails", dest="test_emails", metavar="string")
6066

6167
# Parse the command line arguments accordingly and introduce them to
6268
# main...
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[
2+
{
3+
"name": "rule_SG_A",
4+
"fields": [
5+
{"field": "resource", "regex": "SG_A"}
6+
],
7+
"contacts": ["test1@email.foo"],
8+
"exclude":true
9+
},
10+
{
11+
"name": "rule_SG_C",
12+
"fields": [
13+
{"field": "resource", "regex": "SG_C"}
14+
],
15+
"contacts": ["test2@email.foo"],
16+
"exclude":true
17+
},
18+
{
19+
"name": "rule_SG_D",
20+
"fields": [
21+
{"field": "resource", "regex": "SG_D"}
22+
],
23+
"contacts": ["test1@email.foo"],
24+
"exclude":true
25+
}
26+
]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[
2+
{
3+
"name": "rule_Site-A",
4+
"fields": [
5+
{"field": "resource", "regex": "Site-A"}
6+
],
7+
"contacts": ["test1@email.foo"],
8+
"exclude":true
9+
},
10+
{
11+
"name": "rule_Site-B",
12+
"fields": [
13+
{"field": "resource", "regex": "Site-B"}
14+
],
15+
"contacts": ["test2@email.foo"],
16+
"exclude":true
17+
},
18+
{
19+
"name": "rule_Site-D",
20+
"fields": [
21+
{"field": "resource", "regex": "Site-D"}
22+
],
23+
"contacts": ["test3@email.foo"],
24+
"exclude":true
25+
},
26+
{
27+
"name": "rule_Site-F",
28+
"fields": [
29+
{"field": "resource", "regex": "Site-F"}
30+
],
31+
"contacts": ["test1@email.foo"],
32+
"exclude":true
33+
}
34+
]

tests/test_argoalert.py

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def test_sg_gocdb_to_contacts_notify_flag(self):
6060
exp_json = json.loads(json_data)
6161

6262
use_notif_flag = True
63-
contacts = argoalert.gocdb_to_contacts(xml_data, use_notif_flag)
63+
contacts = argoalert.gocdb_to_contacts(xml_data, use_notif_flag, None)
6464

6565
self.assertEqual(contacts, exp_json)
6666

@@ -70,7 +70,7 @@ def test_sg_gocdb_to_contacts_notify_flag(self):
7070
exp_json = json.loads(json_data)
7171

7272
use_notif_flag = False
73-
contacts = argoalert.gocdb_to_contacts(xml_data, use_notif_flag)
73+
contacts = argoalert.gocdb_to_contacts(xml_data, use_notif_flag, None)
7474

7575
self.assertEqual(contacts, exp_json)
7676

@@ -90,7 +90,7 @@ def test_site_gocdb_to_contacts_notify_flag(self):
9090
exp_json = json.loads(json_data)
9191

9292
use_notif_flag = True
93-
contacts = argoalert.gocdb_to_contacts(xml_data, use_notif_flag)
93+
contacts = argoalert.gocdb_to_contacts(xml_data, use_notif_flag, None)
9494

9595
self.assertEqual(contacts, exp_json)
9696

@@ -100,7 +100,7 @@ def test_site_gocdb_to_contacts_notify_flag(self):
100100
exp_json = json.loads(json_data)
101101

102102
use_notif_flag = False
103-
contacts = argoalert.gocdb_to_contacts(xml_data, use_notif_flag)
103+
contacts = argoalert.gocdb_to_contacts(xml_data, use_notif_flag, None)
104104

105105
self.assertEqual(contacts, exp_json)
106106

@@ -139,4 +139,46 @@ def test_site_contacts_to_alerta(self):
139139
exp_out = json.dumps(exp_json, sort_keys=True)
140140
self.assertEqual(rules_out, exp_out)
141141

142+
# Test gocdb xml with test emails to final rules
143+
def test_site_gocdb_test_mails(self):
144+
xml_fn = "./tests/files/site_gocdb.xml"
145+
site_rules_fn = "./tests/files/site_rules_test_emails.json"
146+
147+
with open(xml_fn, 'r') as xml_file:
148+
xml_data = xml_file.read().replace('\n', '')
149+
150+
# Select contacts using notification flag on
151+
with open(site_rules_fn, 'r') as json_file:
152+
json_data = json_file.read().replace('\n', '')
153+
exp_json = json.loads(json_data)
154+
155+
use_notif_flag = True
156+
test_emails = ["test1@email.foo", "test2@email.foo", "test3@email.foo"]
157+
contacts = argoalert.gocdb_to_contacts(xml_data, use_notif_flag, test_emails)
158+
159+
rules = argoalert.contacts_to_alerta(contacts)
160+
print rules
161+
self.assertEqual(exp_json, rules)
162+
163+
# Test gocdb xml with test emails to final rules
164+
def test_sg_gocdb_test_mails(self):
165+
xml_fn = "./tests/files/sg_gocdb.xml"
166+
site_rules_fn = "./tests/files/sg_rules_test_emails.json"
167+
168+
with open(xml_fn, 'r') as xml_file:
169+
xml_data = xml_file.read().replace('\n', '')
170+
171+
# Select contacts using notification flag on
172+
with open(site_rules_fn, 'r') as json_file:
173+
json_data = json_file.read().replace('\n', '')
174+
exp_json = json.loads(json_data)
175+
176+
use_notif_flag = True
177+
test_emails = ["test1@email.foo", "test2@email.foo"]
178+
contacts = argoalert.gocdb_to_contacts(xml_data, use_notif_flag, test_emails)
179+
180+
rules = argoalert.contacts_to_alerta(contacts)
181+
print rules
182+
self.assertEqual(exp_json, rules)
183+
142184

0 commit comments

Comments
 (0)