test_joyent.py 3.3 KB

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