Commit fff8891f authored by Alain Takoudjou's avatar Alain Takoudjou

monitor: update tests with recent changes

parent 841517de
......@@ -13,6 +13,7 @@ class MonitorBootstrapTest(unittest.TestCase):
def setUp(self):
self.base_dir = tempfile.mkdtemp()
os.mkdir(os.path.join(self.base_dir, 'promise'))
os.mkdir(os.path.join(self.base_dir, 'monitor-promise'))
os.mkdir(os.path.join(self.base_dir, 'public'))
os.mkdir(os.path.join(self.base_dir, 'private'))
os.mkdir(os.path.join(self.base_dir, 'cron.d'))
......@@ -25,13 +26,17 @@ class MonitorBootstrapTest(unittest.TestCase):
self.writeContent(os.path.join(self.base_dir, 'test-httpd-cors.cfg'), '')
self.writeContent(os.path.join(self.base_dir, 'monitor-htpasswd'), '12345')
self.monitor_config_file = os.path.join(self.base_dir, 'monitor.conf')
self.monitor_config_dict = dict(
base_dir=self.base_dir,
root_title="Monitor ROOT",
title="Monitor",
url_list="",
base_url="https://monitor.test.com",
monitor_promise_folder=os.path.join(self.base_dir, 'monitor-promise'),
promise_folder=os.path.join(self.base_dir, 'promise'),
promise_runner_pid=os.path.join(self.base_dir, 'run', 'monitor-promises.pid'),
public_folder=os.path.join(self.base_dir, 'public'),
public_path_list="",
private_path_list="",
promise_run_script="/bin/echo",
......@@ -39,7 +44,9 @@ class MonitorBootstrapTest(unittest.TestCase):
)
self.monitor_conf = """[monitor]
parameter-file-path = %(base_dir)s/knowledge0.cfg
promise-folder-list = %(base_dir)s/promise
promise-folder = %(base_dir)s/promise
service-pid-folder = %(base_dir)s/run
monitor-promise-folder = %(base_dir)s/monitor-promise
private-folder = %(base_dir)s/private
public-folder = %(base_dir)s/public
public-path-list = %(public_path_list)s
......@@ -48,6 +55,7 @@ crond-folder = %(base_dir)s/cron.d
logrotate-folder = %(base_dir)s/logrotate.d
report-folder = %(base_dir)s/monitor-report
root-title = %(root_title)s
pid-file = %(base_dir)s/monitor.pid
parameter-list =
raw monitor-user admin
file sample %(base_dir)s/param
......@@ -105,25 +113,30 @@ promise-runner = %(promise_run_script)s
base_url=url)
self.assertTrue(opml_outline in opml_content)
def check_promises(self):
promise_entry = '* * * * * %(promise_run_script)s --pid_path "%(promise_pid)s" --output "%(promise_output)s" --promise_script "%(promise_executable)s" --promise_name "%(promise_name)s" --monitor_url "%(base_url)s/share/jio_private/" --history_folder "%(base_dir)s/public" --instance_name "%(title)s" --hosting_name "%(root_title)s"'
promise_dir = os.path.join(self.base_dir, 'promise')
def check_promises(self, sequential=False):
promise_cron = os.path.join(self.base_dir, 'cron.d', 'monitor-promises')
self.assertTrue(os.path.exists(promise_cron))
with open(promise_cron) as cronf:
promise_command_list = cronf.read()
for filename in os.listdir(promise_dir):
promise_dict = dict(
promise_pid=os.path.join(self.base_dir, 'run', '%s.pid' % filename),
promise_output=os.path.join(self.base_dir, 'public', '%s.status.json' % filename),
promise_executable=os.path.join(promise_dir, filename),
promise_name=filename
)
promise_dict.update(self.monitor_config_dict)
entry_line = promise_entry % promise_dict
if not sequential:
promise_entry = '* * * * * sleep $((1 + RANDOM %% 30)) && %(promise_run_script)s --pid_path "%(promise_runner_pid)s" --output "%(public_folder)s" --promise_folder "%(promise_folder)s" --monitor_promise_folder "%(monitor_promise_folder)s" --monitor_url "%(base_url)s/share/jio_private/" --history_folder "%(base_dir)s/public" --instance_name "%(title)s" --hosting_name "%(root_title)s"'
entry_line = promise_entry % self.monitor_config_dict
self.assertTrue(entry_line in promise_command_list)
else:
promise_entry = '* * * * * sleep $((1 + RANDOM %% 30)) &&%(promise_run_script)s --pid_path "%(promise_pid)s" --output "%(promise_output)s" --promise_script "%(promise_executable)s" --promise_name "%(promise_name)s" --monitor_url "%(base_url)s/share/jio_private/" --history_folder "%(base_dir)s/public" --instance_name "%(title)s" --hosting_name "%(root_title)s"'
promise_dir = os.path.join(self.base_dir, 'promise')
for filename in os.listdir(promise_dir):
promise_dict = dict(
promise_pid=os.path.join(self.base_dir, 'run', '%s.pid' % filename),
promise_output=os.path.join(self.base_dir, 'public', '%s.status.json' % filename),
promise_executable=os.path.join(promise_dir, filename),
promise_name=filename
)
promise_dict.update(self.monitor_config_dict)
entry_line = promise_entry % promise_dict
self.assertTrue(entry_line in promise_command_list)
def check_report(self):
promise_entry = '* * * * * %(promise_run_script)s --pid_path "%(promise_pid)s" --output "%(promise_output)s" --promise_script "%(promise_executable)s" --promise_name "%(promise_name)s" --monitor_url "%(base_url)s/share/jio_private/" --history_folder "%(data_dir)s" --instance_name "%(title)s" --hosting_name "%(root_title)s" --promise_type "report"'
......
......@@ -6,13 +6,14 @@ import tempfile
import unittest
import json
from datetime import datetime
from slapos.monitor.runpromise import *
from slapos.monitor.runpromise import RunPromise, parseArguments
class MonitorPromiseTest(unittest.TestCase):
def setUp(self):
self.base_dir = tempfile.mkdtemp()
self.promise_dir = os.path.join(self.base_dir, 'promise')
self.monitor_promise_dir = os.path.join(self.base_dir, 'monitor-promise')
self.report_dir = os.path.join(self.base_dir, 'report')
self.public_dir = os.path.join(self.base_dir, 'public')
self.private_dir = os.path.join(self.base_dir, 'private')
......@@ -22,19 +23,22 @@ class MonitorPromiseTest(unittest.TestCase):
os.mkdir(self.private_dir)
os.mkdir(self.report_dir)
os.mkdir(self.run_dir)
os.mkdir(self.monitor_promise_dir)
def tearDown(self):
if os.path.exists(self.base_dir):
shutil.rmtree(self.base_dir)
def writePromiseOK(self, name):
def writePromiseOK(self, name, monitor_folder=False):
content = """#!/bin/sh
echo "success"
exit 0
"""
promise_path = os.path.join(self.promise_dir, name)
if monitor_folder:
promise_path = os.path.join(self.monitor_promise_dir, name)
self.writeContent(promise_path, content)
os.chmod(promise_path, 0755)
return promise_path
......@@ -54,7 +58,7 @@ exit 2
with open(file_path, 'w') as cfg:
cfg.write(config)
def getPromiseParser(self, name, promise_path, promise_type):
def getUniquePromiseParser(self, name, promise_path, promise_type):
pid_path = os.path.join(self.run_dir, '%s.pid' % name)
if promise_type == "report":
output_path = os.path.join(self.private_dir, '%s.report.json' % name)
......@@ -71,11 +75,113 @@ exit 2
arg_parser = parseArguments()
return arg_parser.parse_args(promise_cmd)
def getPromiseParser(self):
pid_path = os.path.join(self.run_dir, 'monitor-promise.pid')
promise_cmd = [
'--pid_path',
'%s' % pid_path, '--output', self.public_dir,
'--promise_folder', self.promise_dir,
'--monitor_promise_folder', self.monitor_promise_dir,
'--promise_type', 'status',
'--monitor_url', 'https://monitor.test.com/share/jio_private/',
'--history_folder', self.public_dir,
'--instance_name', 'Monitor', '--hosting_name', 'Monitor ROOT']
arg_parser = parseArguments()
return arg_parser.parse_args(promise_cmd)
def test_promise_OK(self):
promise = self.writePromiseOK('promise_1')
parser = self.getPromiseParser('promise_1', promise, 'status')
runpromise(parser)
parser = self.getPromiseParser()
promise_runner = RunPromise(parser)
promise_runner.runpromise()
result_file = os.path.join(self.public_dir, 'promise_1.status.json')
self.assertTrue(os.path.exists(result_file))
result1 = json.loads(open(result_file).read())
change_time = result1.pop('change-time', 0)
change_date = datetime.fromtimestamp(change_time)
start_date = result1.pop('start-date')
expected_result = {'status': 'OK', 'hosting_subscription': 'Monitor ROOT',
'title': u'promise_1', 'instance': 'Monitor',
'_links':
{'monitor': {'href': 'https://monitor.test.com/share/jio_private/'}},
'message': 'success\n', 'type': 'status'}
self.assertEquals(expected_result, result1)
# second run
time.sleep(1)
promise_runner.runpromise()
result2 = json.loads(open(result_file).read())
change_time2 = result2.pop('change-time', 0)
start_date2 = result2.pop('start-date')
change_date2 = datetime.fromtimestamp(change_time2)
self.assertEquals(expected_result, result2)
self.assertEquals(change_date.strftime('%Y-%m-%d %H:%M:%S'),
change_date2.strftime('%Y-%m-%d %H:%M:%S'))
history_file = os.path.join(self.public_dir, 'promise_1.history.json')
self.assertTrue(os.path.exists(history_file))
history = json.load(open(history_file))
self.assertTrue(history['date'] > change_time)
self.assertTrue(len(history['data']) == 2)
result1['change-time'] = change_time
result1['start-date'] = start_date
result1.pop('_links')
result2['start-date'] = start_date2
result2['change-time'] = change_time2
# not in history
result2.pop('_links')
result2.pop('hosting_subscription')
result2.pop('title')
result2.pop('instance')
result2.pop('type')
self.assertEquals(history['data'][0], result1)
self.assertEquals(history['data'][1], result2)
def test_promise_two_folder(self):
promise = self.writePromiseOK('promise_1')
promise2 = self.writePromiseOK('promise_2', monitor_folder=True)
parser = self.getPromiseParser()
promise_runner = RunPromise(parser)
promise_runner.runpromise()
result_file = os.path.join(self.public_dir, 'promise_1.status.json')
result2_file = os.path.join(self.public_dir, 'promise_2.status.json')
self.assertTrue(os.path.exists(result_file))
self.assertTrue(os.path.exists(result2_file))
result1 = json.loads(open(result_file).read())
result1.pop('change-time')
result1.pop('start-date')
expected_result = {'status': 'OK', 'hosting_subscription': 'Monitor ROOT',
'title': u'promise_1', 'instance': 'Monitor',
'_links':
{'monitor': {'href': 'https://monitor.test.com/share/jio_private/'}},
'message': 'success\n', 'type': 'status'}
self.assertEquals(expected_result, result1)
result2 = json.loads(open(result2_file).read())
result2.pop('change-time')
result2.pop('start-date')
expected_result = {'status': 'OK', 'hosting_subscription': 'Monitor ROOT',
'title': u'promise_2', 'instance': 'Monitor',
'_links':
{'monitor': {'href': 'https://monitor.test.com/share/jio_private/'}},
'message': 'success\n', 'type': 'status'}
self.assertEquals(expected_result, result2)
def test_promise_One_By_One(self):
promise = self.writePromiseOK('promise_1')
parser = self.getUniquePromiseParser('promise_1', promise, 'status')
promise_runner = RunPromise(parser)
promise_runner.runpromise()
result_file = os.path.join(self.public_dir, 'promise_1.status.json')
self.assertTrue(os.path.exists(result_file))
......@@ -92,7 +198,7 @@ exit 2
self.assertEquals(expected_result, result1)
# second run
runpromise(parser)
promise_runner.runpromise()
result2 = json.loads(open(result_file).read())
change_time2 = result2.pop('change-time', 0)
result2.pop('start-date', '2016-08-05 00:00:00')
......@@ -115,8 +221,9 @@ exit 2
def test_promise_NOK(self):
promise = self.writePromiseNOK('promise_1')
parser = self.getPromiseParser('promise_1', promise, 'status')
runpromise(parser)
parser = self.getPromiseParser()
promise_runner = RunPromise(parser)
promise_runner.runpromise()
result_file = os.path.join(self.public_dir, 'promise_1.status.json')
self.assertTrue(os.path.exists(result_file))
......@@ -133,7 +240,7 @@ exit 2
self.assertEquals(expected_result, result1)
# second run
runpromise(parser)
promise_runner.runpromise()
result2 = json.loads(open(result_file).read())
change_time2 = result2.pop('change-time', 0)
result2.pop('start-date', '2016-08-05 00:00:00')
......@@ -145,8 +252,9 @@ exit 2
def test_promise_mixed(self):
promise = self.writePromiseOK('promise_1')
parser = self.getPromiseParser('promise_1', promise, 'status')
runpromise(parser)
parser = self.getPromiseParser()
promise_runner = RunPromise(parser)
promise_runner.runpromise()
result_file = os.path.join(self.public_dir, 'promise_1.status.json')
self.assertTrue(os.path.exists(result_file))
......@@ -165,10 +273,10 @@ exit 2
# second run with failure
time.sleep(2)
promise = self.writePromiseNOK('promise_1')
parser = self.getPromiseParser('promise_1', promise, 'status')
parser = self.getPromiseParser()
expected_result['message'] = 'failed\n'
expected_result['status'] = 'ERROR'
runpromise(parser)
promise_runner.runpromise()
result2 = json.loads(open(result_file).read())
change_time2 = result2.pop('change-time')
......@@ -181,8 +289,9 @@ exit 2
def test_report_OK(self):
promise = self.writePromiseOK('sample_report')
parser = self.getPromiseParser('sample_report', promise, 'report')
runpromise(parser)
parser = self.getUniquePromiseParser('sample_report', promise, 'report')
promise_runner = RunPromise(parser)
promise_runner.runpromise()
result_file = os.path.join(self.private_dir, 'sample_report.report.json')
self.assertTrue(os.path.exists(result_file))
......@@ -199,7 +308,7 @@ exit 2
self.assertEquals(expected_result, result1)
# second run
runpromise(parser)
promise_runner.runpromise()
result2 = json.loads(open(result_file).read())
change_time2 = result2.pop('change-time', 0)
result2.pop('start-date', '2016-08-05 00:00:00')
......@@ -221,8 +330,9 @@ exit 2
def test_report_mixed(self):
promise = self.writePromiseOK('sample_report')
parser = self.getPromiseParser('sample_report', promise, 'report')
runpromise(parser)
parser = self.getUniquePromiseParser('sample_report', promise, 'report')
promise_runner = RunPromise(parser)
promise_runner.runpromise()
result_file = os.path.join(self.private_dir, 'sample_report.report.json')
self.assertTrue(os.path.exists(result_file))
......@@ -241,10 +351,10 @@ exit 2
# second run with failure
time.sleep(2)
promise = self.writePromiseNOK('sample_report')
parser = self.getPromiseParser('sample_report', promise, 'report')
parser = self.getUniquePromiseParser('sample_report', promise, 'report')
expected_result['message'] = 'failed\n'
expected_result['status'] = 'ERROR'
runpromise(parser)
promise_runner.runpromise()
result2 = json.loads(open(result_file).read())
change_time2 = result2.pop('change-time')
......
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