salt/tests/integration/cloud/providers/digital_ocean.py

116 lines
3.6 KiB
Python
Raw Normal View History

2014-06-09 16:29:46 +00:00
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Nicole Thomas <nicole@saltstack.com>`
'''
# Import Python Libs
from __future__ import absolute_import
2014-06-09 16:29:46 +00:00
import os
import random
import string
2014-06-09 16:29:46 +00:00
# Import Salt Testing Libs
from salttesting.helpers import ensure_in_syspath, expensiveTest
2014-06-09 16:29:46 +00:00
ensure_in_syspath('../../../')
# Import Salt Libs
import integration
from salt.config import cloud_providers_config
2014-06-09 16:29:46 +00:00
2014-06-09 17:32:13 +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-09 16:29:46 +00:00
class DigitalOceanTest(integration.ShellCase):
'''
Integration tests for the DigitalOcean cloud provider in Salt-Cloud
2014-06-09 16:29:46 +00:00
'''
@expensiveTest
2014-06-09 16:29:46 +00:00
def setUp(self):
'''
Sets up the test requirements
'''
super(DigitalOceanTest, self).setUp()
# check if appropriate cloud provider and profile files are present
profile_str = 'digitalocean-config:'
provider = 'digital_ocean'
providers = self.run_cloud('--list-providers')
if profile_str not in providers:
self.skipTest(
'Configuration file for {0} was not found. Check {0}.conf files '
'in tests/integration/files/conf/cloud.*.d/ to run these tests.'
.format(provider)
)
# check if client_key and api_key are present
path = os.path.join(integration.FILES,
'conf',
'cloud.providers.d',
provider + '.conf')
config = cloud_providers_config(path)
2014-06-09 16:29:46 +00:00
api = config['digitalocean-config']['digital_ocean']['api_key']
client = config['digitalocean-config']['digital_ocean']['client_key']
ssh_file = config['digitalocean-config']['digital_ocean']['ssh_key_file']
ssh_name = config['digitalocean-config']['digital_ocean']['ssh_key_name']
if api == '' or client == '' or ssh_file == '' or ssh_name == '':
2014-06-09 16:29:46 +00:00
self.skipTest(
'A client key, an api key, an ssh key file, and an ssh key name '
'must be provided to run these tests. Check '
'tests/integration/files/conf/cloud.providers.d/{0}.conf'
2014-06-09 16:29:46 +00:00
.format(provider)
)
def test_instance(self):
'''
Test creating an instance on DigitalOcean
2014-06-09 16:29:46 +00:00
'''
# create the instance
instance = self.run_cloud('-p digitalocean-test {0}'.format(INSTANCE_NAME))
ret_str = ' {0}'.format(INSTANCE_NAME)
2014-06-09 16:29:46 +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-09 16:29:46 +00:00
except AssertionError:
self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME))
2014-06-09 16:29:46 +00:00
raise
# delete the instance
delete = self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME))
ret_str = ' OK'
2014-06-09 16:29:46 +00:00
try:
2014-06-11 17:42:00 +00:00
self.assertIn(ret_str, delete)
2014-06-09 16:29:46 +00:00
except AssertionError:
raise
def tearDown(self):
'''
Clean up after tests
'''
query = self.run_cloud('--query')
ret_str = ' {0}:'.format(INSTANCE_NAME)
2014-06-09 16:29:46 +00:00
# if test instance is still present, delete it
2014-06-11 17:42:00 +00:00
if ret_str in query:
self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME))
if __name__ == '__main__':
from integration import run_tests
run_tests(DigitalOceanTest)