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