Extract common testcase class

This commit is contained in:
Christophe Bedard 2019-06-18 15:42:44 +02:00
parent cc3c142f9f
commit 05d60cb57a
9 changed files with 194 additions and 223 deletions

View file

@ -0,0 +1,89 @@
# Copyright 2019 Robert Bosch GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Module for a tracing-specific unittest.TestCase extension."""
from typing import List
from typing import Set
import unittest
from .utils import cleanup_trace
from .utils import get_trace_events
from .utils import run_and_trace
class TraceTestCase(unittest.TestCase):
"""
TestCase extension for tests on a trace.
Sets up tracing, traces given nodes, and provides
the resulting events for an extending class to test on.
It also does some basic checks on the resulting trace.
"""
def __init__(
self,
*args,
session_name_prefix: str,
events_ros: List[str],
nodes: List[str],
base_path: str = '/tmp',
events_kernel: List[str] = None,
package: str = 'tracetools_test'
) -> None:
"""Constructor."""
print(f'methodName={args[0]}')
super().__init__(methodName=args[0])
self._base_path = base_path
self._session_name_prefix = session_name_prefix
self._events_ros = events_ros
self._events_kernel = events_kernel
self._package = package
self._nodes = nodes
def setUp(self):
exit_code, full_path = run_and_trace(
self._base_path,
self._session_name_prefix,
self._events_ros,
self._events_kernel,
self._package,
self._nodes)
print(f'trace directory: {full_path}')
self._exit_code = exit_code
self._full_path = full_path
# Check that setUp() ran fine
self.assertEqual(self._exit_code, 0)
# Read events once
self._events = get_trace_events(self._full_path)
self._event_names = self._get_event_names()
# Check that the enabled events are present
ros = set(self._events_ros) if self._events_ros is not None else set()
kernel = set(self._events_kernel) if self._events_kernel is not None else set()
all_event_names = ros | kernel
self.assertSetEqual(all_event_names, self._event_names)
def tearDown(self):
cleanup_trace(self._full_path)
def _get_event_names(self) -> Set[str]:
"""Get a set of names of the events in the trace."""
events_names = set()
for event in self._events:
events_names.add(event.name)
return events_names

View file

@ -17,8 +17,8 @@
import os
import shutil
import time
from typing import Dict
from typing import List
from typing import Set
from typing import Tuple
import babeltrace
@ -41,7 +41,7 @@ def run_and_trace(
kernel_events: List[str],
package_name: str,
node_names: List[str]
) -> Tuple[int, str]:
) -> Tuple[int, str]:
"""
Run a node while tracing.
@ -55,7 +55,6 @@ def run_and_trace(
"""
session_name = f'{session_name_prefix}-{time.strftime("%Y%m%d%H%M%S")}'
full_path = os.path.join(base_path, session_name)
print(f'trace directory: {full_path}')
lttng_setup(session_name, full_path, ros_events=ros_events, kernel_events=kernel_events)
lttng_start(session_name)
@ -89,19 +88,14 @@ def cleanup_trace(full_path: str) -> None:
shutil.rmtree(full_path)
def get_trace_event_names(trace_directory: str) -> Set[str]:
def get_trace_events(trace_directory: str) -> List[Dict[str, str]]:
"""
Get a set of event names in a trace.
Get the events of a trace.
:param trace_directory: the path to the main/top trace directory
:return: event names
:return: events
"""
tc = babeltrace.TraceCollection()
tc.add_traces_recursive(trace_directory, 'ctf')
event_names = set()
for event in tc.events:
event_names.add(event.name)
return event_names
return tc.events