blob: 8c17a49e7db4b2fc0f33fc9ed748e8f42f8bbe07 [file]
"""Recipe for running unit test on a given deivce.
The unit test running on a device involves:
1. Download the OTA zip file
2. Install the OTA
3. Copy test dependencies to the device
4. Run unit test cases
"""
import os
import shlex
import sys
from slave import base_recipe
from slave.step import device_unit_test_step
from slave.step import fetch_ota_step
def GetValidBuildNames():
return ['device_unittest']
def CreateRecipe(build_name: str, **kwargs):
"""Builds a recipe object."""
del build_name # Unused.
property_dict = kwargs['properties']
mandatory_field_list = ['product_name',
'branch_name',
'build_number',
'build_flavor',
'board_type',
'device_id',
'repo_alias']
assert all(mandatory_field in property_dict
for mandatory_field in mandatory_field_list)
return DeviceUnittestRecipe(**kwargs)
class DeviceUnittestRecipe(base_recipe.BaseRecipe):
"""Recipe to run unit tests on a device."""
def __init__(self, **kwargs):
base_recipe.BaseRecipe.__init__(self, **kwargs)
self._property_dict = kwargs['properties']
self._work_dir = kwargs['properties']['recipe_tmp_dir']
def get_tmp_dir(self):
return self._step_kwargs['executor'].get_tmp_dir()
def get_python_path_dict(self):
"""Returns PYTHONPATH used in the unit test."""
python_path_list = sys.path[:]
for py_path in ('test', 'continuous-tests/build'):
python_path_list.append(os.path.join(self._work_dir, py_path))
return {'PYTHONPATH': ':'.join(python_path_list)}
def get_install_ota_command_list(self):
cmd = ('python %s/test/ota/ota_update.py '
'--bypass_init_availability_check '
'--ota_package=%s/ota.zip '
'--mandate_external_storage '
'--use_tunnel '
'--expected_build=%s '
'--expected_build_flavor=%s '
'--device_id=%s '
'--debug ;' % (
self._work_dir,
self.get_tmp_dir(),
self._property_dict['build_number'],
self._property_dict['build_flavor'],
self._property_dict['device_id']))
return shlex.split(cmd)
def get_copy_dependencies_command_list(self):
slave_name = os.path.basename(self.get_tmp_dir())
cmd = ('python %s/test/unit_test/prep_device.py '
'--device_id=%s '
'--repo_alias=%s '
'--branch_name=%s '
'--target_name=%s-%s '
'--build_number=%s '
'--slave_name=%s '
'--test_binary_dir=%s/unit_test_binaries '
'--debug' % (
self._work_dir,
self._property_dict['device_id'],
self._property_dict['repo_alias'],
self._property_dict['branch_name'],
self._property_dict['product_name'],
self._property_dict['build_flavor'],
self._property_dict['build_number'],
slave_name,
self.get_tmp_dir()))
return shlex.split(cmd)
def get_run_test_command_list(self):
slave_name = os.path.basename(self.get_tmp_dir())
cmd = ('python3 %s/test/unit_test/run_unit_test_2.py '
'--debug '
'--use_tunnel '
'--repo_alias=%s '
'--device_id=%s '
'--branch_name=%s '
'--report_directory_on_workstation=%s '
'--unittest_command_list_file=%s/%s/run_test_list.txt '
'--test_binary_dir=%s/unit_test_binaries '
'--cloud_logs_path=%s' % (
self._work_dir,
self._property_dict['repo_alias'],
self._property_dict['device_id'],
self._property_dict['branch_name'],
self._property_dict['local_log_directory'],
self._work_dir,
slave_name,
self.get_tmp_dir(),
self._property_dict['cloud_logs_path']))
return shlex.split(cmd)
def get_setup_steps(self):
# Bypass all the repo operations
return []
def get_steps(self):
slave_name = os.path.basename(self.get_tmp_dir())
return [
fetch_ota_step.FetchOtaStep(
branch=self._property_dict['branch_name'],
product=self._property_dict['product_name'],
board=self._property_dict['board_type'],
flavor=self._property_dict['build_flavor'],
work_dir=self.get_tmp_dir(),
**self._step_kwargs),
device_unit_test_step.UnitTestShellStep(
name='installing',
command=self.get_install_ota_command_list(),
directory=self._work_dir,
env=self.get_python_path_dict(),
halt_on_failure=True,
**self._step_kwargs),
device_unit_test_step.UnitTestShellStep(
name='Create unit test binary directory',
command=['mkdir', '%s/unit_test_binaries' % self.get_tmp_dir()],
directory=self._work_dir,
env=self.get_python_path_dict(),
halt_on_failure=True,
**self._step_kwargs),
device_unit_test_step.UnitTestShellStep(
name='Copy test dependencies to device',
command=self.get_copy_dependencies_command_list(),
directory=self._work_dir,
env=self.get_python_path_dict(),
halt_on_failure=True,
**self._step_kwargs),
device_unit_test_step.UnitTestShellStep(
name='Run unit test on device',
command=self.get_run_test_command_list(),
directory=self._work_dir,
env=self.get_python_path_dict(),
halt_on_failure=True,
**self._step_kwargs),
device_unit_test_step.UnitTestShellStep(
name='Clean up unit test directory',
command=['rm', '-rf', '%s/%s' % (self._work_dir, slave_name)],
directory=self._work_dir,
env=self.get_python_path_dict(),
halt_on_failure=True,
**self._step_kwargs)
]