1
0

test_host.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  2. '''
  3. tests for host state
  4. '''
  5. # Import python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. import shutil
  9. import logging
  10. # Import Salt Testing libs
  11. from tests.support.runtests import RUNTIME_VARS
  12. from tests.support.case import ModuleCase
  13. from tests.support.mixins import SaltReturnAssertsMixin
  14. # Import salt libs
  15. import salt.utils.files
  16. import salt.utils.stringutils
  17. import pytest
  18. log = logging.getLogger(__name__)
  19. @pytest.mark.windows_whitelisted
  20. class HostTest(ModuleCase, SaltReturnAssertsMixin):
  21. '''
  22. Validate the host state
  23. '''
  24. @classmethod
  25. def setUpClass(cls):
  26. cls.hosts_file = os.path.join(RUNTIME_VARS.TMP, 'hosts')
  27. def __clear_hosts(self):
  28. '''
  29. Delete the tmp hosts file
  30. '''
  31. if os.path.isfile(self.hosts_file):
  32. os.remove(self.hosts_file)
  33. def setUp(self):
  34. shutil.copyfile(os.path.join(RUNTIME_VARS.FILES, 'hosts'), self.hosts_file)
  35. self.addCleanup(self.__clear_hosts)
  36. super(HostTest, self).setUp()
  37. def test_present(self):
  38. '''
  39. host.present
  40. '''
  41. name = 'spam.bacon'
  42. ip = '10.10.10.10'
  43. ret = self.run_state('host.present', name=name, ip=ip)
  44. self.assertSaltTrueReturn(ret)
  45. with salt.utils.files.fopen(self.hosts_file) as fp_:
  46. output = salt.utils.stringutils.to_unicode(fp_.read())
  47. self.assertIn('{0}\t\t{1}'.format(ip, name), output)