Commit 0360412b authored by Jérome Perrin's avatar Jérome Perrin

test ok

parent fbed19ae
...@@ -31,6 +31,7 @@ import json ...@@ -31,6 +31,7 @@ import json
import glob import glob
import urlparse import urlparse
import socket import socket
import sys
import time import time
import contextlib import contextlib
import datetime import datetime
...@@ -60,8 +61,8 @@ class MariaDBTestCase(ERP5InstanceTestCase): ...@@ -60,8 +61,8 @@ class MariaDBTestCase(ERP5InstanceTestCase):
return { return {
'tcpv4-port': 3306, 'tcpv4-port': 3306,
'max-connection-count': 5, 'max-connection-count': 5,
'max-slowqueries-threshold': 5, 'max-slowqueries-threshold': 1,
'slowest-query-threshold': 10, 'slowest-query-threshold': 0.1,
# XXX what is this ? should probably not be needed here # XXX what is this ? should probably not be needed here
'name': cls.__name__, 'name': cls.__name__,
'monitor-passwd': 'secret', 'monitor-passwd': 'secret',
...@@ -90,6 +91,26 @@ class MariaDBTestCase(ERP5InstanceTestCase): ...@@ -90,6 +91,26 @@ class MariaDBTestCase(ERP5InstanceTestCase):
) )
def getPluginParameterDict(filepath):
"""Load the slapos monitor plugin and returns the configuration used by this plugin.
This allow to check that monitoring plugin are using a proper config.
"""
extra_config_dict_json = subprocess.check_output([
sys.executable,
"-c",
"""
import json, sys
with open(sys.argv[1]) as f:
exec(f.read())
print(json.dumps(extra_config_dict))
""",
filepath,
])
return json.loads(extra_config_dict_json)
class TestCrontabs(MariaDBTestCase): class TestCrontabs(MariaDBTestCase):
def _getCrontabCommand(self, crontab_name): def _getCrontabCommand(self, crontab_name):
...@@ -105,12 +126,17 @@ class TestCrontabs(MariaDBTestCase): ...@@ -105,12 +126,17 @@ class TestCrontabs(MariaDBTestCase):
crontab_spec = f.read() crontab_spec = f.read()
return " ".join(crontab_spec.split()[5:]) return " ".join(crontab_spec.split()[5:])
def test_full_backup(self): def _executeCrontabAtDate(self, crontab_name, date):
"""Executes a crontab at a given date.
"""
crontab_command = self._getCrontabCommand(crontab_name)
subprocess.check_call( subprocess.check_call(
"faketime 2050-01-01 bash -o pipefail -e -c '%s'" % self._getCrontabCommand('mariadb-backup'), "faketime {date} bash -o pipefail -e -c '{crontab_command}'".format(**locals()),
shell=True, shell=True,
) )
import pdb; pdb.set_trace()
def test_full_backup(self):
self._executeCrontabAtDate('mariadb-backup', '2050-01-01')
with gzip.open( with gzip.open(
os.path.join( os.path.join(
self.computer_partition_root_path, self.computer_partition_root_path,
...@@ -120,46 +146,25 @@ class TestCrontabs(MariaDBTestCase): ...@@ -120,46 +146,25 @@ class TestCrontabs(MariaDBTestCase):
'20500101000000.sql.gz', '20500101000000.sql.gz',
), ),
'r') as dump: 'r') as dump:
self.assertIn('CREATE TABLE `catalog`', dump.read()) self.assertIn('CREATE TABLE', dump.read())
def test_logrotate_and_slow_query_digest(self): def test_logrotate_and_slow_query_digest(self):
# slow query digest needs to run after logrotate, since it operates on the rotated # slow query digest needs to run after logrotate, since it operates on the rotated
# file, so this tests both logrotate and slow query digest. # file, so this tests both logrotate and slow query digest.
# run logrotate a first time so that it create state files # run logrotate a first time so that it create state files
subprocess.check_call( self._executeCrontabAtDate('logrotate', '2000-01-01')
self._getCrontabCommand('logrotate'),
shell=True,
)
# make a slow query # make two slow queries
cnx = self.getDatabaseConnection() cnx = self.getDatabaseConnection()
with contextlib.closing(cnx): with contextlib.closing(cnx):
cnx.query("SELECT SLEEP(1.1)") cnx.query("SELECT SLEEP(1.1)")
cnx.store_result()
cnx.query("SELECT SLEEP(1.2)")
if 0: # slow query crontab depends on crontab for log rotation
# wait for it to be in log files. # to be executed first.
slowquery_log = os.path.join( self._executeCrontabAtDate('logrotate', '2050-01-01')
self.computer_partition_root_path,
'var',
'log',
'mariadb_slowquery.log',
)
for i in range(5):
if os.path.exists(slowquery_log):
print "log exists"
with open(slowquery_log) as f:
if "SLEEP" in f.read():
break
print "no sleep !"
print "log not found, retrying"
time.sleep(i)
# crontab for log rotation executes first
subprocess.check_call(
'faketime 2050-01-01 ' + self._getCrontabCommand('logrotate'),
shell=True,
)
# this logrotate leaves the log for the day as non compressed # this logrotate leaves the log for the day as non compressed
rotated_log_file = os.path.join( rotated_log_file = os.path.join(
self.computer_partition_root_path, self.computer_partition_root_path,
...@@ -170,13 +175,9 @@ class TestCrontabs(MariaDBTestCase): ...@@ -170,13 +175,9 @@ class TestCrontabs(MariaDBTestCase):
) )
self.assertTrue(os.path.exists(rotated_log_file)) self.assertTrue(os.path.exists(rotated_log_file))
# then crontab to generate slow query report executes # then crontab to generate slow query report is executed
subprocess.check_call( self._executeCrontabAtDate('generate-mariadb-slow-query-report', '2050-01-01')
'faketime 2050-01-01 ' + # and it creates a report for the day
self._getCrontabCommand('generate-mariadb-slow-query-report'),
shell=True,
)
# this creates a report for the day
slow_query_report = os.path.join( slow_query_report = os.path.join(
self.computer_partition_root_path, self.computer_partition_root_path,
'srv', 'srv',
...@@ -186,19 +187,32 @@ class TestCrontabs(MariaDBTestCase): ...@@ -186,19 +187,32 @@ class TestCrontabs(MariaDBTestCase):
'slowquery_digest.txt-2050-01-01.xz', 'slowquery_digest.txt-2050-01-01.xz',
) )
with lzma.open(slow_query_report, 'r') as f: with lzma.open(slow_query_report, 'r') as f:
slow_query_report_text = f.read() # this is the hash for our "select sleep(n)" slow query
self.assertIn("SLEEP(1.1)", slow_query_report_text) self.assertIn("ID 0xF9A57DD5A41825CA", f.read())
# this is the hash for that slow query
self.assertIn("ID 0xF9A57DD5A41825CA", slow_query_report_text)
# next day, log files are compressed # on next day execution of logrotate, log files are compressed
subprocess.check_call( self._executeCrontabAtDate('logrotate', '2050-01-02')
'faketime 2050-01-02 ' + self._getCrontabCommand('logrotate'),
shell=True,
)
self.assertTrue(os.path.exists(rotated_log_file + '.xz')) self.assertTrue(os.path.exists(rotated_log_file + '.xz'))
self.assertFalse(os.path.exists(rotated_log_file)) self.assertFalse(os.path.exists(rotated_log_file))
# there's a promise checking that the threshold is not exceeded
# and it reports a problem since we set a threshold of 1 slow query
check_slow_query_promise_plugin = getPluginParameterDict(
os.path.join(
self.computer_partition_root_path,
'etc',
'plugin',
'check-slow-query-pt-digest-result.py',
))
with self.assertRaises(subprocess.CalledProcessError) as error_context:
subprocess.check_output('faketime 2050-01-01 %s' % check_slow_query_promise_plugin['command'], shell=True)
self.assertEqual(
error_context.exception.output,
"""\
Threshold is lower than expected:
Expected total queries : 1.0 and current is: 2
Expected slowest query : 0.1 and current is: 1
""")
class TestMariaDB(MariaDBTestCase): class TestMariaDB(MariaDBTestCase):
def test_utf8_collation(self): def test_utf8_collation(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment