# -*- coding: utf-8 -*-
'''
    :codeauthor: :email:`Nicole Thomas <nicole@saltstack.com>`
'''

# Import Python Libs
import os

# Import Salt Testing Libs
from salttesting import skipIf
from salttesting.helpers import ensure_in_syspath, expensiveTest

ensure_in_syspath('../../../')

# Import Salt Libs
import integration
from salt.config import cloud_providers_config

# Import Third-Party Libs
try:
    import libcloud  # pylint: disable=W0611
    HAS_LIBCLOUD = True
except ImportError:
    HAS_LIBCLOUD = False


@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
    '''

    @expensiveTest
    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(
                '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 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
        '''
        name = 'linode-testing'

        # create the instance
        instance = self.run_cloud('-p linode-test {0}'.format(name))
        ret_str = '        {0}'.format(name)

        # check if instance with salt installed returned
        try:
            self.assertIn(ret_str, instance)
        except AssertionError:
            self.run_cloud('-d {0} --assume-yes'.format(name))
            raise

        # delete the instance
        delete = self.run_cloud('-d {0} --assume-yes'.format(name))
        ret_str = '            True'
        try:
            self.assertIn(ret_str, delete)
        except AssertionError:
            raise

    def tearDown(self):
        '''
        Clean up after tests
        '''
        name = 'linode-testing'
        query = self.run_cloud('--query')
        ret_str = '        {0}:'.format(name)

        # if test instance is still present, delete it
        if ret_str in query:
            self.run_cloud('-d {0} --assume-yes'.format(name))


if __name__ == '__main__':
    from integration import run_tests
    run_tests(LinodeTest)

@@ -119,16 +119,44 @@
 mport os
+%0Aimport random%0Aimport string
 %0A%0A# Impo
@@ -538,16 +538,335 @@
 False%0A%0A%0A
+def __random_name(size=6):%0A    '''%0A    Generates a random cloud instance name%0A    '''%0A    return 'CLOUD-TEST-' + ''.join(%0A        random.choice(string.ascii_uppercase + string.digits)%0A        for x in range(size)%0A    )%0A%0A# Create the cloud instance name to be used throughout the tests%0AINSTANCE_NAME = __random_name()%0A%0A%0A
 @skipIf(
@@ -2473,41 +2473,8 @@
 '''%0A
-        name = 'linode-testing'%0A%0A
     
@@ -2553,36 +2553,45 @@
 est %7B0%7D'.format(
-name
+INSTANCE_NAME
 ))%0A        ret_s
@@ -2608,36 +2608,45 @@
     %7B0%7D'.format(
-name
+INSTANCE_NAME
 )%0A%0A        # che
@@ -2826,36 +2826,45 @@
 ume-yes'.format(
-name
+INSTANCE_NAME
 ))%0A            r
@@ -2960,20 +2960,29 @@
 .format(
-name
+INSTANCE_NAME
 ))%0A     
@@ -3200,40 +3200,8 @@
 '''%0A
-        name = 'linode-testing'%0A
     
@@ -3278,20 +3278,29 @@
 .format(
-name
+INSTANCE_NAME
 )%0A%0A     
@@ -3434,20 +3434,29 @@
 .format(
-name
+INSTANCE_NAME
 ))%0A%0A%0Aif 

Update linode tests to use a randomly generated instance name