diff mbox

[2/6] KVM test: Introducing unattended install subtest

Message ID 1941964121.1414421249352901620.JavaMail.root@zmail05.collab.prod.int.phx2.redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Michael Goldish Aug. 4, 2009, 2:28 a.m. UTC
The test code looks nice and simple.  However, I don't think putting some
of the code in a class helps much (it doesn't hurt either).

What do you think about this alternative, replacing from 'watcher = ...':

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('', 12323))
server.listen(1)

end_time = time.time() + float(params.get("timeout", 3000))

while True:
    server.settimeout(end_time - time.time())
    try:
        (client, addr) = server.accept()
    except socket.timeout:
        server.close()
        raise error.TestFail('Timeout elapsed while waiting for install to '
                             'finish.')
    msg = client.recv(1024)
    logging.debug("Received '%s' from %s", msg, addr)
    if msg == 'done':
        logging.info('Guest reported successful installation')
        server.close()
        break
    else:
        logging.error('Got invalid string from client: %s.' % msg)

It's not necessarily shorter, but I find it a bit simpler.
The only meaningful difference here is the timeout handling.
In your original code, if a client sends anything other than 'done'
(very unlikely), the server will wait another 3000 secs.
With this code, the server will wait up to a total of 'timeout' secs
(user specified).
I find this code short and simple enough to leave outside a class,
but it's a matter of personal preference, so it's up to you.

----- Original Message -----
From: "Lucas Meneghel Rodrigues" <lmr@redhat.com>
To: autotest@test.kernel.org
Cc: kvm@vger.kernel.org, dhuff@redhat.com, "Lucas Meneghel Rodrigues" <lmr@redhat.com>
Sent: Tuesday, August 4, 2009 1:38:50 AM (GMT+0200) Auto-Detected
Subject: [PATCH 2/6] KVM test: Introducing unattended install subtest

In order to resolve the question, 'how will the guest
operating system tell the host operating system that
the unattended install process finish', we took the
simple approach and created a simple socket communication
ack process: The test instantiates a server tcp socket
on port 12323, and waits during a specified amount of time.

For guests, the vast majority of the unattended install
processes can deal with executing commands at the end of
the install process. Let's take advantage of that and
make clients tell the server about the end of the process
using simple programs that can do that. The implementation
of that strategy varies trough different operating systems.

This is the kvm test implementation code, client programs
will follow on later patches.

Signed-off-by: Lucas Meneghel Rodrigues <lmr@redhat.com>
---
 client/tests/kvm/kvm.py       |    2 +
 client/tests/kvm/kvm_tests.py |   81 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 82 insertions(+), 1 deletions(-)
diff mbox

Patch

diff --git a/client/tests/kvm/kvm.py b/client/tests/kvm/kvm.py
index 070e463..db6899b 100644
--- a/client/tests/kvm/kvm.py
+++ b/client/tests/kvm/kvm.py
@@ -56,6 +56,8 @@  class kvm(test.test):
                 "linux_s3":     test_routine("kvm_tests", "run_linux_s3"),
                 "stress_boot":  test_routine("kvm_tests", "run_stress_boot"),
                 "timedrift":    test_routine("kvm_tests", "run_timedrift"),
+                "unattended_install":  test_routine("kvm_tests",
+                                                    "run_unattended_install"),
                 }
 
         # Make it possible to import modules from the test's bindir
diff --git a/client/tests/kvm/kvm_tests.py b/client/tests/kvm/kvm_tests.py
index 9784ec9..f45fefc 100644
--- a/client/tests/kvm/kvm_tests.py
+++ b/client/tests/kvm/kvm_tests.py
@@ -1,4 +1,4 @@ 
-import time, os, logging, re, commands
+import time, os, logging, re, commands, socket
 from autotest_lib.client.common_lib import utils, error
 import kvm_utils, kvm_subprocess, ppm_utils, scan_results
 
@@ -9,6 +9,85 @@  KVM test definitions.
 """
 
 
+class UnattendedInstallWatcher:
+    """
+    Mechanism to verify whether an unattended guest install actually did finish.
+    It opens a TCP socket and waits until it receives a message. If it does get
+    the expected message from the guest, it will finish gracefully.
+    """
+    def __init__(self, timeout, msg):
+        self.port = 12323
+        self.buf_size = 1024
+        self.timeout = timeout
+        self.msg = msg
+
+
+    def check_answer(self, connection):
+        """
+        Verify if client has sent the correct ACK message.
+
+        @param connection: Tuple with client socket connection and address.
+        @return: True, in case the client has responded accordingly; False if
+                the string doesn't match the server expectations.
+        """
+        (client, addr) = connection
+        msg = client.recv(self.buf_size)
+        logging.debug("Received '%s' from %s", msg, addr)
+        if msg == self.msg:
+            logging.info('Guest reported successful installation')
+            return True
+        else:
+            logging.error('Got invalid string from client: %s.' % msg)
+            return False
+
+
+    def run_server(self):
+        """
+        Initializes and runs the server socket and listens for connections.
+        """
+        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        address = ('', self.port)
+        server.bind(address)
+        server.listen(1)
+
+        while True:
+            try:
+                server.settimeout(self.timeout)
+                connection = server.accept()
+                if self.check_answer(connection):
+                    break
+            except:
+                server.close()
+                raise
+
+
+def run_unattended_install(test, params, env):
+    """
+    Unattended install test:
+    1) Starts a VM with an appropriated setup to start an unattended OS install.
+    2) Wait until the install reports to the install watcher its end.
+
+    @param test: KVM test object.
+    @param params: Dictionary with the test parameters.
+    @param env: Dictionary with test environment.
+    """
+    vm = kvm_utils.env_get_vm(env, params.get("main_vm"))
+    if not vm:
+        raise error.TestError("VM object not found in environment")
+    if not vm.is_alive():
+        raise error.TestError("VM seems to be dead; Test requires a living VM")
+
+    logging.info("Starting unattended install watch process")
+    watcher = UnattendedInstallWatcher(timeout=3000, msg='done')
+    try:
+        watcher.run_server()
+    except socket.timeout:
+        raise error.TestFail('Timeout elapsed while waiting for install to '
+                             'finish.')
+
+    logging.info("Unattended install finished successfuly")
+
+
 def run_boot(test, params, env):
     """
     KVM reboot test: