Statistics
| Branch: | Tag: | Revision:

root / test / py / ganeti.ovf_unittest.py @ 14933c17

History | View | Annotate | Download (30.1 kB)

1 1e6fab60 Agata Murawska
#!/usr/bin/python
2 1e6fab60 Agata Murawska
#
3 1e6fab60 Agata Murawska
4 6e8091f9 Dimitris Aragiorgis
# Copyright (C) 2011, 2012 Google Inc.
5 1e6fab60 Agata Murawska
#
6 1e6fab60 Agata Murawska
# This program is free software; you can redistribute it and/or modify
7 1e6fab60 Agata Murawska
# it under the terms of the GNU General Public License as published by
8 1e6fab60 Agata Murawska
# the Free Software Foundation; either version 2 of the License, or
9 1e6fab60 Agata Murawska
# (at your option) any later version.
10 1e6fab60 Agata Murawska
#
11 1e6fab60 Agata Murawska
# This program is distributed in the hope that it will be useful, but
12 1e6fab60 Agata Murawska
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 1e6fab60 Agata Murawska
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 1e6fab60 Agata Murawska
# General Public License for more details.
15 1e6fab60 Agata Murawska
#
16 1e6fab60 Agata Murawska
# You should have received a copy of the GNU General Public License
17 1e6fab60 Agata Murawska
# along with this program; if not, write to the Free Software
18 1e6fab60 Agata Murawska
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 1e6fab60 Agata Murawska
# 02110-1301, USA.
20 1e6fab60 Agata Murawska
21 1e6fab60 Agata Murawska
22 1e6fab60 Agata Murawska
"""Script for testing ganeti.ovf.
23 1e6fab60 Agata Murawska

24 1e6fab60 Agata Murawska
"""
25 1e6fab60 Agata Murawska
26 1e6fab60 Agata Murawska
import optparse
27 1e6fab60 Agata Murawska
import os
28 1e6fab60 Agata Murawska
import os.path
29 1e6fab60 Agata Murawska
import re
30 1e6fab60 Agata Murawska
import shutil
31 1e6fab60 Agata Murawska
import sys
32 1e6fab60 Agata Murawska
import tempfile
33 1e6fab60 Agata Murawska
import unittest
34 1e6fab60 Agata Murawska
35 d92518d3 Agata Murawska
try:
36 d92518d3 Agata Murawska
  import xml.etree.ElementTree as ET
37 d92518d3 Agata Murawska
except ImportError:
38 d92518d3 Agata Murawska
  import elementtree.ElementTree as ET
39 d92518d3 Agata Murawska
40 1e6fab60 Agata Murawska
from ganeti import constants
41 1e6fab60 Agata Murawska
from ganeti import errors
42 1e6fab60 Agata Murawska
from ganeti import ovf
43 1e6fab60 Agata Murawska
from ganeti import utils
44 a56625a2 Michael Hanselmann
from ganeti import pathutils
45 1e6fab60 Agata Murawska
46 1e6fab60 Agata Murawska
import testutils
47 1e6fab60 Agata Murawska
48 1e6fab60 Agata Murawska
OUTPUT_DIR = "newdir"
49 1e6fab60 Agata Murawska
50 1e6fab60 Agata Murawska
GANETI_DISKS = {
51 1e6fab60 Agata Murawska
  "disk_count": "1",
52 1e6fab60 Agata Murawska
  "disk0_dump": "new_disk.raw",
53 1e6fab60 Agata Murawska
  "disk0_size": "0",
54 1e6fab60 Agata Murawska
  "disk0_ivname": "disk/0",
55 1e6fab60 Agata Murawska
}
56 1e6fab60 Agata Murawska
GANETI_NETWORKS = {
57 1e6fab60 Agata Murawska
  "nic_count": "1",
58 1e6fab60 Agata Murawska
  "nic0_mode": "bridged",
59 1e6fab60 Agata Murawska
  "nic0_ip": "none",
60 1e6fab60 Agata Murawska
  "nic0_mac": "aa:00:00:d8:2c:1e",
61 1e6fab60 Agata Murawska
  "nic0_link": "xen-br0",
62 6e8091f9 Dimitris Aragiorgis
  "nic0_network": "auto",
63 1e6fab60 Agata Murawska
}
64 1e6fab60 Agata Murawska
GANETI_HYPERVISOR = {
65 1e6fab60 Agata Murawska
  "hypervisor_name": "xen-pvm",
66 1e6fab60 Agata Murawska
  "root-path": "/dev/sda",
67 1e6fab60 Agata Murawska
  "kernel_args": "ro",
68 1e6fab60 Agata Murawska
}
69 1e6fab60 Agata Murawska
GANETI_OS = {"os_name": "lenny-image"}
70 1e6fab60 Agata Murawska
GANETI_BACKEND = {
71 1e6fab60 Agata Murawska
  "vcpus": "1",
72 1e6fab60 Agata Murawska
  "memory" : "2048",
73 1e6fab60 Agata Murawska
  "auto_balance": "False",
74 1e6fab60 Agata Murawska
}
75 1e6fab60 Agata Murawska
GANETI_NAME = "ganeti-test-xen"
76 1e6fab60 Agata Murawska
GANETI_TEMPLATE = "plain"
77 1e6fab60 Agata Murawska
GANETI_TAGS = None
78 1e6fab60 Agata Murawska
GANETI_VERSION = "0"
79 1e6fab60 Agata Murawska
80 1e6fab60 Agata Murawska
VIRTUALBOX_DISKS = {
81 1e6fab60 Agata Murawska
  "disk_count": "2",
82 1e6fab60 Agata Murawska
  "disk0_ivname": "disk/0",
83 1e6fab60 Agata Murawska
  "disk0_dump": "new_disk.raw",
84 1e6fab60 Agata Murawska
  "disk0_size": "0",
85 1e6fab60 Agata Murawska
  "disk1_ivname": "disk/1",
86 1e6fab60 Agata Murawska
  "disk1_dump": "second_disk.raw",
87 1e6fab60 Agata Murawska
  "disk1_size": "0",
88 1e6fab60 Agata Murawska
}
89 1e6fab60 Agata Murawska
VIRTUALBOX_NETWORKS = {
90 1e6fab60 Agata Murawska
  "nic_count": "1",
91 1e6fab60 Agata Murawska
  "nic0_mode": "bridged",
92 1e6fab60 Agata Murawska
  "nic0_ip": "none",
93 1e6fab60 Agata Murawska
  "nic0_link": "auto",
94 1e6fab60 Agata Murawska
  "nic0_mac": "auto",
95 6e8091f9 Dimitris Aragiorgis
  "nic0_network": "auto",
96 1e6fab60 Agata Murawska
}
97 1e6fab60 Agata Murawska
VIRTUALBOX_HYPERVISOR = {"hypervisor_name": "auto"}
98 1e6fab60 Agata Murawska
VIRTUALBOX_OS = {"os_name": None}
99 1e6fab60 Agata Murawska
VIRTUALBOX_BACKEND = {
100 1e6fab60 Agata Murawska
 "vcpus": "1",
101 1e6fab60 Agata Murawska
  "memory" : "2048",
102 1e6fab60 Agata Murawska
  "auto_balance": "auto",
103 1e6fab60 Agata Murawska
}
104 1e6fab60 Agata Murawska
VIRTUALBOX_NAME = None
105 1e6fab60 Agata Murawska
VIRTUALBOX_TEMPLATE = None
106 1e6fab60 Agata Murawska
VIRTUALBOX_TAGS = None
107 1e6fab60 Agata Murawska
VIRTUALBOX_VERSION = None
108 1e6fab60 Agata Murawska
109 1e6fab60 Agata Murawska
EMPTY_DISKS = {}
110 1e6fab60 Agata Murawska
EMPTY_NETWORKS = {}
111 1e6fab60 Agata Murawska
EMPTY_HYPERVISOR = {"hypervisor_name": "auto"}
112 1e6fab60 Agata Murawska
EMPTY_OS = {}
113 1e6fab60 Agata Murawska
EMPTY_BACKEND = {
114 1e6fab60 Agata Murawska
  "vcpus": "auto",
115 1e6fab60 Agata Murawska
  "memory" : "auto",
116 1e6fab60 Agata Murawska
  "auto_balance": "auto",
117 1e6fab60 Agata Murawska
}
118 1e6fab60 Agata Murawska
EMPTY_NAME = None
119 1e6fab60 Agata Murawska
EMPTY_TEMPLATE = None
120 1e6fab60 Agata Murawska
EMPTY_TAGS = None
121 1e6fab60 Agata Murawska
EMPTY_VERSION = None
122 1e6fab60 Agata Murawska
123 1e6fab60 Agata Murawska
CMDARGS_DISKS = {
124 1e6fab60 Agata Murawska
  "disk_count": "1",
125 1e6fab60 Agata Murawska
  "disk0_ivname": "disk/0",
126 1e6fab60 Agata Murawska
  "disk0_dump": "disk0.raw",
127 1e6fab60 Agata Murawska
  "disk0_size": "8",
128 1e6fab60 Agata Murawska
}
129 1e6fab60 Agata Murawska
CMDARGS_NETWORKS = {
130 1e6fab60 Agata Murawska
  "nic0_link": "auto",
131 1e6fab60 Agata Murawska
  "nic0_mode": "bridged",
132 1e6fab60 Agata Murawska
  "nic0_ip": "none",
133 1e6fab60 Agata Murawska
  "nic0_mac": "auto",
134 1e6fab60 Agata Murawska
  "nic_count": "1",
135 6e8091f9 Dimitris Aragiorgis
  "nic0_network": "auto",
136 1e6fab60 Agata Murawska
}
137 1e6fab60 Agata Murawska
CMDARGS_HYPERVISOR = {
138 1e6fab60 Agata Murawska
  "hypervisor_name": "xen-pvm"
139 1e6fab60 Agata Murawska
}
140 1e6fab60 Agata Murawska
CMDARGS_OS = {"os_name": "lenny-image"}
141 1e6fab60 Agata Murawska
CMDARGS_BACKEND = {
142 1e6fab60 Agata Murawska
  "auto_balance": False,
143 1e6fab60 Agata Murawska
  "vcpus": "1",
144 1e6fab60 Agata Murawska
  "memory": "256",
145 1e6fab60 Agata Murawska
}
146 1e6fab60 Agata Murawska
CMDARGS_NAME = "test-instance"
147 1e6fab60 Agata Murawska
CMDARGS_TEMPLATE = "plain"
148 1e6fab60 Agata Murawska
CMDARGS_TAGS = "test-tag-1,test-tag-2"
149 1e6fab60 Agata Murawska
150 1e6fab60 Agata Murawska
ARGS_EMPTY = {
151 1e6fab60 Agata Murawska
  "output_dir": None,
152 1e6fab60 Agata Murawska
  "nics": [],
153 1e6fab60 Agata Murawska
  "disks": [],
154 1e6fab60 Agata Murawska
  "name": "test-instance",
155 d92518d3 Agata Murawska
  "ova_package": False,
156 d92518d3 Agata Murawska
  "ext_usage": False,
157 d92518d3 Agata Murawska
  "disk_format": "cow",
158 d92518d3 Agata Murawska
  "compression": False,
159 1e6fab60 Agata Murawska
}
160 1e6fab60 Agata Murawska
ARGS_EXPORT_DIR = dict(ARGS_EMPTY, **{
161 1e6fab60 Agata Murawska
  "output_dir": OUTPUT_DIR,
162 1e6fab60 Agata Murawska
  "name": None,
163 1e6fab60 Agata Murawska
  "hypervisor": None,
164 1e6fab60 Agata Murawska
  "os": None,
165 1e6fab60 Agata Murawska
  "beparams": {},
166 1e6fab60 Agata Murawska
  "no_nics": False,
167 1e6fab60 Agata Murawska
  "disk_template": None,
168 1e6fab60 Agata Murawska
  "tags": None,
169 1e6fab60 Agata Murawska
})
170 1e6fab60 Agata Murawska
ARGS_VBOX = dict(ARGS_EXPORT_DIR, **{
171 1e6fab60 Agata Murawska
  "output_dir": OUTPUT_DIR,
172 1e6fab60 Agata Murawska
  "name": "test-instance",
173 1e6fab60 Agata Murawska
  "os": "lenny-image",
174 1e6fab60 Agata Murawska
  "hypervisor": ("xen-pvm", {}),
175 1e6fab60 Agata Murawska
  "osparams":{},
176 1e6fab60 Agata Murawska
  "disks": [],
177 1e6fab60 Agata Murawska
})
178 1e6fab60 Agata Murawska
ARGS_COMPLETE = dict(ARGS_VBOX, **{
179 1e6fab60 Agata Murawska
  "beparams": {"vcpus":"1", "memory":"256", "auto_balance": False},
180 1e6fab60 Agata Murawska
  "disks": [(0,{"size":"5mb"})],
181 1e6fab60 Agata Murawska
  "nics": [("0",{"mode":"bridged"})],
182 1e6fab60 Agata Murawska
  "disk_template": "plain",
183 1e6fab60 Agata Murawska
  "tags": "test-tag-1,test-tag-2",
184 1e6fab60 Agata Murawska
})
185 1e6fab60 Agata Murawska
ARGS_BROKEN = dict(ARGS_EXPORT_DIR , **{
186 1e6fab60 Agata Murawska
  "no_nics": True,
187 1e6fab60 Agata Murawska
  "disk_template": "diskless",
188 1e6fab60 Agata Murawska
  "name": "test-instance",
189 1e6fab60 Agata Murawska
  "os": "lenny-image",
190 1e6fab60 Agata Murawska
  "osparams": {},
191 1e6fab60 Agata Murawska
})
192 1e6fab60 Agata Murawska
193 d92518d3 Agata Murawska
EXP_ARGS_COMPRESSED = dict(ARGS_EXPORT_DIR, **{
194 d92518d3 Agata Murawska
  "compression": True,
195 d92518d3 Agata Murawska
})
196 d92518d3 Agata Murawska
197 d92518d3 Agata Murawska
EXP_DISKS_LIST = [
198 d92518d3 Agata Murawska
  {
199 d92518d3 Agata Murawska
    "format": "vmdk",
200 d92518d3 Agata Murawska
    "compression": "gzip",
201 d92518d3 Agata Murawska
    "virt-size": 90000,
202 d92518d3 Agata Murawska
    "real-size": 203,
203 d92518d3 Agata Murawska
    "path": "new_disk.cow.gz",
204 d92518d3 Agata Murawska
  },
205 d92518d3 Agata Murawska
  {
206 d92518d3 Agata Murawska
    "format": "cow",
207 d92518d3 Agata Murawska
    "virt-size": 15,
208 d92518d3 Agata Murawska
    "real-size": 15,
209 d92518d3 Agata Murawska
    "path": "new_disk.cow",
210 d92518d3 Agata Murawska
  },
211 d92518d3 Agata Murawska
]
212 d92518d3 Agata Murawska
EXP_NETWORKS_LIST = [
213 6e8091f9 Dimitris Aragiorgis
  {"mac": "aa:00:00:d8:2c:1e", "ip":"None", "link":"br0",
214 6e8091f9 Dimitris Aragiorgis
   "mode":"routed", "network": "test"},
215 d92518d3 Agata Murawska
]
216 d92518d3 Agata Murawska
EXP_PARTIAL_GANETI_DICT = {
217 d92518d3 Agata Murawska
  "hypervisor": {"name": "xen-kvm"},
218 d92518d3 Agata Murawska
  "os": {"name": "lenny-image"},
219 d92518d3 Agata Murawska
  "auto_balance": "True",
220 d92518d3 Agata Murawska
  "version": "0",
221 d92518d3 Agata Murawska
}
222 d92518d3 Agata Murawska
EXP_GANETI_DICT = {
223 8c114acd Michael Hanselmann
  "tags": None,
224 8c114acd Michael Hanselmann
  "auto_balance": "False",
225 8c114acd Michael Hanselmann
  "hypervisor": {
226 8c114acd Michael Hanselmann
     "root-path": "/dev/sda",
227 8c114acd Michael Hanselmann
     "name": "xen-pvm",
228 8c114acd Michael Hanselmann
     "kernel_args": "ro"
229 d92518d3 Agata Murawska
   },
230 8c114acd Michael Hanselmann
  "version": "0",
231 8c114acd Michael Hanselmann
  "disk_template": None,
232 8c114acd Michael Hanselmann
  "os": {"name": "lenny-image"}
233 d92518d3 Agata Murawska
}
234 d92518d3 Agata Murawska
EXP_NAME ="xen-dev-i1"
235 d92518d3 Agata Murawska
EXP_VCPUS = 1
236 d92518d3 Agata Murawska
EXP_MEMORY = 512
237 d92518d3 Agata Murawska
238 d92518d3 Agata Murawska
EXPORT_EMPTY = ("<Envelope xml:lang=\"en-US\" xmlns=\"http://schemas.dmtf.org/"
239 d92518d3 Agata Murawska
                "ovf/envelope/1\" xmlns:gnt=\"http://ganeti\" xmlns:ovf=\""
240 d92518d3 Agata Murawska
                "http://schemas.dmtf.org/ovf/envelope/1\" xmlns:rasd=\""
241 d92518d3 Agata Murawska
                "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_Resource"
242 d92518d3 Agata Murawska
                "AllocationSettingData\" xmlns:vssd=\"http://schemas.dmtf.org"
243 d92518d3 Agata Murawska
                "/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData\""
244 d92518d3 Agata Murawska
                " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" />")
245 fa337742 Agata Murawska
EXPORT_DISKS_EMPTY = ("<References /><DiskSection><Info>Virtual disk"
246 fa337742 Agata Murawska
                      " information</Info></DiskSection>")
247 d92518d3 Agata Murawska
EXPORT_DISKS = ("<References><File ovf:compression=\"gzip\" ovf:href=\"new_disk"
248 d92518d3 Agata Murawska
                ".cow.gz\" ovf:id=\"file0\" ovf:size=\"203\" /><File ovf:href="
249 d92518d3 Agata Murawska
                "\"new_disk.cow\" ovf:id=\"file1\" ovf:size=\"15\" />"
250 fa337742 Agata Murawska
                "</References><DiskSection><Info>Virtual disk information"
251 fa337742 Agata Murawska
                "</Info><Disk ovf:capacity=\"90000\" ovf:diskId=\"disk0\" ovf"
252 fa337742 Agata Murawska
                ":fileRef=\"file0\" ovf:format=\"http://www.vmware.com/"
253 fa337742 Agata Murawska
                "interfaces/specifications/vmdk.html#monolithicSparse\" /><Disk"
254 fa337742 Agata Murawska
                " ovf:capacity=\"15\" ovf:diskId=\"disk1\" ovf:fileRef"
255 fa337742 Agata Murawska
                "=\"file1\" ovf:format=\"http://www.gnome.org/~markmc/qcow"
256 fa337742 Agata Murawska
                "-image-format.html\" /></DiskSection>")
257 fa337742 Agata Murawska
EXPORT_NETWORKS_EMPTY = ("<NetworkSection><Info>List of logical networks</Info>"
258 fa337742 Agata Murawska
                         "</NetworkSection>")
259 fa337742 Agata Murawska
EXPORT_NETWORKS = ("<NetworkSection><Info>List of logical networks</Info>"
260 fa337742 Agata Murawska
                   "<Network ovf:name=\"routed0\" /></NetworkSection>")
261 d92518d3 Agata Murawska
EXPORT_GANETI_INCOMPLETE = ("<gnt:GanetiSection><gnt:Version>0</gnt:Version>"
262 d92518d3 Agata Murawska
                            "<gnt:AutoBalance>True</gnt:AutoBalance><gnt:"
263 d92518d3 Agata Murawska
                            "OperatingSystem><gnt:Name>lenny-image</gnt:Name>"
264 d92518d3 Agata Murawska
                            "<gnt:Parameters /></gnt:OperatingSystem><gnt:"
265 d92518d3 Agata Murawska
                            "Hypervisor><gnt:Name>xen-kvm</gnt:Name><gnt:"
266 d92518d3 Agata Murawska
                            "Parameters /></gnt:Hypervisor><gnt:Network><gnt:"
267 d92518d3 Agata Murawska
                            "Nic ovf:name=\"routed0\"><gnt:Mode>routed</gnt:"
268 d92518d3 Agata Murawska
                            "Mode><gnt:MACAddress>aa:00:00:d8:2c:1e</gnt:"
269 d92518d3 Agata Murawska
                            "MACAddress><gnt:IPAddress>None</gnt:IPAddress>"
270 6e8091f9 Dimitris Aragiorgis
                            "<gnt:Link>br0</gnt:Link><gnt:Net>test</gnt:Net>"
271 6e8091f9 Dimitris Aragiorgis
                            "</gnt:Nic></gnt:Network></gnt:GanetiSection>")
272 d92518d3 Agata Murawska
EXPORT_GANETI = ("<gnt:GanetiSection><gnt:Version>0</gnt:Version><gnt:"
273 d92518d3 Agata Murawska
                 "AutoBalance>False</gnt:AutoBalance><gnt:OperatingSystem>"
274 d92518d3 Agata Murawska
                 "<gnt:Name>lenny-image</gnt:Name><gnt:Parameters /></gnt:"
275 d92518d3 Agata Murawska
                 "OperatingSystem><gnt:Hypervisor><gnt:Name>xen-pvm</gnt:Name>"
276 d92518d3 Agata Murawska
                 "<gnt:Parameters><gnt:root-path>/dev/sda</gnt:root-path><gnt:"
277 d92518d3 Agata Murawska
                 "kernel_args>ro</gnt:kernel_args></gnt:Parameters></gnt:"
278 d92518d3 Agata Murawska
                 "Hypervisor><gnt:Network><gnt:Nic ovf:name=\"routed0\"><gnt:"
279 d92518d3 Agata Murawska
                 "Mode>routed</gnt:Mode><gnt:MACAddress>aa:00:00:d8:2c:1e</gnt:"
280 d92518d3 Agata Murawska
                 "MACAddress><gnt:IPAddress>None</gnt:IPAddress><gnt:Link>br0"
281 6e8091f9 Dimitris Aragiorgis
                 "</gnt:Link><gnt:Net>test</gnt:Net></gnt:Nic></gnt:Network>"
282 6e8091f9 Dimitris Aragiorgis
                 "</gnt:GanetiSection>")
283 d92518d3 Agata Murawska
EXPORT_SYSTEM = ("<References><File ovf:compression=\"gzip\" ovf:href=\"new_"
284 d92518d3 Agata Murawska
                 "disk.cow.gz\" ovf:id=\"file0\" ovf:size=\"203\" /><File ovf:"
285 d92518d3 Agata Murawska
                 "href=\"new_disk.cow\" ovf:id=\"file1\" ovf:size=\"15\" />"
286 fa337742 Agata Murawska
                 "</References><DiskSection><Info>Virtual disk information"
287 fa337742 Agata Murawska
                 "</Info><Disk ovf:capacity=\"90000\" ovf:diskId=\"disk0\""
288 fa337742 Agata Murawska
                 " ovf:fileRef=\"file0\" ovf:format=\"http://www.vmware.com"
289 fa337742 Agata Murawska
                 "/interfaces/specifications/vmdk.html#monolithicSparse\" />"
290 fa337742 Agata Murawska
                 "<Disk ovf:capacity=\"15\" ovf:diskId=\"disk1\" ovf:fileRef"
291 fa337742 Agata Murawska
                 "=\"file1\" ovf:format=\"http://www.gnome.org/~markmc/qcow"
292 fa337742 Agata Murawska
                 "-image-format.html\" /></DiskSection><NetworkSection><Info>"
293 fa337742 Agata Murawska
                 "List of logical networks</Info><Network ovf:name=\"routed0\""
294 fa337742 Agata Murawska
                 " /></NetworkSection><VirtualSystem ovf:id=\"xen-dev-i1\">"
295 fa337742 Agata Murawska
                 "<Info>A virtual machine</Info><Name>xen-dev-i1</Name>"
296 fa337742 Agata Murawska
                 "<OperatingSystemSection ovf:id=\"0\"><Info>Installed guest"
297 fa337742 Agata Murawska
                 " operating system</Info></OperatingSystemSection><Virtual"
298 fa337742 Agata Murawska
                 "HardwareSection><Info>Virtual hardware requirements</Info>"
299 fa337742 Agata Murawska
                 "<System><vssd:ElementName>Virtual Hardware Family"
300 fa337742 Agata Murawska
                 "</vssd:ElementName><vssd:InstanceID>0</vssd:InstanceID><vssd:"
301 d92518d3 Agata Murawska
                 "VirtualSystemIdentifier>xen-dev-i1</vssd:VirtualSystem"
302 d92518d3 Agata Murawska
                 "Identifier><vssd:VirtualSystemType>ganeti-ovf</vssd:Virtual"
303 d92518d3 Agata Murawska
                 "SystemType></System><Item><rasd:ElementName>1 virtual CPU(s)"
304 d92518d3 Agata Murawska
                 "</rasd:ElementName><rasd:InstanceID>1</rasd:InstanceID><rasd:"
305 d92518d3 Agata Murawska
                 "ResourceType>3</rasd:ResourceType><rasd:VirtualQuantity>1"
306 d92518d3 Agata Murawska
                 "</rasd:VirtualQuantity></Item><Item><rasd:AllocationUnits>"
307 d92518d3 Agata Murawska
                 "byte * 2^20</rasd:AllocationUnits><rasd:ElementName>512MB of"
308 d92518d3 Agata Murawska
                 " memory</rasd:ElementName><rasd:InstanceID>2</rasd:"
309 d92518d3 Agata Murawska
                 "InstanceID><rasd:ResourceType>4</rasd:ResourceType><rasd:"
310 d92518d3 Agata Murawska
                 "VirtualQuantity>512</rasd:VirtualQuantity></Item><Item>"
311 d92518d3 Agata Murawska
                 "<rasd:Address>0</rasd:Address><rasd:ElementName>scsi"
312 fa337742 Agata Murawska
                 "_controller0</rasd:ElementName><rasd:InstanceID>3"
313 fa337742 Agata Murawska
                 "</rasd:InstanceID><rasd:ResourceSubType>lsilogic</rasd"
314 fa337742 Agata Murawska
                 ":ResourceSubType><rasd:ResourceType>6</rasd:ResourceType>"
315 fa337742 Agata Murawska
                 "</Item><Item><rasd:ElementName>disk0</rasd:ElementName><rasd"
316 fa337742 Agata Murawska
                 ":HostResource>ovf:/disk/disk0</rasd:HostResource><rasd"
317 fa337742 Agata Murawska
                 ":InstanceID>4</rasd:InstanceID><rasd:Parent>3</rasd:Parent>"
318 fa337742 Agata Murawska
                 "<rasd:ResourceType>17</rasd:ResourceType></Item><Item><rasd:"
319 fa337742 Agata Murawska
                 "ElementName>disk1</rasd:ElementName><rasd:HostResource>ovf:/"
320 fa337742 Agata Murawska
                 "disk/disk1</rasd:HostResource><rasd:InstanceID>5</rasd"
321 fa337742 Agata Murawska
                 ":InstanceID><rasd:Parent>3</rasd:Parent><rasd:ResourceType>17"
322 fa337742 Agata Murawska
                 "</rasd:ResourceType></Item><Item><rasd:Address>aa:00"
323 fa337742 Agata Murawska
                 ":00:d8:2c:1e</rasd:Address><rasd:Connection>routed0</rasd"
324 fa337742 Agata Murawska
                 ":Connection><rasd:ElementName>routed0</rasd:ElementName><rasd"
325 fa337742 Agata Murawska
                 ":InstanceID>6</rasd:InstanceID><rasd:ResourceType>10</rasd"
326 fa337742 Agata Murawska
                 ":ResourceType></Item></VirtualHardwareSection>"
327 fa337742 Agata Murawska
                 "</VirtualSystem>")
328 d92518d3 Agata Murawska
329 1e6fab60 Agata Murawska
330 1e6fab60 Agata Murawska
def _GetArgs(args, with_name=False):
331 1e6fab60 Agata Murawska
  options = optparse.Values()
332 1e6fab60 Agata Murawska
  needed = args
333 1e6fab60 Agata Murawska
  if with_name:
334 1e6fab60 Agata Murawska
    needed["name"] = "test-instance"
335 1e6fab60 Agata Murawska
  options._update_loose(needed)
336 1e6fab60 Agata Murawska
  return options
337 1e6fab60 Agata Murawska
338 1e6fab60 Agata Murawska
339 1e6fab60 Agata Murawska
OPTS_EMPTY = _GetArgs(ARGS_EMPTY)
340 1e6fab60 Agata Murawska
OPTS_EXPORT_NO_NAME = _GetArgs(ARGS_EXPORT_DIR)
341 1e6fab60 Agata Murawska
OPTS_EXPORT = _GetArgs(ARGS_EXPORT_DIR, with_name=True)
342 d92518d3 Agata Murawska
343 d92518d3 Agata Murawska
EXP_OPTS = OPTS_EXPORT_NO_NAME
344 d92518d3 Agata Murawska
EXP_OPTS_COMPRESSED = _GetArgs(EXP_ARGS_COMPRESSED)
345 d92518d3 Agata Murawska
346 1e6fab60 Agata Murawska
OPTS_VBOX = _GetArgs(ARGS_VBOX)
347 1e6fab60 Agata Murawska
OPTS_COMPLETE = _GetArgs(ARGS_COMPLETE)
348 1e6fab60 Agata Murawska
OPTS_NONIC_NODISK = _GetArgs(ARGS_BROKEN)
349 1e6fab60 Agata Murawska
350 1e6fab60 Agata Murawska
351 1e6fab60 Agata Murawska
def _GetFullFilename(file_name):
352 1e6fab60 Agata Murawska
  file_path = "%s/test/data/ovfdata/%s" % (testutils.GetSourceDir(),
353 1e6fab60 Agata Murawska
    file_name)
354 1e6fab60 Agata Murawska
  file_path = os.path.abspath(file_path)
355 1e6fab60 Agata Murawska
  return file_path
356 1e6fab60 Agata Murawska
357 1e6fab60 Agata Murawska
358 1e6fab60 Agata Murawska
class BetterUnitTest(unittest.TestCase):
359 1e6fab60 Agata Murawska
  def assertRaisesRegexp(self, exception, regexp_val, function, *args):
360 1e6fab60 Agata Murawska
    try:
361 1e6fab60 Agata Murawska
      function(*args)
362 1e6fab60 Agata Murawska
      self.fail("Expected raising %s" % exception)
363 1e6fab60 Agata Murawska
    except exception, err:
364 1e6fab60 Agata Murawska
      regexp = re.compile(regexp_val)
365 1e6fab60 Agata Murawska
      if re.search(regexp, str(err)) == None:
366 1e6fab60 Agata Murawska
        self.fail("Expected matching '%s', got '%s'" %
367 1e6fab60 Agata Murawska
          (regexp_val, str(err)))
368 1e6fab60 Agata Murawska
369 1e6fab60 Agata Murawska
370 1e6fab60 Agata Murawska
class TestOVFImporter(BetterUnitTest):
371 1e6fab60 Agata Murawska
  def setUp(self):
372 1e6fab60 Agata Murawska
    self.non_existing_file = _GetFullFilename("not_the_file.ovf")
373 1e6fab60 Agata Murawska
    self.ganeti_ovf = _GetFullFilename("ganeti.ovf")
374 1e6fab60 Agata Murawska
    self.virtualbox_ovf = _GetFullFilename("virtualbox.ovf")
375 1e6fab60 Agata Murawska
    self.ova_package = _GetFullFilename("ova.ova")
376 1e6fab60 Agata Murawska
    self.empty_ovf = _GetFullFilename("empty.ovf")
377 1e6fab60 Agata Murawska
    self.wrong_extension = _GetFullFilename("wrong_extension.ovd")
378 1e6fab60 Agata Murawska
    self.wrong_ova_archive = _GetFullFilename("wrong_ova.ova")
379 1e6fab60 Agata Murawska
    self.no_ovf_in_ova = _GetFullFilename("no_ovf.ova")
380 1e6fab60 Agata Murawska
    self.importer = None
381 1e6fab60 Agata Murawska
382 1e6fab60 Agata Murawska
  def tearDown(self):
383 1e6fab60 Agata Murawska
    if self.importer:
384 1e6fab60 Agata Murawska
      self.importer.Cleanup()
385 1e6fab60 Agata Murawska
    del_dir = os.path.abspath(OUTPUT_DIR)
386 1e6fab60 Agata Murawska
    try:
387 1e6fab60 Agata Murawska
      shutil.rmtree(del_dir)
388 1e6fab60 Agata Murawska
    except OSError:
389 1e6fab60 Agata Murawska
      pass
390 1e6fab60 Agata Murawska
391 1e6fab60 Agata Murawska
  def testFileDoesNotExistError(self):
392 1e6fab60 Agata Murawska
    self.assertRaisesRegexp(errors.OpPrereqError, "does not exist",
393 1e6fab60 Agata Murawska
      ovf.OVFImporter, self.non_existing_file, None)
394 1e6fab60 Agata Murawska
395 1e6fab60 Agata Murawska
  def testWrongInputFileExtensionError(self):
396 1e6fab60 Agata Murawska
    self.assertRaisesRegexp(errors.OpPrereqError,
397 1e6fab60 Agata Murawska
      "Unknown file extension", ovf.OVFImporter,
398 1e6fab60 Agata Murawska
      self.wrong_extension, None)
399 1e6fab60 Agata Murawska
400 1e6fab60 Agata Murawska
  def testOVAUnpackingDirectories(self):
401 1e6fab60 Agata Murawska
    self.importer = ovf.OVFImporter(self.ova_package, OPTS_EMPTY)
402 1e6fab60 Agata Murawska
    self.assertTrue(self.importer.input_dir != None)
403 a56625a2 Michael Hanselmann
    self.assertEquals(self.importer.output_dir , pathutils.EXPORT_DIR)
404 1e6fab60 Agata Murawska
    self.assertTrue(self.importer.temp_dir != None)
405 1e6fab60 Agata Murawska
406 1e6fab60 Agata Murawska
  def testOVFUnpackingDirectories(self):
407 1e6fab60 Agata Murawska
    self.importer = ovf.OVFImporter(self.virtualbox_ovf,
408 1e6fab60 Agata Murawska
      OPTS_EMPTY)
409 1e6fab60 Agata Murawska
    self.assertEquals(self.importer.input_dir , _GetFullFilename(""))
410 a56625a2 Michael Hanselmann
    self.assertEquals(self.importer.output_dir , pathutils.EXPORT_DIR)
411 1e6fab60 Agata Murawska
    self.assertEquals(self.importer.temp_dir , None)
412 1e6fab60 Agata Murawska
413 1e6fab60 Agata Murawska
  def testOVFSetOutputDirDirectories(self):
414 1e6fab60 Agata Murawska
    self.importer = ovf.OVFImporter(self.ganeti_ovf, OPTS_EXPORT)
415 1e6fab60 Agata Murawska
    self.assertEquals(self.importer.input_dir , _GetFullFilename(""))
416 1e6fab60 Agata Murawska
    self.assertTrue(OUTPUT_DIR in self.importer.output_dir)
417 1e6fab60 Agata Murawska
    self.assertEquals(self.importer.temp_dir , None)
418 1e6fab60 Agata Murawska
419 1e6fab60 Agata Murawska
  def testWrongOVAArchiveError(self):
420 1e6fab60 Agata Murawska
    self.assertRaisesRegexp(errors.OpPrereqError, "not a proper tar",
421 1e6fab60 Agata Murawska
      ovf.OVFImporter, self.wrong_ova_archive, None)
422 1e6fab60 Agata Murawska
423 1e6fab60 Agata Murawska
  def testNoOVFFileInOVAPackageError(self):
424 1e6fab60 Agata Murawska
    self.assertRaisesRegexp(errors.OpPrereqError, "No .ovf file",
425 1e6fab60 Agata Murawska
      ovf.OVFImporter, self.no_ovf_in_ova, None)
426 1e6fab60 Agata Murawska
427 1e6fab60 Agata Murawska
  def testParseGanetiOvf(self):
428 1e6fab60 Agata Murawska
    self.importer = ovf.OVFImporter(self.ganeti_ovf, OPTS_EXPORT_NO_NAME)
429 1e6fab60 Agata Murawska
    self.importer.Parse()
430 1e6fab60 Agata Murawska
    self.assertTrue("%s/ganeti-test-xen" % OUTPUT_DIR in
431 1e6fab60 Agata Murawska
      self.importer.output_dir)
432 1e6fab60 Agata Murawska
    self.assertEqual(self.importer.results_disk, GANETI_DISKS)
433 1e6fab60 Agata Murawska
    self.assertEqual(self.importer.results_network, GANETI_NETWORKS)
434 1e6fab60 Agata Murawska
    self.assertEqual(self.importer.results_hypervisor, GANETI_HYPERVISOR)
435 1e6fab60 Agata Murawska
    self.assertEqual(self.importer.results_os, GANETI_OS)
436 1e6fab60 Agata Murawska
    self.assertEqual(self.importer.results_backend, GANETI_BACKEND)
437 1e6fab60 Agata Murawska
    self.assertEqual(self.importer.results_name, GANETI_NAME)
438 1e6fab60 Agata Murawska
    self.assertEqual(self.importer.results_template, GANETI_TEMPLATE)
439 1e6fab60 Agata Murawska
    self.assertEqual(self.importer.results_tags, GANETI_TAGS)
440 1e6fab60 Agata Murawska
    self.assertEqual(self.importer.results_version, GANETI_VERSION)
441 1e6fab60 Agata Murawska
442 1e6fab60 Agata Murawska
  def testParseVirtualboxOvf(self):
443 1e6fab60 Agata Murawska
    self.importer = ovf.OVFImporter(self.virtualbox_ovf, OPTS_VBOX)
444 1e6fab60 Agata Murawska
    self.importer.Parse()
445 1e6fab60 Agata Murawska
    self.assertTrue("%s/test-instance" % OUTPUT_DIR in self.importer.output_dir)
446 1e6fab60 Agata Murawska
    self.assertEquals(self.importer.results_disk, VIRTUALBOX_DISKS)
447 1e6fab60 Agata Murawska
    self.assertEquals(self.importer.results_network, VIRTUALBOX_NETWORKS)
448 1e6fab60 Agata Murawska
    self.assertEquals(self.importer.results_hypervisor, CMDARGS_HYPERVISOR)
449 1e6fab60 Agata Murawska
    self.assertEquals(self.importer.results_os, CMDARGS_OS)
450 1e6fab60 Agata Murawska
    self.assertEquals(self.importer.results_backend, VIRTUALBOX_BACKEND)
451 1e6fab60 Agata Murawska
    self.assertEquals(self.importer.results_name, CMDARGS_NAME)
452 1e6fab60 Agata Murawska
    self.assertEquals(self.importer.results_template, VIRTUALBOX_TEMPLATE)
453 1e6fab60 Agata Murawska
    self.assertEqual(self.importer.results_tags, VIRTUALBOX_TAGS)
454 1e6fab60 Agata Murawska
    self.assertEqual(self.importer.results_version, constants.EXPORT_VERSION)
455 1e6fab60 Agata Murawska
456 1e6fab60 Agata Murawska
  def testParseEmptyOvf(self):
457 1e6fab60 Agata Murawska
    self.importer = ovf.OVFImporter(self.empty_ovf, OPTS_COMPLETE)
458 1e6fab60 Agata Murawska
    self.importer.Parse()
459 1e6fab60 Agata Murawska
    self.assertTrue("%s/test-instance" % OUTPUT_DIR in self.importer.output_dir)
460 1e6fab60 Agata Murawska
    self.assertEquals(self.importer.results_disk, CMDARGS_DISKS)
461 1e6fab60 Agata Murawska
    self.assertEquals(self.importer.results_network, CMDARGS_NETWORKS)
462 1e6fab60 Agata Murawska
    self.assertEquals(self.importer.results_hypervisor, CMDARGS_HYPERVISOR)
463 1e6fab60 Agata Murawska
    self.assertEquals(self.importer.results_os, CMDARGS_OS)
464 1e6fab60 Agata Murawska
    self.assertEquals(self.importer.results_backend, CMDARGS_BACKEND)
465 1e6fab60 Agata Murawska
    self.assertEquals(self.importer.results_name, CMDARGS_NAME)
466 1e6fab60 Agata Murawska
    self.assertEquals(self.importer.results_template, CMDARGS_TEMPLATE)
467 1e6fab60 Agata Murawska
    self.assertEqual(self.importer.results_tags, CMDARGS_TAGS)
468 1e6fab60 Agata Murawska
    self.assertEqual(self.importer.results_version, constants.EXPORT_VERSION)
469 1e6fab60 Agata Murawska
470 1e6fab60 Agata Murawska
  def testParseNameOptions(self):
471 1e6fab60 Agata Murawska
    self.importer = ovf.OVFImporter(self.empty_ovf, OPTS_COMPLETE)
472 1e6fab60 Agata Murawska
    results = self.importer._ParseNameOptions()
473 1e6fab60 Agata Murawska
    self.assertEquals(results, CMDARGS_NAME)
474 1e6fab60 Agata Murawska
475 1e6fab60 Agata Murawska
  def testParseHypervisorOptions(self):
476 1e6fab60 Agata Murawska
    self.importer = ovf.OVFImporter(self.empty_ovf, OPTS_COMPLETE)
477 1e6fab60 Agata Murawska
    results = self.importer._ParseHypervisorOptions()
478 1e6fab60 Agata Murawska
    self.assertEquals(results, CMDARGS_HYPERVISOR)
479 1e6fab60 Agata Murawska
480 1e6fab60 Agata Murawska
  def testParseOSOptions(self):
481 1e6fab60 Agata Murawska
    self.importer = ovf.OVFImporter(self.empty_ovf, OPTS_COMPLETE)
482 1e6fab60 Agata Murawska
    results = self.importer._ParseOSOptions()
483 1e6fab60 Agata Murawska
    self.assertEquals(results, CMDARGS_OS)
484 1e6fab60 Agata Murawska
485 1e6fab60 Agata Murawska
  def testParseBackendOptions(self):
486 1e6fab60 Agata Murawska
    self.importer = ovf.OVFImporter(self.empty_ovf, OPTS_COMPLETE)
487 1e6fab60 Agata Murawska
    results = self.importer._ParseBackendOptions()
488 1e6fab60 Agata Murawska
    self.assertEquals(results, CMDARGS_BACKEND)
489 1e6fab60 Agata Murawska
490 1e6fab60 Agata Murawska
  def testParseTags(self):
491 1e6fab60 Agata Murawska
    self.importer = ovf.OVFImporter(self.empty_ovf, OPTS_COMPLETE)
492 1e6fab60 Agata Murawska
    results = self.importer._ParseTags()
493 1e6fab60 Agata Murawska
    self.assertEquals(results, CMDARGS_TAGS)
494 1e6fab60 Agata Murawska
495 1e6fab60 Agata Murawska
  def testParseNicOptions(self):
496 1e6fab60 Agata Murawska
    self.importer = ovf.OVFImporter(self.empty_ovf, OPTS_COMPLETE)
497 1e6fab60 Agata Murawska
    results = self.importer._ParseNicOptions()
498 1e6fab60 Agata Murawska
    self.assertEquals(results, CMDARGS_NETWORKS)
499 1e6fab60 Agata Murawska
500 1e6fab60 Agata Murawska
  def testParseDiskOptionsFromGanetiOVF(self):
501 1e6fab60 Agata Murawska
    self.importer = ovf.OVFImporter(self.ganeti_ovf, OPTS_EXPORT)
502 1e6fab60 Agata Murawska
    os.mkdir(OUTPUT_DIR)
503 1e6fab60 Agata Murawska
    results = self.importer._GetDiskInfo()
504 1e6fab60 Agata Murawska
    self.assertEquals(results, GANETI_DISKS)
505 1e6fab60 Agata Murawska
506 1e6fab60 Agata Murawska
  def testParseTemplateOptions(self):
507 1e6fab60 Agata Murawska
    self.importer = ovf.OVFImporter(self.empty_ovf, OPTS_COMPLETE)
508 1e6fab60 Agata Murawska
    results = self.importer._ParseTemplateOptions()
509 1e6fab60 Agata Murawska
    self.assertEquals(results, GANETI_TEMPLATE)
510 1e6fab60 Agata Murawska
511 1e6fab60 Agata Murawska
  def testParseDiskOptionsFromCmdLine(self):
512 1e6fab60 Agata Murawska
    self.importer = ovf.OVFImporter(self.empty_ovf, OPTS_COMPLETE)
513 1e6fab60 Agata Murawska
    os.mkdir(OUTPUT_DIR)
514 1e6fab60 Agata Murawska
    results = self.importer._ParseDiskOptions()
515 1e6fab60 Agata Murawska
    self.assertEquals(results, CMDARGS_DISKS)
516 1e6fab60 Agata Murawska
517 1e6fab60 Agata Murawska
  def testGetDiskFormat(self):
518 1e6fab60 Agata Murawska
    self.importer = ovf.OVFImporter(self.ganeti_ovf, OPTS_EXPORT)
519 1e6fab60 Agata Murawska
    disks_list = self.importer.ovf_reader.GetDisksNames()
520 1e6fab60 Agata Murawska
    results = [self.importer._GetDiskQemuInfo("%s/%s" %
521 1e6fab60 Agata Murawska
      (self.importer.input_dir, path), "file format: (\S+)")
522 1e6fab60 Agata Murawska
      for (path, _) in disks_list]
523 1e6fab60 Agata Murawska
    self.assertEqual(results, ["vmdk"])
524 1e6fab60 Agata Murawska
525 1e6fab60 Agata Murawska
  def testNoInstanceNameOVF(self):
526 1e6fab60 Agata Murawska
    self.importer = ovf.OVFImporter(self.empty_ovf, OPTS_EXPORT_NO_NAME)
527 1e6fab60 Agata Murawska
    self.assertRaisesRegexp(errors.OpPrereqError, "Name of instance",
528 1e6fab60 Agata Murawska
      self.importer.Parse)
529 1e6fab60 Agata Murawska
530 1e6fab60 Agata Murawska
  def testErrorNoOSNameOVF(self):
531 1e6fab60 Agata Murawska
    self.importer = ovf.OVFImporter(self.virtualbox_ovf, OPTS_EXPORT)
532 1e6fab60 Agata Murawska
    self.assertRaisesRegexp(errors.OpPrereqError, "OS name",
533 1e6fab60 Agata Murawska
      self.importer.Parse)
534 1e6fab60 Agata Murawska
535 1e6fab60 Agata Murawska
  def testErrorNoDiskAndNoNetwork(self):
536 1e6fab60 Agata Murawska
    self.importer = ovf.OVFImporter(self.empty_ovf, OPTS_NONIC_NODISK)
537 1e6fab60 Agata Murawska
    self.assertRaisesRegexp(errors.OpPrereqError,
538 1e6fab60 Agata Murawska
      "Either disk specification or network"
539 1e6fab60 Agata Murawska
      " description", self.importer.Parse)
540 1e6fab60 Agata Murawska
541 1e6fab60 Agata Murawska
542 d92518d3 Agata Murawska
class TestOVFExporter(BetterUnitTest):
543 d92518d3 Agata Murawska
  def setUp(self):
544 d92518d3 Agata Murawska
    self.exporter = None
545 d92518d3 Agata Murawska
    self.wrong_config_file = _GetFullFilename("wrong_config.ini")
546 d92518d3 Agata Murawska
    self.unsafe_path_to_disk = _GetFullFilename("unsafe_path.ini")
547 d92518d3 Agata Murawska
    self.disk_image_not_exist = _GetFullFilename("no_disk.ini")
548 d92518d3 Agata Murawska
    self.empty_config = _GetFullFilename("empty.ini")
549 d92518d3 Agata Murawska
    self.standard_export = _GetFullFilename("config.ini")
550 d92518d3 Agata Murawska
    self.wrong_network_mode = self.disk_image_not_exist
551 d92518d3 Agata Murawska
    self.no_memory = self.disk_image_not_exist
552 d92518d3 Agata Murawska
    self.no_vcpus = self.disk_image_not_exist
553 d92518d3 Agata Murawska
    self.no_os = _GetFullFilename("no_os.ini")
554 d92518d3 Agata Murawska
    self.no_hypervisor = self.disk_image_not_exist
555 d92518d3 Agata Murawska
556 d92518d3 Agata Murawska
  def tearDown(self):
557 d92518d3 Agata Murawska
    if self.exporter:
558 d92518d3 Agata Murawska
      self.exporter.Cleanup()
559 d92518d3 Agata Murawska
    del_dir = os.path.abspath(OUTPUT_DIR)
560 d92518d3 Agata Murawska
    try:
561 d92518d3 Agata Murawska
      shutil.rmtree(del_dir)
562 d92518d3 Agata Murawska
    except OSError:
563 d92518d3 Agata Murawska
      pass
564 d92518d3 Agata Murawska
565 d92518d3 Agata Murawska
  def testErrorWrongConfigFile(self):
566 d92518d3 Agata Murawska
    self.assertRaisesRegexp(errors.OpPrereqError,
567 d92518d3 Agata Murawska
      "Error when trying to read", ovf.OVFExporter,
568 d92518d3 Agata Murawska
      self.wrong_config_file, EXP_OPTS)
569 d92518d3 Agata Murawska
570 d92518d3 Agata Murawska
  def testErrorPathToTheDiskIncorrect(self):
571 d92518d3 Agata Murawska
    self.exporter = ovf.OVFExporter(self.unsafe_path_to_disk, EXP_OPTS)
572 d92518d3 Agata Murawska
    self.assertRaisesRegexp(errors.OpPrereqError, "contains a directory name",
573 d92518d3 Agata Murawska
      self.exporter._ParseDisks)
574 d92518d3 Agata Murawska
575 d92518d3 Agata Murawska
  def testErrorDiskImageNotExist(self):
576 d92518d3 Agata Murawska
    self.exporter = ovf.OVFExporter(self.disk_image_not_exist, EXP_OPTS)
577 d92518d3 Agata Murawska
    self.assertRaisesRegexp(errors.OpPrereqError, "Disk image does not exist",
578 d92518d3 Agata Murawska
      self.exporter._ParseDisks)
579 d92518d3 Agata Murawska
580 d92518d3 Agata Murawska
  def testParseNetworks(self):
581 d92518d3 Agata Murawska
    self.exporter = ovf.OVFExporter(self.standard_export, EXP_OPTS)
582 d92518d3 Agata Murawska
    results = self.exporter._ParseNetworks()
583 d92518d3 Agata Murawska
    self.assertEqual(results, EXP_NETWORKS_LIST)
584 d92518d3 Agata Murawska
585 d92518d3 Agata Murawska
  def testErrorWrongNetworkMode(self):
586 d92518d3 Agata Murawska
    self.exporter = ovf.OVFExporter(self.wrong_network_mode, EXP_OPTS)
587 d92518d3 Agata Murawska
    self.assertRaisesRegexp(errors.OpPrereqError,
588 d92518d3 Agata Murawska
      "Network mode nic not recognized", self.exporter._ParseNetworks)
589 d92518d3 Agata Murawska
590 d92518d3 Agata Murawska
  def testParseVCPusMem(self):
591 d92518d3 Agata Murawska
    self.exporter = ovf.OVFExporter(self.standard_export, EXP_OPTS)
592 d92518d3 Agata Murawska
    vcpus = self.exporter._ParseVCPUs()
593 d92518d3 Agata Murawska
    memory = self.exporter._ParseMemory()
594 d92518d3 Agata Murawska
    self.assertEqual(vcpus, EXP_VCPUS)
595 d92518d3 Agata Murawska
    self.assertEqual(memory, EXP_MEMORY)
596 d92518d3 Agata Murawska
597 d92518d3 Agata Murawska
  def testErrorNoVCPUs(self):
598 d92518d3 Agata Murawska
    self.exporter = ovf.OVFExporter(self.no_vcpus, EXP_OPTS)
599 d92518d3 Agata Murawska
    self.assertRaisesRegexp(errors.OpPrereqError, "No CPU information found",
600 d92518d3 Agata Murawska
      self.exporter._ParseVCPUs)
601 d92518d3 Agata Murawska
602 d92518d3 Agata Murawska
  def testErrorNoMemory(self):
603 d92518d3 Agata Murawska
    self.exporter = ovf.OVFExporter(self.no_memory, EXP_OPTS)
604 d92518d3 Agata Murawska
    self.assertRaisesRegexp(errors.OpPrereqError, "No memory information found",
605 d92518d3 Agata Murawska
      self.exporter._ParseMemory)
606 d92518d3 Agata Murawska
607 d92518d3 Agata Murawska
  def testParseGaneti(self):
608 d92518d3 Agata Murawska
    self.exporter = ovf.OVFExporter(self.standard_export, EXP_OPTS)
609 d92518d3 Agata Murawska
    results = self.exporter._ParseGaneti()
610 d92518d3 Agata Murawska
    self.assertEqual(results, EXP_GANETI_DICT)
611 d92518d3 Agata Murawska
612 d92518d3 Agata Murawska
  def testErrorNoHypervisor(self):
613 d92518d3 Agata Murawska
    self.exporter = ovf.OVFExporter(self.no_hypervisor, EXP_OPTS)
614 d92518d3 Agata Murawska
    self.assertRaisesRegexp(errors.OpPrereqError,
615 d92518d3 Agata Murawska
      "No hypervisor information found", self.exporter._ParseGaneti)
616 d92518d3 Agata Murawska
617 d92518d3 Agata Murawska
  def testErrorNoOS(self):
618 d92518d3 Agata Murawska
    self.exporter = ovf.OVFExporter(self.no_os, EXP_OPTS)
619 d92518d3 Agata Murawska
    self.assertRaisesRegexp(errors.OpPrereqError,
620 d92518d3 Agata Murawska
      "No operating system information found", self.exporter._ParseGaneti)
621 d92518d3 Agata Murawska
622 d92518d3 Agata Murawska
  def testErrorParseNoInstanceName(self):
623 d92518d3 Agata Murawska
    self.exporter = ovf.OVFExporter(self.empty_config, EXP_OPTS)
624 d92518d3 Agata Murawska
    self.assertRaisesRegexp(errors.OpPrereqError, "No instance name found",
625 d92518d3 Agata Murawska
      self.exporter.Parse)
626 d92518d3 Agata Murawska
627 d92518d3 Agata Murawska
628 1e6fab60 Agata Murawska
class TestOVFReader(BetterUnitTest):
629 1e6fab60 Agata Murawska
  def setUp(self):
630 1e6fab60 Agata Murawska
    self.wrong_xml_file = _GetFullFilename("wrong_xml.ovf")
631 1e6fab60 Agata Murawska
    self.ganeti_ovf = _GetFullFilename("ganeti.ovf")
632 1e6fab60 Agata Murawska
    self.virtualbox_ovf = _GetFullFilename("virtualbox.ovf")
633 1e6fab60 Agata Murawska
    self.corrupted_ovf = _GetFullFilename("corrupted_resources.ovf")
634 1e6fab60 Agata Murawska
    self.wrong_manifest_ovf = _GetFullFilename("wrong_manifest.ovf")
635 1e6fab60 Agata Murawska
    self.no_disk_in_ref_ovf = _GetFullFilename("no_disk_in_ref.ovf")
636 1e6fab60 Agata Murawska
    self.empty_ovf = _GetFullFilename("empty.ovf")
637 1e6fab60 Agata Murawska
    self.compressed_disk = _GetFullFilename("gzip_disk.ovf")
638 1e6fab60 Agata Murawska
639 1e6fab60 Agata Murawska
  def tearDown(self):
640 1e6fab60 Agata Murawska
    pass
641 1e6fab60 Agata Murawska
642 1e6fab60 Agata Murawska
  def testXMLParsingError(self):
643 1e6fab60 Agata Murawska
    self.assertRaisesRegexp(errors.OpPrereqError,
644 1e6fab60 Agata Murawska
      "Error while reading .ovf", ovf.OVFReader, self.wrong_xml_file)
645 1e6fab60 Agata Murawska
646 1e6fab60 Agata Murawska
  def testFileInResourcesDoesNotExistError(self):
647 1e6fab60 Agata Murawska
    self.assertRaisesRegexp(errors.OpPrereqError, "does not exist",
648 1e6fab60 Agata Murawska
      ovf.OVFReader, self.corrupted_ovf)
649 1e6fab60 Agata Murawska
650 1e6fab60 Agata Murawska
  def testWrongManifestChecksumError(self):
651 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.wrong_manifest_ovf)
652 1e6fab60 Agata Murawska
    self.assertRaisesRegexp(errors.OpPrereqError,
653 1e6fab60 Agata Murawska
      "does not match the value in manifest file", reader.VerifyManifest)
654 1e6fab60 Agata Murawska
655 1e6fab60 Agata Murawska
  def testGoodManifestChecksum(self):
656 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.ganeti_ovf)
657 1e6fab60 Agata Murawska
    self.assertEqual(reader.VerifyManifest(), None)
658 1e6fab60 Agata Murawska
659 1e6fab60 Agata Murawska
  def testGetDisksNamesOVFCorruptedError(self):
660 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.no_disk_in_ref_ovf)
661 1e6fab60 Agata Murawska
    self.assertRaisesRegexp(errors.OpPrereqError,
662 1e6fab60 Agata Murawska
      "not found in references", reader.GetDisksNames)
663 1e6fab60 Agata Murawska
664 1e6fab60 Agata Murawska
  def testGetDisksNamesVirtualbox(self):
665 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.virtualbox_ovf)
666 1e6fab60 Agata Murawska
    disk_names = reader.GetDisksNames()
667 1e6fab60 Agata Murawska
    expected_names = [
668 1e6fab60 Agata Murawska
      ("new_disk.vmdk", None) ,
669 1e6fab60 Agata Murawska
      ("second_disk.vmdk", None),
670 1e6fab60 Agata Murawska
    ]
671 1e6fab60 Agata Murawska
    self.assertEqual(sorted(disk_names), sorted(expected_names))
672 1e6fab60 Agata Murawska
673 1e6fab60 Agata Murawska
  def testGetDisksNamesEmpty(self):
674 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.empty_ovf)
675 1e6fab60 Agata Murawska
    disk_names = reader.GetDisksNames()
676 1e6fab60 Agata Murawska
    self.assertEqual(disk_names, [])
677 1e6fab60 Agata Murawska
678 1e6fab60 Agata Murawska
  def testGetDisksNamesCompressed(self):
679 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.compressed_disk)
680 1e6fab60 Agata Murawska
    disk_names = reader.GetDisksNames()
681 1e6fab60 Agata Murawska
    self.assertEqual(disk_names, [("compr_disk.vmdk.gz", "gzip")])
682 1e6fab60 Agata Murawska
683 1e6fab60 Agata Murawska
  def testGetNetworkDataGaneti(self):
684 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.ganeti_ovf)
685 1e6fab60 Agata Murawska
    networks = reader.GetNetworkData()
686 1e6fab60 Agata Murawska
    self.assertEqual(networks, GANETI_NETWORKS)
687 1e6fab60 Agata Murawska
688 1e6fab60 Agata Murawska
  def testGetNetworkDataVirtualbox(self):
689 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.virtualbox_ovf)
690 1e6fab60 Agata Murawska
    networks = reader.GetNetworkData()
691 1e6fab60 Agata Murawska
    self.assertEqual(networks, VIRTUALBOX_NETWORKS)
692 1e6fab60 Agata Murawska
693 1e6fab60 Agata Murawska
  def testGetNetworkDataEmpty(self):
694 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.empty_ovf)
695 1e6fab60 Agata Murawska
    networks = reader.GetNetworkData()
696 1e6fab60 Agata Murawska
    self.assertEqual(networks, EMPTY_NETWORKS)
697 1e6fab60 Agata Murawska
698 1e6fab60 Agata Murawska
  def testGetHypervisorDataGaneti(self):
699 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.ganeti_ovf)
700 1e6fab60 Agata Murawska
    hypervisor = reader.GetHypervisorData()
701 1e6fab60 Agata Murawska
    self.assertEqual(hypervisor, GANETI_HYPERVISOR)
702 1e6fab60 Agata Murawska
703 1e6fab60 Agata Murawska
  def testGetHypervisorDataEmptyOvf(self):
704 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.empty_ovf)
705 1e6fab60 Agata Murawska
    hypervisor = reader.GetHypervisorData()
706 1e6fab60 Agata Murawska
    self.assertEqual(hypervisor, EMPTY_HYPERVISOR)
707 1e6fab60 Agata Murawska
708 1e6fab60 Agata Murawska
  def testGetOSDataGaneti(self):
709 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.ganeti_ovf)
710 1e6fab60 Agata Murawska
    osys = reader.GetOSData()
711 1e6fab60 Agata Murawska
    self.assertEqual(osys, GANETI_OS)
712 1e6fab60 Agata Murawska
713 1e6fab60 Agata Murawska
  def testGetOSDataEmptyOvf(self):
714 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.empty_ovf)
715 1e6fab60 Agata Murawska
    osys = reader.GetOSData()
716 1e6fab60 Agata Murawska
    self.assertEqual(osys, EMPTY_OS)
717 1e6fab60 Agata Murawska
718 1e6fab60 Agata Murawska
  def testGetBackendDataGaneti(self):
719 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.ganeti_ovf)
720 1e6fab60 Agata Murawska
    backend = reader.GetBackendData()
721 1e6fab60 Agata Murawska
    self.assertEqual(backend, GANETI_BACKEND)
722 1e6fab60 Agata Murawska
723 1e6fab60 Agata Murawska
  def testGetBackendDataVirtualbox(self):
724 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.virtualbox_ovf)
725 1e6fab60 Agata Murawska
    backend = reader.GetBackendData()
726 1e6fab60 Agata Murawska
    self.assertEqual(backend, VIRTUALBOX_BACKEND)
727 1e6fab60 Agata Murawska
728 1e6fab60 Agata Murawska
  def testGetBackendDataEmptyOvf(self):
729 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.empty_ovf)
730 1e6fab60 Agata Murawska
    backend = reader.GetBackendData()
731 1e6fab60 Agata Murawska
    self.assertEqual(backend, EMPTY_BACKEND)
732 1e6fab60 Agata Murawska
733 1e6fab60 Agata Murawska
  def testGetInstanceNameGaneti(self):
734 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.ganeti_ovf)
735 1e6fab60 Agata Murawska
    name = reader.GetInstanceName()
736 1e6fab60 Agata Murawska
    self.assertEqual(name, GANETI_NAME)
737 1e6fab60 Agata Murawska
738 1e6fab60 Agata Murawska
  def testGetInstanceNameDataEmptyOvf(self):
739 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.empty_ovf)
740 1e6fab60 Agata Murawska
    name = reader.GetInstanceName()
741 1e6fab60 Agata Murawska
    self.assertEqual(name, EMPTY_NAME)
742 1e6fab60 Agata Murawska
743 1e6fab60 Agata Murawska
  def testGetDiskTemplateGaneti(self):
744 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.ganeti_ovf)
745 1e6fab60 Agata Murawska
    name = reader.GetDiskTemplate()
746 1e6fab60 Agata Murawska
    self.assertEqual(name, GANETI_TEMPLATE)
747 1e6fab60 Agata Murawska
748 1e6fab60 Agata Murawska
  def testGetDiskTemplateEmpty(self):
749 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.empty_ovf)
750 1e6fab60 Agata Murawska
    name = reader.GetDiskTemplate()
751 1e6fab60 Agata Murawska
    self.assertEqual(name, EMPTY_TEMPLATE)
752 1e6fab60 Agata Murawska
753 1e6fab60 Agata Murawska
  def testGetTagsGaneti(self):
754 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.ganeti_ovf)
755 1e6fab60 Agata Murawska
    tags = reader.GetTagsData()
756 1e6fab60 Agata Murawska
    self.assertEqual(tags, GANETI_TAGS)
757 1e6fab60 Agata Murawska
758 1e6fab60 Agata Murawska
  def testGetTagsEmpty(self):
759 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.empty_ovf)
760 1e6fab60 Agata Murawska
    tags = reader.GetTagsData()
761 1e6fab60 Agata Murawska
    self.assertEqual(tags, EMPTY_TAGS)
762 1e6fab60 Agata Murawska
763 1e6fab60 Agata Murawska
  def testGetVersionGaneti(self):
764 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.ganeti_ovf)
765 1e6fab60 Agata Murawska
    version = reader.GetVersionData()
766 1e6fab60 Agata Murawska
    self.assertEqual(version, GANETI_VERSION)
767 1e6fab60 Agata Murawska
768 1e6fab60 Agata Murawska
  def testGetVersionEmpty(self):
769 1e6fab60 Agata Murawska
    reader = ovf.OVFReader(self.empty_ovf)
770 1e6fab60 Agata Murawska
    version = reader.GetVersionData()
771 1e6fab60 Agata Murawska
    self.assertEqual(version, EMPTY_VERSION)
772 1e6fab60 Agata Murawska
773 1e6fab60 Agata Murawska
774 d92518d3 Agata Murawska
class TestOVFWriter(BetterUnitTest):
775 d92518d3 Agata Murawska
  def setUp(self):
776 d92518d3 Agata Murawska
    self.writer = ovf.OVFWriter(True)
777 d92518d3 Agata Murawska
778 d92518d3 Agata Murawska
  def tearDown(self):
779 d92518d3 Agata Murawska
    pass
780 d92518d3 Agata Murawska
781 d92518d3 Agata Murawska
  def testOVFWriterInit(self):
782 d92518d3 Agata Murawska
    result = ET.tostring(self.writer.tree)
783 d92518d3 Agata Murawska
    self.assertTrue(EXPORT_EMPTY in result)
784 d92518d3 Agata Murawska
785 d92518d3 Agata Murawska
  def testSaveDisksDataEmpty(self):
786 d92518d3 Agata Murawska
    self.writer.SaveDisksData([])
787 d92518d3 Agata Murawska
    result = ET.tostring(self.writer.tree)
788 d92518d3 Agata Murawska
    self.assertTrue(EXPORT_DISKS_EMPTY in result)
789 d92518d3 Agata Murawska
790 d92518d3 Agata Murawska
  def testSaveDisksData(self):
791 d92518d3 Agata Murawska
    self.writer.SaveDisksData(EXP_DISKS_LIST)
792 d92518d3 Agata Murawska
    result = ET.tostring(self.writer.tree)
793 d92518d3 Agata Murawska
    self.assertTrue(EXPORT_DISKS in result)
794 d92518d3 Agata Murawska
795 d92518d3 Agata Murawska
  def testSaveNetworkDataEmpty(self):
796 d92518d3 Agata Murawska
    self.writer.SaveNetworksData([])
797 d92518d3 Agata Murawska
    result = ET.tostring(self.writer.tree)
798 d92518d3 Agata Murawska
    self.assertTrue(EXPORT_NETWORKS_EMPTY in result)
799 d92518d3 Agata Murawska
800 d92518d3 Agata Murawska
  def testSaveNetworksData(self):
801 d92518d3 Agata Murawska
    self.writer.SaveNetworksData(EXP_NETWORKS_LIST)
802 d92518d3 Agata Murawska
    result = ET.tostring(self.writer.tree)
803 d92518d3 Agata Murawska
    self.assertTrue(EXPORT_NETWORKS in result)
804 d92518d3 Agata Murawska
805 d92518d3 Agata Murawska
  def testSaveGanetiDataIncomplete(self):
806 d92518d3 Agata Murawska
    self.writer.SaveGanetiData(EXP_PARTIAL_GANETI_DICT, EXP_NETWORKS_LIST)
807 d92518d3 Agata Murawska
    result = ET.tostring(self.writer.tree)
808 d92518d3 Agata Murawska
    self.assertTrue(EXPORT_GANETI_INCOMPLETE in result)
809 d92518d3 Agata Murawska
810 d92518d3 Agata Murawska
  def testSaveGanetiDataComplete(self):
811 d92518d3 Agata Murawska
    self.writer.SaveGanetiData(EXP_GANETI_DICT, EXP_NETWORKS_LIST)
812 d92518d3 Agata Murawska
    result = ET.tostring(self.writer.tree)
813 d92518d3 Agata Murawska
    self.assertTrue(EXPORT_GANETI in result)
814 d92518d3 Agata Murawska
815 d92518d3 Agata Murawska
  def testSaveVirtualSystem(self):
816 d92518d3 Agata Murawska
    self.writer.SaveDisksData(EXP_DISKS_LIST)
817 d92518d3 Agata Murawska
    self.writer.SaveNetworksData(EXP_NETWORKS_LIST)
818 d92518d3 Agata Murawska
    self.writer.SaveVirtualSystemData(EXP_NAME, EXP_VCPUS, EXP_MEMORY)
819 d92518d3 Agata Murawska
    result = ET.tostring(self.writer.tree)
820 d92518d3 Agata Murawska
    self.assertTrue(EXPORT_SYSTEM in result)
821 d92518d3 Agata Murawska
822 d92518d3 Agata Murawska
823 1e6fab60 Agata Murawska
if __name__ == "__main__":
824 1e6fab60 Agata Murawska
  testutils.GanetiTestProgram()