blob: be4b3c49a25db07b53e7b0c9ec53b3459969b7c4 [file] [log] [blame]
Neels Hofmeyr3531a192017-03-28 14:30:28 +02001#!/usr/bin/env python3
2import os
Pau Espin Pedrol30637302020-05-06 21:11:02 +02003import sys
Neels Hofmeyr3531a192017-03-28 14:30:28 +02004import _prep
Pau Espin Pedrol9d489c82019-09-18 18:43:45 +02005import shutil
Neels Hofmeyr9596b212020-12-02 09:39:01 +01006import re
Pau Espin Pedrol4e6b5072020-05-11 15:12:07 +02007from osmo_gsm_tester.core import log
8from osmo_gsm_tester.core import config
9from osmo_gsm_tester.core import util
10from osmo_gsm_tester.core import report
11from osmo_gsm_tester.core import scenario
Pau Espin Pedrolf574a462020-05-05 12:18:35 +020012from osmo_gsm_tester.core import suite
Pau Espin Pedrol30637302020-05-06 21:11:02 +020013from osmo_gsm_tester.core.schema import generate_schemas, get_all_schema
Neels Hofmeyr3531a192017-03-28 14:30:28 +020014
Neels Hofmeyr9596b212020-12-02 09:39:01 +010015import xml.etree.ElementTree as et
16
Pau Espin Pedrol6c6c0e82020-05-11 18:30:58 +020017config.override_conf = os.path.join(os.path.dirname(sys.argv[0]), 'paths.conf')
Neels Hofmeyr3531a192017-03-28 14:30:28 +020018
Pau Espin Pedrol9d489c82019-09-18 18:43:45 +020019example_trial_dir = os.path.join('test_trial_tmp')
20
21class 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 Hofmeyr3531a192017-03-28 14:30:28 +020034#log.style_change(trace=True)
35
Pau Espin Pedrolea8c3d42020-05-04 12:05:05 +020036# Generate supported schemas dynamically from objects:
37generate_schemas()
38
Neels Hofmeyr3531a192017-03-28 14:30:28 +020039print('- non-existing suite dir')
40assert(log.run_logging_exceptions(suite.load, 'does_not_exist') == None)
41
42print('- no suite.conf')
43assert(log.run_logging_exceptions(suite.load, 'empty_dir') == None)
44
45print('- valid suite dir')
46example_suite_dir = os.path.join('test_suite')
47s_def = suite.load(example_suite_dir)
48assert(isinstance(s_def, suite.SuiteDefinition))
49print(config.tostr(s_def.conf))
50
51print('- run hello world test')
Pau Espin Pedrol9d489c82019-09-18 18:43:45 +020052trial = FakeTrial()
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020053s = suite.SuiteRun(trial, 'test_suite', s_def)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020054results = s.run_tests('hello_world.py')
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020055print(report.suite_to_text(s))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020056
Neels Hofmeyr9596b212020-12-02 09:39:01 +010057print('- run report fragment test')
58results = s.run_tests('test_report_fragment.py')
59print(report.suite_to_text(s))
60xml = et.tostring(report.suite_to_junit(s)).decode('utf-8')
61xml = re.sub('Traceback.*raise', '[BACKTRACE]\nraise', xml, flags=re.M + re.DOTALL)
62print('\n\n################################### junit XML:\n'
63 + xml
64 + '\n###################################\n\n')
65
Neels Hofmeyr3531a192017-03-28 14:30:28 +020066log.style_change(src=True)
67#log.style_change(trace=True)
68print('\n- a test with an error')
69results = s.run_tests('test_error.py')
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020070output = report.suite_to_text(s)
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020071print(output)
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020072
73print('\n- a test with a failure')
74results = s.run_tests('test_fail.py')
75output = report.suite_to_text(s)
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020076print(output)
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020077
78print('\n- a test with a raised failure')
79results = s.run_tests('test_fail_raise.py')
80output = report.suite_to_text(s)
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020081print(output)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020082
Pau Espin Pedrol0b302792017-09-10 16:33:10 +020083print('- test with half empty scenario')
Pau Espin Pedrol9d489c82019-09-18 18:43:45 +020084trial = FakeTrial()
Pau Espin Pedrol4e6b5072020-05-11 15:12:07 +020085sc = scenario.Scenario('foo', 'bar')
86sc['resources'] = { 'bts': [{'type': 'osmo-bts-trx'}] }
87s = suite.SuiteRun(trial, 'test_suite', s_def, [sc])
Pau Espin Pedrol0b302792017-09-10 16:33:10 +020088results = s.run_tests('hello_world.py')
89print(report.suite_to_text(s))
90
91print('- test with scenario')
Pau Espin Pedrol9d489c82019-09-18 18:43:45 +020092trial = FakeTrial()
Pau Espin Pedrol4e6b5072020-05-11 15:12:07 +020093sc = scenario.Scenario('foo', 'bar')
94sc['resources'] = { 'bts': [{ 'times': '2', 'type': 'osmo-bts-trx', 'trx_list': [{'nominal_power': '10'}, {'nominal_power': '12'}]}, {'type': 'sysmo'}] }
95s = suite.SuiteRun(trial, 'test_suite', s_def, [sc])
Pau Espin Pedrol0b302792017-09-10 16:33:10 +020096results = s.run_tests('hello_world.py')
97print(report.suite_to_text(s))
98
Pau Espin Pedrolaab56922018-08-21 14:58:29 +020099print('- test with scenario and modifiers')
Pau Espin Pedrol9d489c82019-09-18 18:43:45 +0200100trial = FakeTrial()
Pau Espin Pedrol4e6b5072020-05-11 15:12:07 +0200101sc = scenario.Scenario('foo', 'bar')
102sc['resources'] = { 'bts': [{ 'times': '2', 'type': 'osmo-bts-trx', 'trx_list': [{'nominal_power': '10'}, {'nominal_power': '12'}]}, {'type': 'sysmo'}] }
103sc['modifiers'] = { 'bts': [{ 'times': '2', 'trx_list': [{'nominal_power': '20'}, {'nominal_power': '20'}]}, {'type': 'sysmo'}] }
104s = suite.SuiteRun(trial, 'test_suite', s_def, [sc])
Pau Espin Pedrolaab56922018-08-21 14:58:29 +0200105s.reserve_resources()
106print(repr(s.reserved_resources))
107results = s.run_tests('hello_world.py')
108print(report.suite_to_text(s))
109
Pau Espin Pedrol30637302020-05-06 21:11:02 +0200110print('- test with suite-specific config')
111trial = FakeTrial()
Pau Espin Pedrol4e6b5072020-05-11 15:12:07 +0200112sc = scenario.Scenario('foo', 'bar')
113sc['config'] = {'suite': {s.name(): { 'some_suite_global_param': 'heyho', 'test_suite_params': {'one_bool_parameter': 'true', 'second_list_parameter': ['23', '45']}}}}
114s = suite.SuiteRun(trial, 'test_suite', s_def, [sc])
Pau Espin Pedrol30637302020-05-06 21:11:02 +0200115s.reserve_resources()
116print(repr(s.reserved_resources))
Pau Espin Pedrolc3cf6822020-06-12 17:54:55 +0200117results = s.run_tests(['test_suite_params.py', 'test_timeout.py'])
Pau Espin Pedrol30637302020-05-06 21:11:02 +0200118print(report.suite_to_text(s))
119
Pau Espin Pedrol166dc102020-06-04 18:44:42 +0200120print('- test with template overlay')
121trial = FakeTrial()
122s_def = suite.load('suiteC')
123s = suite.SuiteRun(trial, 'suiteC', s_def)
124results = s.run_tests('test_template_overlay.py')
125print(report.suite_to_text(s))
126
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200127print('\n- graceful exit.')
Pau Espin Pedrol9d489c82019-09-18 18:43:45 +0200128#deleting generated tmp trial dir:
129shutil.rmtree(example_trial_dir, ignore_errors=True)
130
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200131# vim: expandtab tabstop=4 shiftwidth=4