Skip to content

Unify the importing to avoid exception not caught issue#1864

Merged
lguohan merged 3 commits intosonic-net:masterfrom
wangxin:fix-imports
Jul 10, 2020
Merged

Unify the importing to avoid exception not caught issue#1864
lguohan merged 3 commits intosonic-net:masterfrom
wangxin:fix-imports

Conversation

@wangxin
Copy link
Copy Markdown
Collaborator

@wangxin wangxin commented Jul 8, 2020

Description of PR

Summary:
Fixes # (issue)

The sonic-mgmt/tests folder was added to sys.path so that the scripts
and libs can use format from common.xx import xx to import from
the common folder. By this way, objects may be identified as 'tests.common.xx'
or 'common.xx' under different context. This caused some side effects
like RunAnsibleModuleFail exception cannot be caught. The raised
exception is identified as tests.common.errors.RunAnsibleModuleFail.
But the script tries to catch common.errors.RunAnsibleModuleFail and
fail to catch the exception.

This PR removes the code for adding sonic-mgmt/tests/ to sys.path and
updated the related importing to format like from tests.common.xx import xx.
When we use pytest to run scripts under sonic-mgmt/tests folder, its parent
folder sonic-mgmt is automatically added to sys.path. That's why we can
use the new format to do importing.

Type of change

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

Approach

What is the motivation for this PR?

Since the sonic/tests folder is made as a package, adding subfolder of this package to sys.path would cause ambiguity issue of object identification. For example, in tests/common/platform/ssh_utils.py, the RunAnsibleModuleFail exception cannot be caught:

    try:
        ptfhost.shell('stat /root/.ssh/known_hosts')
    except RunAnsibleModuleFail:
        pass # files does not exist
    else:
        ptfhost.shell('ssh-keygen -f /root/.ssh/known_hosts -R ' + dut_ip)

The reason is that the raised exception has identification tests.common.errors.RunAnsibleModuleFail. However, the script here expects common.errors.RunAnsibleModuleFail here.

To avoid this issue, we should always explicitly use absolute import statement like 'from tests.common.xx import xx', or use relative import like 'from .common.xx import xx'. To prevent this issue, the code for adding tests/common folder to sys.path is also removed from tests/conftest.py.

How did you do it?

  • Remove the code for adding tests/common to sys.path.
  • Update all affected imports to either absolute import and relative import
  • Replace the hacks in scripts under tests/platform_tests for adding subfolders to sys.path with relative import

How did you verify/test it?

Tested collect all the tests. No importing issue. Tested the scripts depend on ssh_util, the exception can be caught as expected.

Any platform specific information?

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

Documentation

@wangxin wangxin requested review from a team, keboliu and tahmed-dev July 8, 2020 04:08
@lgtm-com
Copy link
Copy Markdown

lgtm-com bot commented Jul 8, 2020

This pull request introduces 15 alerts and fixes 3 when merging a7863d4c33bec6ec70607dd1dc54638855f2f498 into 52b5b15 - view on LGTM.com

new alerts:

  • 12 for Unused import
  • 3 for Wrong number of arguments in a call

fixed alerts:

  • 3 for Unused import

@lgtm-com
Copy link
Copy Markdown

lgtm-com bot commented Jul 8, 2020

This pull request introduces 6 alerts and fixes 3 when merging c4dd424a1a251bfa5952653c84b2b307dd0e7c70 into 77a6d1c - view on LGTM.com

new alerts:

  • 3 for Unused import
  • 3 for Wrong number of arguments in a call

fixed alerts:

  • 3 for Unused import

@jleveque
Copy link
Copy Markdown
Contributor

jleveque commented Jul 8, 2020

Retest this please

@tahmed-dev
Copy link
Copy Markdown
Contributor

@wangxin can we instead of removing the current dir from sys path, catch the exception with its full name?

Copy link
Copy Markdown
Contributor

@tahmed-dev tahmed-dev left a comment

Choose a reason for hiding this comment

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

Good catch!

wangxin added 2 commits July 9, 2020 06:07
The sonic-mgmt/tests folder was added to sys.path so that the scripts
and libs can use format `from common.xx import xx` to import from
the `common` folder. By this way, objects may be identified as 'tests.common.xx'
or 'common.xx' under different context. This caused some side effects
like RunAnsibleModuleFail exception cannot be caught. The raised
exception is identified as tests.common.errors.RunAnsibleModuleFail.
But the script tries to catch common.errors.RunAnsibleModuleFail and
fail to catch the exception.

This PR removes the code for adding sonic-mgmt/tests/ to sys.path and
updated the related importing to format like `from tests.common.xx import xx`.
When we use pytest to run scripts under `sonic-mgmt/tests` folder, its parent
folder `sonic-mgmt` is automatically added to sys.path. That's why we can
use the new format to do importing.

Signed-off-by: Xin Wang <xiwang5@microsoft.com>
@wangxin
Copy link
Copy Markdown
Collaborator Author

wangxin commented Jul 9, 2020

@tahmed-dev Yes, we can specify full name to catch the exceptions. But it would be better to enforce importing from the tests package to avoid other potential similar issues that are kind of difficult to troubleshoot.

@lgtm-com
Copy link
Copy Markdown

lgtm-com bot commented Jul 9, 2020

This pull request introduces 3 alerts and fixes 3 when merging 225bc7a into 53222ad - view on LGTM.com

new alerts:

  • 3 for Unused import

fixed alerts:

  • 3 for Unused import

@lgtm-com
Copy link
Copy Markdown

lgtm-com bot commented Jul 9, 2020

This pull request introduces 3 alerts and fixes 3 when merging 4a13e38 into 53222ad - view on LGTM.com

new alerts:

  • 3 for Unused import

fixed alerts:

  • 3 for Unused import

Copy link
Copy Markdown
Collaborator

@lolyu lolyu left a comment

Choose a reason for hiding this comment

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

LGTM

@lguohan lguohan merged commit 8f3cafc into sonic-net:master Jul 10, 2020
@wangxin wangxin deleted the fix-imports branch July 20, 2020 08:57
wangxin added a commit that referenced this pull request Jul 20, 2020
After PR #1864 is merged, `from common.xx import yy' is
no longer supported. Probably this PR was created/tested
before PR#1864 was merged. This commit is to fix the
`from common.xx import yy' issue.

Signed-off-by: Xin Wang <xiwang5@microsoft.com>
@wangxin wangxin mentioned this pull request Sep 4, 2020
3 tasks
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.

6 participants