~ubuntu-branches/ubuntu/quantal/virtinst/quantal-proposed

« back to all changes in this revision

Viewing changes to tests/utils.py

  • Committer: Bazaar Package Importer
  • Author(s): Marc Deslauriers
  • Date: 2011-02-01 15:40:11 UTC
  • mfrom: (1.3.16 experimental)
  • Revision ID: james.westby@ubuntu.com-20110201154011-op0nusgc240xajvb
Tags: 0.500.5-1ubuntu1
* Merge from debian experimental. Remaining changes:
  - debian/patches/9001_Ubuntu.patch:
     + Updated to add maverick and natty to OS list and enable virtio
       for them.
  - debian/patches/9003-fix-path-to-hvmloader-in-testsuite.patch: adjust
    testsuite for 0001-fix-path-to-hvmloader.patch and
    0002-Fix-path-to-pygrub.patch. (refreshed)
  - debian/control: added acl package to depends.
  - Demote virt-viewer to Suggests, as it's in universe.
  - Recommends libvirt-bin
* Removed patches:
  - debian/patches/9002-libvirt_disk_format.patch: Upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# This program is free software; you can redistribute it and/or modify
 
3
# it under the terms of the GNU General Public License as published by
 
4
# the Free  Software Foundation; either version 2 of the License, or
 
5
# (at your option)  any later version.
 
6
#
 
7
# This program is distributed in the hope that it will be useful,
 
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
# GNU General Public License for more details.
 
11
#
 
12
# You should have received a copy of the GNU General Public License
 
13
# along with this program; if not, write to the Free Software
 
14
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
15
# MA 02110-1301 USA.
 
16
 
 
17
import difflib
 
18
import os
 
19
import logging
 
20
 
 
21
import libvirt
 
22
import virtinst.cli
 
23
 
 
24
# Used to ensure consistent SDL xml output
 
25
os.environ["HOME"] = "/tmp"
 
26
os.environ["DISPLAY"] = ":3.4"
 
27
 
 
28
_cwd        = os.getcwd()
 
29
_testuri    = "test:///%s/tests/testdriver.xml" % _cwd
 
30
_fakeuri    = "__virtinst_test__" + _testuri + ",predictable"
 
31
_kvmcaps    = "%s/tests/capabilities-xml/libvirt-0.7.6-qemu-caps.xml" % _cwd
 
32
_kvmuri     = "%s,caps=%s,qemu" % (_fakeuri, _kvmcaps)
 
33
 
 
34
def get_debug():
 
35
    return ("DEBUG_TESTS" in os.environ and
 
36
            os.environ["DEBUG_TESTS"] == "1")
 
37
 
 
38
def open_testdriver():
 
39
    return virtinst.cli.getConnection(_testuri)
 
40
 
 
41
def open_testkvmdriver():
 
42
    return virtinst.cli.getConnection(_kvmuri)
 
43
 
 
44
# Register libvirt handler
 
45
def libvirt_callback(ignore, err):
 
46
    logging.warn("libvirt errmsg: %s" % err[2])
 
47
libvirt.registerErrorHandler(f=libvirt_callback, ctx=None)
 
48
 
 
49
def sanitize_xml_for_define(xml):
 
50
    # Libvirt throws errors since we are defining domain
 
51
    # type='xen', when test driver can only handle type='test'
 
52
    # Sanitize the XML so we can define
 
53
    if not xml:
 
54
        return xml
 
55
 
 
56
    xml = xml.replace("<domain type='xen'>",
 
57
                      "<domain type='test'>")
 
58
    xml = xml.replace(">linux<", ">xen<")
 
59
 
 
60
    return xml
 
61
 
 
62
def test_create(testconn, xml):
 
63
    xml = sanitize_xml_for_define(xml)
 
64
 
 
65
    try:
 
66
        dom = testconn.defineXML(xml)
 
67
    except Exception, e:
 
68
        raise RuntimeError(str(e) + "\n" + xml)
 
69
 
 
70
    try:
 
71
        dom.create()
 
72
        dom.destroy()
 
73
        dom.undefine()
 
74
    except:
 
75
        try:
 
76
            dom.destroy()
 
77
        except:
 
78
            pass
 
79
        try:
 
80
            dom.undefine()
 
81
        except:
 
82
            pass
 
83
 
 
84
def read_file(filename):
 
85
    """Helper function to read a files contents and return them"""
 
86
    f = open(filename, "r")
 
87
    out = f.read()
 
88
    f.close()
 
89
 
 
90
    return out
 
91
 
 
92
def diff_compare(actual_out, filename=None, expect_out=None):
 
93
    """Compare passed string output to contents of filename"""
 
94
    if not expect_out:
 
95
        expect_out = read_file(filename)
 
96
 
 
97
    diff = "".join(difflib.unified_diff(expect_out.splitlines(1),
 
98
                                        actual_out.splitlines(1),
 
99
                                        fromfile=filename,
 
100
                                        tofile="Generated Output"))
 
101
    if diff:
 
102
        raise AssertionError("Conversion outputs did not match.\n%s" % diff)