Пример #1
0
    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
def test_build_pytest_command_2():
    """Build Pytest command with json"""
    from demisto_sdk.commands.lint.commands_builder import build_pytest_command
    command = "python -m pytest -ra --junitxml=/devwork/report_pytest.xml --json=/devwork/report_pytest.json"
    assert command == build_pytest_command(test_xml="test",
                                           json=True)
def test_build_pytest_command_3():
    """Build Pytest command with cov"""
    from demisto_sdk.commands.lint.commands_builder import build_pytest_command
    command = "python -m pytest -ra --junitxml=/devwork/report_pytest.xml --cov-report= --cov=test"
    assert command == build_pytest_command(test_xml="test", cov="test")
Пример #4
0
def test_build_pytest_command_1():
    """Build Pytest command without json"""
    from demisto_sdk.commands.lint.commands_builder import build_pytest_command
    command = "python -m pytest --junitxml=/devwork/report_pytest.xml"
    assert command == build_pytest_command(test_xml="test")