test_joyent.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Eric Radman <ericshane@eradman.com>
  4. '''
  5. # Import Salt Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase, skipIf
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch,
  13. )
  14. # Import Salt Libs
  15. from salt.cloud.clouds import joyent
  16. # Stubs
  17. def fake_wait_for_ip(check_for_ip_fn,
  18. interval=None,
  19. timeout=None,
  20. interval_multiplier=None):
  21. '''
  22. Callback that returns immediately instead of waiting
  23. '''
  24. assert isinstance(interval, int)
  25. assert isinstance(timeout, int)
  26. assert isinstance(interval_multiplier, int)
  27. return check_for_ip_fn()
  28. @skipIf(joyent.HAS_REQUIRED_CRYPTO is False, reason='PyCrypto or Cryptodome not installed')
  29. class JoyentTestCase(TestCase, LoaderModuleMockMixin):
  30. '''
  31. Unit TestCase for the salt.cloud.clouds.joyent module
  32. '''
  33. def setup_loader_modules(self):
  34. patcher = patch('salt.utils.cloud.wait_for_ip', fake_wait_for_ip)
  35. patcher.start()
  36. self.addCleanup(patcher.stop)
  37. return {
  38. joyent: {
  39. '__utils__': {
  40. 'cloud.fire_event': MagicMock(),
  41. 'cloud.bootstrap': MagicMock()
  42. },
  43. '__opts__': {
  44. 'sock_dir': True,
  45. 'transport': True,
  46. 'providers': {'my_joyent': {}},
  47. 'profiles': {'my_joyent': {}}
  48. },
  49. '__active_provider_name__': 'my_joyent:joyent'
  50. }
  51. }
  52. def setUp(self):
  53. self.vm_ = {
  54. 'profile': 'my_joyent',
  55. 'name': 'vm3',
  56. 'driver': 'joyent',
  57. 'size': 'k4-highcpu-kvm-750M',
  58. 'image': 'freebsd10',
  59. 'location': 'us-east-1'
  60. }
  61. def tearDown(self):
  62. del self.vm_
  63. def test_query_instance_init(self):
  64. '''
  65. Initial provisioning, no IP assigned
  66. '''
  67. # Not yet reachable
  68. reply = (200, {'state': 'provisioning'})
  69. with patch.object(joyent, 'show_instance', return_value=reply):
  70. result = joyent.query_instance(self.vm_)
  71. self.assertTrue(joyent.__utils__['cloud.fire_event'].called_once())
  72. self.assertEqual(result, None)
  73. def test_query_instance_has_ip(self):
  74. '''
  75. IP address assigned but not yet ready
  76. '''
  77. reply = (200, {'primaryIp': '1.1.1.1', 'state': 'provisioning'})
  78. with patch.object(joyent, 'show_instance', return_value=reply):
  79. result = joyent.query_instance(self.vm_)
  80. self.assertTrue(joyent.__utils__['cloud.fire_event'].called_once())
  81. self.assertEqual(result, None)
  82. def test_query_instance_ready(self):
  83. '''
  84. IP address assigned, and VM is ready
  85. '''
  86. reply = (200, {'primaryIp': '1.1.1.1', 'state': 'running'})
  87. with patch.object(joyent, 'show_instance', return_value=reply):
  88. result = joyent.query_instance(self.vm_)
  89. self.assertTrue(joyent.__utils__['cloud.fire_event'].called_once())
  90. self.assertEqual(result, '1.1.1.1')