def test_build_pwsh_analyze(): """Build Pytest command with json""" from demisto_sdk.commands.lint.commands_builder import \ build_pwsh_analyze_command file = MagicMock() command = f"pwsh -Command Invoke-ScriptAnalyzer -EnableExit -Path {file.name}" assert command == build_pwsh_analyze_command(file)
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