test_joyent.py 3.2 KB

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