test_proxmox.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Tyler Johnson <tjohnson@saltstack.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 proxmox
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import ANY, MagicMock, patch
  12. from tests.support.unit import TestCase
  13. class ProxmoxTest(TestCase, LoaderModuleMockMixin):
  14. def setup_loader_modules(self):
  15. return {
  16. proxmox: {
  17. "__utils__": {
  18. "cloud.fire_event": MagicMock(),
  19. "cloud.filter_event": MagicMock(),
  20. "cloud.bootstrap": MagicMock(),
  21. },
  22. "__opts__": {
  23. "sock_dir": True,
  24. "transport": True,
  25. "providers": {"my_proxmox": {}},
  26. "profiles": {"my_proxmox": {}},
  27. },
  28. "__active_provider_name__": "my_proxmox:proxmox",
  29. }
  30. }
  31. def setUp(self):
  32. self.vm_ = {
  33. "profile": "my_proxmox",
  34. "name": "vm4",
  35. "driver": "proxmox",
  36. "technology": "qemu",
  37. "host": "127.0.0.1",
  38. "clone": True,
  39. "ide0": "data",
  40. "sata0": "data",
  41. "scsi0": "data",
  42. "net0": "a=b,c=d",
  43. }
  44. def tearDown(self):
  45. del self.vm_
  46. def test__stringlist_to_dictionary(self):
  47. result = proxmox._stringlist_to_dictionary("")
  48. self.assertEqual(result, {})
  49. result = proxmox._stringlist_to_dictionary(
  50. "foo=bar, ignored_space=bar,internal space=bar"
  51. )
  52. self.assertEqual(
  53. result, {"foo": "bar", "ignored_space": "bar", "internal space": "bar"}
  54. )
  55. # Negative cases
  56. self.assertRaises(ValueError, proxmox._stringlist_to_dictionary, "foo=bar,foo")
  57. self.assertRaises(
  58. ValueError,
  59. proxmox._stringlist_to_dictionary,
  60. "foo=bar,totally=invalid=assignment",
  61. )
  62. def test__dictionary_to_stringlist(self):
  63. result = proxmox._dictionary_to_stringlist({})
  64. self.assertEqual(result, "")
  65. result = proxmox._dictionary_to_stringlist({"a": "a"})
  66. self.assertEqual(result, "a=a")
  67. result = proxmox._dictionary_to_stringlist({"a": "a", "b": "b"})
  68. self.assertEqual(result, "a=a,b=b")
  69. def test__reconfigure_clone(self):
  70. # The return_value is for the net reconfigure assertions, it is irrelevant for the rest
  71. with patch.object(
  72. proxmox, "query", return_value={"net0": "c=overwritten,g=h"}
  73. ) as query:
  74. # Test a vm that lacks the required attributes
  75. proxmox._reconfigure_clone({}, 0)
  76. query.assert_not_called()
  77. # Test a fully mocked vm
  78. proxmox._reconfigure_clone(self.vm_, 0)
  79. # net reconfigure
  80. query.assert_any_call("get", "nodes/127.0.0.1/qemu/0/config")
  81. query.assert_any_call(
  82. "post", "nodes/127.0.0.1/qemu/0/config", {"net0": "a=b,c=d,g=h"}
  83. )
  84. # hdd reconfigure
  85. query.assert_any_call(
  86. "post", "nodes/127.0.0.1/qemu/0/config", {"ide0": "data"}
  87. )
  88. query.assert_any_call(
  89. "post", "nodes/127.0.0.1/qemu/0/config", {"sata0": "data"}
  90. )
  91. query.assert_any_call(
  92. "post", "nodes/127.0.0.1/qemu/0/config", {"scsi0": "data"}
  93. )
  94. def test_clone(self):
  95. """
  96. Test that an integer value for clone_from
  97. """
  98. mock_query = MagicMock(return_value="")
  99. with patch(
  100. "salt.cloud.clouds.proxmox._get_properties", MagicMock(return_value=[])
  101. ), patch("salt.cloud.clouds.proxmox.query", mock_query):
  102. vm_ = {
  103. "technology": "qemu",
  104. "name": "new2",
  105. "host": "myhost",
  106. "clone": True,
  107. "clone_from": 123,
  108. }
  109. # CASE 1: Numeric ID
  110. result = proxmox.create_node(vm_, ANY)
  111. mock_query.assert_called_once_with(
  112. "post", "nodes/myhost/qemu/123/clone", {"newid": ANY},
  113. )
  114. assert result == {}
  115. # CASE 2: host:ID notation
  116. mock_query.reset_mock()
  117. vm_["clone_from"] = "otherhost:123"
  118. result = proxmox.create_node(vm_, ANY)
  119. mock_query.assert_called_once_with(
  120. "post", "nodes/otherhost/qemu/123/clone", {"newid": ANY},
  121. )
  122. assert result == {}