test_host.py 1.5 KB

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