1
0

test_host.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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.mixins import SaltReturnAssertsMixin
  14. from tests.support.runtests import RUNTIME_VARS
  15. log = logging.getLogger(__name__)
  16. @pytest.mark.windows_whitelisted
  17. class HostTest(ModuleCase, SaltReturnAssertsMixin):
  18. """
  19. Validate the host state
  20. """
  21. @classmethod
  22. def setUpClass(cls):
  23. cls.hosts_file = os.path.join(RUNTIME_VARS.TMP, "hosts")
  24. def __clear_hosts(self):
  25. """
  26. Delete the tmp hosts file
  27. """
  28. if os.path.isfile(self.hosts_file):
  29. os.remove(self.hosts_file)
  30. def setUp(self):
  31. shutil.copyfile(os.path.join(RUNTIME_VARS.FILES, "hosts"), self.hosts_file)
  32. self.addCleanup(self.__clear_hosts)
  33. super(HostTest, self).setUp()
  34. @pytest.mark.slow_test(seconds=30) # Test takes >10 and <=30 seconds
  35. def test_present(self):
  36. """
  37. host.present
  38. """
  39. name = "spam.bacon"
  40. ip = "10.10.10.10"
  41. ret = self.run_state("host.present", name=name, ip=ip)
  42. self.assertSaltTrueReturn(ret)
  43. with salt.utils.files.fopen(self.hosts_file) as fp_:
  44. output = salt.utils.stringutils.to_unicode(fp_.read())
  45. self.assertIn("{0}\t\t{1}".format(ip, name), output)