4
Script to test virtualization functionality
6
Copyright (C) 2013 Canonical Ltd.
9
Jeff Marcom <jeff.marcom@canonical.com>
11
This program is free software: you can redistribute it and/or modify
12
it under the terms of the GNU General Public License version 3,
13
as published by the Free Software Foundation.
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
GNU General Public License for more details.
20
You should have received a copy of the GNU General Public License
21
along with this program. If not, see <http://www.gnu.org/licenses/>.
25
from argparse import ArgumentParser
31
from subprocess import Popen, PIPE
38
class XENTest(object):
42
class KVMTest(object):
44
def __init__(self, image=None, timeout=500, debug_file="virt_debug"):
46
self.timeout = timeout
47
self.debug_file = debug_file
50
def download_image(cls):
52
Downloads Cloud image for same release as host machine
55
# Check Ubuntu release info. Example {quantal, precise}
56
release = lsb_release.get_lsb_information()["CODENAME"]
59
cloud_url = "http://cloud-images.ubuntu.com"
60
cloud_iso = release + "-server-cloudimg-i386-disk1.img"
61
image_url = "/".join((
62
cloud_url, release, "current", cloud_iso))
64
logging.debug("Downloading {}, from {}".format(cloud_iso, cloud_url))
68
resp = urllib.request.urlretrieve(image_url, cloud_iso)
69
except urllib.error.HTTPError as exception:
70
logging.error("Failed download {}: {}".format(image_url, exception))
73
if not os.path.isfile(cloud_iso):
78
def boot_image(self, data_disk):
80
Attempts to boot the newly created qcow image using
81
the config data defined in config.iso. Return instance
82
of currently running virtual machine
85
logging.debug("Attempting boot for:{}".format(data_disk))
87
# Set Arbitrary IP values
88
netrange = "10.0.0.0/8"
90
hostfwd = "tcp::2222-:22"
94
kvm -m {0} -net nic -net user,net={1},host={2},
95
hostfwd={3} -drive file={4},if=virtio -display none -nographic
101
data_disk).replace("\n", "").replace(" ", "")
103
logging.debug("Using params:{}".format(params))
105
# Default file location for log file is in checkbox output directory
106
checkbox_dir = os.getenv("CHECKBOX_DATA")
108
if checkbox_dir != None:
109
self.debug_file = os.path.join(checkbox_dir, self.debug_file)
111
# Open VM STDERR/STDOUT log file for writing
113
file = open(self.debug_file, 'w')
115
logging.error("Failed creating file:{}".format(self.debug_file))
118
# Start Virtual machine
119
self.process = Popen(params, stderr=file,
120
stdout=file, universal_newlines=True, shell=True)
124
# Create temp directory:
125
with tempfile.TemporaryDirectory("_kvm_test",
126
time.strftime("%b_%d_%Y_")) as temp_dir:
129
if self.image is None:
130
# Download cloud image
131
self.image = self.download_image()
133
if os.path.isfile(self.image):
135
# Boot Virtual Machine
136
instance = self.boot_image(self.image)
138
if instance is not False:
139
time.sleep(self.timeout)
140
# Check to be sure VM boot was successful
141
if "END SSH HOST KEY KEYS" \
142
in open(self.debug_file, 'r').read():
143
print("Booted successfully", file=sys.stderr)
146
print("KVM instance failed to boot", file=sys.stderr)
147
self.process.terminate()
149
print("Could not find: {}".format(self.image), file=sys.stderr)
155
print("Executing KVM Test", file=sys.stderr)
157
DEFAULT_CFG = "/etc/checkbox.d/virtualization.cfg"
161
config_file = DEFAULT_CFG
162
config = configparser.SafeConfigParser()
165
config.readfp(open(config_file))
166
timeout = config.get("KVM", "timeout")
167
image = config.get("KVM", "image")
169
logging.warn("No config file found")
170
except Exception as exception:
171
logging.warn(exception)
176
timeout = args.timeout
178
kvm_test = KVMTest(image, timeout)
179
result = kvm_test.start()
186
parser = ArgumentParser(description="Virtualization Test")
187
subparsers = parser.add_subparsers()
190
kvm_test_parser = subparsers.add_parser('kvm',
191
help=("Run kvm virtualization test"))
193
#xen_test_parser = subparsers.add_parser('xen',
194
# help=("Run xen virtualization test"))
197
kvm_test_parser.add_argument('-i', '--image',
198
type=str, default=None)
199
kvm_test_parser.add_argument('-t', '--timeout',
200
type=int, default=500)
201
kvm_test_parser.add_argument('--debug',
203
kvm_test_parser.set_defaults(func=test_kvm)
205
args = parser.parse_args()
208
logging.basicConfig(level=logging.DEBUG)
215
if __name__ == "__main__":