1
0

test_digitalocean.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # -*- coding: utf-8 -*-
  2. """
  3. Integration tests for DigitalOcean APIv2
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import base64
  8. import hashlib
  9. import salt.crypt
  10. # Import Salt Libs
  11. import salt.ext.six as six
  12. import salt.utils.stringutils
  13. from salt.ext.six.moves import range
  14. # Import Salt Testing Libs
  15. from tests.integration.cloud.helpers.cloud_test_base import TIMEOUT, CloudTest
  16. class DigitalOceanTest(CloudTest):
  17. """
  18. Integration tests for the DigitalOcean cloud provider in Salt-Cloud
  19. """
  20. PROVIDER = "digitalocean"
  21. REQUIRED_PROVIDER_CONFIG_ITEMS = (
  22. "personal_access_token",
  23. "ssh_key_file",
  24. "ssh_key_name",
  25. )
  26. def test_list_images(self):
  27. """
  28. Tests the return of running the --list-images command for digitalocean
  29. """
  30. image_list = self.run_cloud("--list-images {0}".format(self.PROVIDER))
  31. self.assertIn("ubuntu-18-04-x64", [i.strip() for i in image_list])
  32. def test_list_locations(self):
  33. """
  34. Tests the return of running the --list-locations command for digitalocean
  35. """
  36. _list_locations = self.run_cloud("--list-locations {0}".format(self.PROVIDER))
  37. self.assertIn("San Francisco 2", [i.strip() for i in _list_locations])
  38. def test_list_sizes(self):
  39. """
  40. Tests the return of running the --list-sizes command for digitalocean
  41. """
  42. _list_sizes = self.run_cloud("--list-sizes {0}".format(self.PROVIDER))
  43. self.assertIn("16gb", [i.strip() for i in _list_sizes])
  44. def test_key_management(self):
  45. """
  46. Test key management
  47. """
  48. do_key_name = self.instance_name + "-key"
  49. # generate key and fingerprint
  50. if salt.crypt.HAS_M2:
  51. rsa_key = salt.crypt.RSA.gen_key(4096, 65537, lambda: None)
  52. pub = six.b(
  53. "ssh-rsa {}".format(
  54. base64.b64encode(
  55. six.b("\x00\x00\x00\x07ssh-rsa{}{}".format(*rsa_key.pub()))
  56. )
  57. )
  58. )
  59. else:
  60. ssh_key = salt.crypt.RSA.generate(4096)
  61. pub = ssh_key.publickey().exportKey("OpenSSH")
  62. pub = salt.utils.stringutils.to_str(pub)
  63. key_hex = hashlib.md5(
  64. base64.b64decode(pub.strip().split()[1].encode())
  65. ).hexdigest()
  66. finger_print = ":".join([key_hex[x : x + 2] for x in range(0, len(key_hex), 2)])
  67. try:
  68. _key = self.run_cloud(
  69. '-f create_key {0} name="{1}" public_key="{2}"'.format(
  70. self.PROVIDER, do_key_name, pub
  71. )
  72. )
  73. # Upload public key
  74. self.assertIn(finger_print, [i.strip() for i in _key])
  75. # List all keys
  76. list_keypairs = self.run_cloud("-f list_keypairs {0}".format(self.PROVIDER))
  77. self.assertIn(finger_print, [i.strip() for i in list_keypairs])
  78. # List key
  79. show_keypair = self.run_cloud(
  80. "-f show_keypair {0} keyname={1}".format(self.PROVIDER, do_key_name)
  81. )
  82. self.assertIn(finger_print, [i.strip() for i in show_keypair])
  83. except AssertionError:
  84. # Delete the public key if the above assertions fail
  85. self.run_cloud(
  86. "-f remove_key {0} id={1}".format(self.PROVIDER, finger_print)
  87. )
  88. raise
  89. finally:
  90. # Delete public key
  91. self.assertTrue(
  92. self.run_cloud(
  93. "-f remove_key {0} id={1}".format(self.PROVIDER, finger_print)
  94. )
  95. )
  96. def test_instance(self):
  97. """
  98. Test creating an instance on DigitalOcean
  99. """
  100. # check if instance with salt installed returned
  101. ret_str = self.run_cloud(
  102. "-p digitalocean-test {0}".format(self.instance_name), timeout=TIMEOUT
  103. )
  104. self.assertInstanceExists(ret_str)
  105. self.assertDestroyInstance()