test_digitalocean.py 4.0 KB

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