test_utils.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # -*- coding: utf-8 -*-
  2. # vim: tabstop=4 shiftwidth=4 softtabstop=4
  3. # Copyright 2013, Red Hat, Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. # not use this file except in compliance with the License. You may obtain
  7. # a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. # License for the specific language governing permissions and limitations
  15. # under the License.
  16. """
  17. Test cases for packstack.installer.utils module.
  18. """
  19. import shutil
  20. import tempfile
  21. from unittest import TestCase
  22. from ..test_base import FakePopen
  23. from ..test_base import PackstackTestCaseMixin
  24. from packstack.installer.utils import *
  25. from packstack.installer.utils.strings import STR_MASK
  26. from packstack.installer.exceptions import ExecuteRuntimeError
  27. cnt = 0
  28. class ParameterTestCase(PackstackTestCaseMixin, TestCase):
  29. def setUp(self):
  30. # Creating a temp directory that can be used by tests
  31. self.tempdir = tempfile.mkdtemp()
  32. FakePopen.register('echo "this is test"',
  33. stdout='this is test')
  34. def tearDown(self):
  35. # remove the temp directory
  36. shutil.rmtree(self.tempdir)
  37. def test_sorteddict(self):
  38. """Test packstack.installer.utils.datastructures.SortedDict."""
  39. sdict = SortedDict()
  40. sdict['1'] = 1
  41. sdict['2'] = 2
  42. sdict.update(SortedDict([('3', 3), ('4', 4), ('5', 5)]))
  43. self.assertListEqual(sdict.keys(), ['1', '2', '3', '4', '5'])
  44. self.assertListEqual(sdict.values(), [1, 2, 3, 4, 5])
  45. def test_retry(self):
  46. """Test packstack.installer.utils.decorators.retry."""
  47. @retry(count=3, delay=0, retry_on=ValueError)
  48. def test_sum():
  49. global cnt
  50. cnt += 1
  51. raise ValueError
  52. global cnt
  53. cnt = 0
  54. try:
  55. test_sum()
  56. except ValueError:
  57. pass
  58. self.assertEqual(cnt, 4)
  59. self.assertRaises(ValueError, test_sum)
  60. def test_network(self):
  61. """Test packstack.installer.utils.network functions."""
  62. self.assertIn(host2ip('localhost', allow_localhost=True),
  63. ['127.0.0.1', '::1'])
  64. def test_shell(self):
  65. """Test packstack.installer.utils.shell functions."""
  66. rc, out = execute(['echo', 'this is test'])
  67. self.assertEqual(out.strip(), 'this is test')
  68. rc, out = execute('echo "this is test"', use_shell=True)
  69. self.assertEqual(out.strip(), 'this is test')
  70. try:
  71. execute('echo "mask the password" && exit 1',
  72. use_shell=True, mask_list=['password'])
  73. raise AssertionError('Masked execution failed.')
  74. except ExecuteRuntimeError as ex:
  75. should_be = ('Failed to execute command, stdout: mask the %s\n\n'
  76. 'stderr: ' % STR_MASK)
  77. self.assertEqual(str(ex), should_be)
  78. script = ScriptRunner()
  79. script.append('echo "this is test"')
  80. rc, out = script.execute()
  81. self.assertEqual(out.strip(), 'this is test')
  82. def test_strings(self):
  83. """Test packstack.installer.utils.strings functions."""
  84. self.assertEqual(color_text('test text', 'red'),
  85. '\033[0;31mtest text\033[0m')
  86. self.assertEqual(mask_string('test text', mask_list=['text']),
  87. 'test %s' % STR_MASK)
  88. masked = mask_string("test '\\''text'\\''",
  89. mask_list=["'text'"],
  90. replace_list=[("'", "'\\''")])
  91. self.assertEqual(masked, 'test %s' % STR_MASK)
  92. def test_shortcuts(self):
  93. """Test packstack.installer.utils.shortcuts functions."""
  94. conf = {"A_HOST": "1.1.1.1", "B_HOSTS": "2.2.2.2,1.1.1.1",
  95. "C_HOSTS": "3.3.3.3/vdc"}
  96. hostlist = list(hosts(conf))
  97. hostlist.sort()
  98. self.assertEqual(['1.1.1.1', '2.2.2.2', '3.3.3.3'], hostlist)