def delete_currentdata(): """Delete the rundata file when exiting of the script.""" logger = getlogger() singletest = get_from_currentdata(SINGLETEST) if singletest: container = get_from_currentdata('container') cdir = get_container_dir(container) shutil.rmtree('%s/snapshots' % cdir) logger.critical("END: Completed the run and cleaning up.") runctx = get_currentdata() runctx['end'] = int(time.time() * 1000) runctx['log'] = FWLOGFILENAME if 'start' in runctx: runctx['duration'] = '%d seconds' % int( (runctx['end'] - runctx['start']) / 1000) runctx['start'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(runctx['start'] / 1000)) runctx['end'] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(runctx['end'] / 1000)) for field in exclude_list: if field in runctx: del runctx[field] logger.info("\033[92m Run Stats: %s\033[00m" % json.dumps(runctx, indent=2)) run_file = framework_currentdata() remove_file(run_file)
def get_currentdata(): """Get the current run data, if present else empty json object""" runctx = framework_currentdata() curr_data = json_from_file(runctx) if not curr_data: curr_data = {} return curr_data
def test_delete_run_config(): runcfg = framework_currentdata() init_currentdata() assert True == os.path.exists(runcfg) put_in_currentdata('token', 'abcd') delete_currentdata() assert False == os.path.exists(runcfg)
def test_delete_from_run_config(load_json_file): runcfg = framework_currentdata() init_currentdata() assert True == os.path.exists(runcfg) put_in_currentdata('a', 'val1') runconfig = load_json_file(runcfg) result = True if runconfig and 'a' in runconfig and runconfig[ 'a'] == 'val1' else False assert result == True delete_from_currentdata('a') runconfig = load_json_file(runcfg) result = False if runconfig and 'a' in runconfig else True assert result == True
def init_currentdata(): """ Initialises data structure to store runtime data. """ started = int(time.time() * 1000) runctx = framework_currentdata() run_dir = os.path.dirname(runctx) if not exists_dir(run_dir): mkdir_path(run_dir) run_data = { 'start': started, 'end': started, 'errors': [], 'host': socket.gethostname(), 'timestamp': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') } save_currentdata(run_data)
def test_init_config(): runcfg = framework_currentdata() rundir = os.path.dirname(runcfg) # if os.path.exists(rundir): # shutil.rmtree(rundir) # assert False == os.path.exists(rundir) assert True == os.path.exists(runcfg) init_currentdata() assert True == os.path.exists(rundir) assert True == os.path.exists(runcfg) os.remove(runcfg) assert True == os.path.exists(rundir) assert False == os.path.exists(runcfg) init_currentdata() assert True == os.path.exists(rundir) assert True == os.path.exists(runcfg)
def test_add_to_run_config(load_json_file): runcfg = framework_currentdata() init_currentdata() assert True == os.path.exists(runcfg) put_in_currentdata('a', 'val1') runconfig = load_json_file(runcfg) result = True if runconfig and 'a' in runconfig and runconfig[ 'a'] == 'val1' else False assert result == True put_in_currentdata('b', ['val1']) runconfig = load_json_file(runcfg) result = True if runconfig and 'b' in runconfig and runconfig['b'] == [ 'val1' ] else False assert result == True put_in_currentdata('b', 'val2') runconfig = load_json_file(runcfg) result = True if runconfig and 'b' in runconfig and runconfig['b'] == [ 'val1', 'val2' ] else False assert result == True
def save_currentdata(curr_data): """Save the key value rundata for further access, if None store it empty.""" if not curr_data: curr_data = {} runctx = framework_currentdata() save_json_to_file(curr_data, runctx)