test_tencentcloud.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Li Kexian <doyenli@tencent.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. # Import Salt Libs
  9. from salt.config import cloud_providers_config
  10. # Import Salt Testing Libs
  11. from tests.support.case import ShellCase
  12. from tests.support.runtests import RUNTIME_VARS
  13. from tests.support.helpers import expensiveTest, generate_random_name
  14. # Create the cloud instance name to be used throughout the tests
  15. INSTANCE_NAME = generate_random_name('CLOUD-TEST-')
  16. PROVIDER_NAME = 'tencentcloud'
  17. @expensiveTest
  18. class TencentCloudTest(ShellCase):
  19. '''
  20. Integration tests for the Tencent Cloud cloud provider in Salt-Cloud
  21. '''
  22. def setUp(self):
  23. '''
  24. Sets up the test requirements
  25. '''
  26. super(TencentCloudTest, self).setUp()
  27. # check if appropriate cloud provider and profile files are present
  28. profile_str = 'tencentcloud-config'
  29. providers = self.run_cloud('--list-providers')
  30. if profile_str + ':' not in providers:
  31. self.skipTest(
  32. 'Configuration file for {0} was not found. Check {0}.conf files '
  33. 'in tests/integration/files/conf/cloud.*.d/ to run these tests.'
  34. .format(PROVIDER_NAME)
  35. )
  36. # check if personal access token, ssh_key_file, and ssh_key_names are present
  37. config = cloud_providers_config(
  38. os.path.join(
  39. RUNTIME_VARS.FILES,
  40. 'conf',
  41. 'cloud.providers.d',
  42. PROVIDER_NAME + '.conf'
  43. )
  44. )
  45. tid = config[profile_str][PROVIDER_NAME]['id']
  46. key = config[profile_str][PROVIDER_NAME]['key']
  47. if tid == '' or key == '':
  48. self.skipTest(
  49. 'An api id and key must be provided to run these tests. Check '
  50. 'tests/integration/files/conf/cloud.providers.d/{0}.conf'.format(
  51. PROVIDER_NAME
  52. )
  53. )
  54. def test_instance(self):
  55. '''
  56. Test creating an instance on Tencent Cloud
  57. '''
  58. # check if instance with salt installed returned
  59. try:
  60. self.assertIn(
  61. INSTANCE_NAME,
  62. [i.strip() for i in self.run_cloud(
  63. '-p tencentcloud-test {0}'.format(INSTANCE_NAME), timeout=500)]
  64. )
  65. except AssertionError:
  66. self.run_cloud(
  67. '-d {0} --assume-yes'.format(INSTANCE_NAME), timeout=500)
  68. raise
  69. # delete the instance
  70. self.assertIn(
  71. INSTANCE_NAME + ':',
  72. [i.strip() for i in self.run_cloud(
  73. '-d {0} --assume-yes'.format(INSTANCE_NAME), timeout=500)]
  74. )
  75. def tearDown(self):
  76. '''
  77. Clean up after tests
  78. '''
  79. query = self.run_cloud('--query')
  80. ret_str = ' {0}:'.format(INSTANCE_NAME)
  81. # if test instance is still present, delete it
  82. if ret_str in query:
  83. self.run_cloud(
  84. '-d {0} --assume-yes'.format(INSTANCE_NAME), timeout=500)