2014-06-05 23:34:53 +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-06-05 23:34:53 +00:00
|
|
|
import os
|
2014-09-05 15:23:40 +00:00
|
|
|
import random
|
|
|
|
import string
|
2014-06-05 23:34:53 +00:00
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
|
|
|
from salttesting import skipIf
|
2014-06-10 18:25:04 +00:00
|
|
|
from salttesting.helpers import ensure_in_syspath, expensiveTest
|
2014-06-05 23:34:53 +00:00
|
|
|
|
|
|
|
ensure_in_syspath('../../../')
|
|
|
|
|
2014-06-22 23:58:39 +00:00
|
|
|
# Import Salt Libs
|
|
|
|
import integration
|
|
|
|
from salt.config import cloud_providers_config
|
|
|
|
|
2014-06-05 23:34:53 +00:00
|
|
|
# Import Third-Party Libs
|
2014-11-22 10:49:11 +00:00
|
|
|
# pylint: disable=import-error
|
|
|
|
from salt.ext.six.moves import range # pylint: disable=redefined-builtin
|
2014-06-05 23:34:53 +00:00
|
|
|
try:
|
2014-11-22 10:49:11 +00:00
|
|
|
import libcloud # pylint: disable=unused-import
|
2014-06-05 23:34:53 +00:00
|
|
|
HAS_LIBCLOUD = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_LIBCLOUD = False
|
2014-11-22 10:49:11 +00:00
|
|
|
# pylint: enable=import-error
|
2014-06-05 23:34:53 +00:00
|
|
|
|
|
|
|
|
2014-09-05 15:23:40 +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()
|
|
|
|
|
|
|
|
|
2014-06-05 23:34:53 +00:00
|
|
|
@skipIf(HAS_LIBCLOUD is False, 'salt-cloud requires >= libcloud 0.13.2')
|
|
|
|
class LinodeTest(integration.ShellCase):
|
|
|
|
'''
|
|
|
|
Integration tests for the Linode cloud provider in Salt-Cloud
|
|
|
|
'''
|
|
|
|
|
2014-06-22 23:58:39 +00:00
|
|
|
@expensiveTest
|
2014-06-05 23:34:53 +00:00
|
|
|
def setUp(self):
|
|
|
|
'''
|
|
|
|
Sets up the test requirements
|
|
|
|
'''
|
|
|
|
super(LinodeTest, self).setUp()
|
|
|
|
|
|
|
|
# check if appropriate cloud provider and profile files are present
|
|
|
|
profile_str = 'linode-config:'
|
|
|
|
provider = 'linode'
|
|
|
|
providers = self.run_cloud('--list-providers')
|
|
|
|
if profile_str not in providers:
|
|
|
|
self.skipTest(
|
2014-06-05 23:42:16 +00:00
|
|
|
'Configuration file for {0} was not found. Check {0}.conf files '
|
|
|
|
'in tests/integration/files/conf/cloud.*.d/ to run these tests.'
|
|
|
|
.format(provider)
|
2014-06-05 23:34:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# check if apikey and password are present
|
|
|
|
path = os.path.join(integration.FILES,
|
|
|
|
'conf',
|
|
|
|
'cloud.providers.d',
|
|
|
|
provider + '.conf')
|
|
|
|
config = cloud_providers_config(path)
|
|
|
|
api = config['linode-config']['linode']['apikey']
|
|
|
|
password = config['linode-config']['linode']['password']
|
|
|
|
if api == '' or password == '':
|
|
|
|
self.skipTest(
|
|
|
|
'An api key and password must be provided to run these tests. Check '
|
|
|
|
'tests/integration/files/conf/cloud.providers.d/{0}.conf'.format(
|
|
|
|
provider
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_instance(self):
|
|
|
|
'''
|
|
|
|
Test creating an instance on Linode
|
|
|
|
'''
|
|
|
|
# create the instance
|
2014-09-05 15:23:40 +00:00
|
|
|
instance = self.run_cloud('-p linode-test {0}'.format(INSTANCE_NAME))
|
|
|
|
ret_str = ' {0}'.format(INSTANCE_NAME)
|
2014-06-05 23:34:53 +00:00
|
|
|
|
|
|
|
# check if instance with salt installed returned
|
|
|
|
try:
|
2014-06-11 17:42:00 +00:00
|
|
|
self.assertIn(ret_str, instance)
|
2014-06-05 23:34:53 +00:00
|
|
|
except AssertionError:
|
2014-09-05 15:23:40 +00:00
|
|
|
self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME))
|
2014-06-05 23:34:53 +00:00
|
|
|
raise
|
|
|
|
|
|
|
|
# delete the instance
|
2014-09-05 15:23:40 +00:00
|
|
|
delete = self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME))
|
2014-06-11 17:42:00 +00:00
|
|
|
ret_str = ' True'
|
2014-06-05 23:34:53 +00:00
|
|
|
try:
|
2014-06-11 17:42:00 +00:00
|
|
|
self.assertIn(ret_str, delete)
|
2014-06-05 23:34:53 +00:00
|
|
|
except AssertionError:
|
|
|
|
raise
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
'''
|
|
|
|
Clean up after tests
|
|
|
|
'''
|
|
|
|
query = self.run_cloud('--query')
|
2014-09-05 15:23:40 +00:00
|
|
|
ret_str = ' {0}:'.format(INSTANCE_NAME)
|
2014-06-05 23:34:53 +00:00
|
|
|
|
|
|
|
# if test instance is still present, delete it
|
2014-06-11 17:42:00 +00:00
|
|
|
if ret_str in query:
|
2014-09-05 15:23:40 +00:00
|
|
|
self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME))
|
2014-06-10 16:20:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(LinodeTest)
|