[libvirt-test-API PATCH 1/2] domain:screenshot: fixes and cleanup

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



After the "conn" object was transferred into sharedmod, its import was missing.
I also cleaned up and commented few lines in order to use them for
documentation purposes.
---
 repos/domain/screenshot.py |   45 ++++++++++++++++++++++++++++++++++---------
 1 files changed, 35 insertions(+), 10 deletions(-)

diff --git a/repos/domain/screenshot.py b/repos/domain/screenshot.py
index eb5d0e2..49028fe 100644
--- a/repos/domain/screenshot.py
+++ b/repos/domain/screenshot.py
@@ -4,42 +4,67 @@

 import os
 import mimetypes
-
 import libvirt

+# "sharedmod" is needed in order to get the shared connection object
+import sharedmod
+
+# These variables are mandatory for running the test
+# The test won't be run unless both "guestname" and "filename" are specified.
 required_params = ('guestname', 'filename',)
 optional_params = ('screen',)

-last_filename = None
+# In this filename we store the last filename that has a screenshot
+# saved inside in order to be able to clean it after
+filename = None

+# This is just a helper function that will be used later.
 def saver(stream, data, file_):
     return file_.write(data)

+# This is the main test
 def screenshot(params):
     """This method takes a screenshot of a running machine and saves
     it in a filename"""
-    ret = 1
+
+    # Shared logger to be used for messages output
     logger = params['logger']

+    # Shared connection object to be used for messages output
     conn = sharedmod.libvirtobj['conn']
-    dom = conn.lookupByName(params['guestname'])
+    guestname = params['guestname']
+
+    logger.info('Looking for domain %s' % guestname)
+    dom = conn.lookupByName(guestname)
+    logger.debug('Domain %s found' % guestname)

     st = conn.newStream(0)
     screen = params.get('screen', 0)
+    logger.info('Taking a screenshot')
     mime = dom.screenshot(st, int(screen), 0)
+    logger.debug('Screenshot taken')

+    # As mentioned before, the screenshot format is
+    # hypervisor-specific
     ext = mimetypes.guess_extension(mime) or '.ppm'
-    last_filename = params['filename'] + ext
+    filename = params['filename'] + ext
+
+    # Here we create a file object used later
     f = file(filename, 'w')

-    logger.debug('Saving screenshot into %s' % filename)
+    logger.info('Saving screenshot into %s' % filename)
+    logger.info('Mimetype of the file is %s' % mime)
+    # Here is the use of the "saver" function
     st.recvAll(saver, f)
-    logger.debug('Mimetype of the file is %s' % mime)
-
-    ret = st.finish()
+    logger.debug('Screenshot saved')

-    return ret
+    # The test fails (return is different from 0) if the stream is not
+    # properly ended
+    return st.finish()

+# This cleanup function is called if specified by the configuration
+# file. In this file we remove the saved screenshot if the filename is
+# known.
 def cleanup(params):
     if last_filename:
         os.remove(last_filename)
--
1.7.8.5

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]