test_host.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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.runtime 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. log = logging.getLogger(__name__)
  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. 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)