Commit 572e915d authored by Alain Takoudjou's avatar Alain Takoudjou

monitor: import checkpromise method from slapos.core now

parent 8b1572ea
......@@ -170,8 +170,6 @@ def run(args_list):
instance_dict = {}
global_state_dict['title'] = config.get('instance', 'name')
# XXX - hosting-title should be removed at some point in favour of specialise_title
global_state_dict['hosting-title'] = config.get('instance', 'root-name')
global_state_dict['specialise_title'] = config.get('instance', 'root-name')
global_state_dict['aggregate_reference'] = config.get('instance', 'computer')
if not global_state_dict['title']:
......
......@@ -104,6 +104,7 @@ class Monitoring(object):
self.config_folder = os.path.join(self.private_folder, 'config')
self.report_folder = self.private_folder
self.data_folder = os.path.join(self.private_folder, 'documents')
self.log_folder = os.path.join(self.private_folder, 'monitor-log')
self.promise_output_file = config.get("monitor", "promise-output-file")
self.bootstrap_is_ok = True
......@@ -387,7 +388,8 @@ class Monitoring(object):
'--history_folder "%s"' % self.data_folder,
'--instance_name "%s"' % self.title,
'--hosting_name "%s"' % self.root_title,
'--promise_type "report"']
'--promise_type "report"',
'--log_file "%s.report.log"' % os.path.join(self.log_folder, report_name)]
cron_line_list.append(' '.join(report_cmd_line))
......@@ -427,7 +429,9 @@ class Monitoring(object):
'--monitor_url "%s/private/"' % self.webdav_url, # XXX hardcoded,
'--history_folder "%s"' % self.public_folder,
'--instance_name "%s"' % self.title,
'--hosting_name "%s"' % self.root_title]
'--hosting_name "%s"' % self.root_title,
'--log_file "%s.log"' % os.path.join(self.log_folder,
self.title.replace(' ', '_'))]
registered_promise_list = os.listdir(self.promise_folder)
registered_promise_list.extend(os.listdir(self.monitor_promise_folder))
......@@ -501,6 +505,15 @@ class Monitoring(object):
file_list = ["%s/*.history.json" % self.public_folder]
self.generateLogrotateEntry('monitor.service.status', file_list, option_list)
# Rotate monitor log files
option_list = [
'daily', 'dateext', 'create', 'rotate 180',
'compress', 'delaycompress', 'notifempty',
'missingok'
]
file_list = ["%s/*.log" % self.log_folder]
self.generateLogrotateEntry('monitor.promise.log', file_list, option_list)
# Add cron entry for SlapOS Collect
command = "sleep $((1 + RANDOM % 60)) && " # Random sleep between 1 to 60 seconds
if self.nice_command:
......
......@@ -12,6 +12,8 @@ from shutil import copyfile
import glob
import argparse
import traceback
import logging
from slapos.grid.utils import checkPromiseList as _checkPromiseList
# Promise timeout after 20 seconds by default
promise_timeout = 20
......@@ -49,13 +51,28 @@ def parseArguments():
parser.add_argument('--hosting_name',
default='UNKNOWN Hosting Subscription',
help='Hosting Subscription name.')
parser.add_argument('--log_file',
help='Path of log file.')
return parser
class RunPromise(object):
def __init__(self, config_parser):
self.config = config_parser
self.logger = logging.getLogger("RunPromise")
self.logger.setLevel(logging.DEBUG)
if not self.config.log_file:
if len(self.logger.handlers) == 0 or \
not isinstance(self.logger.handlers[0], logging.StreamHandler):
self.logger.addHandler(logging.StreamHandler())
else:
file_handler = logging.FileHandler(self.config.log_file)
file_handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s"))
self.logger.addHandler(file_handler)
self.promise_timeout = promise_timeout
if self.config.timeout_file and \
os.path.exists(self.config.timeout_file):
......@@ -64,15 +81,20 @@ class RunPromise(object):
if timeout.isdigit():
self.promise_timeout = int(timeout)
else:
print "%s it not a valid promise-timeout value" % timeout
self.logger.warning("%s it not a valid promise-timeout value" % timeout)
def runpromise(self):
def runPromise(self):
if self.config.promise_folder:
# run all promises from the given folder in a synchronous way
return self.runpromise_synchronous()
return self.runPromiseSynchronous()
# run the promises in a new process
return self.runSinglePromise()
def runSinglePromise(self):
"""
run a single promise in a new process
"""
if os.path.exists(self.config.pid_path):
with open(self.config.pid_path, "r") as pidfile:
try:
......@@ -80,23 +102,27 @@ class RunPromise(object):
except ValueError:
pid = None
if pid and os.path.exists("/proc/" + str(pid)):
print("A process is already running with pid " + str(pid))
self.logger.warning("A process is already running with pid " + str(pid))
return 1
start_date = ""
start_date = datetime.utcnow()
with open(self.config.pid_path, "w") as pidfile:
self.logger.info("Running script %s..." % self.config.promise_script)
process = self.executeCommand(self.config.promise_script)
ps_process = psutil.Process(process.pid)
start_date = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S')
pidfile.write(str(process.pid))
status_json = self.generateStatusJsonFromProcess(process, start_date=start_date)
status_json = self.generateStatusJsonFromProcess(
process,
start_date=start_date,
title=self.config.promise_name
)
status_json['_links'] = {"monitor": {"href": self.config.monitor_url}}
status_json['title'] = self.config.promise_name
status_json['instance'] = self.config.instance_name
status_json['hosting_subscription'] = self.config.hosting_name
status_json['type'] = self.config.promise_type
status_json['portal_type'] = "promise" if \
status_json['portal-type'] = "promise" if \
self.config.promise_type == "status" else self.config.promise_type
# Save the lastest status change date (needed for rss)
......@@ -124,8 +150,7 @@ class RunPromise(object):
os.rename(output_tmp, self.config.output)
os.remove(self.config.pid_path)
def runpromise_synchronous(self):
def runPromiseSynchronous(self):
"""
run all promises in sequential ways
"""
......@@ -137,7 +162,7 @@ class RunPromise(object):
except ValueError:
pid = None
if pid and os.path.exists("/proc/" + str(pid)):
print("A process is already running with pid " + str(pid))
self.logger.warning("A process is already running with pid " + str(pid))
return []
with open(self.config.pid_path, 'w') as fpid:
......@@ -146,7 +171,7 @@ class RunPromise(object):
promise_folder_list = [self.config.promise_folder]
if self.config.monitor_promise_folder:
promise_folder_list.append(self.config.monitor_promise_folder)
status_list = self.checkPromises(promise_folder_list)
status_list = self.checkPromisesList(promise_folder_list)
promises_status_file = os.path.join(self.config.output, '_promise_status')
previous_state_dict = {}
new_state_dict = {}
......@@ -232,7 +257,7 @@ class RunPromise(object):
status_dict.pop('title', '')
status_dict.pop('instance', '')
status_dict.pop('type', '')
status_dict.pop('portal_type', '')
status_dict.pop('portal-type', '')
with open (history_file, mode="r+") as f_history:
f_history.seek(0,2)
......@@ -246,25 +271,12 @@ class RunPromise(object):
name)
copyfile(status_file, os.path.join(history_path, filename))
"""# Don't let history foler grow too much, keep xx files
file_list = filter(os.path.isfile,
glob.glob("%s/*.%s.history.json" % (history_path, promise_type))
)
file_count = len(file_list)
if file_count > keep_item_amount:
file_list.sort(key=lambda x: os.path.getmtime(x))
while file_count > keep_item_amount:
to_delete = file_list.pop(0)
try:
os.unlink(to_delete)
file_count -= 1
except OSError:
raise"""
def generateStatusJsonFromProcess(self, process, start_date=None, title=None):
stdout, stderr = process.communicate()
status_json = {}
if process.returncode != 0:
status_json["returncode"] = process.returncode
status_json["status"] = "ERROR"
if not stdout:
status_json["message"] = stderr
......@@ -273,93 +285,54 @@ class RunPromise(object):
else:
status_json["status"] = "OK"
status_json["message"] = stdout
status_json["returncode"] = 0
if start_date:
status_json["start-date"] = start_date
date_diff = (datetime.utcnow() - start_date)
status_json["start-date"] = start_date.strftime('%Y-%m-%dT%H:%M:%S')
status_json["execution-time"] = date_diff.total_seconds()
self.logger.info("Finished execution of %s in %s second(s)." % (
title, date_diff.total_seconds()))
if title:
status_json["title"] = title
return status_json
def executeCommand(self, args):
return subprocess.Popen(
args,
#cwd=instance_path,
#env=None if sys.platform == 'cygwin' else {},
stdin=None,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
def checkPromises(self, promise_dir_list):
def checkPromisesList(self, promise_dir_list):
"""
Run all promises found into specified folder
"""
promise_list = []
self.logger.info("Checking promises...")
for promise_dir in promise_dir_list:
if not os.path.exists(promise_dir) or not os.path.isdir(promise_dir):
continue
promise_list.extend([os.path.join(promise_dir, promise)
for promise in os.listdir(promise_dir)])
promise_result_list = []
promise_list.extend(_checkPromiseList(
promise_dir,
self.promise_timeout,
profile=False,
raise_on_failure=False,
logger=self.logger))
# Check whether every promise is kept
for promise_script in promise_list:
if not os.path.isfile(promise_script) or not os.access(promise_script, os.X_OK):
# Not executable file
continue
command = [promise_script]
promise_name = os.path.basename(command[0])
result_dict = {
"status": "ERROR",
"type": "status", # keep compatibility
"portal_type": "promise",
"title": promise_name,
"start-date" : datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S'),
"change-time": time.time()
}
process_handler = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
process_handler.stdin.flush()
process_handler.stdin.close()
process_handler.stdin = None
sleep_time = 0.1
increment_limit = int(self.promise_timeout / sleep_time)
for current_increment in range(0, increment_limit):
if process_handler.poll() is None:
time.sleep(sleep_time)
continue
if process_handler.poll() == 0:
# Success!
result_dict["message"] = process_handler.communicate()[0]
result_dict["status"] = "OK"
else:
stdout, stderr = process_handler.communicate()
result_dict["message"] = stderr
if not stderr:
result_dict["message"] = stdout
break
else:
process_handler.terminate()
message = process_handler.stderr.read()
if message is None:
message = process_handler.stdout.read() or ""
message += '\nPROMISE TIMED OUT AFTER %s SECONDS' % self.promise_timeout
result_dict["message"] = message
promise_result_list.append(result_dict)
for i in range(0, len(promise_list)):
promise_list[i]["status"] = "OK" if promise_list[i]["returncode"] == 0 else "ERROR"
promise_list[i]["type"] = "status"
promise_list[i]["portal-type"] = "promise"
promise_list[i]["change-time"] = time.mktime(promise_list[i]["start-date"].timetuple())
promise_list[i]["start-date"] = promise_list[i]["start-date"].strftime('%Y-%m-%dT%H:%M:%S')
return promise_result_list
self.logger.info("Finished promises.")
self.logger.info("---")
return promise_list
def main():
arg_parser = parseArguments()
promise_runner = RunPromise(arg_parser.parse_args())
sys.exit(promise_runner.runpromise())
sys.exit(promise_runner.runPromise())
......@@ -141,7 +141,7 @@ exit %(code)s
self.writePromise('promise_4')
parser = self.getPromiseParser()
promise_runner = RunPromise(parser)
promise_runner.runpromise()
promise_runner.runPromise()
self.assertTrue(os.path.exists(os.path.join(self.public_dir, 'promise_1.status.json')))
self.assertTrue(os.path.exists(os.path.join(self.public_dir, 'promise_2.status.json')))
......@@ -191,7 +191,7 @@ exit %(code)s
"process_resource": "monitor_resource_process.data",
"monitor_process_state": "monitor_resource.status"
},
"portal_type": "instance_state",
"portal_type": "Software Instance",
"state": {
"success": 2,
"error": 2
......@@ -207,9 +207,9 @@ exit %(code)s
"href": "https://monitor.test.com/share/private/"
}
},
"computer_reference": "COMP-1234",
"aggregate_reference": "COMP-1234",
"type": "global",
"hosting-title": "Monitor ROOT"
"specialise_title": "Monitor ROOT"
}"""
with open(os.path.join(self.private_dir, 'monitor.global.json')) as r:
......@@ -221,7 +221,7 @@ exit %(code)s
# all promises are OK now
self.writePromise('promise_2', success=True)
self.writePromise('promise_3', success=True)
promise_runner.runpromise()
promise_runner.runPromise()
globalstate.run([self.monitor_config_file, os.path.join(self.base_dir, 'instance.cfg')])
expected_result_dict = json.loads(expected_result)
......
......@@ -95,30 +95,32 @@ exit 2
promise = self.writePromiseOK('promise_1')
parser = self.getPromiseParser()
promise_runner = RunPromise(parser)
promise_runner.runpromise()
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().decode("utf-8"))
change_time = result1.pop('change-time', 0)
change_date = datetime.fromtimestamp(change_time)
execution_time = result1.pop('execution-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/private/'}},
'message': 'success\n', 'type': 'status',
'portal_type': 'promise'}
expected_result = {u'status': u'OK', u'hosting_subscription': u'Monitor ROOT',
u'title': u'promise_1', u'instance': u'Monitor',
u'_links':
{u'monitor': {u'href': u'https://monitor.test.com/share/private/'}},
u'message': u'success\n', u'type': u'status',
u'portal-type': u'promise', u'returncode': 0}
self.assertEquals(expected_result, result1)
# second run
time.sleep(1)
promise_runner.runpromise()
promise_runner.runPromise()
result2 = json.loads(open(result_file).read().decode("utf-8"))
change_time2 = result2.pop('change-time', 0)
start_date2 = result2.pop('start-date')
change_date2 = datetime.fromtimestamp(change_time2)
execution_time2 = result2.pop('execution-time')
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'))
......@@ -132,15 +134,17 @@ exit 2
result1['change-time'] = change_time
result1['start-date'] = start_date
result1.pop('_links')
result1['execution-time'] = execution_time
result2['start-date'] = start_date2
result2['change-time'] = change_time2
result2['execution-time'] = execution_time2
# not in history
result2.pop('_links')
result2.pop('hosting_subscription')
result2.pop('title')
result2.pop('instance')
result2.pop('type')
result2.pop('portal_type')
result2.pop('portal-type')
self.assertEquals(history['data'][0], result1)
self.assertEquals(history['data'][1], result2)
......@@ -150,7 +154,7 @@ exit 2
promise2 = self.writePromiseOK('promise_2', monitor_folder=True)
parser = self.getPromiseParser()
promise_runner = RunPromise(parser)
promise_runner.runpromise()
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')
......@@ -159,25 +163,27 @@ exit 2
result1 = json.loads(open(result_file).read().decode("utf-8"))
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/private/'}},
'message': 'success\n', 'type': 'status',
'portal_type': 'promise'}
self.assertTrue(result1.pop('execution-time', None) is not None)
expected_result = {u'status': u'OK', u'hosting_subscription': u'Monitor ROOT',
u'title': u'promise_1', u'instance': u'Monitor',
u'_links':
{u'monitor': {u'href': u'https://monitor.test.com/share/private/'}},
u'message': u'success\n', u'type': u'status',
u'portal-type': u'promise', u'returncode': 0}
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/private/'}},
'message': 'success\n', 'type': 'status',
'portal_type': 'promise'}
self.assertTrue(result2.pop('execution-time', None) is not None)
expected_result = {u'status': u'OK', u'hosting_subscription': u'Monitor ROOT',
u'title': u'promise_2', u'instance': u'Monitor',
u'_links':
{u'monitor': {u'href': u'https://monitor.test.com/share/private/'}},
u'message': u'success\n', u'type': u'status',
u'portal-type': u'promise', u'returncode': 0}
self.assertEquals(expected_result, result2)
def test_promise_One_By_One(self):
......@@ -185,29 +191,31 @@ exit 2
promise = self.writePromiseOK('promise_1')
parser = self.getUniquePromiseParser('promise_1', promise, 'status')
promise_runner = RunPromise(parser)
promise_runner.runpromise()
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().decode("utf-8"))
change_time = result1.pop('change-time', 0)
change_date = datetime.fromtimestamp(change_time)
execution_time = result1.pop('execution-time', None)
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/private/'}},
'message': 'success\n', 'type': 'status',
'portal_type': 'promise'}
expected_result = {u'status': u'OK', u'hosting_subscription': u'Monitor ROOT',
u'title': u'promise_1', u'instance': u'Monitor',
u'_links':
{u'monitor': {u'href': u'https://monitor.test.com/share/private/'}},
u'message': u'success\n', u'type': u'status',
u'portal-type': u'promise', u'returncode': 0}
self.assertEquals(expected_result, result1)
# second run
promise_runner.runpromise()
promise_runner.runPromise()
result2 = json.loads(open(result_file).read().decode("utf-8"))
change_time2 = result2.pop('change-time', 0)
result2.pop('start-date', '2016-08-05 00:00:00')
change_time2 = result2.pop('change-time', 2)
result2.pop('start-date')
change_date2 = datetime.fromtimestamp(change_time2)
self.assertTrue(result2.pop('execution-time', None) is not None)
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'))
......@@ -220,6 +228,7 @@ exit 2
result1['change-time'] = change_time
result1['start-date'] = start_date
result1['execution-time'] = execution_time
result1.pop('_links')
self.assertEquals(history['data'][0], result1)
......@@ -228,7 +237,7 @@ exit 2
promise = self.writePromiseNOK('promise_1')
parser = self.getPromiseParser()
promise_runner = RunPromise(parser)
promise_runner.runpromise()
promise_runner.runPromise()
result_file = os.path.join(self.public_dir, 'promise_1.status.json')
self.assertTrue(os.path.exists(result_file))
......@@ -236,20 +245,22 @@ exit 2
change_time = result1.pop('change-time', 0)
change_date = datetime.fromtimestamp(change_time)
start_date = result1.pop('start-date')
expected_result = {'status': 'ERROR', 'hosting_subscription': 'Monitor ROOT',
'title': u'promise_1', 'instance': 'Monitor',
'_links':
{'monitor': {'href': 'https://monitor.test.com/share/private/'}},
'message': 'failed\n', 'type': 'status',
'portal_type': 'promise'}
self.assertTrue(result1.pop('execution-time', None) is not None)
expected_result = {u'status': u'ERROR', u'hosting_subscription': u'Monitor ROOT',
u'title': u'promise_1', u'instance': u'Monitor',
u'_links':
{u'monitor': {u'href': u'https://monitor.test.com/share/private/'}},
u'message': u'failed\n', u'type': u'status',
u'portal-type': u'promise', u'returncode': 2}
self.assertEquals(expected_result, result1)
# second run
promise_runner.runpromise()
promise_runner.runPromise()
result2 = json.loads(open(result_file).read().decode("utf-8"))
change_time2 = result2.pop('change-time', 0)
result2.pop('start-date', '2016-08-05 00:00:00')
change_time2 = result2.pop('change-time', 1)
result2.pop('start-date')
self.assertTrue(result2.pop('execution-time', None) is not None)
change_date2 = datetime.fromtimestamp(change_time2)
self.assertEquals(expected_result, result2)
self.assertEquals(change_date.strftime('%Y-%m-%d %H:%M:%S'),
......@@ -260,7 +271,7 @@ exit 2
promise = self.writePromiseOK('promise_1')
parser = self.getPromiseParser()
promise_runner = RunPromise(parser)
promise_runner.runpromise()
promise_runner.runPromise()
result_file = os.path.join(self.public_dir, 'promise_1.status.json')
self.assertTrue(os.path.exists(result_file))
......@@ -268,13 +279,14 @@ exit 2
change_time = result1.pop('change-time')
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/private/'}},
'message': 'success\n', 'type': 'status',
'portal_type': 'promise'}
self.assertTrue(result1.pop('execution-time', None) is not None)
expected_result = {u'status': u'OK', u'hosting_subscription': u'Monitor ROOT',
u'title': u'promise_1', u'instance': u'Monitor',
u'_links':
{u'monitor': {u'href': u'https://monitor.test.com/share/private/'}},
u'message': u'success\n', u'type': u'status',
u'portal-type': u'promise', u'returncode': 0}
self.assertEquals(expected_result, result1)
# second run with failure
......@@ -283,12 +295,14 @@ exit 2
parser = self.getPromiseParser()
expected_result['message'] = 'failed\n'
expected_result['status'] = 'ERROR'
promise_runner.runpromise()
expected_result['returncode'] = 2
promise_runner.runPromise()
result2 = json.loads(open(result_file).read().decode("utf-8"))
change_time2 = result2.pop('change-time')
result2.pop('start-date')
change_date2 = datetime.fromtimestamp(change_time2)
self.assertTrue(result2.pop('execution-time', None) is not None)
self.assertEquals(expected_result, result2)
self.assertNotEquals(change_date.strftime('%Y-%m-%d %H:%M:%S'),
change_date2.strftime('%Y-%m-%d %H:%M:%S'))
......@@ -298,7 +312,7 @@ exit 2
promise = self.writePromiseOK('sample_report')
parser = self.getUniquePromiseParser('sample_report', promise, 'report')
promise_runner = RunPromise(parser)
promise_runner.runpromise()
promise_runner.runPromise()
result_file = os.path.join(self.private_dir, 'sample_report.report.json')
self.assertTrue(os.path.exists(result_file))
......@@ -306,20 +320,22 @@ exit 2
change_time = result1.pop('change-time', 0)
change_date = datetime.fromtimestamp(change_time)
start_date = result1.pop('start-date')
execution_time = result1.pop('execution-time')
expected_result = {'status': 'OK', 'hosting_subscription': 'Monitor ROOT',
'title': 'sample_report', 'instance': 'Monitor',
'_links':
{'monitor': {'href': 'https://monitor.test.com/share/private/'}},
'message': 'success\n', 'type': 'report',
'portal_type': 'report'}
expected_result = {u'status': u'OK', u'hosting_subscription': u'Monitor ROOT',
u'title': u'sample_report', u'instance': u'Monitor',
u'_links':
{u'monitor': {u'href': u'https://monitor.test.com/share/private/'}},
u'message': u'success\n', u'type': u'report',
u'portal-type': u'report', u'returncode': 0}
self.assertEquals(expected_result, result1)
# second run
promise_runner.runpromise()
promise_runner.runPromise()
result2 = json.loads(open(result_file).read().decode("utf-8"))
change_time2 = result2.pop('change-time', 0)
result2.pop('start-date', '2016-08-05 00:00:00')
change_time2 = result2.pop('change-time', 2)
result2.pop('start-date')
result2.pop('execution-time')
change_date2 = datetime.fromtimestamp(change_time2)
self.assertEquals(expected_result, result2)
self.assertEquals(change_date.strftime('%Y-%m-%d %H:%M:%S'),
......@@ -332,6 +348,7 @@ exit 2
result1['change-time'] = change_time
result1['start-date'] = start_date
result1['execution-time'] = execution_time
#result1.pop('_links')
self.assertEquals(history, result1)
......@@ -340,7 +357,7 @@ exit 2
promise = self.writePromiseOK('sample_report')
parser = self.getUniquePromiseParser('sample_report', promise, 'report')
promise_runner = RunPromise(parser)
promise_runner.runpromise()
promise_runner.runPromise()
result_file = os.path.join(self.private_dir, 'sample_report.report.json')
self.assertTrue(os.path.exists(result_file))
......@@ -348,13 +365,14 @@ exit 2
change_time = result1.pop('change-time', 0)
change_date = datetime.fromtimestamp(change_time)
start_date = result1.pop('start-date')
execution_time = result1.pop('execution-time')
expected_result = {'status': 'OK', 'hosting_subscription': 'Monitor ROOT',
'title': 'sample_report', 'instance': 'Monitor',
'_links':
{'monitor': {'href': 'https://monitor.test.com/share/private/'}},
'message': 'success\n', 'type': 'report',
'portal_type': 'report'}
expected_result = {u'status': u'OK', u'hosting_subscription': u'Monitor ROOT',
u'title': u'sample_report', u'instance': u'Monitor',
u'_links':
{u'monitor': {u'href': u'https://monitor.test.com/share/private/'}},
u'message': u'success\n', u'type': u'report',
u'portal-type': u'report', u'returncode': 0}
self.assertEquals(expected_result, result1)
# second run with failure
......@@ -363,11 +381,13 @@ exit 2
parser = self.getUniquePromiseParser('sample_report', promise, 'report')
expected_result['message'] = 'failed\n'
expected_result['status'] = 'ERROR'
promise_runner.runpromise()
expected_result['returncode'] = 2
promise_runner.runPromise()
result2 = json.loads(open(result_file).read().decode("utf-8"))
change_time2 = result2.pop('change-time')
result2.pop('start-date')
result2.pop('execution-time')
change_date2 = datetime.fromtimestamp(change_time2)
self.assertEquals(expected_result, result2)
self.assertNotEquals(change_date.strftime('%Y-%m-%d %H:%M:%S'),
......
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