test_dnsutil.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Nicole Thomas <nicole@saltstack.com>
  4. '''
  5. # Import python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import logging
  8. # Import Salt Testing Libs
  9. from tests.support.unit import TestCase, skipIf
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch,
  13. mock_open,
  14. )
  15. # Import Salt Libs
  16. import salt.modules.dnsutil as dnsutil
  17. import salt.utils.stringutils
  18. log = logging.getLogger(__name__)
  19. mock_hosts_file = salt.utils.stringutils.to_str(
  20. '##\n'
  21. '# Host Database\n'
  22. '#\n'
  23. '# localhost is used to configure the loopback interface\n'
  24. '# when the system is booting. Do not change this entry.\n'
  25. '##\n'
  26. '127.0.0.1 localhost\n'
  27. '255.255.255.255 broadcasthost\n'
  28. '::1 localhost\n'
  29. 'fe80::1%lo0 localhost')
  30. mock_hosts_file_rtn = {'::1': ['localhost'], '255.255.255.255': ['broadcasthost'],
  31. '127.0.0.1': ['localhost'], 'fe80::1%lo0': ['localhost']}
  32. mock_soa_zone = salt.utils.stringutils.to_str(
  33. '$TTL 3D\n'
  34. '@ IN SOA land-5.com. root.land-5.com. (\n'
  35. '199609203 ; Serial\n'
  36. '28800 ; Refresh\n'
  37. '7200 ; Retry\n'
  38. '604800 ; Expire\n'
  39. '86400) ; Minimum TTL\n'
  40. 'NS land-5.com.\n\n'
  41. '1 PTR localhost.')
  42. mock_writes_list = salt.utils.data.decode([
  43. '##\n',
  44. '# Host Database\n',
  45. '#\n',
  46. '# localhost is used to configure the loopback interface\n',
  47. '# when the system is booting. Do not change this entry.\n',
  48. '##\n',
  49. '127.0.0.1 localhost',
  50. '\n',
  51. '255.255.255.255 broadcasthost',
  52. '\n',
  53. '::1 localhost',
  54. '\n',
  55. 'fe80::1%lo0 localhost',
  56. '\n'
  57. ], to_str=True
  58. )
  59. class DNSUtilTestCase(TestCase):
  60. def test_parse_hosts(self):
  61. with patch('salt.utils.files.fopen', mock_open(read_data=mock_hosts_file)):
  62. self.assertEqual(dnsutil.parse_hosts(), {'::1': ['localhost'],
  63. '255.255.255.255': ['broadcasthost'],
  64. '127.0.0.1': ['localhost'],
  65. 'fe80::1%lo0': ['localhost']})
  66. def test_hosts_append(self):
  67. with patch('salt.utils.files.fopen', mock_open(read_data=mock_hosts_file)) as m_open, \
  68. patch('salt.modules.dnsutil.parse_hosts', MagicMock(return_value=mock_hosts_file_rtn)):
  69. dnsutil.hosts_append('/etc/hosts', '127.0.0.1', 'ad1.yuk.co,ad2.yuk.co')
  70. writes = m_open.write_calls()
  71. # We should have called .write() only once, with the expected
  72. # content
  73. num_writes = len(writes)
  74. assert num_writes == 1, num_writes
  75. expected = salt.utils.stringutils.to_str('\n127.0.0.1 ad1.yuk.co ad2.yuk.co')
  76. assert writes[0] == expected, writes[0]
  77. def test_hosts_remove(self):
  78. to_remove = 'ad1.yuk.co'
  79. new_mock_file = mock_hosts_file + '\n127.0.0.1 ' + to_remove + '\n'
  80. with patch('salt.utils.files.fopen', mock_open(read_data=new_mock_file)) as m_open:
  81. dnsutil.hosts_remove('/etc/hosts', to_remove)
  82. writes = m_open.write_calls()
  83. assert writes == mock_writes_list, writes
  84. @skipIf(True, 'Waiting on bug report fixes')
  85. def test_parse_zone(self):
  86. with patch('salt.utils.files.fopen', mock_open(read_data=mock_soa_zone)):
  87. log.debug(mock_soa_zone)
  88. log.debug(dnsutil.parse_zone('/var/lib/named/example.com.zone'))
  89. def test_to_seconds_hour(self):
  90. self.assertEqual(dnsutil._to_seconds('4H'), 14400,
  91. msg='Did not detect valid hours as invalid')
  92. def test_to_seconds_day(self):
  93. self.assertEqual(dnsutil._to_seconds('1D'), 86400,
  94. msg='Did not detect valid day as invalid')
  95. def test_to_seconds_week(self):
  96. self.assertEqual(dnsutil._to_seconds('2W'), 604800,
  97. msg='Did not set time greater than one week to one week')
  98. def test_to_seconds_empty(self):
  99. self.assertEqual(dnsutil._to_seconds(''), 604800,
  100. msg='Did not set empty time to one week')
  101. def test_to_seconds_large(self):
  102. self.assertEqual(dnsutil._to_seconds('604801'), 604800,
  103. msg='Did not set time greater than one week to one week')