test_tencentcloud.py 3.1 KB

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