test_utils.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. # Debug was called for each item we expect
  33. self.assertEqual(
  34. len(self.debug_all_params),
  35. _il.debug.call_count)
  36. # Each item we expect was logged
  37. six.assertCountEqual(
  38. self,
  39. self.expected,
  40. _il.debug.call_args_list)
  41. def test_utils_debug_env_some_debugged(self):
  42. """Verify debug_env skips non-wanted env variables"""
  43. debug_some_params = copy.deepcopy(self.debug_all_params)
  44. # This will not be logged by debug_env
  45. debug_some_params['MG_FRBBR'] = "SKIPPED"
  46. with mock.patch('ooinstall.utils.installer_log') as _il:
  47. debug_env(debug_some_params)
  48. # The actual number of debug calls was less than the
  49. # number of items passed to debug_env
  50. self.assertLess(
  51. _il.debug.call_count,
  52. len(debug_some_params))
  53. six.assertCountEqual(
  54. self,
  55. self.expected,
  56. _il.debug.call_args_list)
  57. ######################################################################
  58. def test_utils_is_valid_hostname_invalid(self):
  59. """Verify is_valid_hostname can detect None or too-long hostnames"""
  60. # A hostname that's empty, None, or more than 255 chars is invalid
  61. empty_hostname = ''
  62. res = is_valid_hostname(empty_hostname)
  63. self.assertFalse(res)
  64. none_hostname = None
  65. res = is_valid_hostname(none_hostname)
  66. self.assertFalse(res)
  67. too_long_hostname = "a" * 256
  68. res = is_valid_hostname(too_long_hostname)
  69. self.assertFalse(res)
  70. def test_utils_is_valid_hostname_ends_with_dot(self):
  71. """Verify is_valid_hostname can parse hostnames with trailing periods"""
  72. hostname = "foo.example.com."
  73. res = is_valid_hostname(hostname)
  74. self.assertTrue(res)
  75. def test_utils_is_valid_hostname_normal_hostname(self):
  76. """Verify is_valid_hostname can parse regular hostnames"""
  77. hostname = "foo.example.com"
  78. res = is_valid_hostname(hostname)
  79. self.assertTrue(res)