[libvirt] [test-API PATCH] Added screenshot test

Peter Krempa pkrempa at redhat.com
Tue Apr 3 13:34:55 UTC 2012


On 04/02/2012 08:18 PM, Martin Kletzander wrote:
> This patch adds a test that obtains a screenshot of a domain and saves
> it in a file.

Maybe we shoud later on modify this or add another test to allow 
comparing the returned image file against a pre-defined pattern, so we 
could actualy check if the complete stack including the OS, qemu, video 
drivers and libvirt's streams work.

> ---
>   repos/domain/screenshot.py |   57 ++++++++++++++++++++++++++++++++++++++++++++
>   1 files changed, 57 insertions(+), 0 deletions(-)
>   create mode 100644 repos/domain/screenshot.py
>
> diff --git a/repos/domain/screenshot.py b/repos/domain/screenshot.py
> new file mode 100644
> index 0000000..9986cab
> --- /dev/null
> +++ b/repos/domain/screenshot.py
> @@ -0,0 +1,57 @@
> +#!/usr/bin/env python
> +"""This test is used for creating a screenshot of a domain and saving
> +   it in a file. The Screenshot format is hypervisor specific.
> +
> +   mandatory arguments: guestname
> +                        screen
> +                        filename
> +"""
> +
> +import os
> +import mimetypes
> +
> +import libvirt
> +
> +def check_params(params):
> +    """Verify input parameters"""
> +    for key in ('guestname', 'screen', 'filename'):
> +        if key not in params:
> +            raise KeyError('Missing key %s required for screenshot test' % key)
> +
> +    params['screen'] = int(params['screen'])
> +    params['filename'] = os.path.abspath(params['filename'])
> +
> +def saver(stream, data, file_):
> +    return file_.write(data)
> +
> +def screenshot(params):
> +    """This method takes a screenshot of a running machine and saves
> +    it in a filename"""
> +    ret = 1

I'm starting to notice this pattern. My test contain something very 
similar ... a try block, that contains all of the code for testing and 
then some exception and other cleanup code. I think this pattern will be 
very common and we should consider moving it one level up, to free the 
test writers from copying blocks of common code across all test cases.

> +    try:
> +        logger = params['logger']
> +
> +        check_params(params)
> +
> +        conn = libvirt.open(params['uri'])
> +        dom = conn.lookupByName(params['guestname'])
> +
> +        st = conn.newStream(0)
> +        mime = dom.screenshot(st, params['screen'], 0)
> +
> +        ext = mimetypes.guess_extension(mime) or '.ppm'
> +        filename = params['filename'] + ext
> +        f = file(filename, 'w')
> +
> +        logger.debug('Saving screenshot into %s' % filename)
> +        st.recvAll(saver, f)
> +        logger.debug('Mimetype of the file is %s' % mime)
> +
> +        ret = st.finish()
> +
> +    finally:
> +        # Some error occurred, cleanup
> +        if 'conn' in locals() and conn.isAlive():
> +            conn.close()
> +
> +    return ret

Looks good, but I'm not a very experienced Pythonist, so if somebody 
else (Guannan?) could have a quick look and confirm my ACK before pushing.

Peter




More information about the libvir-list mailing list