test_proxmox.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 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.bootstrap": MagicMock(),
  20. },
  21. "__opts__": {
  22. "sock_dir": True,
  23. "transport": True,
  24. "providers": {"my_proxmox": {}},
  25. "profiles": {"my_proxmox": {}},
  26. },
  27. "__active_provider_name__": "my_proxmox:proxmox",
  28. }
  29. }
  30. def setUp(self):
  31. self.vm_ = {
  32. "profile": "my_proxmox",
  33. "name": "vm4",
  34. "driver": "proxmox",
  35. "technology": "qemu",
  36. "host": "127.0.0.1",
  37. "clone": True,
  38. "ide0": "data",
  39. "sata0": "data",
  40. "scsi0": "data",
  41. "net0": "a=b,c=d",
  42. }
  43. def tearDown(self):
  44. del self.vm_
  45. def test__stringlist_to_dictionary(self):
  46. result = proxmox._stringlist_to_dictionary("")
  47. self.assertEqual(result, {})
  48. result = proxmox._stringlist_to_dictionary(
  49. "foo=bar, ignored_space=bar,internal space=bar"
  50. )
  51. self.assertEqual(
  52. result, {"foo": "bar", "ignored_space": "bar", "internal space": "bar"}
  53. )
  54. # Negative cases
  55. self.assertRaises(ValueError, proxmox._stringlist_to_dictionary, "foo=bar,foo")
  56. self.assertRaises(
  57. ValueError,
  58. proxmox._stringlist_to_dictionary,
  59. "foo=bar,totally=invalid=assignment",
  60. )
  61. def test__dictionary_to_stringlist(self):
  62. result = proxmox._dictionary_to_stringlist({})
  63. self.assertEqual(result, "")
  64. result = proxmox._dictionary_to_stringlist({"a": "a"})
  65. self.assertEqual(result, "a=a")
  66. result = proxmox._dictionary_to_stringlist({"a": "a", "b": "b"})
  67. self.assertEqual(result, "a=a,b=b")
  68. def test__reconfigure_clone(self):
  69. # The return_value is for the net reconfigure assertions, it is irrelevant for the rest
  70. with patch.object(
  71. proxmox, "query", return_value={"net0": "c=overwritten,g=h"}
  72. ) as query:
  73. # Test a vm that lacks the required attributes
  74. proxmox._reconfigure_clone({}, 0)
  75. query.assert_not_called()
  76. # Test a fully mocked vm
  77. proxmox._reconfigure_clone(self.vm_, 0)
  78. # net reconfigure
  79. query.assert_any_call("get", "nodes/127.0.0.1/qemu/0/config")
  80. query.assert_any_call(
  81. "post", "nodes/127.0.0.1/qemu/0/config", {"net0": "a=b,c=d,g=h"}
  82. )
  83. # hdd reconfigure
  84. query.assert_any_call(
  85. "post", "nodes/127.0.0.1/qemu/0/config", {"ide0": "data"}
  86. )
  87. query.assert_any_call(
  88. "post", "nodes/127.0.0.1/qemu/0/config", {"sata0": "data"}
  89. )
  90. query.assert_any_call(
  91. "post", "nodes/127.0.0.1/qemu/0/config", {"scsi0": "data"}
  92. )