2014-09-15 22:58:26 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Nicole Thomas <nicole@saltstack.com>`
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import Python Libs
|
2014-11-21 19:05:13 +00:00
|
|
|
from __future__ import absolute_import
|
2014-09-15 22:58:26 +00:00
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import string
|
2015-06-25 21:14:21 +00:00
|
|
|
from distutils.version import LooseVersion
|
2014-09-15 22:58:26 +00:00
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2014-09-16 12:35:12 +00:00
|
|
|
from salttesting import skipIf
|
2014-09-15 22:58:26 +00:00
|
|
|
from salttesting.helpers import ensure_in_syspath, expensiveTest
|
|
|
|
|
|
|
|
ensure_in_syspath('../../../')
|
|
|
|
|
|
|
|
# Import Salt Libs
|
2015-06-25 21:14:21 +00:00
|
|
|
import integration
|
2014-09-15 22:58:26 +00:00
|
|
|
from salt.config import cloud_providers_config
|
|
|
|
|
2014-09-16 12:35:12 +00:00
|
|
|
# Import Third-Party Libs
|
2015-06-25 21:14:21 +00:00
|
|
|
from salt.ext.six.moves import range
|
|
|
|
|
2014-09-16 12:35:12 +00:00
|
|
|
try:
|
2014-11-22 10:50:08 +00:00
|
|
|
import azure # pylint: disable=unused-import
|
2014-09-16 12:35:12 +00:00
|
|
|
HAS_AZURE = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_AZURE = False
|
|
|
|
|
2015-09-03 19:16:36 +00:00
|
|
|
if HAS_AZURE and not hasattr(azure, '__version__'):
|
|
|
|
import azure.common
|
2014-09-15 22:58:26 +00:00
|
|
|
|
|
|
|
def __random_name(size=6):
|
|
|
|
'''
|
|
|
|
Generates a random cloud instance name
|
|
|
|
'''
|
|
|
|
return 'CLOUD-TEST-' + ''.join(
|
|
|
|
random.choice(string.ascii_uppercase + string.digits)
|
|
|
|
for x in range(size)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Create the cloud instance name to be used throughout the tests
|
|
|
|
INSTANCE_NAME = __random_name()
|
2015-06-25 21:14:21 +00:00
|
|
|
PROVIDER_NAME = 'azure'
|
2015-08-25 15:49:32 +00:00
|
|
|
PROFILE_NAME = 'azure-test'
|
2015-06-25 21:14:21 +00:00
|
|
|
REQUIRED_AZURE = '0.11.1'
|
|
|
|
|
|
|
|
|
|
|
|
def __has_required_azure():
|
|
|
|
'''
|
|
|
|
Returns True/False if the required version of the Azure SDK is installed.
|
|
|
|
'''
|
2015-09-03 16:00:36 +00:00
|
|
|
if hasattr(azure, '__version__'):
|
|
|
|
version = LooseVersion(azure.__version__)
|
2015-09-03 16:21:41 +00:00
|
|
|
else:
|
2015-09-03 16:00:36 +00:00
|
|
|
version = LooseVersion(azure.common.__version__)
|
2015-06-25 21:14:21 +00:00
|
|
|
if HAS_AZURE is True and REQUIRED_AZURE <= version:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2014-09-15 22:58:26 +00:00
|
|
|
|
|
|
|
|
2015-06-25 21:14:21 +00:00
|
|
|
@skipIf(HAS_AZURE is False, 'These tests require the Azure Python SDK to be installed.')
|
|
|
|
@skipIf(__has_required_azure() is False, 'The Azure Python SDK must be >= 0.11.1.')
|
2014-09-15 22:58:26 +00:00
|
|
|
class AzureTest(integration.ShellCase):
|
|
|
|
'''
|
|
|
|
Integration tests for the Azure cloud provider in Salt-Cloud
|
|
|
|
'''
|
|
|
|
|
|
|
|
@expensiveTest
|
|
|
|
def setUp(self):
|
|
|
|
'''
|
|
|
|
Sets up the test requirements
|
|
|
|
'''
|
|
|
|
super(AzureTest, self).setUp()
|
|
|
|
|
|
|
|
# check if appropriate cloud provider and profile files are present
|
2015-08-25 15:49:32 +00:00
|
|
|
provider_str = 'azure-config'
|
2014-09-15 22:58:26 +00:00
|
|
|
providers = self.run_cloud('--list-providers')
|
2015-08-25 15:49:32 +00:00
|
|
|
if provider_str + ':' not in providers:
|
2014-09-15 22:58:26 +00:00
|
|
|
self.skipTest(
|
|
|
|
'Configuration file for {0} was not found. Check {0}.conf files '
|
|
|
|
'in tests/integration/files/conf/cloud.*.d/ to run these tests.'
|
2015-06-25 21:14:21 +00:00
|
|
|
.format(PROVIDER_NAME)
|
2014-09-15 22:58:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# check if subscription_id and certificate_path are present in provider file
|
2015-08-25 15:49:32 +00:00
|
|
|
provider_config = cloud_providers_config(
|
|
|
|
os.path.join(
|
|
|
|
integration.FILES,
|
|
|
|
'conf',
|
|
|
|
'cloud.providers.d',
|
|
|
|
PROVIDER_NAME + '.conf'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
sub_id = provider_config[provider_str][PROVIDER_NAME]['subscription_id']
|
|
|
|
cert_path = provider_config[provider_str][PROVIDER_NAME]['certificate_path']
|
2014-09-16 14:40:43 +00:00
|
|
|
if sub_id == '' or cert_path == '':
|
2014-09-15 22:58:26 +00:00
|
|
|
self.skipTest(
|
2014-09-16 14:40:43 +00:00
|
|
|
'A subscription_id and certificate_path must be provided to run '
|
|
|
|
'these tests. Check '
|
2014-09-16 13:08:18 +00:00
|
|
|
'tests/integration/files/conf/cloud.providers.d/{0}.conf'.format(
|
2015-06-25 21:14:21 +00:00
|
|
|
PROVIDER_NAME
|
2014-09-15 22:58:26 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-09-16 14:40:43 +00:00
|
|
|
# check if ssh_username, ssh_password, and media_link are present
|
|
|
|
# in the azure configuration file
|
2015-06-25 21:14:21 +00:00
|
|
|
profile_config = cloud_providers_config(
|
|
|
|
os.path.join(
|
|
|
|
integration.FILES,
|
|
|
|
'conf',
|
|
|
|
'cloud.profiles.d',
|
|
|
|
PROVIDER_NAME + '.conf'
|
|
|
|
)
|
|
|
|
)
|
2015-08-25 15:49:32 +00:00
|
|
|
ssh_user = profile_config[PROFILE_NAME][provider_str]['ssh_username']
|
|
|
|
ssh_pass = profile_config[PROFILE_NAME][provider_str]['ssh_password']
|
|
|
|
media_link = profile_config[PROFILE_NAME][provider_str]['media_link']
|
2014-09-16 14:40:43 +00:00
|
|
|
|
|
|
|
if ssh_user == '' or ssh_pass == '' or media_link == '':
|
|
|
|
self.skipTest(
|
|
|
|
'An ssh_username, ssh_password, and media_link must be provided to run '
|
|
|
|
'these tests. One or more of these elements is missing. Check '
|
|
|
|
'tests/integration/files/conf/cloud.profiles.d/{0}.conf'.format(
|
2015-06-25 21:14:21 +00:00
|
|
|
PROVIDER_NAME
|
2014-09-16 14:40:43 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-09-15 22:58:26 +00:00
|
|
|
def test_instance(self):
|
|
|
|
'''
|
|
|
|
Test creating an instance on Azure
|
|
|
|
'''
|
2015-06-25 21:14:21 +00:00
|
|
|
# check if instance with salt installed returned
|
2014-09-15 22:58:26 +00:00
|
|
|
try:
|
2015-06-25 21:14:21 +00:00
|
|
|
self.assertIn(
|
|
|
|
INSTANCE_NAME,
|
2015-08-25 15:49:32 +00:00
|
|
|
[i.strip() for i in self.run_cloud(
|
|
|
|
'-p {0} {1}'.format(
|
|
|
|
PROFILE_NAME,
|
|
|
|
INSTANCE_NAME
|
|
|
|
)
|
|
|
|
)]
|
2015-06-25 21:14:21 +00:00
|
|
|
)
|
2014-09-15 22:58:26 +00:00
|
|
|
except AssertionError:
|
|
|
|
self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME))
|
|
|
|
raise
|
|
|
|
|
|
|
|
# delete the instance
|
|
|
|
try:
|
2015-06-25 21:14:21 +00:00
|
|
|
self.assertIn(
|
|
|
|
INSTANCE_NAME + ':',
|
2015-08-25 15:49:32 +00:00
|
|
|
[i.strip() for i in self.run_cloud(
|
|
|
|
'-d {0} --assume-yes'.format(
|
|
|
|
INSTANCE_NAME
|
|
|
|
)
|
|
|
|
)]
|
2015-06-25 21:14:21 +00:00
|
|
|
)
|
2014-09-15 22:58:26 +00:00
|
|
|
except AssertionError:
|
|
|
|
raise
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
'''
|
|
|
|
Clean up after tests
|
|
|
|
'''
|
|
|
|
query = self.run_cloud('--query')
|
|
|
|
ret_str = ' {0}:'.format(INSTANCE_NAME)
|
|
|
|
|
|
|
|
# if test instance is still present, delete it
|
|
|
|
if ret_str in query:
|
|
|
|
self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2014-11-24 03:09:09 +00:00
|
|
|
from integration import run_tests # pylint: disable=import-error
|
2014-09-15 22:58:26 +00:00
|
|
|
run_tests(AzureTest)
|