test_tencentcloud.py 3.1 KB

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