2016-01-09 14:37:01 +00:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import os
|
2015-10-11 20:09:16 +00:00
|
|
|
import unittest
|
2016-02-09 21:31:28 +00:00
|
|
|
from unittest2 import skipIf
|
2015-10-17 18:47:58 +00:00
|
|
|
|
2016-01-09 14:37:01 +00:00
|
|
|
import integration
|
|
|
|
from integration.cloud.helpers import random_name
|
2016-01-03 22:38:15 +00:00
|
|
|
from salt.utils import virtualbox
|
2015-09-28 20:46:15 +00:00
|
|
|
|
2016-01-09 14:37:01 +00:00
|
|
|
# Create the cloud instance name to be used throughout the tests
|
|
|
|
INSTANCE_NAME = random_name()
|
|
|
|
PROVIDER_NAME = "virtualbox"
|
|
|
|
CONFIG_NAME = PROVIDER_NAME + "-config"
|
|
|
|
PROFILE_NAME = PROVIDER_NAME + "-test"
|
2016-01-23 20:52:49 +00:00
|
|
|
DEPLOY_PROFILE_NAME = PROVIDER_NAME + "-deploy-test"
|
2016-01-09 14:37:01 +00:00
|
|
|
DRIVER_NAME = "virtualbox"
|
|
|
|
BASE_BOX_NAME = "__temp_test_vm__"
|
|
|
|
BOOTABLE_BASE_BOX_NAME = "SaltMiniBuntuTest"
|
|
|
|
|
|
|
|
# Setup logging
|
|
|
|
log = logging.getLogger()
|
|
|
|
log_handler = logging.StreamHandler()
|
|
|
|
log_handler.setLevel(logging.INFO)
|
|
|
|
log.addHandler(log_handler)
|
|
|
|
log.setLevel(logging.INFO)
|
|
|
|
info = log.info
|
|
|
|
|
2015-09-28 20:46:15 +00:00
|
|
|
|
2016-02-09 21:31:28 +00:00
|
|
|
@skipIf(virtualbox.HAS_LIBS is False, 'virtualbox has to be installed')
|
2015-10-11 20:09:16 +00:00
|
|
|
class VirtualboxTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2015-12-26 18:03:03 +00:00
|
|
|
self.vbox = virtualbox.vb_get_box()
|
2015-10-11 20:09:16 +00:00
|
|
|
|
|
|
|
def assertMachineExists(self, name, msg=None):
|
|
|
|
try:
|
|
|
|
self.vbox.findMachine(name)
|
|
|
|
except Exception as e:
|
|
|
|
if msg:
|
|
|
|
self.fail(msg)
|
|
|
|
else:
|
|
|
|
self.fail(e.message)
|
|
|
|
|
|
|
|
def assertMachineDoesNotExist(self, name, msg=None):
|
|
|
|
self.assertRaisesRegexp(Exception, "Could not find a registered machine", self.vbox.findMachine, name)
|
|
|
|
|
|
|
|
|
2016-02-09 21:31:28 +00:00
|
|
|
@skipIf(virtualbox.HAS_LIBS is False, 'salt-cloud requires virtualbox to be installed')
|
2016-01-09 14:37:01 +00:00
|
|
|
class VirtualboxCloudTestCase(integration.ShellCase):
|
|
|
|
def run_cloud(self, arg_str, catch_stderr=False, timeout=None):
|
|
|
|
"""
|
|
|
|
Execute salt-cloud with json output and try to interpret it
|
|
|
|
|
|
|
|
@return:
|
|
|
|
@rtype: dict
|
|
|
|
"""
|
|
|
|
config_path = os.path.join(
|
|
|
|
integration.FILES,
|
|
|
|
'conf'
|
|
|
|
)
|
|
|
|
arg_str = '--out=json -c {0} {1}'.format(config_path, arg_str)
|
|
|
|
# arg_str = "%s --log-level=error" % arg_str
|
|
|
|
log.debug("running salt-cloud with %s" % arg_str)
|
|
|
|
output = self.run_script('salt-cloud', arg_str, catch_stderr, timeout=timeout)
|
|
|
|
|
|
|
|
# Sometimes tuples are returned???
|
|
|
|
if isinstance(output, tuple) and len(output) == 2:
|
|
|
|
output = output[0]
|
|
|
|
|
|
|
|
# Attempt to clean json output before fix of https://github.com/saltstack/salt/issues/27629
|
|
|
|
valid_initial_chars = ['{', '[', '"']
|
|
|
|
for line in output[:]:
|
2016-01-23 20:43:45 +00:00
|
|
|
if len(line) == 0 or (line[0] not in valid_initial_chars):
|
|
|
|
output.pop(0)
|
2016-01-09 14:37:01 +00:00
|
|
|
else:
|
|
|
|
break
|
|
|
|
if len(output) is 0:
|
|
|
|
return dict()
|
|
|
|
else:
|
|
|
|
return json.loads("".join(output))
|
|
|
|
|
|
|
|
def run_cloud_function(self, function, kw_function_args=None, **kwargs):
|
|
|
|
"""
|
|
|
|
A helper to call `salt-cloud -f function provider`
|
|
|
|
|
|
|
|
@param function:
|
|
|
|
@type function:
|
|
|
|
@param kw_function_args: Keyword arguments for the argument string in the commandline
|
|
|
|
@type dict:
|
|
|
|
@param kwargs: For the `run_cloud` function
|
|
|
|
@type kwargs:
|
|
|
|
@return:
|
|
|
|
@rtype: dict
|
|
|
|
"""
|
|
|
|
args = []
|
|
|
|
# Args converted in the form of key1='value1' ... keyN='valueN'
|
|
|
|
if kw_function_args:
|
|
|
|
args = [
|
|
|
|
"%s='%s'" % (key, value)
|
|
|
|
for key, value in kw_function_args.iteritems()
|
|
|
|
]
|
|
|
|
|
|
|
|
output = self.run_cloud("-f %s %s %s" % (function, CONFIG_NAME, " ".join(args)), **kwargs)
|
|
|
|
return output.get(CONFIG_NAME, {}).get(PROVIDER_NAME, {})
|
|
|
|
|
|
|
|
def run_cloud_action(self, action, instance_name, **kwargs):
|
|
|
|
"""
|
|
|
|
A helper to call `salt-cloud -a action instance_name`
|
|
|
|
|
|
|
|
@param action:
|
|
|
|
@type action: str
|
|
|
|
@param instance_name:
|
|
|
|
@type instance_name: str
|
|
|
|
@return:
|
|
|
|
@rtype: dict
|
|
|
|
"""
|
|
|
|
|
2016-01-09 15:28:21 +00:00
|
|
|
output = self.run_cloud("-a %s %s --assume-yes" % (action, instance_name), **kwargs)
|
|
|
|
return output.get(CONFIG_NAME, {}).get(PROVIDER_NAME, {})
|
2016-01-09 14:37:01 +00:00
|
|
|
|
|
|
|
|
2015-09-28 20:46:15 +00:00
|
|
|
def list_machines():
|
2015-12-26 18:03:03 +00:00
|
|
|
vbox = virtualbox.vb_get_box()
|
2015-09-28 20:46:15 +00:00
|
|
|
for machine in vbox.getArray(vbox, "Machines"):
|
|
|
|
print "Machine '%s' logs in '%s'" % (
|
|
|
|
machine.name,
|
|
|
|
machine.logFolder
|
|
|
|
)
|