Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | import os |
Pau Espin Pedrol | 3063730 | 2020-05-06 21:11:02 +0200 | [diff] [blame] | 3 | import sys |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 4 | import _prep |
Pau Espin Pedrol | 9d489c8 | 2019-09-18 18:43:45 +0200 | [diff] [blame] | 5 | import shutil |
Neels Hofmeyr | 9596b21 | 2020-12-02 09:39:01 +0100 | [diff] [blame] | 6 | import re |
Pau Espin Pedrol | 4e6b507 | 2020-05-11 15:12:07 +0200 | [diff] [blame] | 7 | from osmo_gsm_tester.core import log |
| 8 | from osmo_gsm_tester.core import config |
| 9 | from osmo_gsm_tester.core import util |
| 10 | from osmo_gsm_tester.core import report |
| 11 | from osmo_gsm_tester.core import scenario |
Pau Espin Pedrol | f574a46 | 2020-05-05 12:18:35 +0200 | [diff] [blame] | 12 | from osmo_gsm_tester.core import suite |
Pau Espin Pedrol | 3063730 | 2020-05-06 21:11:02 +0200 | [diff] [blame] | 13 | from osmo_gsm_tester.core.schema import generate_schemas, get_all_schema |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 14 | |
Neels Hofmeyr | 9596b21 | 2020-12-02 09:39:01 +0100 | [diff] [blame] | 15 | import xml.etree.ElementTree as et |
| 16 | |
Pau Espin Pedrol | 6c6c0e8 | 2020-05-11 18:30:58 +0200 | [diff] [blame] | 17 | config.override_conf = os.path.join(os.path.dirname(sys.argv[0]), 'paths.conf') |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 18 | |
Pau Espin Pedrol | 9d489c8 | 2019-09-18 18:43:45 +0200 | [diff] [blame] | 19 | example_trial_dir = os.path.join('test_trial_tmp') |
| 20 | |
| 21 | class FakeTrial(log.Origin): |
| 22 | def __init__(self): |
| 23 | super().__init__(log.C_TST, 'trial') |
| 24 | self.dir = util.Dir(example_trial_dir) |
| 25 | self._run_dir = None |
| 26 | |
| 27 | def get_run_dir(self): |
| 28 | if self._run_dir is not None: |
| 29 | return self._run_dir |
| 30 | self._run_dir = util.Dir(self.dir.new_child('test_run')) |
| 31 | self._run_dir.mkdir() |
| 32 | return self._run_dir |
| 33 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 34 | #log.style_change(trace=True) |
| 35 | |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 36 | # Generate supported schemas dynamically from objects: |
| 37 | generate_schemas() |
| 38 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 39 | print('- non-existing suite dir') |
| 40 | assert(log.run_logging_exceptions(suite.load, 'does_not_exist') == None) |
| 41 | |
| 42 | print('- no suite.conf') |
| 43 | assert(log.run_logging_exceptions(suite.load, 'empty_dir') == None) |
| 44 | |
| 45 | print('- valid suite dir') |
| 46 | example_suite_dir = os.path.join('test_suite') |
| 47 | s_def = suite.load(example_suite_dir) |
| 48 | assert(isinstance(s_def, suite.SuiteDefinition)) |
| 49 | print(config.tostr(s_def.conf)) |
| 50 | |
| 51 | print('- run hello world test') |
Pau Espin Pedrol | 9d489c8 | 2019-09-18 18:43:45 +0200 | [diff] [blame] | 52 | trial = FakeTrial() |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 53 | s = suite.SuiteRun(trial, 'test_suite', s_def) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 54 | results = s.run_tests('hello_world.py') |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 55 | print(report.suite_to_text(s)) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 56 | |
Neels Hofmeyr | 9596b21 | 2020-12-02 09:39:01 +0100 | [diff] [blame] | 57 | print('- run report fragment test') |
| 58 | results = s.run_tests('test_report_fragment.py') |
| 59 | print(report.suite_to_text(s)) |
| 60 | xml = et.tostring(report.suite_to_junit(s)).decode('utf-8') |
| 61 | xml = re.sub('Traceback.*raise', '[BACKTRACE]\nraise', xml, flags=re.M + re.DOTALL) |
| 62 | print('\n\n################################### junit XML:\n' |
| 63 | + xml |
| 64 | + '\n###################################\n\n') |
| 65 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 66 | log.style_change(src=True) |
| 67 | #log.style_change(trace=True) |
| 68 | print('\n- a test with an error') |
| 69 | results = s.run_tests('test_error.py') |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 70 | output = report.suite_to_text(s) |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 71 | print(output) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 72 | |
| 73 | print('\n- a test with a failure') |
| 74 | results = s.run_tests('test_fail.py') |
| 75 | output = report.suite_to_text(s) |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 76 | print(output) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 77 | |
| 78 | print('\n- a test with a raised failure') |
| 79 | results = s.run_tests('test_fail_raise.py') |
| 80 | output = report.suite_to_text(s) |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 81 | print(output) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 82 | |
Pau Espin Pedrol | 0b30279 | 2017-09-10 16:33:10 +0200 | [diff] [blame] | 83 | print('- test with half empty scenario') |
Pau Espin Pedrol | 9d489c8 | 2019-09-18 18:43:45 +0200 | [diff] [blame] | 84 | trial = FakeTrial() |
Pau Espin Pedrol | 4e6b507 | 2020-05-11 15:12:07 +0200 | [diff] [blame] | 85 | sc = scenario.Scenario('foo', 'bar') |
| 86 | sc['resources'] = { 'bts': [{'type': 'osmo-bts-trx'}] } |
| 87 | s = suite.SuiteRun(trial, 'test_suite', s_def, [sc]) |
Pau Espin Pedrol | 0b30279 | 2017-09-10 16:33:10 +0200 | [diff] [blame] | 88 | results = s.run_tests('hello_world.py') |
| 89 | print(report.suite_to_text(s)) |
| 90 | |
| 91 | print('- test with scenario') |
Pau Espin Pedrol | 9d489c8 | 2019-09-18 18:43:45 +0200 | [diff] [blame] | 92 | trial = FakeTrial() |
Pau Espin Pedrol | 4e6b507 | 2020-05-11 15:12:07 +0200 | [diff] [blame] | 93 | sc = scenario.Scenario('foo', 'bar') |
| 94 | sc['resources'] = { 'bts': [{ 'times': '2', 'type': 'osmo-bts-trx', 'trx_list': [{'nominal_power': '10'}, {'nominal_power': '12'}]}, {'type': 'sysmo'}] } |
| 95 | s = suite.SuiteRun(trial, 'test_suite', s_def, [sc]) |
Pau Espin Pedrol | 0b30279 | 2017-09-10 16:33:10 +0200 | [diff] [blame] | 96 | results = s.run_tests('hello_world.py') |
| 97 | print(report.suite_to_text(s)) |
| 98 | |
Pau Espin Pedrol | aab5692 | 2018-08-21 14:58:29 +0200 | [diff] [blame] | 99 | print('- test with scenario and modifiers') |
Pau Espin Pedrol | 9d489c8 | 2019-09-18 18:43:45 +0200 | [diff] [blame] | 100 | trial = FakeTrial() |
Pau Espin Pedrol | 4e6b507 | 2020-05-11 15:12:07 +0200 | [diff] [blame] | 101 | sc = scenario.Scenario('foo', 'bar') |
| 102 | sc['resources'] = { 'bts': [{ 'times': '2', 'type': 'osmo-bts-trx', 'trx_list': [{'nominal_power': '10'}, {'nominal_power': '12'}]}, {'type': 'sysmo'}] } |
| 103 | sc['modifiers'] = { 'bts': [{ 'times': '2', 'trx_list': [{'nominal_power': '20'}, {'nominal_power': '20'}]}, {'type': 'sysmo'}] } |
| 104 | s = suite.SuiteRun(trial, 'test_suite', s_def, [sc]) |
Pau Espin Pedrol | aab5692 | 2018-08-21 14:58:29 +0200 | [diff] [blame] | 105 | s.reserve_resources() |
| 106 | print(repr(s.reserved_resources)) |
| 107 | results = s.run_tests('hello_world.py') |
| 108 | print(report.suite_to_text(s)) |
| 109 | |
Pau Espin Pedrol | 3063730 | 2020-05-06 21:11:02 +0200 | [diff] [blame] | 110 | print('- test with suite-specific config') |
| 111 | trial = FakeTrial() |
Pau Espin Pedrol | 4e6b507 | 2020-05-11 15:12:07 +0200 | [diff] [blame] | 112 | sc = scenario.Scenario('foo', 'bar') |
| 113 | sc['config'] = {'suite': {s.name(): { 'some_suite_global_param': 'heyho', 'test_suite_params': {'one_bool_parameter': 'true', 'second_list_parameter': ['23', '45']}}}} |
| 114 | s = suite.SuiteRun(trial, 'test_suite', s_def, [sc]) |
Pau Espin Pedrol | 3063730 | 2020-05-06 21:11:02 +0200 | [diff] [blame] | 115 | s.reserve_resources() |
| 116 | print(repr(s.reserved_resources)) |
Pau Espin Pedrol | c3cf682 | 2020-06-12 17:54:55 +0200 | [diff] [blame] | 117 | results = s.run_tests(['test_suite_params.py', 'test_timeout.py']) |
Pau Espin Pedrol | 3063730 | 2020-05-06 21:11:02 +0200 | [diff] [blame] | 118 | print(report.suite_to_text(s)) |
| 119 | |
Pau Espin Pedrol | 166dc10 | 2020-06-04 18:44:42 +0200 | [diff] [blame] | 120 | print('- test with template overlay') |
| 121 | trial = FakeTrial() |
| 122 | s_def = suite.load('suiteC') |
| 123 | s = suite.SuiteRun(trial, 'suiteC', s_def) |
| 124 | results = s.run_tests('test_template_overlay.py') |
| 125 | print(report.suite_to_text(s)) |
| 126 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 127 | print('\n- graceful exit.') |
Pau Espin Pedrol | 9d489c8 | 2019-09-18 18:43:45 +0200 | [diff] [blame] | 128 | #deleting generated tmp trial dir: |
| 129 | shutil.rmtree(example_trial_dir, ignore_errors=True) |
| 130 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 131 | # vim: expandtab tabstop=4 shiftwidth=4 |