def test_docker_image(self, image: typing.Tuple[Image, str], commands: typing.List[str], test_name: str, is_cached: bool = False, **kwargs: typing.Optional[typing.Dict]): """Running list of commands inside the container, logging the output and handling possible exceptions""" if isinstance(image, Image): image_tag = image.tags[0] elif isinstance(image, str): image_tag = image else: raise FailedTestError( f'{image} is not a proper image, must be of "str" or "docker.models.images.Image"' ) logfile = self._get_logfile_path(image_tag, test_name) run_kwargs = { 'auto_remove': True, 'detach': True, 'use_config_proxy': True, 'environment': get_system_proxy(), 'stdin_open': True, 'tty': True, 'user': '******' } if kwargs is not None: run_kwargs.update(kwargs) try: if self.container and image not in self.container.image.tags: self.container.stop() self.container = None if self.container and not is_cached: self.container.stop() if not self.container or not is_cached: try: self.client.images.get(image_tag) except ImageNotFound: image_tag_full = f'{self.registry}{"/" if self.registry else ""}{image_tag}' log.warning( f'Image {image_tag_full} not found. Trying to pull it...' ) self.client.images.pull(image_tag_full) self.client.images.get(image_tag_full).tag(image_tag) self.container = self.client.containers.run(image=image, **run_kwargs) except APIError as err: raise FailedTestError( f'Docker daemon API error while starting the container: {err}') if not self.container: raise FailedTestError('Cannot create/start the container') try: self._exec_and_log_output(commands, logfile, f'Test {test_name}') except APIError as err: raise FailedTestError( f'Docker daemon API error while executing test {test_name}: {err}' )
def __init__(self, image, container_name, init_commands: typing.List[str], registry='', **init_kwargs: typing.Optional[typing.Dict]): super().__init__(registry) if isinstance(image, Image): image_tag = image.tags[0] elif isinstance(image, str): image_tag = image else: raise FailedTestError( f'{image} is not a proper image, must be of "str" or "docker.models.images.Image"' ) self.container_name: str = container_name self.image_tag: str = image_tag logfile = self._get_logfile_path(self.image_tag, container_name) run_kwargs = { 'auto_remove': True, 'detach': True, 'use_config_proxy': True, 'environment': get_system_proxy(), 'stdin_open': True, 'tty': True, 'user': '******' } if init_kwargs is not None: run_kwargs.update(init_kwargs) try: self.container = self.client.containers.run(image=image, **run_kwargs) except APIError as err: raise FailedTestError( f'Docker daemon API error while starting the container: {err}') if not self.container: raise FailedTestError('Cannot create/start the container') try: self._exec_and_log_output(init_commands, logfile, f'Init {container_name}') except APIError as err: raise FailedTestError( f'Docker daemon API error while initializing container {container_name}: {err}' )
def setup_build_args(self): """Setting up arguments passed to `docker build` command""" self.kwargs.update({ 'product_name': self.product_name, 'package_url': self.args.package_url, 'build_id': self.args.build_id, 'year': self.args.year, 'distribution': self.args.distribution, }) self.kwargs.update(get_system_proxy()) self.kwargs.update(INTEL_OCL_RELEASE[self.args.ocl_release]) if self.args.build_arg: for arg in self.args.build_arg: self.kwargs.update({arg.split('=')[0]: arg.split('=')[-1]})
def setup_build_args(self): """Setting up arguments passed to a template engine and `docker build` command""" self.kwargs.update({ 'product_name': self.product_name, 'product_version': self.args.product_version, 'package_url': self.args.package_url, 'build_id': self.args.build_id, 'year': self.args.year, 'distribution': self.args.distribution, 'openshift': self.args.openshift, 'os': self.args.os, 'INTEL_OPENCL': self.args.ocl_release, }) self.kwargs.update(get_system_proxy()) if self.args.build_arg: for arg in self.args.build_arg: self.kwargs.update({arg.split('=')[0]: arg.split('=')[-1]})
def setup_build_args(self): """Setting up arguments passed to a template engine and `docker build` command""" self.kwargs.update({ 'product_name': self.product_name, 'product_version': self.args.product_version, 'package_url': self.args.package_url, 'build_id': self.args.build_id, 'year': self.args.year, 'distribution': self.args.distribution, 'rhel_platform': self.args.rhel_platform, 'os': self.args.os, 'OPENVINO_WHEELS_URL': self.args.wheels_url, 'OPENVINO_WHEELS_VERSION': self.args.wheels_version, }) self.kwargs.update(get_system_proxy()) if self.args.build_arg: for arg in self.args.build_arg: self.kwargs.update({arg.split('=')[0]: arg.split('=')[-1]})
def test_docker_image(self, image: typing.Tuple[Image, str], commands: typing.List[str], test_name: str, is_cached: bool = False, **kwargs: typing.Optional[typing.Dict]): """Running list of commands inside the container, logging the output and handling possible exceptions""" if isinstance(image, Image): file_tag = image.tags[0].replace('/', '_').replace(':', '_') elif isinstance(image, str): file_tag = image.replace('/', '_').replace(':', '_') else: raise FailedTest( f'{image} is not a proper image, must be of "str" or "docker.models.images.Image"' ) log_filename = f'{test_name}.log' logfile = pathlib.Path( self.location) / 'logs' / file_tag / log_filename run_kwargs = { 'auto_remove': True, 'detach': True, 'use_config_proxy': True, 'environment': get_system_proxy(), 'stdin_open': True, 'tty': True, 'user': '******' } if kwargs is not None: run_kwargs.update(kwargs) try: if self.container and image not in self.container.image.tags: self.container.stop() self.container = None if self.container and not is_cached: self.container.stop() if not self.container or not is_cached: self.container = self.client.containers.run(image=image, **run_kwargs) except APIError as err: raise FailedTest( f'Docker daemon API error while starting the container: {err}') if not self.container: raise FailedTest('Cannot create/start the container') try: output_total = [] for command in commands: output_total.append( f' === executing command: {command} ===') exit_code, output = self.container.exec_run(cmd=command) output_total.append(output.decode('utf-8')) if exit_code != 0: log.error( f'- Test {test_name}: command {command} have returned non-zero exit code {exit_code}' ) log.error(f'Failed command stdout: {output_total[-1]}') logger.switch_to_custom(logfile, str(logfile.parent)) for output in output_total: log.error(str(output)) logger.switch_to_summary() raise FailedTest( f'Test {test_name}: command {command} ' f'have returned non-zero exit code {exit_code}') self.container.reload() if self.container.status != 'running': raise FailedTest( f'Test {test_name}: command exit code is 0, but container status != "running" ' 'after this command') logger.switch_to_custom(logfile, str(logfile.parent)) for output in output_total: log.info(str(output)) logger.switch_to_summary() except APIError as err: raise FailedTest( f'Docker daemon API error while executing test {test_name}: {err}' )
def test_get_system_proxy(mock_data, res): with mock.patch('os.getenv') as mock_env: mock_env.return_value = mock_data assert utilities.get_system_proxy() == res # noqa: S101 # nosec