Skip to content

[Python3 migrate] Fix some type upgrade issue#7399

Merged
lerry-lee merged 2 commits intosonic-net:masterfrom
lerry-lee:py3_migrate/fix_type_upgrade_issue
Feb 7, 2023
Merged

[Python3 migrate] Fix some type upgrade issue#7399
lerry-lee merged 2 commits intosonic-net:masterfrom
lerry-lee:py3_migrate/fix_type_upgrade_issue

Conversation

@lerry-lee
Copy link
Copy Markdown
Contributor

@lerry-lee lerry-lee commented Feb 6, 2023

Description of PR

Some test cases like pc/test_lag_2.py failed in Python3 environment, caused by key error like the below:

KeyError('str2-7260cx3-acs-fan-15')
Traceback (most recent call last):
  File "/azp/_work/25/s/tests/common/plugins/log_section_start/__init__.py", line 84, in _fixture_generator_decorator
    res = next(it)
  File "/azp/_work/25/s/tests/pfcwd/test_pfcwd_timer_accuracy.py", line 75, in pfcwd_timer_setup_restore
    storm_handle = set_storm_params(dut, fanout_info, fanout, peer_params)
  File "/azp/_work/25/s/tests/pfcwd/test_pfcwd_timer_accuracy.py", line 133, in set_storm_params
    storm_handle = PFCStorm(dut, fanout_info, fanout, pfc_queue_idx=pfc_queue_index,
  File "/azp/_work/25/s/tests/common/helpers/pfc_storm.py", line 51, in __init__
    self.peer_device = self.fanout_hosts[self.peer_info['peerdevice']]
KeyError: 'str2-7260cx3-acs-fan-15'

Actually, it is caused by these codes (When parsing conn_graph_facts):

@pytest.fixture(scope="module")
def fanouthosts(ansible_adhoc, conn_graph_facts, creds, ditheists):      # noqa F811
    """
    Shortcut fixture for getting Fanout hosts
    """

    dev_conn = conn_graph_facts.get('device_conn', {})
    fanout_hosts = {}
    # WA for virtual testbed which has no fanout
    try:
        for dut_host, value in dev_conn.items():
            duthost = duthosts[dut_host]

In Python2 env, the dut_host(the key in dev_conn.items()) is unicode object, but in Python3 is AnsileUnsafeText object, because Ansible marks all strings inside returned data as Unsafe.
image

So, we convert the AnsileUnsafeText to str in Py3 after got ansible return value.

Other fixes:

In Python2, dict.keys()/dict.values() returns list object, but in Python3 returns an iterable but not indexable object. So that need to convert to list.

The Python "NameError name 'unicode' is not defined" occurs when using the unicode object in Python 3. To solve the error, replace all calls to unicode() with str() because unicode was renamed to str in Python 3.

Type of change

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

Back port request

  • 201911
  • 202012
  • 202205

Approach

What is the motivation for this PR?

Python3 migrate fix key error.

How did you do it?

Add explicit convert.

How did you verify/test it?

Run TC and no key error.

Any platform specific information?

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

Documentation

Signed-off-by: Chun'ang Li <chunangli@microsoft.com>
Signed-off-by: Chun'ang Li <chunangli@microsoft.com>
@lerry-lee lerry-lee merged commit 9567ca2 into sonic-net:master Feb 7, 2023
@lerry-lee lerry-lee deleted the py3_migrate/fix_type_upgrade_issue branch February 7, 2023 06:58
kellyyeh pushed a commit to kellyyeh/sonic-mgmt that referenced this pull request Mar 31, 2023
What is the motivation for this PR?
Python3 migrate fix key error.

How did you do it?
Add explicit convert.

How did you verify/test it?
Run TC and no key error.

Signed-off-by: Chun'ang Li <chunangli@microsoft.com>
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.

2 participants