Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding PYMAPDL_APDL_LOG env var for testing #3328

Merged
merged 4 commits into from
Aug 9, 2024

Conversation

germa89
Copy link
Collaborator

@germa89 germa89 commented Aug 5, 2024

Description

  • Fix not passing apdl_log argument when testing.
  • New env var (for testing only) to log apdl calls.
  • Refactor the autouse fixtures.
  • Logging new tests id in APDL logs.

Issue linked

NA

Checklist

@germa89 germa89 requested a review from a team as a code owner August 5, 2024 17:04
@germa89 germa89 requested review from clatapie and pyansys-ci-bot and removed request for a team August 5, 2024 17:04
@ansys-reviewer-bot
Copy link
Contributor

Thanks for opening a Pull Request. If you want to perform a review write a comment saying:

@ansys-reviewer-bot review

@germa89 germa89 self-assigned this Aug 5, 2024
@github-actions github-actions bot added dependencies maintenance General maintenance of the repo (libraries, cicd, etc) new feature Request or proposal for a new feature labels Aug 5, 2024
Copy link

codecov bot commented Aug 5, 2024

Codecov Report

Attention: Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.

Project coverage is 87.14%. Comparing base (a050034) to head (6576f14).
Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3328      +/-   ##
==========================================
- Coverage   87.17%   87.14%   -0.04%     
==========================================
  Files          55       55              
  Lines        9773     9776       +3     
==========================================
- Hits         8520     8519       -1     
- Misses       1253     1257       +4     

@germa89
Copy link
Collaborator Author

germa89 commented Aug 5, 2024

If we use:

PYMAPDL_APDL_LOG="true"

$ export PYMAPDL_APDL_LOG="true"
$ pytest -k TestFieldComponentValueGetter
============================================================ test session starts ============================================================
platform darwin -- Python 3.10.13, pytest-8.2.2, pluggy-1.5.0 -- /Users/user/pymapdl/.venv/bin/python3.10
--------------------------------------------------------------Testing variables--------------------------------------------------------------
Session dependent: ON_CI (False), TESTING_MINIMAL (False), SUPPORT_PLOTTING (True)
OS dependent: ON_LINUX (False), ON_UBUNTU (False), ON_WINDOWS (False), ON_MACOS (True)
MAPDL dependent: ON_LOCAL (False), ON_STUDENT (False), HAS_GRPC (True), HAS_DPF (True), IS_SMP (True)
------------------------------------------------------------Environment variables------------------------------------------------------------
PYMAPDL_START_INSTANCE ('False'), PYMAPDL_PORT ('50052'), PYMAPDL_DB_PORT ('50095'), DPF_PORT ('50056'), DPF_START_SERVER ('false'), 
-------------------------------------------------------------Pytest configuration------------------------------------------------------------
cachedir: .pytest_cache
rootdir: /Users/german.ayuso/pymapdl
configfile: pyproject.toml
testpaths: tests
plugins: cov-5.0.0, anyio-4.3.0, rerunfailures-14.0, pytest_pyvista-0.1.9, sphinx-0.6.3, memprof-0.2.0
collected 1745 items / 1741 deselected / 4 selected                                                                                         

tests/test_inline_functions/test_field_component_queries.py::TestFieldComponentValueGetter::test_temp PASSED                          [ 25%]
tests/test_inline_functions/test_field_component_queries.py::TestFieldComponentValueGetter::test_pressure FAILED                      [ 50%]
tests/test_inline_functions/test_field_component_queries.py::TestFieldComponentValueGetter::test_volt PASSED                          [ 75%]
tests/test_inline_functions/test_field_component_queries.py::TestFieldComponentValueGetter::test_mag PASSED                           [100%]

================================================================= FAILURES ==================================================================
________________________________________________ TestFieldComponentValueGetter.test_pressure ________________________________________________

self = <test_field_component_queries.TestFieldComponentValueGetter object at 0x1730c6230>
box_with_fields = <ansys.mapdl.core.mapdl_grpc.MapdlGrpc object at 0x1730c4520>

    def test_pressure(self, box_with_fields):
        mapdl = box_with_fields
        mapdl.type(2)
        mapdl.vmesh(1)
        mapdl.d("all", "pres", 5.0)
        mapdl.d("all", "ux", 0.0, lab2="uy", lab3="uz")
        mapdl.slashsolu()
>       mapdl.solve()

tests/test_inline_functions/test_field_component_queries.py:42: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
src/ansys/mapdl/core/_commands/solution/analysis_options.py:3477: in solve
    return self.run(command, **kwargs)
src/ansys/mapdl/core/mapdl_core.py:2242: in run
    self._raise_errors(text)
src/ansys/mapdl/core/mapdl_core.py:2757: in _raise_errors
    self._raise_output_errors(text)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <ansys.mapdl.core.mapdl_grpc.MapdlGrpc object at 0x1730c4520>
response = '*****  MAPDL SOLVE    COMMAND  *****\n\n *** NOTE ***                            CP =     145.177   TIME= 17:06:29\n ...esav\n        0.062 MB WRITTEN ON ASSEMBLED MATRIX FILE: file.full\n        0.312 MB WRITTEN ON RESULTS FILE: file.rst'

    def _raise_output_errors(self, response):
        """Raise errors in the MAPDL response.
    
        Parameters
        ----------
        response : str
            Response from MAPDL.
    
        Raises
        ------
        MapdlRuntimeError
            For most of the errors.
        """
        # The logic is to iterate for each line. If the error header is found,
        # we analyse the following 'lines_number' in other to get the full error method.
        # Then with regex, we collect the error message, and raise it.
        for index, each_line in enumerate(response.splitlines()):
            if "*** ERROR ***" in each_line:
                error_is_fine = False
    
                # Extracting only the first 'lines_number' lines.
                # This is important. Regex has problems parsing long messages.
                lines_number = 20
                if len(response.splitlines()) <= lines_number:
                    partial_output = response
                else:
                    partial_output = "\n".join(
                        response.splitlines()[index : (index + lines_number)]
                    )
    
                # Find the error message.
                # Either ends with the beginning of another error message or with double empty line.
                error_message = re.search(
                    r"(\*\*\* ERROR \*\*\*.*?).*(?=\*\*\*|.*\n\n)",  # we might consider to use only one \n.
                    partial_output,
                    re.DOTALL,
                )
    
                if not error_message:
                    # Since we couldn't find an error message, the full partial message (10 lines) is analysed
                    self._log.debug(
                        f"PyMAPDL could not identify the error message, the full partial message ({lines_number} lines) is analysed"
                    )
                    error_message = partial_output
                else:
                    # Catching only the first error.
                    error_message = error_message.group(0)
    
                # Trimming empty lines
                error_message = "\n".join(
                    [each for each in error_message.splitlines() if each]
                )
    
                # Trimming empty lines
                error_message = "\n".join(
                    [each for each in error_message.splitlines() if each]
                )
    
                # Checking for permitted error.
                for each_error in _PERMITTED_ERRORS:
                    permited_error_message = re.search(each_error, error_message)
    
                    if permited_error_message:
                        error_is_fine = True
                        break
    
                # Raising errors
                if error_is_fine:
                    self._log.warn("PERMITTED ERROR: " + permited_error_message.string)
                    continue
                else:
                    # We don't need to log exception because they already included in the main logger.
                    # logger.error(response)
                    # However, exceptions are recorded in the global logger which do not record
                    # information of the instances name, hence we edit the error message.
>                   raise MapdlRuntimeError(
                        f"\n\nError in instance {self.name}\n\n" + error_message
                    )
E                   ansys.mapdl.core.errors.MapdlRuntimeError: 
E                   
E                   Error in instance GRPC_127.0.0.1:50052
E                   
E                   *** ERROR ***                           CP =     145.177   TIME= 17:06:29
E                    Element type 215 requires the definition of TB,PM with the activation   
E                    of pressure DOF (KEYOPT(12)).                                           
E                    *** NOTE ***                            CP =     145.177   TIME= 17:06:29
E                    Present time 0 is less than or equal to the previous time.  Time will   
E                    default to 1.                                                           
E                    *** NOTE ***                            CP =     145.177   TIME= 17:06:29
E                    This nonlinear analysis defaults to using the full Newton-Raphson       
E                    solution procedure.  This can be modified using the NROPT command.      
E                    *** WARNING ***                         CP =     145.177   TIME= 17:06:29
E                    The program chosen initial timestep/load-factor is arbitrary.  It is    
E                    necessary for the user to supply a suitable initial                     
E                    timestep/load-factor through the NSUB or DELTIM command for             
E                    convergence and overall efficiency.

src/ansys/mapdl/core/mapdl_core.py:2834: MapdlRuntimeError
======================================================= memory consumption estimates ========================================================
pymapdl::tests::test_inline_functions::test_field_component_queries.py::TestFieldComponentValueGetter::test_mag       - 26.3 MB
pymapdl::tests::test_inline_functions::test_field_component_queries.py::TestFieldComponentValueGetter::test_pressure  - 800.0 KB
pymapdl::tests::test_inline_functions::test_field_component_queries.py::TestFieldComponentValueGetter::test_temp      - 400.0 KB
======================================================= PyMAPDL Pytest short summary ========================================================
[FAILED] TestFieldComponentValueGetter.test_pressure - E                    convergence and overall efficiency.
=============================================== 1 failed, 3 passed, 1741 deselected in 3.09s ================================================

and a file apdl.log:

! APDL log script generated using PyMAPDL (ansys.mapdl.core 0.69.dev0)
/POST26
NUMVAR,200
/SOLUTION
/SHOW,PNG,,,
/NERR,,,-1,,
/INQUIRE,,DIRECTORY,,
/CLE,NOSTART
!
! PyMAPDL running test: tests/test_inline_functions/test_field_component_queries.py::TestFieldComponentValueGetter::test_temp (setup)
!
FINISH,
/CLE,NOSTART
/PREP7,
/PREP7,
MP,kxx,1,45,,,,
MP,ex,1,20000000000.0,,,,
MP,perx,1,1,,,,
MP,murx,1,1,,,,
ET,1,SOLID70,,,,,,,
ET,2,CPT215,,,,,,,
KEYOPT,2,12,1
ET,3,SOLID122,,,,,,,
ET,4,SOLID96,,,,,,,
BLOCK,0,1,0,1,0,1
ESIZE,0.5,
TYPE,1
VMESH,1,,
D,all,temp,5.0,,,,,,,,
/SOLU,
SOLVE,
!
! PyMAPDL running test: tests/test_inline_functions/test_field_component_queries.py::TestFieldComponentValueGetter::test_pressure (setup)
!
FINISH,
/CLE,NOSTART
/PREP7,
/PREP7,
MP,kxx,1,45,,,,
MP,ex,1,20000000000.0,,,,
MP,perx,1,1,,,,
MP,murx,1,1,,,,
ET,1,SOLID70,,,,,,,
ET,2,CPT215,,,,,,,
KEYOPT,2,12,1
ET,3,SOLID122,,,,,,,
ET,4,SOLID96,,,,,,,
BLOCK,0,1,0,1,0,1
ESIZE,0.5,
TYPE,2
VMESH,1,,
D,all,pres,5.0,,,,,,,,
D,all,ux,0.0,,,,uy,uz,,,
/SOLU,
SOLVE,
!
! PyMAPDL running test: tests/test_inline_functions/test_field_component_queries.py::TestFieldComponentValueGetter::test_volt (setup)
!
FINISH,
/CLE,NOSTART
/PREP7,
/PREP7,
MP,kxx,1,45,,,,
MP,ex,1,20000000000.0,,,,
MP,perx,1,1,,,,
MP,murx,1,1,,,,
ET,1,SOLID70,,,,,,,
ET,2,CPT215,,,,,,,
KEYOPT,2,12,1
ET,3,SOLID122,,,,,,,
ET,4,SOLID96,,,,,,,
BLOCK,0,1,0,1,0,1
ESIZE,0.5,
TYPE,3
VMESH,1,,
D,all,volt,5.0,,,,,,,,
/SOLU,
SOLVE,
!
! PyMAPDL running test: tests/test_inline_functions/test_field_component_queries.py::TestFieldComponentValueGetter::test_mag (setup)
!
FINISH,
/CLE,NOSTART
/PREP7,
/PREP7,
MP,kxx,1,45,,,,
MP,ex,1,20000000000.0,,,,
MP,perx,1,1,,,,
MP,murx,1,1,,,,
ET,1,SOLID70,,,,,,,
ET,2,CPT215,,,,,,,
KEYOPT,2,12,1
ET,3,SOLID122,,,,,,,
ET,4,SOLID96,,,,,,,
BLOCK,0,1,0,1,0,1
ESIZE,0.5,
TYPE,4
VMESH,1,,
D,all,mag,5.0,,,,,,,,
/SOLU,
SOLVE,

PYMAPDL_APDL_LOG="myapdl.log"

We can specify another file name:

$ export PYMAPDL_APDL_LOG="myapdl.log"
$ pytest -k TestFieldComponentValueGetter

which will generate a file named myapdl.log.

@germa89
Copy link
Collaborator Author

germa89 commented Aug 9, 2024

@pyansys-ci-bot LGTM.

Copy link
Contributor

@pyansys-ci-bot pyansys-ci-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approving this PR because germa89 said so in here 😬

LGTM

@germa89 germa89 merged commit 893f741 into main Aug 9, 2024
65 of 66 checks passed
@germa89 germa89 deleted the feat/adding-apdl-logging-to-testing branch August 9, 2024 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies maintenance General maintenance of the repo (libraries, cicd, etc) new feature Request or proposal for a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants