-
Notifications
You must be signed in to change notification settings - Fork 181
migration: Add case about migration_port_min/max #6657
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
Open
cliping
wants to merge
1
commit into
autotest:master
Choose a base branch
from
cliping:port
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
libvirt/tests/cfg/migration/migration_uri/tcp_port_min_and_port_max.cfg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| - migration.migration_uri.tcp.migration_port_allocation.port_min_and_port_max: | ||
| type = tcp_port_min_and_port_max | ||
| migration_setup = 'yes' | ||
| storage_type = 'nfs' | ||
| setup_local_nfs = 'yes' | ||
| disk_type = "file" | ||
| disk_source_protocol = "netfs" | ||
| mnt_path_name = ${nfs_mount_dir} | ||
| # Console output can only be monitored via virsh console output | ||
| only_pty = True | ||
| take_regular_screendumps = no | ||
| # Extra options to pass after <domain> <desturi> | ||
| virsh_migrate_extra = '' | ||
| # SSH connection time out | ||
| ssh_timeout = 60 | ||
| # Local URI | ||
| virsh_migrate_connect_uri = 'qemu:///system' | ||
| virsh_migrate_dest_state = "running" | ||
| virsh_migrate_src_state = "shut off" | ||
| image_convert = 'no' | ||
| server_ip = "${migrate_dest_host}" | ||
| server_user = "root" | ||
| server_pwd = "${migrate_dest_pwd}" | ||
| status_error = "no" | ||
| check_network_accessibility_after_mig = "yes" | ||
| migrate_desturi_port = "16509" | ||
| migrate_desturi_type = "tcp" | ||
| virsh_migrate_desturi = "qemu+tcp://${migrate_dest_host}/system" | ||
| vms = "avocado-vt-vm1 vm2 vm3" | ||
| qemu_conf_path = '/etc/libvirt/qemu.conf' | ||
| variants: | ||
| - p2p: | ||
| virsh_migrate_options = '--live --p2p --verbose --bandwidth 15' | ||
| - non_p2p: | ||
| virsh_migrate_options = '--live --verbose --bandwidth 15' | ||
| variants test_case: | ||
| - default: | ||
| qemu_conf_dest = '{r".*migration_port_min\s*=.*": "migration_port_min=49152", r".*migration_port_max\s*=.*": "migration_port_max=49215"}' | ||
| port_list = ["49152", "49153", "49154"] | ||
| - min_49111_and_max_49222: | ||
| qemu_conf_dest = '{r".*migration_port_min\s*=.*": "migration_port_min=49111", r".*migration_port_max\s*=.*": "migration_port_max=49222"}' | ||
| port_list = ["49111", "49112", "49113"] | ||
| - min_49333_and_max_49334: | ||
| qemu_conf_dest = '{r".*migration_port_min\s*=.*": "migration_port_min=49333", r".*migration_port_max\s*=.*": "migration_port_max=49334"}' | ||
| port_list = ["49333", "49334"] |
145 changes: 145 additions & 0 deletions
145
libvirt/tests/src/migration/migration_uri/tcp_port_min_and_port_max.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| # | ||
| # Copyright Redhat | ||
| # | ||
| # SPDX-License-Identifier: GPL-2.0 | ||
| # | ||
| # Author: Liping Cheng <lcheng@redhat.com> | ||
| # | ||
| # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| import re | ||
| import os | ||
| import threading | ||
| import time | ||
|
|
||
| from avocado.utils import process | ||
|
|
||
| from virttest import libvirt_remote | ||
| from virttest import remote | ||
| from virttest import virsh | ||
| from virttest.utils_libvirt import libvirt_vmxml | ||
| from virttest.libvirt_xml import vm_xml | ||
|
|
||
| from provider.migration import base_steps | ||
| from provider.migration import migration_base | ||
|
|
||
|
|
||
| def check_port(test, port_list): | ||
| """ | ||
| Check port during migration | ||
|
|
||
| """ | ||
| time.sleep(5) | ||
| cmd = "netstat -tunap|grep qemu-kvm|grep 49" | ||
| ret = process.run(cmd, shell=True, verbose=True, ignore_status=True).stdout_text.strip() | ||
| for port in port_list: | ||
| if not re.findall(port, ret): | ||
| test.fail(f"Not found {port} in {ret}.") | ||
| else: | ||
| test.log.debug(f"Checked {port} successfully.") | ||
|
|
||
|
|
||
| def run(test, params, env): | ||
| """ | ||
| This case is to verify that when migration port is not specified, libvirt | ||
| will allocate a port in range [migration_port_min, migration_port_max]. | ||
|
|
||
| """ | ||
| def setup_test(): | ||
| """ | ||
| Setup steps | ||
|
|
||
| """ | ||
| qemu_conf_path = params.get("qemu_conf_path") | ||
| qemu_conf_dest = params.get("qemu_conf_dest", "{}") | ||
| nfs_mount_dir = params.get("nfs_mount_dir") | ||
| migrate_desturi_port = params.get("migrate_desturi_port") | ||
| migrate_desturi_type = params.get("migrate_desturi_type", "tcp") | ||
|
|
||
| migration_obj.conn_list.append(migration_base.setup_conn_obj(migrate_desturi_type, params, test)) | ||
| migration_obj.remote_add_or_remove_port(migrate_desturi_port) | ||
|
|
||
| nonlocal remote_obj | ||
| remote_obj = libvirt_remote.update_remote_file(params, | ||
| qemu_conf_dest, | ||
| qemu_conf_path) | ||
| for vm in vms: | ||
| disk_dict = {'source': {'attrs': {'file': os.path.join(nfs_mount_dir, | ||
| os.path.basename(vm.get_first_disk_devices()['source']))}}} | ||
| libvirt_vmxml.modify_vm_device( | ||
| vm_xml.VMXML.new_from_inactive_dumpxml(vm.name), | ||
| 'disk', disk_dict) | ||
| vm.start() | ||
| vm.wait_for_login() | ||
|
|
||
| def run_test(): | ||
| """ | ||
| run test steps | ||
|
|
||
| """ | ||
| src_uri = params.get("virsh_migrate_connect_uri") | ||
| options = params.get("virsh_migrate_options", "") | ||
| status_error = "yes" == params.get("status_error", "no") | ||
| migrate_timeout = int(params.get("virsh_migrate_thread_timeout", 900)) | ||
| port_list = eval(params.get("port_list", "[]")) | ||
| test_case = params.get("test_case") | ||
|
|
||
| test.log.debug("run test.") | ||
| p = threading.Thread(target=check_port, args=(test, port_list,)) | ||
| p.start() | ||
|
|
||
| try: | ||
| migration_obj.migration_test.do_migration(vms=vms, srcuri=src_uri, | ||
| desturi=dest_uri, | ||
| migration_type="simultaneous", | ||
| options=options, | ||
| thread_timeout=migrate_timeout, | ||
| ignore_status=False, | ||
| status_error=status_error) | ||
| except Exception as info: | ||
| if test_case == "min_49333_and_max_49334": | ||
| err_msg = "Unable to find an unused port in range 'migration' (49333-49334)" | ||
| if err_msg not in info: | ||
| test.fail("Migrate vms failed: %s", info) | ||
| else: | ||
| migration_obj.migration_test.do_migration(vms=vm[2], srcuri=src_uri, | ||
| desturi=dest_uri, | ||
| migration_type="orderly", | ||
| options=options, | ||
| thread_timeout=migrate_timeout, | ||
| ignore_status=False, | ||
| status_error=status_error) | ||
| else: | ||
| test.fail("Migrate vms failed: %s", info) | ||
|
|
||
| def cleanup_test(): | ||
| """ | ||
| Cleanup steps | ||
|
|
||
| """ | ||
| test.log.info("Cleanup steps.") | ||
| for vm in vms: | ||
| virsh.destroy(vm.name, debug=True, uri=dest_uri) | ||
| migration_obj.cleanup_connection() | ||
| nonlocal remote_obj | ||
| if remote_obj: | ||
| del remote_obj | ||
|
|
||
| dest_uri = params.get("virsh_migrate_desturi") | ||
| vm_names = params.get("vms").split() | ||
| remote_obj = None | ||
| vms = [] | ||
| for vm_name in vm_names: | ||
| vm = env.get_vm(vm_name) | ||
| vms.append(vm) | ||
| test.log.debug("vm name: %s", vm.name) | ||
|
|
||
| migration_obj = base_steps.MigrationBase(test, vms[0], params) | ||
|
|
||
| try: | ||
| setup_test() | ||
| run_test() | ||
| migration_obj.verify_default() | ||
| finally: | ||
| cleanup_test() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Fix exception-message check, VM selection, and
test.failformatting in the error pathThere are a few correctness issues in this block:
if err_msg not in info:compares a string to theExceptionobject, which is not iterable and can raiseTypeError. You likely want to inspect the exception message instead.vms=vm[2]usesvm(the last VM object from the outer loop) as if it were a list; this will fail at runtime ('LibvirtVM' object is not subscriptable) and also ignores thevmslist you built.test.fail("Migrate vms failed: %s", info)assumes a printf-style API; Avocado’sfailgenerally takes a single message string, so this may not format as intended.A concrete fix could look like:
Additionally, if
do_migrationraises a specific exception type for migration failures, narrowing theexcept Exceptionto that type would address the BLE001 warning and avoid masking unrelated errors.I'll verify the review comment by examining the actual code and understanding the context.
🏁 Script executed:
Length of output: 2106
🏁 Script executed:
Length of output: 235
🏁 Script executed:
Length of output: 1319
🏁 Script executed:
Length of output: 1279
🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 344
🏁 Script executed:
Length of output: 755
🏁 Script executed:
Length of output: 628
🏁 Script executed:
Length of output: 1050
🏁 Script executed:
Length of output: 1846
🏁 Script executed:
Length of output: 264
🏁 Script executed:
Length of output: 74
🏁 Script executed:
Length of output: 2370
🏁 Script executed:
Length of output: 240
🏁 Script executed:
Length of output: 5299
🌐 Web query:
💡 Result:
In Avocado tests call:
self.fail(message)
where message is an optional string describing the failure. Calling it raises the test failure (TestFail) and marks the test as FAIL. [1][2]
Sources:
🌐 Web query:
💡 Result:
In Avocado Python tests use the Test.fail method (called as self.fail(msg)) to mark a test as FAIL with an explanatory message — typically used to convert a caught exception or an unexpected condition into a failure (instead of an ERROR). Example:
See the Avocado "Writing Avocado Tests" guide for details. [1][2]
Sources
[1] Avocado documentation — Writing Avocado Tests (v105.0).
[2] Avocado documentation — Writing Avocado Tests (v76.0).
Fix exception-message check, VM selection, and
test.failformatting in the error pathAll three issues identified are confirmed:
if err_msg not in info:compares against an Exception object; useif err_msg not in str(info):vms=vm[2]fails becausevm(from the outer loop) is a single VM object, not subscriptable; usevms=[vms[2]]test.fail("Migrate vms failed: %s", info)uses printf-style formatting not supported by Avocado; use f-string:test.fail(f"Migrate vms failed: {info}")Suggested fix:
🧰 Tools
🪛 Ruff (0.14.5)
100-100: Do not catch blind exception:
Exception(BLE001)
🤖 Prompt for AI Agents