test_utils.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """
  2. Unittests for ooinstall utils.
  3. """
  4. import six
  5. import unittest
  6. import logging
  7. import sys
  8. import copy
  9. from ooinstall.utils import debug_env, is_valid_hostname
  10. import mock
  11. class TestUtils(unittest.TestCase):
  12. """
  13. Parent unittest TestCase.
  14. """
  15. def setUp(self):
  16. self.debug_all_params = {
  17. 'OPENSHIFT_FOO': 'bar',
  18. 'ANSIBLE_FOO': 'bar',
  19. 'OO_FOO': 'bar'
  20. }
  21. self.expected = [
  22. mock.call('ANSIBLE_FOO: bar'),
  23. mock.call('OPENSHIFT_FOO: bar'),
  24. mock.call('OO_FOO: bar'),
  25. ]
  26. ######################################################################
  27. # Validate ooinstall.utils.debug_env functionality
  28. def test_utils_debug_env_all_debugged(self):
  29. """Verify debug_env debugs specific env variables"""
  30. with mock.patch('ooinstall.utils.installer_log') as _il:
  31. debug_env(self.debug_all_params)
  32. print(_il.debug.call_args_list)
  33. # Debug was called for each item we expect
  34. self.assertEqual(
  35. len(self.debug_all_params),
  36. _il.debug.call_count)
  37. # Each item we expect was logged
  38. six.assertCountEqual(
  39. self,
  40. self.expected,
  41. _il.debug.call_args_list)
  42. def test_utils_debug_env_some_debugged(self):
  43. """Verify debug_env skips non-wanted env variables"""
  44. debug_some_params = copy.deepcopy(self.debug_all_params)
  45. # This will not be logged by debug_env
  46. debug_some_params['MG_FRBBR'] = "SKIPPED"
  47. with mock.patch('ooinstall.utils.installer_log') as _il:
  48. debug_env(debug_some_params)
  49. # The actual number of debug calls was less than the
  50. # number of items passed to debug_env
  51. self.assertLess(
  52. _il.debug.call_count,
  53. len(debug_some_params))
  54. six.assertCountEqual(
  55. self,
  56. self.expected,
  57. _il.debug.call_args_list)
  58. ######################################################################
  59. def test_utils_is_valid_hostname_invalid(self):
  60. """Verify is_valid_hostname can detect None or too-long hostnames"""
  61. # A hostname that's empty, None, or more than 255 chars is invalid
  62. empty_hostname = ''
  63. res = is_valid_hostname(empty_hostname)
  64. self.assertFalse(res)
  65. none_hostname = None
  66. res = is_valid_hostname(none_hostname)
  67. self.assertFalse(res)
  68. too_long_hostname = "a" * 256
  69. res = is_valid_hostname(too_long_hostname)
  70. self.assertFalse(res)
  71. def test_utils_is_valid_hostname_ends_with_dot(self):
  72. """Verify is_valid_hostname can parse hostnames with trailing periods"""
  73. hostname = "foo.example.com."
  74. res = is_valid_hostname(hostname)
  75. self.assertTrue(res)
  76. def test_utils_is_valid_hostname_normal_hostname(self):
  77. """Verify is_valid_hostname can parse regular hostnames"""
  78. hostname = "foo.example.com"
  79. res = is_valid_hostname(hostname)
  80. self.assertTrue(res)