2020-08-24 16:03:00 -04:00
# Copyright (C) 2020 Red Hat, Inc.
#
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.
2020-08-29 13:07:19 -04:00
# This file is a collection of code used for testing
# code paths primarily via our uitests/
import os
2020-08-24 16:03:00 -04:00
2020-08-27 16:29:10 -04:00
def fake_job_info ( ) :
import random
total = 1024 * 1024 * 1024
fakepcent = random . choice ( range ( 1 , 100 ) )
remaining = ( ( total / 100 ) * fakepcent )
return [ None , None , None , total , None , remaining ]
def fake_interface_addresses ( iface , source ) :
import libvirt
mac = iface . macaddr
if source == libvirt . VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT :
ret = {
' enp1s0 ' : { ' hwaddr ' : mac , ' addrs ' : [
{ ' addr ' : ' 10.0.0.1 ' , ' prefix ' : 24 , ' type ' : 0 } ,
{ ' addr ' : ' fd00:beef::1 ' , ' prefix ' : 128 , ' type ' : 1 } ,
{ ' addr ' : ' fe80::1 ' , ' prefix ' : 64 , ' type ' : 1 } ] ,
} ,
' lo ' : { ' hwaddr ' : ' 00:00:00:00:00:00 ' , ' addrs ' : [
{ ' addr ' : ' 127.0.0.1 ' , ' prefix ' : 8 , ' type ' : 0 } ,
{ ' addr ' : ' ::1 ' , ' prefix ' : 128 , ' type ' : 1 } ] ,
} ,
}
else :
ret = { ' vnet0 ' : { ' hwaddr ' : mac , ' addrs ' : [
{ ' addr ' : ' 10.0.0.3 ' , ' prefix ' : 0 , ' type ' : 0 } ] ,
} }
return ret
def fake_dhcp_leases ( ) :
ret = [ {
' clientid ' : ' XXX ' ,
' expirytime ' : 1598570993 ,
' hostname ' : None ,
' iaid ' : ' 1448103320 ' ,
' iface ' : ' virbr1 ' ,
' ipaddr ' : ' fd00:beef::2 ' ,
' mac ' : ' BAD ' ,
' prefix ' : 64 ,
' type ' : 1 } , {
' clientid ' : ' YYY ' ,
' expirytime ' : 1598570993 ,
' hostname ' : None ,
' iaid ' : None ,
' iface ' : ' virbr1 ' ,
' ipaddr ' : ' 10.0.0.2 ' ,
' mac ' : ' NOPE ' ,
' prefix ' : 24 ,
' type ' : 0 } ]
return ret
2020-09-02 13:38:48 -04:00
def schedule_fake_agent_event ( conn , cb ) :
import libvirt
vmname = conn . config . CLITestOptions . fake_agent_event
backend = conn . get_backend ( )
state = libvirt . VIR_CONNECT_DOMAIN_EVENT_AGENT_LIFECYCLE_STATE_CONNECTED
reason = libvirt . VIR_CONNECT_DOMAIN_EVENT_AGENT_LIFECYCLE_REASON_CHANNEL
def time_cb ( ) :
dom = backend . lookupByName ( vmname )
cb ( backend , dom , state , reason , None )
2020-09-20 10:07:15 -04:00
conn . timeout_add ( 500 , time_cb )
2020-09-02 13:38:48 -04:00
def schedule_fake_nodedev_event ( conn , lifecycle_cb , update_cb ) :
import libvirt
nodename = conn . config . CLITestOptions . fake_nodedev_event
backend = conn . get_backend ( )
def lifecycle_cb_wrapper ( ) :
nodedev = backend . nodeDeviceLookupByName ( nodename )
state = libvirt . VIR_NODE_DEVICE_EVENT_CREATED
reason = 0
lifecycle_cb ( backend , nodedev , state , reason , None )
def update_cb_wrapper ( ) :
nodedev = backend . nodeDeviceLookupByName ( nodename )
update_cb ( backend , nodedev , None )
2020-09-20 10:07:15 -04:00
conn . timeout_add ( 500 , lifecycle_cb_wrapper )
conn . timeout_add ( 1000 , update_cb_wrapper )
2020-09-02 13:38:48 -04:00
2020-09-02 14:17:54 -04:00
def fake_openauth ( conn , cb , data ) :
ignore = conn
import libvirt
creds = [
[ libvirt . VIR_CRED_USERNAME , " Username " , None , None , None ] ,
[ libvirt . VIR_CRED_PASSPHRASE , " Password " , None , None , None ] ,
]
cb ( creds , data )
assert all ( [ bool ( cred [ 4 ] ) for cred in creds ] )
2022-01-17 15:12:42 -05:00
class fakeVirtBootstrap :
2022-01-17 16:07:25 -05:00
@staticmethod
2022-01-17 15:12:42 -05:00
def bootstrap ( * * kwargs ) :
import time
2022-01-17 16:07:25 -05:00
import logging
log = logging . getLogger ( " virtBootstrap " )
log . info ( " mock virtBootstrap msg1 " )
kwargs [ " progress_cb " ] ( { " status " : " msg1 " } )
time . sleep ( .5 )
log . info ( " mock virtBootstrap msg2 " )
kwargs [ " progress_cb " ] ( { " status " : " msg2 " } )
time . sleep ( .5 )
log . info ( " mock virtBootstrap msg3 " )
kwargs [ " progress_cb " ] ( { " status " : " msg3 " } )
2022-01-17 15:12:42 -05:00
if " username " in kwargs :
raise RuntimeError ( " fakeVirtBootstrap mock auth failure! " )
2020-08-24 16:03:00 -04:00
class CLITestOptionsClass :
"""
Helper class for parsing and tracking - - test - * options .
The suboptions are :
2020-08-29 13:07:19 -04:00
* first - run : Run the app with fresh gsettings values saved to
a keyfile , mimicking a first app run . Also sets
GSETTINGS to use memory backend , in case any other app
preferences would be affected . The ui testsuite sets this
for most tests .
2020-08-24 16:03:00 -04:00
2020-08-29 13:07:19 -04:00
* xmleditor - enabled : Force the xmleditor gsettings preference on .
2020-08-24 16:03:00 -04:00
* gsettings - keyfile : Override the gsettings values with those
from the passed in keyfile , to test with different default
settings .
* leak - debug : Enabling this will tell us , at app exit time ,
which vmmGObjects were not garbage collected . This is caused
by circular references to other objects , like a signal that
wasn ' t disconnected. It ' s not a big deal , but if we have objects
that can be created and destroyed a lot over the course of
the app lifecycle , every non - garbage collected class is a
memory leak . So it ' s nice to poke at this every now and then
and try to track down what we need to add to class _cleanup handling .
* no - events : Force disable libvirt event APIs for testing fallback
* break_setfacl : For setfacl calls to fail , for test scenarios .
This is hit via the directory search permissions checking
for disk image usage for qemu
2020-08-29 13:07:19 -04:00
* enable - libguestfs : Force enable the libguestfs gsetting
* disable - libguestfs : Force disable the libguestfs gsetting
2020-08-26 13:13:36 -04:00
* test - managed - save : Triggers a couple conditions for testing
managed save issues
2020-08-26 13:26:08 -04:00
* test - vm - run - fail : Make VM run fail , so we can test the error path
2020-08-28 11:55:36 -04:00
* spice - agent : Make spice - agent detection return true in viewer . py
2020-08-29 13:07:19 -04:00
* firstrun - uri : If set , use this as the initial connection URI
if we are doing firstrun testing
2020-09-15 14:46:36 -04:00
* fake - no - libvirtd : If doing firstrun testing , fake that
libvirtd is not installed
2020-08-29 13:32:37 -04:00
* fake - vnc - username : Fake VNC username auth request
* fake - console - resolution : Fake viewer console resolution response .
Spice doesn ' t return values here when we are just testing
against seabios in uitests , this fakes it to hit more code paths
2020-09-01 14:51:30 -04:00
* fake - systray : Enable the fake systray window
2022-01-17 15:12:42 -05:00
* fake - virtbootstrap : Mock the virtBootstrap module , since getting
it to actually work across fedora versions is hard
2020-09-23 14:33:17 -04:00
* object - denylist = NAME : Make object initialize for that name
2020-09-02 11:22:02 -04:00
fail to test some connection code paths
* conn - crash : Test connection abruptly closing like when
libvirtd is restarted .
2020-09-02 13:38:48 -04:00
* fake - agent - event : Fake a qemu guest agent API event
* fake - nodedev - event : Fake nodedev API events
2020-09-02 14:17:54 -04:00
* fake - openauth : Fake user + pass response from libvirt openauth ,
for testing the TCP URI auth dialog
2020-09-02 15:38:03 -04:00
* fake - session - error : Fake a connection open error that
triggers logind session lookup
2020-09-20 10:07:15 -04:00
* short - poll : Use a polling interval of only .1 seconds to speed
up the uitests a bit
2020-08-24 16:03:00 -04:00
"""
2020-08-29 13:07:19 -04:00
def __init__ ( self , test_options_str ) :
2020-08-24 16:03:00 -04:00
optset = set ( )
for optstr in test_options_str :
optset . update ( set ( optstr . split ( " , " ) ) )
2020-08-29 13:07:19 -04:00
first_run = self . _parse ( optset )
self . _process ( first_run )
2020-08-24 16:03:00 -04:00
def _parse ( self , optset ) :
def _get ( optname ) :
if optname not in optset :
return False
optset . remove ( optname )
return True
def _get_value ( optname ) :
for opt in optset :
if opt . startswith ( optname + " = " ) :
optset . remove ( opt )
return opt . split ( " = " , 1 ) [ 1 ]
2020-08-29 13:07:19 -04:00
first_run = _get ( " first-run " )
2020-08-24 16:03:00 -04:00
self . leak_debug = _get ( " leak-debug " )
self . no_events = _get ( " no-events " )
self . xmleditor_enabled = _get ( " xmleditor-enabled " )
self . gsettings_keyfile = _get_value ( " gsettings-keyfile " )
self . break_setfacl = _get ( " break-setfacl " )
2020-08-29 13:07:19 -04:00
self . disable_libguestfs = _get ( " disable-libguestfs " )
self . enable_libguestfs = _get ( " enable-libguestfs " )
2020-08-26 13:13:36 -04:00
self . test_managed_save = _get ( " test-managed-save " )
2020-08-26 13:26:08 -04:00
self . test_vm_run_fail = _get ( " test-vm-run-fail " )
2020-08-28 11:55:36 -04:00
self . spice_agent = _get ( " spice-agent " )
2020-08-29 13:07:19 -04:00
self . firstrun_uri = _get_value ( " firstrun-uri " )
2020-09-15 14:46:36 -04:00
self . fake_no_libvirtd = _get ( " fake-no-libvirtd " )
2020-08-29 13:32:37 -04:00
self . fake_vnc_username = _get ( " fake-vnc-username " )
self . fake_console_resolution = _get ( " fake-console-resolution " )
2020-09-01 14:51:30 -04:00
self . fake_systray = _get ( " fake-systray " )
2020-09-23 14:33:17 -04:00
self . object_denylist = _get_value ( " object-denylist " )
2020-09-02 11:22:02 -04:00
self . conn_crash = _get ( " conn-crash " )
2020-09-02 13:38:48 -04:00
self . fake_agent_event = _get_value ( " fake-agent-event " )
self . fake_nodedev_event = _get_value ( " fake-nodedev-event " )
2020-09-02 14:17:54 -04:00
self . fake_openauth = _get ( " fake-openauth " )
2020-09-02 15:38:03 -04:00
self . fake_session_error = _get ( " fake-session-error " )
2020-09-20 10:07:15 -04:00
self . short_poll = _get ( " short-poll " )
2022-01-17 15:12:42 -05:00
self . fake_virtbootstrap = _get ( " fake-virtbootstrap " )
2020-08-24 16:03:00 -04:00
if optset : # pragma: no cover
raise RuntimeError ( " Unknown --test-options keys: %s " % optset )
2020-08-29 13:07:19 -04:00
return first_run
def _process ( self , first_run ) :
if first_run :
# So other settings like gtk are reset and not affected
os . environ [ " GSETTINGS_BACKEND " ] = " memory "
if first_run and not self . gsettings_keyfile :
2020-08-24 16:03:00 -04:00
import atexit
import tempfile
filename = tempfile . mktemp ( prefix = " virtmanager-firstrun-keyfile " )
self . gsettings_keyfile = filename
atexit . register ( lambda : os . unlink ( filename ) )
if self . break_setfacl :
import virtinst . diskbackend
def fake_search ( * args , * * kwargs ) :
raise RuntimeError ( " Fake search fix fail from test suite " )
virtinst . diskbackend . SETFACL = " getfacl "
# pylint: disable=protected-access
virtinst . diskbackend . _fix_perms_chmod = fake_search