test_smartos.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Libs
  5. import salt.states.smartos as smartos
  6. from salt.utils.odict import OrderedDict
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.mock import patch
  10. from tests.support.unit import TestCase
  11. class SmartOsTestCase(TestCase, LoaderModuleMockMixin):
  12. """
  13. TestCase for salt.states.smartos
  14. """
  15. def setup_loader_modules(self):
  16. return {smartos: {"__opts__": {"test": False}}}
  17. def test_config_present_does_not_exist(self):
  18. """
  19. Test salt.states.smartos.config_present
  20. when the config files does not exist
  21. """
  22. name = "test"
  23. value = "test_value"
  24. with patch("os.path.isfile", return_value=False):
  25. with patch("salt.utils.atomicfile.atomic_open", side_effect=IOError):
  26. ret = smartos.config_present(name=name, value=value)
  27. assert not ret["result"]
  28. assert ret[
  29. "comment"
  30. ] == 'Could not add property {0} with value "{1}" to config'.format(name, value)
  31. def test_parse_vmconfig_vrrp(self):
  32. """
  33. Test _parse_vmconfig's vrid -> mac convertor
  34. SmartOS will always use a mac based on the vrrp_vrid,
  35. so we will replace the provided mac with the one based
  36. on this value.
  37. Doing so ensures that 'old' nics are removed and 'new'
  38. nics get added as these actions are keyed on the mac
  39. property.
  40. """
  41. # NOTE: vmconfig is not a full vmadm payload,
  42. # this is not an issue given we are only testing
  43. # the vrrp_vrid to mac conversions
  44. ret = smartos._parse_vmconfig(
  45. OrderedDict(
  46. [
  47. (
  48. "nics",
  49. OrderedDict(
  50. [
  51. (
  52. "00:00:5e:00:01:01",
  53. OrderedDict(
  54. [
  55. ("vrrp_vrid", 1),
  56. ("vrrp_primary_ip", "12.34.5.6"),
  57. ]
  58. ),
  59. ),
  60. (
  61. "00:00:5e:00:01:24",
  62. OrderedDict(
  63. [
  64. ("vrrp_vrid", 240),
  65. ("vrrp_primary_ip", "12.34.5.6"),
  66. ]
  67. ),
  68. ),
  69. (
  70. "00:22:06:00:00:01",
  71. OrderedDict([("ips", ["12.34.5.6/24"])]),
  72. ),
  73. ]
  74. ),
  75. )
  76. ]
  77. ),
  78. {"nics": "mac"},
  79. )
  80. # NOTE: nics.0 is a vrrp nic with correct mac (check mac == vrid based -> unchanged)
  81. assert ret["nics"][0]["mac"] == "00:00:5e:00:01:01"
  82. # NOTE: nics.1 is a vrrp nic with incorrect mac (check mac == vrid based -> changed)
  83. assert ret["nics"][1]["mac"] == "00:00:5e:00:01:f0"
  84. # NOTE: nics.2 was not a vrrp nic (check mac was not changed)
  85. assert ret["nics"][2]["mac"] == "00:22:06:00:00:01"