Skip to content

Commit

Permalink
Merge pull request autotest#5624 from chloerh/attach-user
Browse files Browse the repository at this point in the history
Add new network case of attach user type interface
  • Loading branch information
dzhengfy authored Jul 8, 2024
2 parents 78c57f8 + 29dd0cd commit b08b61d
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
- virtual_network.attach_device.user_type_iface:
type = attach_user_type_iface
start_vm = no
outside_ip = 'www.redhat.com'
vm_ping_outside = pass
variants user_type:
- root_user:
test_user = root
virsh_uri = 'qemu:///system'
- non_root_user:
test_user = USER.EXAMPLE
test_passwd = PASSWORD.EXAMPLE
unpr_vm_name = UNPRIVILEGED_VM.EXAMPLE
virsh_uri = 'qemu+ssh://${test_user}@localhost/session'
iface_attrs = {'type_name': 'user', 'mac_address': '00:11:22:33:44:55', 'model': 'virtio', 'ips': [{'family': 'ipv4', 'address': '172.17.2.0', 'prefix': '24'}, {'family': 'ipv6', 'address': '2001:db8:ac10:fd01::', 'prefix': '64'}]}
expect_ipv4 = 172.17.2.15
expect_ipv6 = 2001:db8:ac10:fd01:
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import logging

import aexpect
from virttest import remote
from virttest import utils_net
from virttest import virsh
from virttest.libvirt_xml import vm_xml
from virttest.utils_libvirt import libvirt_unprivileged
from virttest.utils_libvirt import libvirt_vmxml

from provider.virtual_network import network_base

VIRSH_ARGS = {'ignore_status': False, 'debug': True}

LOG = logging.getLogger('avocado.' + __name__)


def run(test, params, env):
"""
Test attach interface with user type
"""
root = 'root_user' == params.get('user_type', '')
if root:
vm_name = params.get('main_vm')
vm = env.get_vm(vm_name)
virsh_ins = virsh
else:
vm_name = params.get('unpr_vm_name')
test_user = params.get('test_user', '')
test_passwd = params.get('test_passwd', '')
unpr_vm_args = {
'username': params.get('username'),
'password': params.get('password'),
}
vm = libvirt_unprivileged.get_unprivileged_vm(vm_name, test_user,
test_passwd,
**unpr_vm_args)
uri = f'qemu+ssh://{test_user}@localhost/session'
virsh_ins = virsh.VirshPersistent(uri=uri)
host_session = aexpect.ShellSession('su')
remote.VMManager.set_ssh_auth(host_session, 'localhost', test_user,
test_passwd)
host_session.close()

iface_attrs = eval(params.get('iface_attrs', '{}'))
expect_ipv4 = params.get('expect_ipv4')
expect_ipv6 = params.get('expect_ipv6')
vmxml = vm_xml.VMXML.new_from_inactive_dumpxml(
vm_name, virsh_instance=virsh_ins)
bkxml = vmxml.copy()

try:
vmxml.del_device('interface', by_tag=True)
vmxml.sync(virsh_instance=virsh_ins)
mac = iface_attrs['mac_address']
LOG.debug(
f'VMXML of {vm_name}:\n{virsh_ins.dumpxml(vm_name).stdout_text}')

vm.start()
vm.wait_for_serial_login().close()

iface = libvirt_vmxml.create_vm_device_by_type(
'interface', iface_attrs)
LOG.debug(f'Interface to attach:\n{iface}')

virsh_ins.attach_device(vm_name, iface.xml, **VIRSH_ARGS)
LOG.debug(virsh_ins.dumpxml(vm_name).stdout_text)

# Check attached xml
iface_at = network_base.get_iface_xml_inst(
vm_name, 'after attached on vm', virsh_ins=virsh_ins)
if iface_at.mac_address != iface.mac_address:
test.fail(f'Mac address of attached iface xml does not match with '
f'original xml, attach might failed')

# log in the guest, and check the network function, it works well,
# ip and ping
session = vm.wait_for_serial_login()
vm_iface_info = utils_net.get_linux_iface_info(
mac=mac, session=session)
LOG.debug(f'Interface in vm: {vm_iface_info}')
for addr in vm_iface_info['addr_info']:
if addr['scope'] != 'global':
continue
if addr['family'] == 'inet':
if addr['local'] != expect_ipv4:
test.fail(f'Wrong ipv4 addr: {addr["local"]}')
if addr['family'] == 'inet6':
if not addr['local'].startswith(expect_ipv6):
test.fail(f'Wrong ipv6 addr {addr["local"]}')

LOG.info('Login vm to check whether the network works well via ping.')
ips = {'outside_ip': params.get('outside_ip')}
network_base.ping_check(params, ips, session, force_ipv4=True)

# Detach iface
virsh_ins.detach_device(
vm_name, iface.xml, wait_for_event=True, **VIRSH_ARGS)

# Check iface in vm disappeared
vm_iface_info_unplug = utils_net.get_linux_iface_info(
mac=mac, session=session)
LOG.debug(vm_iface_info_unplug)
if vm_iface_info_unplug is not None:
test.fail(f'iface should be removed, should not found '
f'{vm_iface_info_unplug} in vm')

# Check xml disappeared
vmxml = vm_xml.VMXML.new_from_inactive_dumpxml(
vm_name, virsh_instance=virsh_ins)
iface_list = vmxml.devices.by_device_tag('interface')
if len(iface_list):
test.fail('Found interface xml on vm which should be detached:'
f'{iface_list}')
finally:
bkxml.sync(virsh_instance=virsh_ins)
5 changes: 3 additions & 2 deletions provider/virtual_network/network_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def set_static_ip(iface, ip, netmask, session):
session.cmd(f'ifconfig {iface} {ip}/{netmask}')


def get_iface_xml_inst(vm_name, comment, index=0, options=''):
def get_iface_xml_inst(vm_name, comment, index=0, options='', virsh_ins=virsh):
"""
Get iface xml instance with given vm and index
Expand All @@ -300,7 +300,8 @@ def get_iface_xml_inst(vm_name, comment, index=0, options=''):
:param index: index of interface on vm, defaults to 0
:return: xml instance of interface
"""
vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name, options=options)
vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name, options=options,
virsh_instance=virsh_ins)
iface = vmxml.get_devices('interface')[index]
LOG.debug(f'Interface xml ({comment}):\n{iface}')
return iface
Expand Down

0 comments on commit b08b61d

Please sign in to comment.