test_dnsmasq.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Rupesh Tare <rupesht@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. import textwrap
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.unit import TestCase
  12. from tests.support.mock import (
  13. mock_open,
  14. MagicMock,
  15. patch,
  16. )
  17. # Import Salt Libs
  18. from salt.exceptions import CommandExecutionError
  19. import salt.modules.dnsmasq as dnsmasq
  20. class DnsmasqTestCase(TestCase, LoaderModuleMockMixin):
  21. '''
  22. TestCase for the salt.modules.at module
  23. '''
  24. def setup_loader_modules(self):
  25. return {dnsmasq: {}}
  26. def test_version(self):
  27. '''
  28. test to show installed version of dnsmasq.
  29. '''
  30. mock = MagicMock(return_value='A B C')
  31. with patch.dict(dnsmasq.__salt__, {'cmd.run': mock}):
  32. self.assertEqual(dnsmasq.version(), "C")
  33. def test_fullversion(self):
  34. '''
  35. Test to Show installed version of dnsmasq and compile options.
  36. '''
  37. mock = MagicMock(return_value='A B C\nD E F G H I')
  38. with patch.dict(dnsmasq.__salt__, {'cmd.run': mock}):
  39. self.assertDictEqual(dnsmasq.fullversion(),
  40. {'version': 'C',
  41. 'compile options': ['G', 'H', 'I']})
  42. def test_set_config(self):
  43. '''
  44. test to show installed version of dnsmasq.
  45. '''
  46. mock = MagicMock(return_value={'conf-dir': 'A'})
  47. with patch.object(dnsmasq, 'get_config', mock):
  48. mock = MagicMock(return_value=['.', '~', 'bak', '#'])
  49. with patch.object(os, 'listdir', mock):
  50. self.assertDictEqual(dnsmasq.set_config(), {})
  51. def test_set_config_filter_pub_kwargs(self):
  52. '''
  53. Test that the kwargs returned from running the set_config function
  54. do not contain the __pub that may have been passed through in **kwargs.
  55. '''
  56. with patch('salt.modules.dnsmasq.get_config', MagicMock(return_value={'conf-dir': 'A'})):
  57. mock_domain = 'local'
  58. mock_address = '/some-test-address.local/8.8.4.4'
  59. with patch.dict(dnsmasq.__salt__, {'file.append': MagicMock()}):
  60. ret = dnsmasq.set_config(follow=False,
  61. domain=mock_domain,
  62. address=mock_address,
  63. __pub_pid=8184,
  64. __pub_jid=20161101194639387946,
  65. __pub_tgt='salt-call')
  66. self.assertEqual(ret, {'domain': mock_domain, 'address': mock_address})
  67. def test_get_config(self):
  68. '''
  69. test to dumps all options from the config file.
  70. '''
  71. mock = MagicMock(return_value={'conf-dir': 'A'})
  72. with patch.object(dnsmasq, 'get_config', mock):
  73. mock = MagicMock(return_value=['.', '~', 'bak', '#'])
  74. with patch.object(os, 'listdir', mock):
  75. self.assertDictEqual(dnsmasq.get_config(), {'conf-dir': 'A'})
  76. def test_parse_dnsmasq_no_file(self):
  77. '''
  78. Tests that a CommandExecutionError is when a filename that doesn't exist is
  79. passed in.
  80. '''
  81. self.assertRaises(CommandExecutionError, dnsmasq._parse_dnamasq, 'filename')
  82. def test_parse_dnamasq(self):
  83. '''
  84. test for generic function for parsing dnsmasq files including includes.
  85. '''
  86. with patch('os.path.isfile', MagicMock(return_value=True)):
  87. text_file_data = textwrap.dedent('''\
  88. line here
  89. second line
  90. A=B
  91. #''')
  92. with patch('salt.utils.files.fopen',
  93. mock_open(read_data=text_file_data)):
  94. self.assertDictEqual(dnsmasq._parse_dnamasq('filename'),
  95. {'A': 'B',
  96. 'unparsed': ['line here\n',
  97. 'second line\n']})