test_win_dns_client.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Rahul Handay <rahulha@saltstack.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch,
  13. )
  14. # Import Salt Libs
  15. import salt.states.win_dns_client as win_dns_client
  16. class WinDnsClientTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Validate the win_dns_client state
  19. '''
  20. def setup_loader_modules(self):
  21. return {win_dns_client: {}}
  22. def test_dns_exists(self):
  23. '''
  24. Test to configure the DNS server list in the specified interface
  25. '''
  26. ret = {'name': 'salt',
  27. 'changes': {},
  28. 'result': False,
  29. 'comment': ''}
  30. with patch.dict(win_dns_client.__opts__, {"test": False}):
  31. ret.update({'changes': {'Servers Added': [],
  32. 'Servers Removed': [],
  33. 'Servers Reordered': []},
  34. 'comment': 'servers entry is not a list !'})
  35. self.assertDictEqual(win_dns_client.dns_exists('salt'), ret)
  36. mock = MagicMock(return_value=[2, 'salt'])
  37. with patch.dict(win_dns_client.__salt__,
  38. {'win_dns_client.get_dns_servers': mock}):
  39. ret.update({'changes': {}, 'comment': repr([2, 'salt']) + " are already"
  40. " configured", 'result': True})
  41. self.assertDictEqual(win_dns_client.dns_exists('salt',
  42. [2, 'salt']),
  43. ret)
  44. mock = MagicMock(side_effect=[False, True, True])
  45. with patch.dict(win_dns_client.__salt__,
  46. {'win_dns_client.add_dns': mock}):
  47. ret.update({'comment': 'Failed to add 1 as DNS'
  48. ' server number 1', 'result': False})
  49. self.assertDictEqual(win_dns_client.dns_exists('salt',
  50. [1, 'salt']
  51. ), ret)
  52. mock = MagicMock(return_value=False)
  53. with patch.dict(win_dns_client.__salt__,
  54. {'win_dns_client.rm_dns': mock}):
  55. ret.update({'changes': {'Servers Added': ['a'],
  56. 'Servers Removed': [],
  57. 'Servers Reordered': []},
  58. 'comment': 'Failed to remove 2 from DNS'
  59. ' server list'})
  60. self.assertDictEqual(win_dns_client.dns_exists('salt',
  61. ['a'],
  62. 'a',
  63. 1),
  64. ret)
  65. ret.update({'comment': 'DNS Servers have been updated',
  66. 'result': True})
  67. self.assertDictEqual(win_dns_client.dns_exists('salt',
  68. ['a']), ret)
  69. def test_dns_dhcp(self):
  70. '''
  71. Test to configure the DNS server list from DHCP Server
  72. '''
  73. ret = {'name': 'salt',
  74. 'changes': {},
  75. 'result': True,
  76. 'comment': ''}
  77. mock = MagicMock(side_effect=['dhcp', 'salt', 'salt'])
  78. with patch.dict(win_dns_client.__salt__,
  79. {'win_dns_client.get_dns_config': mock}):
  80. ret.update({'comment': 'Local Area Connection already configured'
  81. ' with DNS from DHCP'})
  82. self.assertDictEqual(win_dns_client.dns_dhcp('salt'), ret)
  83. with patch.dict(win_dns_client.__opts__, {"test": True}):
  84. ret.update({'comment': '', 'result': None,
  85. 'changes': {'dns': 'configured from DHCP'}})
  86. self.assertDictEqual(win_dns_client.dns_dhcp('salt'), ret)
  87. with patch.dict(win_dns_client.__opts__, {"test": False}):
  88. mock = MagicMock(return_value=True)
  89. with patch.dict(win_dns_client.__salt__,
  90. {'win_dns_client.dns_dhcp': mock}):
  91. ret.update({'result': True})
  92. self.assertDictEqual(win_dns_client.dns_dhcp('salt'), ret)
  93. def test_primary_suffix(self):
  94. '''
  95. Test to configure the global primary DNS suffix of a DHCP client.
  96. '''
  97. ret = {'name': 'salt',
  98. 'changes': {},
  99. 'result': False,
  100. 'comment': ''}
  101. ret.update({'comment': "'updates' must be a boolean value"})
  102. self.assertDictEqual(win_dns_client.primary_suffix('salt', updates='a'
  103. ), ret)
  104. mock = MagicMock(side_effect=[{'vdata': 'a'}, {'vdata': False}, {'vdata': 'b'}, {'vdata': False}])
  105. with patch.dict(win_dns_client.__salt__, {'reg.read_value': mock}):
  106. ret.update({'comment': 'No changes needed', 'result': True})
  107. self.assertDictEqual(win_dns_client.primary_suffix('salt', 'a'),
  108. ret)
  109. mock = MagicMock(return_value=True)
  110. with patch.dict(win_dns_client.__salt__, {'reg.set_value': mock}):
  111. ret.update({'changes': {'new': {'suffix': 'a'},
  112. 'old': {'suffix': 'b'}},
  113. 'comment': 'Updated primary DNS suffix (a)'})
  114. self.assertDictEqual(win_dns_client.primary_suffix('salt',
  115. 'a'), ret)