Skip to content

Update ptf_runner to detect PTF image type and test#13644

Merged
wangxin merged 20 commits intosonic-net:masterfrom
opcoder0:sagummaraj/ptf-py3
Aug 21, 2024
Merged

Update ptf_runner to detect PTF image type and test#13644
wangxin merged 20 commits intosonic-net:masterfrom
opcoder0:sagummaraj/ptf-py3

Conversation

@opcoder0
Copy link
Contributor

@opcoder0 opcoder0 commented Jul 12, 2024

The PR makes the following changes with backward compatibility (with older branches to allow backporting) -

  1. Enhances ptf_runner function to detect the python environment and set ptf binary path accordingly.
  2. Removes use of is_python3
  3. Adds exabgp v4 config and uses six to keep the calls to Py2 and Py3 to support back-porting.

The PR makes backward compatible changes to support -

  • Python 2 only
  • Python 2 + Python 3 (mixed environments with python3 virtualenv)
  • Python 3 only environments

Summary:
Fixes # Not applicable

Type of change

  • Bug fix
  • Testbed and Framework(new/improvement)
  • Test case(new/improvement)

Back port request

  • 202012
  • 202205
  • 202305
  • 202311
  • 202405

Approach

What is the motivation for this PR?

As Python 2 has reached EOL it is important to migrate all the PTF test scripts running in Python 2 to 3.

How did you do it?

  1. Create a Python 3 only PTF image buildable with sonic-buildimage - Completed
  2. Migrate individual PTF test scripts to Python 3 - Completed (several PRs)
  3. Update test scripts to run in the Python 3 only environment - This PR

How did you verify/test it?

By running individual PTF tests on S6100 and Cisco 8111 devices.

Any platform specific information?

These changes are not platform specific.

Supported testbed topology if it's a new test case?

Not applicable

- Modify http_api to work with Flask in Py3 environment
- Modify exabgp configuration to be compatible with v4
- All PTF test scripts are Py3 compatible the 'py3' directory
  is no longer required and hence removed.
- PTF path on PTF container is /usr/local/bin/ptf
- Remove Python 3 virtual environment path /root/env-python3
  and use Python 3 from global environment /usr/bin
- Remove is_python3 from ptf_runner and update all callers
@opcoder0 opcoder0 requested a review from StormLiangMS as a code owner July 12, 2024 04:34
@opcoder0 opcoder0 marked this pull request as draft July 12, 2024 04:34
@mssonicbld
Copy link
Collaborator

The pre-commit check detected issues in the files touched by this pull request.
The pre-commit check is a mandatory check, please fix detected issues.

Detailed pre-commit check results:
trim trailing whitespace.................................................Passed
fix end of files.........................................................Passed
check yaml...........................................(no files to check)Skipped
check for added large files..............................................Passed
check python ast.........................................................Passed
flake8...................................................................Failed
- hook id: flake8
- exit code: 1

tests/ptf_runner.py:89:24: F821 undefined name 'pdb'
tests/ptf_runner.py:102:12: F821 undefined name 'pdb'

flake8...............................................(no files to check)Skipped
check conditional mark sort..........................(no files to check)Skipped

To run the pre-commit checks locally, you can follow below steps:

  1. Ensure that default python is python3. In sonic-mgmt docker container, default python is python2. You can run
    the check by activating the python3 virtual environment in sonic-mgmt docker container or outside of sonic-mgmt
    docker container.
  2. Ensure that the pre-commit package is installed:
sudo pip install pre-commit
  1. Go to repository root folder
  2. Install the pre-commit hooks:
pre-commit install
  1. Use pre-commit to check staged file:
pre-commit
  1. Alternatively, you can check committed files using:
pre-commit run --from-ref <commit_id> --to-ref <commit_id>

@opcoder0 opcoder0 marked this pull request as ready for review August 5, 2024 06:40
@opcoder0
Copy link
Contributor Author

opcoder0 commented Aug 7, 2024

These build failures have been fixed in the PR #14001

@opcoder0 opcoder0 changed the title Update all PTF scripts to run in Python 3 (py3) PTF container Update ptf_runner to detect PTF image type and test Aug 8, 2024
http_api_py = '''\
from flask import Flask, request
import sys
import six
Copy link
Collaborator

Choose a reason for hiding this comment

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

Duplicated import six here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This import here is part of http_api_py which is the HTTP API processor generated as /usr/share/exabgp/http_api.py on the PTF container and is not for the exabgp.py.

}
'''

# exabgp v3 configuration file format
Copy link
Collaborator

Choose a reason for hiding this comment

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

exabgp v4 means IPv4. So, the v3 here is misleading.

Copy link
Contributor Author

@opcoder0 opcoder0 Aug 14, 2024

Choose a reason for hiding this comment

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

I think the file here was referring to ExaBGP v3 in the context of version 3. And v4 is for ExaBGP v4 which supported Python 3. I can probably modify the variable name to indicate release v4 instead of just v4.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Modified variable names to disambiguate.

}
'''

# exabgp v4 for py3 uses a different configuration file
Copy link
Collaborator

Choose a reason for hiding this comment

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

exabgp v4 is kind of misleading as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Modified variable names to disambiguate.

[program:icmp_responder]
command=/root/env-python3/bin/python /opt/icmp_responder.py {{ icmp_responder_args }}
process_name=icmp_responder
environment=PATH="/root/env-python3/bin"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why remove this line and keep /root/env-python3/bin/python in line 2? If the purpose is to use whatever default python binary, then line 2 should be updated as well. Eventually /root/env-python3 may be removed in "py3only" ptf image.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The environment is not required for the script to run. To keep backporting simpler I have done away from modifying the path to invoke Python 3. This PR enables that sonic-net/sonic-buildimage#19813. Can you please review this too.

logger.info('Test file path {}, in py3: {}'.format(test_fpath, in_py3))
is_python3 = is_py3_compat(test_fpath)

ptf_cmd = None
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably add some comments here to better explain the logic here.
The expectation of PTF image is either "mixed" or "py3only". "mixed" means that global python is python2. Python3 and dependent package is installed in /root/env-python3. "py3only" means that there is no python2. The global python is python3 and it has all required dependent packages.
For "mixed" ptf image, use python3 version ptf command to run python3 compatible ptf scripts. Use python2 version ptf command to run legacy python2 PTF scripts.
For "py3only" ptf image, always use the global python3 ptf command to run PTF scripts. Raise exception if ptf script is not python3 compatible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have added the comment.

@wangxin
Copy link
Collaborator

wangxin commented Aug 15, 2024

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@wangxin wangxin merged commit 18e6509 into sonic-net:master Aug 21, 2024
arista-hpandya pushed a commit to arista-hpandya/sonic-mgmt that referenced this pull request Oct 2, 2024
The PR makes the following changes with backward compatibility (with older branches to allow backporting) -

Enhances ptf_runner function to detect the python environment and set ptf binary path accordingly.
Removes use of is_python3
Adds exabgp v4 config and uses six to keep the calls to Py2 and Py3 to support back-porting.
The PR makes backward compatible changes to support -

Python 2 only
Python 2 + Python 3 (mixed environments with python3 virtualenv)
Python 3 only environments

What is the motivation for this PR?
As Python 2 has reached EOL it is important to migrate all the PTF test scripts running in Python 2 to 3.

How did you do it?
Create a Python 3 only PTF image buildable with sonic-buildimage - Completed
Migrate individual PTF test scripts to Python 3 - Completed (several PRs)
Update test scripts to run in the Python 3 only environment - This PR
How did you verify/test it?
By running individual PTF tests on S6100 and Cisco 8111 devices.

Any platform specific information?
These changes are not platform specific.
vikshaw-Nokia pushed a commit to vikshaw-Nokia/sonic-mgmt that referenced this pull request Oct 23, 2024
The PR makes the following changes with backward compatibility (with older branches to allow backporting) -

Enhances ptf_runner function to detect the python environment and set ptf binary path accordingly.
Removes use of is_python3
Adds exabgp v4 config and uses six to keep the calls to Py2 and Py3 to support back-porting.
The PR makes backward compatible changes to support -

Python 2 only
Python 2 + Python 3 (mixed environments with python3 virtualenv)
Python 3 only environments

What is the motivation for this PR?
As Python 2 has reached EOL it is important to migrate all the PTF test scripts running in Python 2 to 3.

How did you do it?
Create a Python 3 only PTF image buildable with sonic-buildimage - Completed
Migrate individual PTF test scripts to Python 3 - Completed (several PRs)
Update test scripts to run in the Python 3 only environment - This PR
How did you verify/test it?
By running individual PTF tests on S6100 and Cisco 8111 devices.

Any platform specific information?
These changes are not platform specific.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants