[MediaWiki-commits] [Gerrit] operations/puppet[production]: mtail: add test scaffolding

2017-11-13 Thread Filippo Giunchedi (Code Review)
Filippo Giunchedi has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/388478 )

Change subject: mtail: add test scaffolding
..


mtail: add test scaffolding

Provide unittest-based tests for catching regressions in mtail rules.

Bug: T179565
Change-Id: I4f3f41125f41f95eee028feb7f08453bd5f9b452
---
A modules/mtail/files/test/logs/exim.test
A modules/mtail/files/test/mtail_store.py
A modules/mtail/files/test/mtail_test.py
3 files changed, 114 insertions(+), 0 deletions(-)

Approvals:
  jenkins-bot: Verified
  Filippo Giunchedi: Looks good to me, approved



diff --git a/modules/mtail/files/test/logs/exim.test 
b/modules/mtail/files/test/logs/exim.test
new file mode 100644
index 000..ba23501
--- /dev/null
+++ b/modules/mtail/files/test/logs/exim.test
@@ -0,0 +1,13 @@
+2017-11-02 10:20:48 1eACc9-0002y4-Ik => :blackhole:  
R=aliases
+2017-11-02 10:41:52 1eACwY-0004ON-3Z => foo...@wikimedia.org R=otrs 
T=remote_smtp S=1789 H=mendelevium.eqiad.wmnet [10.64.32.174] C="250 OK 
id=1eACwa-0001jO-8e" DT=0s
+2017-11-02 10:44:25 1eACyz-0004Vc-Il => z...@wikimedia.org 
(b...@wikimedia.org, r...@wikimedia.org, r...@wikisource.org, 
postmas...@wikisource.org)  R=ldap_account T=remote_smtp 
S=181295 H=aspmx.l.google.com [209.85.232.27] 
X=TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128 DN="C=US,ST=California,L=Mountain 
View,O=Google Inc,CN=mx.google.com" C="250 2.0.0 OK 1509619465 v37si20676qtg.44 
- gsmtp" DT=2s
+2017-11-06 10:30:30 1eBefh-0007eo-Eh DKIM: d=asdf.com s=dkim c=relaxed/relaxed 
a=rsa-sha256 [verification failed - signature did not verify (headers probably 
modified in transit)]
+2017-11-06 14:54:35 1eBinK-00048M-Tr DKIM: d=bogus.xyz s=default 
c=relaxed/relaxed a=rsa-sha256 [invalid - public key record (currently?) 
unavailable]
+2017-11-06 15:10:11 1eBj2Q-0004sd-MC DKIM: d=test-foo.com s=subject 
c=relaxed/relaxed a=rsa-sha256 [verification succeeded]
+2017-11-07 14:13:42 H=zomg.org [1.2.3.4]:54729 I=[4.3.2.1]:25 F= 
rejected RCPT : refusing mail from 1.2.3.4 - address is 
blocklisted by zen.spamhaus.org (127.0.0.3, 127.0.0.4: 
https://www.spamhaus.org/query/ip/1.2.3.4)
+2017-11-07 12:47:58 TLS error on connection from foo.com [1.2.3.4]:36384 
I=[1.2.3.4]:25 (gnutls_handshake): Could not negotiate a supported cipher suite.
+2017-11-07 06:26:10 H=([1.2.3.4]) [1.2.3.4]:18173 I=[4.3.2.1]:25 sender verify 
fail for : Address f...@wikimedia.org does not exist
+2017-11-07 10:04:35 H=([1.2.3.4]) [1.2.3.4]:53610 I=[1.2.3.4]:25 sender verify 
defer for : host lookup did not complete
+2017-11-07 14:08:47 Connection from [1.2.3.4]:57094 I=[1.2.3.4]:25 refused: 
too many connections from that IP address
+2017-11-07 14:15:21 rejected EHLO from [1.2.3.4]:2320 I=[1.2.3.4]:25: 
syntactically invalid argument(s): zomg
+2017-11-07 14:14:33 SMTP command timeout on connection from [1.2.3.4]:61601 
I=[1.2.3.4]:25
diff --git a/modules/mtail/files/test/mtail_store.py 
b/modules/mtail/files/test/mtail_store.py
new file mode 100644
index 000..527e315
--- /dev/null
+++ b/modules/mtail/files/test/mtail_store.py
@@ -0,0 +1,51 @@
+import json
+import subprocess
+
+
+class MtailMetricStore(object):
+def __init__(self, progs, logs):
+self._store = {}
+self._progs = progs
+self._logs = logs
+self.parse_metric_store(self.run_mtail()[0])
+
+def run_mtail(self):
+stdout, stderr = subprocess.Popen(
+['mtail', '-one_shot', '-one_shot_metrics', '-logtostderr',
+ '-progs', self._progs, '-logs', self._logs], 
stderr=subprocess.PIPE,
+stdout=subprocess.PIPE).communicate()
+return stdout, stderr
+
+def parse_metric_store(self, output):
+metrics_store = []
+
+in_json = False
+for line in output.splitlines():
+if in_json:
+metrics_store.append(line)
+if line.startswith('Metrics store:{'):
+in_json = True
+metrics_store.append('{')
+
+self._store = json.loads(''.join(metrics_store))
+
+def get_metric(self, name):
+if name not in self._store:
+raise ValueError('metric %s not found in store', name)
+return MtailMetric(self._store[name][0].get('Keys', []),
+   
self._store[name][0]['LabelValues'][0].get('Labels', []),
+   
self._store[name][0]['LabelValues'][0]['Value']['Value'])
+
+
+class MtailMetric(object):
+def __init__(self, keys, labels, value):
+self._keys = keys
+self._labels = labels
+self._value = value
+self._labelpairs = self.get_labelpairs(keys, labels)
+
+def get_labelpairs(self, keys, labels):
+res = []
+for k, v in zip(keys, labels):
+res.append('%s=%s' % (k, v))
+return 

[MediaWiki-commits] [Gerrit] operations/puppet[production]: mtail: add test scaffolding

2017-11-03 Thread Filippo Giunchedi (Code Review)
Filippo Giunchedi has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/388478 )

Change subject: mtail: add test scaffolding
..

mtail: add test scaffolding

Provide unittest-based tests for catching regressions in mtail rules.

Bug: T179565
Change-Id: I4f3f41125f41f95eee028feb7f08453bd5f9b452
---
A modules/mtail/files/test/logs/exim.test
A modules/mtail/files/test/mtail_store.py
A modules/mtail/files/test/mtail_test.py
3 files changed, 75 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/puppet 
refs/changes/78/388478/1

diff --git a/modules/mtail/files/test/logs/exim.test 
b/modules/mtail/files/test/logs/exim.test
new file mode 100644
index 000..a3dc38e
--- /dev/null
+++ b/modules/mtail/files/test/logs/exim.test
@@ -0,0 +1,3 @@
+2017-11-02 10:20:48 1eACc9-0002y4-Ik => :blackhole:  
R=aliases
+2017-11-02 10:41:52 1eACwY-0004ON-3Z => foo...@wikimedia.org R=otrs 
T=remote_smtp S=1789 H=mendelevium.eqiad.wmnet [10.64.32.174] C="250 OK 
id=1eACwa-0001jO-8e" DT=0s
+2017-11-02 10:44:25 1eACyz-0004Vc-Il => z...@wikimedia.org 
(b...@wikimedia.org, r...@wikimedia.org, r...@wikisource.org, 
postmas...@wikisource.org)  R=ldap_account T=remote_smtp 
S=181295 H=aspmx.l.google.com [209.85.232.27] 
X=TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128 DN="C=US,ST=California,L=Mountain 
View,O=Google Inc,CN=mx.google.com" C="250 2.0.0 OK 1509619465 v37si20676qtg.44 
- gsmtp" DT=2s
diff --git a/modules/mtail/files/test/mtail_store.py 
b/modules/mtail/files/test/mtail_store.py
new file mode 100644
index 000..527e315
--- /dev/null
+++ b/modules/mtail/files/test/mtail_store.py
@@ -0,0 +1,51 @@
+import json
+import subprocess
+
+
+class MtailMetricStore(object):
+def __init__(self, progs, logs):
+self._store = {}
+self._progs = progs
+self._logs = logs
+self.parse_metric_store(self.run_mtail()[0])
+
+def run_mtail(self):
+stdout, stderr = subprocess.Popen(
+['mtail', '-one_shot', '-one_shot_metrics', '-logtostderr',
+ '-progs', self._progs, '-logs', self._logs], 
stderr=subprocess.PIPE,
+stdout=subprocess.PIPE).communicate()
+return stdout, stderr
+
+def parse_metric_store(self, output):
+metrics_store = []
+
+in_json = False
+for line in output.splitlines():
+if in_json:
+metrics_store.append(line)
+if line.startswith('Metrics store:{'):
+in_json = True
+metrics_store.append('{')
+
+self._store = json.loads(''.join(metrics_store))
+
+def get_metric(self, name):
+if name not in self._store:
+raise ValueError('metric %s not found in store', name)
+return MtailMetric(self._store[name][0].get('Keys', []),
+   
self._store[name][0]['LabelValues'][0].get('Labels', []),
+   
self._store[name][0]['LabelValues'][0]['Value']['Value'])
+
+
+class MtailMetric(object):
+def __init__(self, keys, labels, value):
+self._keys = keys
+self._labels = labels
+self._value = value
+self._labelpairs = self.get_labelpairs(keys, labels)
+
+def get_labelpairs(self, keys, labels):
+res = []
+for k, v in zip(keys, labels):
+res.append('%s=%s' % (k, v))
+return res
diff --git a/modules/mtail/files/test/mtail_test.py 
b/modules/mtail/files/test/mtail_test.py
new file mode 100644
index 000..4de6c74
--- /dev/null
+++ b/modules/mtail/files/test/mtail_test.py
@@ -0,0 +1,21 @@
+import mtail_store
+import unittest
+import os
+
+test_dir = os.path.join(os.path.dirname(__file__))
+
+
+class EximTest(unittest.TestCase):
+def setUp(self):
+self.store = mtail_store.MtailMetricStore(
+os.path.join(test_dir, '../programs/exim.mtail'),
+os.path.join(test_dir, 'logs/exim.test'))
+
+def testEximMessages(self):
+m = self.store.get_metric('exim_messages_total')
+self.assertEqual(3, m._value)
+self.assertIn('status=out', m._labelpairs)
+
+m = self.store.get_metric('exim_messages_bytes')
+self.assertEqual(183084, m._value)
+self.assertIn('status=out', m._labelpairs)

-- 
To view, visit https://gerrit.wikimedia.org/r/388478
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4f3f41125f41f95eee028feb7f08453bd5f9b452
Gerrit-PatchSet: 1
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: Filippo Giunchedi 

___
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits