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 test_get_container_dir():
    container = 'container1'
    mytest_dir = '%s/realm/validation/%s' % (TESTSDIR, container)
    os.chdir(mytest_dir)
    mytest_curdir = os.getcwd()
    test_dir = get_container_dir(container)
    os.chdir(test_dir)
    test_curdir = os.getcwd()
    assert mytest_curdir == test_curdir
Esempio n. 3
0
def main(arg_vals=None):
    """Main driver utility for running validator tests."""
    logger.info("Comand: '%s %s'",
                sys.executable.rsplit('/', 1)[-1], ' '.join(sys.argv))
    cmd_parser = argparse.ArgumentParser("Comparator functional tests.")
    cmd_parser.add_argument('container',
                            action='store',
                            help='Container tests directory.')
    cmd_parser.add_argument('testfile',
                            action='store',
                            help='test file in the container')

    args = cmd_parser.parse_args(arg_vals)
    # Delete the rundata at the end of the script.
    atexit.register(delete_currentdata)
    logger.info(args)
    init_currentdata()
    init_db()
    snapshot_dir, snapshot_files = get_container_snapshot_json_files(
        args.container)
    if not snapshot_files:
        logger.info("No Snapshot files in %s, exiting!...", snapshot_dir)
        return False
    logger.info('Snapshot files: %s', snapshot_files)
    dbname = config_value(DATABASE, DBNAME)
    snapshot_ids = []
    for fl in snapshot_files:
        snapshot_ids = populate_snapshots_from_file(fl)
    logger.debug(snapshot_ids)
    for sid, coll in snapshot_ids.items():
        docs = get_documents(coll, {'snapshotId': sid},
                             dbname,
                             sort=[('timestamp', pymongo.DESCENDING)],
                             limit=1)
        logger.debug('Number of Snapshot Documents: %s', len(docs))
        if docs and len(docs):
            doc = docs[0]['json']
            logger.info('#' * 80)
            logger.info(json.dumps(doc, indent=2))
    test6 = '%s/%s' % (get_container_dir(args.container), args.testfile)
    test_json = json_from_file(test6)
    if not test_json:
        return
    logger.debug(test_json)
    otherdata = {'dbname': dbname, 'snapshots': snapshot_ids}
    # for testcase in test_json['testSet'][0]['cases']:
    for testset in test_json['testSet']:
        for testcase in testset['cases']:
            rulestr = get_field_value(testcase, 'rule')
            if rulestr:
                main_comparator(rulestr, otherdata)