summaryrefslogtreecommitdiff
path: root/bin/virtualization
diff options
authorJeff Lane <jeffrey.lane@canonical.com>2017-03-28 15:00:50 -0400
committerSylvain Pineau <sylvain.pineau@canonical.com>2017-04-03 09:39:22 +0200
commitbafb9ca4236e0faeb3a3f5a59626396863043eb3 (patch)
tree0d33c7b913dcd6b2f252904a4a7232a3a8d171ca /bin/virtualization
parent9b211a8de70e3d3661c0b473a4efbfded235105b (diff)
More refinements to LXD tests LP: #1677003
Diffstat (limited to 'bin/virtualization')
-rwxr-xr-xbin/virtualization75
1 files changed, 54 insertions, 21 deletions
diff --git a/bin/virtualization b/bin/virtualization
index 289b1d0..f06839a 100755
--- a/bin/virtualization
+++ b/bin/virtualization
@@ -30,6 +30,7 @@ import os
import re
import logging
import lsb_release
+import platform
import requests
import shlex
import signal
@@ -49,6 +50,7 @@ import tarfile
import time
import urllib.request
from urllib.parse import urlparse
+from uuid import uuid4
DEFAULT_TIMEOUT = 500
@@ -550,7 +552,9 @@ class LXDTest(object):
self.rootfs_tarball = None
self.template_tarball = None
self.name = 'testbed'
- self.image_alias = 'ubuntu'
+ self.image_alias = uuid4().hex
+ self.default_remote = "ubuntu:"
+ self.os_version = platform.linux_distribution()[1]
def run_command(self, cmd):
task = RunCommand(cmd)
@@ -560,8 +564,15 @@ class LXDTest(object):
logging.error(' STDOUT: {}'.format(task.stdout))
logging.error(' STDERR: {}'.format(task.stderr))
return False
-
- return True
+ else:
+ logging.debug('Command {}:'.format(task.cmd))
+ if task.stdout != '':
+ logging.debug(' STDOUT: {}'.format(task.stdout))
+ elif task.stderr != '':
+ logging.debug(' STDERR: {}'.format(task.stderr))
+ else:
+ logging.debug(' Command returned no output')
+ return True
def setup(self):
# Initialize LXD
@@ -569,12 +580,12 @@ class LXDTest(object):
logging.debug("Attempting to initialize LXD")
# TODO: Need a method to see if LXD is already initialized
if not self.run_command('lxd init --auto'):
- logging.error('Error encounterd while initializing LXD')
+ logging.debug('Error encounterd while initializing LXD')
result = False
# Retrieve and insert LXD images
- logging.debug("Downloading template.")
if self.template_url is not None:
+ logging.debug("Downloading template.")
targetfile = urlparse(self.template_url).path.split('/')[-1]
filename = os.path.join('/tmp', targetfile)
if not os.path.isfile(filename):
@@ -591,8 +602,8 @@ class LXDTest(object):
"Skipping Download.".format(filename))
self.template_tarball = filename
- logging.debug("Downloading rootfs.")
if self.rootfs_url is not None:
+ logging.debug("Downloading rootfs.")
targetfile = urlparse(self.rootfs_url).path.split('/')[-1]
filename = os.path.join('/tmp', targetfile)
if not os.path.isfile(filename):
@@ -609,15 +620,24 @@ class LXDTest(object):
self.rootfs_tarball = filename
# Insert images
- # TODO: check to see if ubuntu already exists
- logging.debug("Importing images into LXD")
- cmd = 'lxc image import {} rootfs {} --alias {}'.format(
- self.template_tarball, self.rootfs_tarball,
- self.image_alias)
- if not self.run_command(cmd):
- logging.error('Error encountered while attempting to '
- 'import images into LXD')
- result = False
+ if result is True:
+ logging.debug("Importing images into LXD")
+ cmd = 'lxc image import {} rootfs {} --alias {}'.format(
+ self.template_tarball, self.rootfs_tarball,
+ self.image_alias)
+ if not self.run_command(cmd):
+ logging.error('Error encountered while attempting to '
+ 'import images into LXD')
+ result = False
+ else:
+ logging.debug("No local image available, attempting to "
+ "import from default remote.")
+ cmd = 'lxc image copy {}{} local: --alias {}'.format(
+ self.default_remote, self.os_version, self.image_alias)
+ if not self.run_command(cmd):
+ loggging.error('Error encountered while attempting to '
+ 'import images from default remote.')
+ result = False
return result
@@ -625,16 +645,21 @@ class LXDTest(object):
"""
Downloads LXD files for same release as host machine
"""
+ # TODO: Clean this up to use a non-internet simplestream on MAAS server
logging.debug("Attempting download of {} from {}".format(filename,
url))
try:
resp = urllib.request.urlretrieve(url, filename)
except (IOError,
- Error,
+ OSError,
urllib.error.HTTPError,
urllib.error.URLError) as exception:
logging.error("Failed download of image from %s: %s",
- image_url, exception)
+ url, exception)
+ return False
+ except ValueError as verr:
+ logging.error("Invalid URL %s" % url)
+ logging.error("%s" % verr)
return False
if not os.path.isfile(filename):
@@ -664,10 +689,18 @@ class LXDTest(object):
if not self.run_command('lxc launch {} {}'.format(self.image_alias,
self.name)):
return False
+
+ logging.debug("Container listing:")
+ cmd = ("lxc list")
+ if not self.run_command(cmd):
+ return False
+
+ logging.debug("Testing container")
cmd = ("lxc exec {} dd if=/dev/urandom of=testdata.txt "
"bs=1024 count=1000".format(self.name))
if not self.run_command(cmd):
return False
+
return True
@@ -763,12 +796,12 @@ def main():
# silence normal output from requests module
logging.getLogger("requests").setLevel(logging.WARNING)
- # to check if not len(sys.argv) > 1
- if len(vars(args)) == 0:
+ # Verify args
+ try:
+ args.func(args)
+ except AttributeError:
parser.print_help()
return False
- args.func(args)
-
if __name__ == "__main__":
main()