File: clonetest.py

package info (click to toggle)
virtinst 0.500.3-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,084 kB
  • ctags: 2,393
  • sloc: python: 14,166; xml: 5,181; sh: 126; makefile: 6
file content (237 lines) | stat: -rw-r--r-- 8,254 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free  Software Foundation; either version 2 of the License, or
# (at your option)  any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.

import unittest
import os, os.path
import logging
import tests

from virtinst import CloneManager
CloneDesign = CloneManager.CloneDesign

ORIG_NAME  = "clone-orig"
CLONE_NAME = "clone-new"

# Create some files to use as test images
FILE1 = "/tmp/virtinst-test1.img"
FILE2 = "/tmp/virtinst-test2.img"
P1_VOL1  = "/default-pool/testvol1.img"
P1_VOL2  = "/default-pool/testvol2.img"
P2_VOL1  = "/cross-pool/testvol1.img"
P2_VOL2  = "/cross-pool/testvol2.img"

POOL1 = "/default-pool"
POOL2 = "/cross-pool"
DISKPOOL = "/disk-pool"

local_files = [ FILE1, FILE2]

clonexml_dir = os.path.join(os.getcwd(), "tests/clone-xml")
clone_files = []

for tmpf in os.listdir(clonexml_dir):
    black_list = [ "managed-storage", "cross-pool", "force", "skip",
                   "fullpool"]
    if tmpf.endswith("-out.xml"):
        tmpf = tmpf[0:(len(tmpf) - len("-out.xml"))]
        if tmpf not in clone_files and tmpf not in black_list:
            clone_files.append(tmpf)

conn = tests.open_testdriver()

def fake_is_uri_remote(ignore):
    return True

class TestClone(unittest.TestCase):

    def setUp(self):
        for f in local_files:
            os.system("touch %s" % f)

    def tearDown(self):
        for f in local_files:
            os.unlink(f)

    def _clone_helper(self, filebase, disks=None, force_list=None,
                      skip_list=None, compare=True):
        """Helper for comparing clone input/output from 2 xml files"""
        infile = os.path.join(clonexml_dir, filebase + "-in.xml")
        in_content = tests.read_file(infile)

        cloneobj = CloneDesign(connection=conn)
        cloneobj.original_xml = in_content
        for force in force_list or []:
            cloneobj.force_target = force
        for skip in skip_list or []:
            cloneobj.skip_target = skip

        cloneobj = self._default_clone_values(cloneobj, disks)

        if compare:
            self._clone_compare(cloneobj, filebase)
            self._clone_define(filebase)
        else:
            cloneobj.setup()

    def _default_clone_values(self, cloneobj, disks=None):
        """Sets default values for the cloned VM."""
        cloneobj.clone_name = "clone-new"
        cloneobj.clone_uuid = "12345678-1234-1234-1234-123456789012"

        cloneobj.clone_mac = "01:23:45:67:89:00"
        cloneobj.clone_mac = "01:23:45:67:89:01"

        if disks != None:
            for disk in disks:
                cloneobj.clone_devices = disk
        else:
            cloneobj.clone_devices = "/dev/loop0"
            cloneobj.clone_devices = "/tmp/clone2.img"
            cloneobj.clone_devices = "/tmp/clone3.img"
            cloneobj.clone_devices = "/tmp/clone4.img"
            cloneobj.clone_devices = "/tmp/clone5.img"
            cloneobj.clone_devices = None

        return cloneobj

    def _clone_compare(self, cloneobj, outbase):
        """Helps compare output from passed clone instance with an xml file"""
        outfile = os.path.join(clonexml_dir, outbase + "-out.xml")

        cloneobj.setup()

        tests.diff_compare(cloneobj.clone_xml, outfile)

    def _clone_define(self, filebase):
        """Take the valid output xml and attempt to define it on the
           connection to ensure we don't get any errors"""
        outfile = os.path.join(clonexml_dir, filebase + "-out.xml")
        outxml = tests.read_file(outfile)

        vm = None
        try:
            vm = conn.defineXML(outxml)
        finally:
            if vm:
                vm.undefine()

    # Skip this test, since libvirt can add new XML elements to the defined
    # XML (<video>) that make roundtrip a pain
    def notestCloneGuestLookup(self):
        """Test using a vm name lookup for cloning"""
        for base in clone_files:
            infile = os.path.join(clonexml_dir, base + "-in.xml")

            vm = None
            try:
                vm = conn.defineXML(tests.read_file(infile))

                cloneobj = CloneDesign(connection=conn)
                cloneobj.original_guest = ORIG_NAME

                cloneobj = self._default_clone_values(cloneobj)
                self._clone_compare(cloneobj, base)
            finally:
                if vm:
                    vm.undefine()

    def testCloneFromFile(self):
        """Test using files for input and output"""
        for base in clone_files:
            self._clone_helper(base)

    def testRemoteNoStorage(self):
        """Test remote clone where VM has no storage that needs cloning"""
        oldfunc = CloneManager._util.is_uri_remote
        try:
            CloneManager._util.is_uri_remote = fake_is_uri_remote

            for base in [ "nostorage", "noclone-storage" ] :
                self._clone_helper(base, disks=[])

        finally:
            CloneManager._util.is_uri_remote = oldfunc

    def testRemoteWithStorage(self):
        """
        Test remote clone with storage needing cloning. Should fail,
        since libvirt has no storage clone api.
        """
        oldfunc = CloneManager._util.is_uri_remote
        try:
            CloneManager._util.is_uri_remote = fake_is_uri_remote

            for base in [ "general-cfg" ] :
                try:
                    self._clone_helper(base, disks=["%s/1.img" % POOL1,
                                                    "%s/2.img" % POOL1])

                    # We shouldn't succeed, so test fails
                    raise AssertionError("Remote clone with storage passed "
                                         "when it shouldn't.")
                except (ValueError, RuntimeError), e:
                    # Exception expected
                    logging.debug("Received expected exception: %s" % str(e))
        finally:
            CloneManager._util.is_uri_remote = oldfunc

    def testCloneStorage(self):
        base = "managed-storage"
        self._clone_helper(base, ["%s/new1.img" % POOL1,
                                  "%s/new2.img" % DISKPOOL])

    def testCloneStorageCrossPool(self):
        base = "cross-pool"
        self._clone_helper(base, ["%s/new1.img" % POOL2,
                                  "%s/new2.img" % POOL2])

    def testCloneStorageForce(self):
        base = "force"
        self._clone_helper(base,
                           disks=["/dev/loop0", None, "/tmp/clone2.img"],
                           force_list=["hda", "fdb", "sdb"])

    def testCloneStorageSkip(self):
        base = "skip"
        self._clone_helper(base,
                           disks=["/dev/loop0", None, "/tmp/clone2.img"],
                           skip_list=["hda", "fdb"])

    def testCloneFullPool(self):
        base = "fullpool"
        try:
            self._clone_helper(base, disks=["/full-pool/test.img"],
                               compare=False)
        except Exception:
            return

        raise AssertionError("Expected exception, but none raised.")

    def testCloneManagedToUnmanaged(self):
        base = "managed-storage"

        # We are trying to clone from a pool (/default-pool) to unmanaged
        # storage. For this case, the cloning needs to fail back to manual
        # operation (no libvirt calls), but since /default-pool doesn't exist,
        # this should fail.
        try:
            self._clone_helper(base, ["/tmp/new1.img", "/tmp/new2.img"])

            raise AssertionError("Managed to unmanaged succeeded, expected "
                                 "failure.")
        except (ValueError, RuntimeError), e:
            logging.debug("Received expected exception: %s" % str(e))