Example #1
0
import sys, os

script_dir = sys.path[0]
top_dir = os.path.join(script_dir, '..', '..')
src_dir = os.path.join(top_dir, 'src')

# to find the osmo_gsm_tester py module
sys.path.append(src_dir)

from osmo_gsm_tester.core import log

log.TestsTarget()
log.set_all_levels(log.L_DBG)

if '-v' in sys.argv:
    log.style_change(trace=True)
Example #2
0
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import _prep

import sys
import os

from osmo_gsm_tester.core import log

#log.targets[0].get_time_str = lambda: '01:02:03'
fake_time = '01:02:03'
log.style_change(time=True, time_fmt=fake_time)
log.set_all_levels(None)

print('- Testing global log functions')
log.log('from log.log()', _origin='<origin>', _category=log.C_TST)
log.dbg('from log.dbg(), not seen', _origin='<origin>', _category=log.C_TST)
log.set_level(log.C_TST, log.L_DBG)
log.dbg('from log.dbg()', _origin='<origin>', _category=log.C_TST)
log.set_level(log.C_TST, log.L_LOG)
log.err('from log.err()', _origin='<origin>', _category=log.C_TST)

print('- Testing log.Origin functions')
class LogTest(log.Origin):
    def __init__(self, *name_items, **detail_items):
        super().__init__(log.C_TST, *name_items, **detail_items)
Example #3
0
def main():

    for sig in (SIGINT, SIGTERM, SIGQUIT, SIGPIPE, SIGHUP):
        signal(sig, sig_handler_cleanup)

    parser = argparse.ArgumentParser(
        epilog=__doc__, formatter_class=argparse.RawTextHelpFormatter)
    # Note: since we're using RawTextHelpFormatter to keep nicely separate
    # paragraphs in the long help text, we unfortunately also need to take care
    # of line wraps in the shorter cmdline options help.
    # The line width here is what remains of screen width after the list of
    # options placed by ArgumentParser. That's unfortunately subject to change
    # and undefined, so when things change, just run a local
    # ./osmo-gsm-tester.py --help and try to keep everything in 80 chars width.
    # The help text is indented automatically, but line width is manual.
    # Using multi-line strings here -- doesn't look nice in the python flow but
    # is easiest to maintain.
    parser.add_argument('-V',
                        '--version',
                        action='store_true',
                        help='Show version')
    parser.add_argument('-c',
                        '--conf-path',
                        dest='conf_path',
                        help='''Specify main configuration file path''')
    parser.add_argument('trial_dir',
                        nargs='?',
                        default=None,
                        help='Directory containing binaries to test')
    parser.add_argument('-s',
                        '--suite-scenario',
                        dest='suite_scenario',
                        action='append',
                        help='''A suite-scenarios combination
like suite:scenario+scenario''')
    parser.add_argument('-S',
                        '--suites-file',
                        dest='suites_file',
                        action='append',
                        default=[],
                        help='''Read suites to run from a yml listing,
like default-suites.conf. The path is relative to
--conf-path.''')
    parser.add_argument('-t',
                        '--test',
                        dest='test',
                        action='append',
                        help='''Run only tests matching this name.
Any test name that contains the given string is run.
To get an exact match, prepend a "=" like
"-t =my_exact_name". The ".py" suffix is always
optional.''')
    parser.add_argument(
        '-l',
        '--log-level',
        dest='log_level',
        choices=log.LEVEL_STRS.keys(),
        default=None,
        help='Set logging level for all categories (on stdout)')
    parser.add_argument('-T',
                        '--traceback',
                        dest='trace',
                        action='store_true',
                        help='Enable stdout logging of tracebacks')
    parser.add_argument('-R',
                        '--source',
                        dest='source',
                        action='store_true',
                        help='Enable stdout logging of source file')
    args = parser.parse_args()

    if args.version:
        print(__version__)
        exit(0)

    print('combinations:', repr(args.suite_scenario))
    print('trial:', repr(args.trial_dir))
    print('tests:', repr(args.test))

    # create a default log to stdout
    log.LogTarget().style(all_origins_on_levels=(log.L_ERR, log.L_TRACEBACK),
                          src=False)

    if args.log_level:
        log.set_all_levels(log.LEVEL_STRS.get(args.log_level))
    if args.trace:
        log.style_change(trace=True)
    if args.source:
        log.style_change(src=True)
    if args.conf_path:
        config.override_conf = args.conf_path

    if args.trial_dir is not None:
        trial_dir = args.trial_dir
    else:
        trial_dir = config.get_main_config_value(config.CFG_TRIAL_DIR)

    combination_strs = list(args.suite_scenario or [])

    for suites_file in args.suites_file:
        suites_file = config.main_config_path_to_abspath(suites_file)
        from_this_file = config.read(suites_file)
        print(('Running suites from %r:\n  ' % suites_file) +
              ('\n  '.join(from_this_file)))
        combination_strs.extend(from_this_file)

    if not combination_strs:
        combination_strs = config.read_config_file(
            config.CFG_DEFAULT_SUITES_CONF, if_missing_return=[])

        if combination_strs:
            print('Running default suites:\n  ' +
                  ('\n  '.join(combination_strs)))
        else:
            print('Failed to load default suites (%r)' %
                  config.get_main_config_value(config.DEFAULT_SUITES_CONF,
                                               fail_if_missing=False))

    if not combination_strs:
        raise RuntimeError('Need at least one suite:scenario to run')

    # Generate supported schemas dynamically from objects:
    generate_schemas()

    # make sure all suite:scenarios exist
    suite_scenarios = []
    for combination_str in combination_strs:
        suite_scenarios.append(suite.load_suite_scenario_str(combination_str))

    # pick tests and make sure they exist
    test_names = []
    for test_name in (args.test or []):
        found = False
        if test_name.startswith('=') and not test_name.endswith('.py'):
            test_name = test_name + '.py'
        for suite_scenario_str, suite_def, scenarios in suite_scenarios:
            for def_test_name in suite_def.test_basenames:
                if test_name.startswith('='):
                    match = test_name[1:] == def_test_name
                else:
                    match = test_name in def_test_name
                if match:
                    found = True
                    test_names.append(def_test_name)
        if not found:
            raise RuntimeError('No test found for %r' % test_name)
    if test_names:
        test_names = sorted(set(test_names))
        print(repr(test_names))

    with trial.Trial(trial_dir) as current_trial:
        current_trial.verify()
        for suite_scenario_str, suite_def, scenarios in suite_scenarios:
            current_trial.add_suite_run(suite_scenario_str, suite_def,
                                        scenarios)
        current_trial.run_suites(test_names)

        if current_trial.status != trial.Trial.PASS:
            return 1
        return 0
Example #4
0
print('- no suite.conf')
assert (log.run_logging_exceptions(suite.load, 'empty_dir') == None)

print('- valid suite dir')
example_suite_dir = os.path.join('test_suite')
s_def = suite.load(example_suite_dir)
assert (isinstance(s_def, suite.SuiteDefinition))
print(config.tostr(s_def.conf))

print('- run hello world test')
trial = FakeTrial()
s = suite.SuiteRun(trial, 'test_suite', s_def)
results = s.run_tests('hello_world.py')
print(report.suite_to_text(s))

log.style_change(src=True)
#log.style_change(trace=True)
print('\n- a test with an error')
results = s.run_tests('test_error.py')
output = report.suite_to_text(s)
print(output)

print('\n- a test with a failure')
results = s.run_tests('test_fail.py')
output = report.suite_to_text(s)
print(output)

print('\n- a test with a raised failure')
results = s.run_tests('test_fail_raise.py')
output = report.suite_to_text(s)
print(output)