rv_input.py 10.7 KB
Newer Older
1 2 3 4 5
"""
rv_input.py - test keyboard inputs through spice

Requires: Two VMs - client and guest and remote-viewer session
          from client VM to guest VM created by rv_connect test.
6 7 8
          Deployed PyGTK on guest VM.

          Presumes the numlock state at startup is 'OFF'.
9 10
"""

L
Lucas Meneghel Rodrigues 已提交
11 12
import logging
import os
13
from autotest.client.shared import error
14
from virttest.aexpect import ShellCmdError
15
from virttest import utils_misc, utils_spice, aexpect
16

L
Lucas Meneghel Rodrigues 已提交
17

18
def install_pygtk(guest_session, params):
19
    """
20
    Install PyGTK to a VM with yum package manager.
21

L
Lucas Meneghel Rodrigues 已提交
22 23
    :param guest_session - ssh session to guest VM
    :param params
24 25
    """

26
    cmd = "rpm -q pygtk2"
27 28 29
    try:
        guest_session.cmd(cmd)
    except ShellCmdError:
30 31 32
        cmd = "yum -y install pygtk2 --nogpgcheck > /dev/null"
        logging.info("Installing pygtk2 package to %s",
                    params.get("guest_vm"))
33 34
        guest_session.cmd(cmd, timeout=60)

L
Lucas Meneghel Rodrigues 已提交
35

36 37 38 39 40
def deploy_test_form(test, guest_vm, params):
    """
    Copy wxPython Test form to guest VM.
    Test form is copied to /tmp directory.

L
Lucas Meneghel Rodrigues 已提交
41 42 43
    :param test
    :param guest_vm - vm object
    :param params
44 45 46 47 48 49 50 51
    """

    script = params.get("guest_script")
    scriptdir = os.path.join("scripts", script)
    script_path = utils_misc.get_path(test.virtdir, scriptdir)
    guest_vm.copy_files_to(script_path, "/tmp/%s" % params.get("guest_script"),
                           timeout=60)

L
Lucas Meneghel Rodrigues 已提交
52

53 54 55 56 57
def run_test_form(guest_session, params):
    """
    Start wxPython simple test form on guest VM.
    Test form catches KeyEvents and is located in /tmp.

L
Lucas Meneghel Rodrigues 已提交
58 59
    :param guest_session - ssh session to guest VM
    :param params
60 61 62 63 64 65 66 67
    """

    logging.info("Starting test form for catching key events on guest")
    cmd = "python /tmp/%s &> /dev/null &" % params.get("guest_script")
    guest_session.cmd(cmd)
    cmd = "disown -ar"
    guest_session.cmd(cmd)

L
Lucas Meneghel Rodrigues 已提交
68

69 70
def get_test_results(guest_vm):
    """
L
Lucas Meneghel Rodrigues 已提交
71
    :param guest_vm - vm object
72 73 74 75 76 77 78
    """

    path = "/tmp/autotest-rv_input"
    guest_vm.copy_files_from(path, path, timeout=60)

    return path

L
Lucas Meneghel Rodrigues 已提交
79

80 81 82 83 84
def test_type_and_func_keys(client_vm, guest_session, params):
    """
    Test typewriter and functional keys.
    Function sends various keys through qemu monitor to client VM.

L
Lucas Meneghel Rodrigues 已提交
85 86 87
    :param client_vm - vm object
    :param guest_session - ssh session to guest VM
    :param params
88 89 90 91 92
    """

    run_test_form(guest_session, params)
    utils_spice.wait_timeout(3)

L
Lucas Meneghel Rodrigues 已提交
93
    # Send typewriter and functional keys to client machine based on scancodes
94 95
    logging.info("Sending typewriter and functional keys to client machine")
    for i in range(1, 69):
L
Lucas Meneghel Rodrigues 已提交
96
        # Avoid Ctrl, RSH, LSH, PtScr, Alt, CpsLk
97 98 99 100
        if not (i in [29, 42, 54, 55, 56, 58]):
            client_vm.send_key(str(hex(i)))
            utils_spice.wait_timeout(0.3)

L
Lucas Meneghel Rodrigues 已提交
101

102 103 104 105 106
def test_leds_and_esc_keys(client_vm, guest_session, params):
    """
    Test LEDS and Escaped keys.
    Function sends various keys through qemu monitor to client VM.

L
Lucas Meneghel Rodrigues 已提交
107 108 109
    :param client_vm - vm object
    :param guest_session - ssh session to guest VM
    :param params
110 111
    """

112
    #Run PyGTK form catching KeyEvents on guest
113 114 115
    run_test_form(guest_session, params)
    utils_spice.wait_timeout(3)

L
Lucas Meneghel Rodrigues 已提交
116
    # Prepare lists with the keys to be sent to client machine
117
    leds = ['a', 'caps_lock', 'a', 'caps_lock', 'num_lock', 'kp_1', 'num_lock',
L
Lucas Meneghel Rodrigues 已提交
118
            'kp_1']
119
    shortcuts = ['a', 'shift-a', 'shift_r-a', 'ctrl-a', 'ctrl-c', 'ctrl-v',
L
Lucas Meneghel Rodrigues 已提交
120
                 'alt-x']
121
    escaped = ['insert', 'delete', 'home', 'end', 'pgup', 'pgdn', 'up',
L
Lucas Meneghel Rodrigues 已提交
122
               'down', 'right', 'left']
123 124 125

    test_keys = leds + shortcuts + escaped

L
Lucas Meneghel Rodrigues 已提交
126
    # Send keys to client machine
127 128 129 130 131
    logging.info("Sending leds and escaped keys to client machine")
    for key in test_keys:
        client_vm.send_key(key)
        utils_spice.wait_timeout(0.3)

L
Lucas Meneghel Rodrigues 已提交
132

133 134 135 136 137
def test_nonus_layout(client_vm, guest_session, params):
    """
    Test some keys of non-us keyboard layouts (de, cz).
    Function sends various keys through qemu monitor to client VM.

L
Lucas Meneghel Rodrigues 已提交
138 139 140
    :param client_vm - vm object
    :param guest_session - ssh session to guest VM
    :param params
141 142
    """

143
    #Run PyGTK form catching KeyEvents on guest
144 145 146
    run_test_form(guest_session, params)
    utils_spice.wait_timeout(3)

L
Lucas Meneghel Rodrigues 已提交
147
    # Czech layout - test some special keys
148 149 150 151 152 153 154 155
    cmd = "setxkbmap cz"
    guest_session.cmd(cmd)
    test_keys = ['7', '8', '9', '0', 'alt_r-x', 'alt_r-c', 'alt_r-v']
    logging.info("Sending czech keys to client machine")
    for key in test_keys:
        client_vm.send_key(key)
        utils_spice.wait_timeout(0.3)

L
Lucas Meneghel Rodrigues 已提交
156
    # German layout - test some special keys
157 158 159 160 161 162 163 164 165 166 167
    cmd = "setxkbmap de"
    guest_session.cmd(cmd)
    test_keys = ['minus', '0x1a', 'alt_r-q', 'alt_r-m']
    logging.info("Sending german keys to client machine")
    for key in test_keys:
        client_vm.send_key(key)
        utils_spice.wait_timeout(0.3)

    cmd = "setxkbmap us"
    guest_session.cmd(cmd)

L
Lucas Meneghel Rodrigues 已提交
168

169 170 171 172 173 174
def test_leds_migration(client_vm, guest_vm, guest_session, params):
    """
    Check LEDS after migration.
    Function sets LEDS (caps, num) to ON and send scancodes of "a" and "1 (num)"
    and expected to get keycodes of "A" and "1" after migration.

L
Lucas Meneghel Rodrigues 已提交
175 176 177 178
    :param client_vm - vm object
    :param guest_vm - vm object
    :param guest_session - ssh session to guest VM
    :param params
179 180
    """

181 182 183 184 185 186 187 188 189 190
    # Turn numlock on RHEL6 on before the test begins:
    grep_ver_cmd = "grep -o 'release [[:digit:]]' /etc/redhat-release"
    rhel_ver = guest_session.cmd(grep_ver_cmd).strip()

    logging.info("RHEL version: #{0}#".format(rhel_ver))

    if rhel_ver == "release 6":
        client_vm.send_key('num_lock')

    #Run PyGTK form catching KeyEvents on guest
191 192 193
    run_test_form(guest_session, params)
    utils_spice.wait_timeout(3)

L
Lucas Meneghel Rodrigues 已提交
194
    # Tested keys before migration
195 196 197 198 199 200 201
    test_keys = ['a', 'kp_1', 'caps_lock', 'num_lock', 'a', 'kp_1']
    logging.info("Sending leds keys to client machine before migration")
    for key in test_keys:
        client_vm.send_key(key)
        utils_spice.wait_timeout(0.3)

    guest_vm.migrate()
202
    utils_spice.wait_timeout(8)
203

204 205
    #Tested keys after migration
    test_keys = ['a', 'kp_1', 'caps_lock', 'num_lock']
206 207 208 209
    logging.info("Sending leds keys to client machine after migration")
    for key in test_keys:
        client_vm.send_key(key)
        utils_spice.wait_timeout(0.3)
210
    utils_spice.wait_timeout(30)
211

L
Lucas Meneghel Rodrigues 已提交
212

213 214 215 216
def analyze_results(file_path, test_type):
    """
    Analyze results - compare caught keycodes and expected keycodes.

L
Lucas Meneghel Rodrigues 已提交
217 218
    :param file_path - path to file with results
    :param test_type - type of the test
219 220 221
    """

    if test_type == "type_and_func_keys":
222 223 224 225 226 227 228 229 230 231
        #List of expected keycodes from guest machine
        correct_keycodes = ['65307', '49', '50', '51', '52', '53', '54', '55',
                            '56', '57', '48', '45', '61', '65288', '65289',
                            '113', '119', '101', '114', '116', '121', '117',
                            '105', '111', '112', '91', '93', '65293', '97',
                            '115', '100', '102', '103', '104', '106', '107',
                            '108', '59', '39', '96', '92', '122', '120', '99',
                            '118', '98', '110', '109', '44', '46', '47', '32',
                            '65470', '65471', '65472', '65473', '65474',
                             '65475', '65476', '65477', '65478', '65479']
232
    elif test_type == "leds_and_esc_keys":
233 234 235 236 237 238
        correct_keycodes = ['97', '65509', '65', '65509', '65407', '65457',
                            '65407', '65436', '97', '65505', '65', '65506',
                            '65', '65507', '97', '65507', '99', '65507', '118',
                            '65513', '120', '65379', '65535', '65360', '65367',
                            '65365', '65366', '65362', '65364', '65363',
                            '65361']
239
    elif test_type == "nonus_layout":
240 241 242
        correct_keycodes = ['253', '225', '237', '233', '65027', '35', '65027',
                            '38', '65027', '64', '223', '252', '65027', '64',
                            '65027', '181']
243
    elif test_type == "leds_migration":
244 245
        correct_keycodes = ['97', '65457', '65509', '65407', '65', '65436',
                            '65', '65436', '65509', '65407']
246

L
Lucas Meneghel Rodrigues 已提交
247
    # Read caught keycodes on guest machine
248 249 250
    fileobj = open(file_path, "r")
    keycodes = fileobj.read()
    fileobj.close()
251

252 253
    #Compare caught keycodes with expected keycodes
    test_keycodes = keycodes.split()
254 255 256 257 258 259 260
    logging.info("Caught keycodes:%s", test_keycodes)
    for i in range(len(correct_keycodes)):
        if not (test_keycodes[i] == correct_keycodes[i]):
            return correct_keycodes[i]

    return None

L
Lucas Meneghel Rodrigues 已提交
261

262 263 264 265 266
def run_rv_input(test, params, env):
    """
    Test for testing keyboard inputs through spice.
    Test depends on rv_connect test.

L
Lucas Meneghel Rodrigues 已提交
267 268 269
    :param test: QEMU test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
270 271 272 273 274 275 276 277 278
    """

    guest_vm = env.get_vm(params["guest_vm"])
    guest_vm.verify_alive()

    client_vm = env.get_vm(params["client_vm"])
    client_vm.verify_alive()

    guest_session = guest_vm.wait_for_login(
L
Lucas Meneghel Rodrigues 已提交
279
        timeout=int(params.get("login_timeout", 360)))
280
    guest_root_session = guest_vm.wait_for_login(
281 282
              timeout=int(params.get("login_timeout", 360)),
              username="root", password="123456")
283 284

    # Verify that gnome is now running on the guest
285 286 287 288 289 290
    try:
        guest_session.cmd("ps aux | grep -v grep | grep gnome-session")
    except aexpect.ShellCmdError:
        raise error.TestWarn("gnome-session was probably not corretly started")

    guest_session.cmd("export DISPLAY=:0.0")
291

292
    install_pygtk(guest_root_session, params)
293 294 295

    deploy_test_form(test, guest_vm, params)

L
Lucas Meneghel Rodrigues 已提交
296
    # Get test type and perform proper test
297 298 299 300 301
    test_type = params.get("config_test")
    test_mapping = {'type_and_func_keys': test_type_and_func_keys,
                    'leds_and_esc_keys': test_leds_and_esc_keys,
                    'nonus_layout': test_nonus_layout,
                    'leds_migration': test_leds_migration}
L
Lucas Meneghel Rodrigues 已提交
302 303 304 305 306
    test_parameters = {
        'type_and_func_keys': (client_vm, guest_session, params),
        'leds_and_esc_keys': (client_vm, guest_session, params),
        'nonus_layout': (client_vm, guest_session, params),
        'leds_migration': (client_vm, guest_vm, guest_session, params)}
307 308 309 310 311 312 313 314 315

    try:
        func = test_mapping[test_type]
        args = test_parameters[test_type]
    except:
        raise error.TestFail("Unknown type of test")

    func(*args)

L
Lucas Meneghel Rodrigues 已提交
316
    # Get file with caught keycodes from guest
317
    result_path = get_test_results(guest_vm)
L
Lucas Meneghel Rodrigues 已提交
318 319
    # Analyze results and raise fail exp. If sent scancodes
    # do not match with expected keycodes
320 321 322
    result = analyze_results(result_path, test_type)
    if result is not None:
        raise error.TestFail("Testing of sending keys failed:"
L
Lucas Meneghel Rodrigues 已提交
323
                             "  Expected keycode = %s" % result)
324 325

    guest_session.close()