1
0

test_joyent.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. class JoyentTestCase(TestCase, LoaderModuleMockMixin):
  32. '''
  33. Unit TestCase for the salt.cloud.clouds.joyent module
  34. '''
  35. def setup_loader_modules(self):
  36. patcher = patch('salt.utils.cloud.wait_for_ip', fake_wait_for_ip)
  37. patcher.start()
  38. self.addCleanup(patcher.stop)
  39. return {
  40. joyent: {
  41. '__utils__': {
  42. 'cloud.fire_event': MagicMock(),
  43. 'cloud.bootstrap': MagicMock()
  44. },
  45. '__opts__': {
  46. 'sock_dir': True,
  47. 'transport': True,
  48. 'providers': {'my_joyent': {}},
  49. 'profiles': {'my_joyent': {}}
  50. },
  51. '__active_provider_name__': 'my_joyent:joyent'
  52. }
  53. }
  54. def setUp(self):
  55. self.vm_ = {
  56. 'profile': 'my_joyent',
  57. 'name': 'vm3',
  58. 'driver': 'joyent',
  59. 'size': 'k4-highcpu-kvm-750M',
  60. 'image': 'freebsd10',
  61. 'location': 'us-east-1'
  62. }
  63. def tearDown(self):
  64. del self.vm_
  65. def test_query_instance_init(self):
  66. '''
  67. Initial provisioning, no IP assigned
  68. '''
  69. # Not yet reachable
  70. reply = (200, {'state': 'provisioning'})
  71. with patch.object(joyent, 'show_instance', return_value=reply):
  72. result = joyent.query_instance(self.vm_)
  73. self.assertTrue(joyent.__utils__['cloud.fire_event'].called_once())
  74. self.assertEqual(result, None)
  75. def test_query_instance_has_ip(self):
  76. '''
  77. IP address assigned but not yet ready
  78. '''
  79. reply = (200, {'primaryIp': '1.1.1.1', 'state': 'provisioning'})
  80. with patch.object(joyent, 'show_instance', return_value=reply):
  81. result = joyent.query_instance(self.vm_)
  82. self.assertTrue(joyent.__utils__['cloud.fire_event'].called_once())
  83. self.assertEqual(result, None)
  84. def test_query_instance_ready(self):
  85. '''
  86. IP address assigned, and VM is ready
  87. '''
  88. reply = (200, {'primaryIp': '1.1.1.1', 'state': 'running'})
  89. with patch.object(joyent, 'show_instance', return_value=reply):
  90. result = joyent.query_instance(self.vm_)
  91. self.assertTrue(joyent.__utils__['cloud.fire_event'].called_once())
  92. self.assertEqual(result, '1.1.1.1')