def _docker_run_pwsh_analyze(self, test_image: str, keep_container: bool) -> Tuple[int, str]: """ Run Powershell code analyze in created test image Args: test_image(str): test image id/name keep_container(bool): True if to keep container after excution finished Returns: int: 0 on successful, errors 1, need to retry 2 str: Container log """ log_prompt = f'{self._pack_name} - Powershell analyze - Image {test_image}' logger.info(f"{log_prompt} - Start") container_name = f"{self._pack_name}-pwsh-analyze" # Check if previous run left container a live if it do, we remove it container_obj: docker.models.containers.Container try: container_obj = self._docker_client.containers.get(container_name) container_obj.remove(force=True) except docker.errors.NotFound: pass # Run container exit_code = SUCCESS output = "" try: container_obj = self._docker_client.containers.run( name=container_name, image=test_image, command=build_pwsh_analyze_command( self._facts["lint_files"][0]), user=f"{os.getuid()}:4000", detach=True, environment=self._facts["env_vars"]) stream_docker_container_output(container_obj.logs(stream=True)) # wait for container to finish container_status = container_obj.wait(condition="exited") # Get container exit code container_exit_code = container_status.get("StatusCode") # Getting container logs container_log = container_obj.logs().decode("utf-8") logger.info(f"{log_prompt} - exit-code: {container_exit_code}") if container_exit_code: # 1-fatal message issued # 2-Error message issued logger.info(f"{log_prompt} - Finished errors found") output = container_log exit_code = FAIL else: logger.info(f"{log_prompt} - Successfully finished") # Keeping container if needed or remove it if keep_container: print(f"{log_prompt} - container name {container_name}") else: try: container_obj.remove(force=True) except docker.errors.NotFound as e: logger.critical( f"{log_prompt} - Unable to delete container - {e}") except (docker.errors.ImageNotFound, docker.errors.APIError) as e: logger.critical( f"{log_prompt} - Unable to run powershell test - {e}") exit_code = RERUN return exit_code, output
def _docker_run_pytest(self, test_image: str, keep_container: bool, test_xml: str) -> Tuple[int, str, dict]: """ Run Pytest in created test image Args: test_image(str): Test image id/name keep_container(bool): True if to keep container after execution finished test_xml(str): Xml saving path Returns: int: 0 on successful, errors 1, need to retry 2 str: Unit test json report """ log_prompt = f'{self._pack_name} - Pytest - Image {test_image}' logger.info(f"{log_prompt} - Start") container_name = f"{self._pack_name}-pytest" # Check if previous run left container a live if it does, Remove it container_obj: docker.models.containers.Container try: container_obj = self._docker_client.containers.get(container_name) container_obj.remove(force=True) except docker.errors.NotFound: pass # Collect tests exit_code = SUCCESS output = '' test_json = {} try: # Running pytest container container_obj = self._docker_client.containers.run( name=container_name, image=test_image, command=[build_pytest_command(test_xml=test_xml, json=True)], user=f"{os.getuid()}:4000", detach=True, environment=self._facts["env_vars"]) stream_docker_container_output(container_obj.logs(stream=True)) # Waiting for container to be finished container_status: dict = container_obj.wait(condition="exited") # Getting container exit code container_exit_code = container_status.get("StatusCode") # Getting container logs logger.info(f"{log_prompt} - exit-code: {container_exit_code}") if container_exit_code in [0, 1, 2, 5]: # 0-All tests passed # 1-Tests were collected and run but some of the tests failed # 2-Test execution was interrupted by the user # 5-No tests were collected if test_xml: test_data_xml = get_file_from_container( container_obj=container_obj, container_path="/devwork/report_pytest.xml") xml_apth = Path(test_xml) / f'{self._pack_name}_pytest.xml' with open(file=xml_apth, mode='bw') as f: f.write(test_data_xml) # type: ignore test_json = json.loads( get_file_from_container( container_obj=container_obj, container_path="/devwork/report_pytest.json", encoding="utf-8")) for test in test_json.get('report', {}).get("tests"): if test.get("call", {}).get("longrepr"): test["call"]["longrepr"] = test["call"][ "longrepr"].split('\n') if container_exit_code in [0, 5]: logger.info(f"{log_prompt} - Successfully finished") exit_code = SUCCESS elif container_exit_code in [2]: output = container_obj.logs().decode('utf-8') exit_code = FAIL else: logger.info(f"{log_prompt} - Finished errors found") exit_code = FAIL elif container_exit_code in [3, 4]: # 3-Internal error happened while executing tests # 4-pytest command line usage error logger.critical(f"{log_prompt} - Usage error") exit_code = RERUN output = container_obj.logs().decode('utf-8') # Remove container if not needed if keep_container: print(f"{log_prompt} - Container name {container_name}") else: try: container_obj.remove(force=True) except docker.errors.NotFound as e: logger.critical( f"{log_prompt} - Unable to remove container {e}") except (docker.errors.ImageNotFound, docker.errors.APIError) as e: logger.critical( f"{log_prompt} - Unable to run pytest container {e}") exit_code = RERUN return exit_code, output, test_json