test_sequences.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. import sys
  17. import StringIO
  18. from unittest import TestCase
  19. from packstack.installer import utils
  20. from packstack.installer.core.sequences import *
  21. from ..test_base import PackstackTestCaseMixin
  22. class StepTestCase(PackstackTestCaseMixin, TestCase):
  23. def setUp(self):
  24. super(StepTestCase, self).setUp()
  25. self._stdout = sys.stdout
  26. sys.stdout = StringIO.StringIO()
  27. def tearDown(self):
  28. super(StepTestCase, self).tearDown()
  29. sys.stdout = self._stdout
  30. def test_run(self):
  31. """
  32. Test packstack.instaler.core.sequences.Step run.
  33. """
  34. def func(config, messages):
  35. if 'test' not in config:
  36. raise AssertionError('Missing config value.')
  37. step = Step('test', func, title='Running test')
  38. step.run(config={'test': 'test'})
  39. contents = sys.stdout.getvalue()
  40. state = '[ %s ]\n' % utils.color_text('DONE', 'green')
  41. if(not contents.startswith('Running test') or
  42. not contents.endswith(state)):
  43. raise AssertionError('Step run test failed: %s' % contents)
  44. class SequenceTestCase(PackstackTestCaseMixin, TestCase):
  45. def setUp(self):
  46. super(SequenceTestCase, self).setUp()
  47. self._stdout = sys.stdout
  48. sys.stdout = StringIO.StringIO()
  49. self.steps = [{'name': '1', 'function': lambda x, y: True,
  50. 'title': 'Step 1'},
  51. {'name': '2', 'function': lambda x, y: True,
  52. 'title': 'Step 2'},
  53. {'name': '3', 'function': lambda x, y: True,
  54. 'title': 'Step 3'}]
  55. self.seq = Sequence('test', self.steps, condition='test',
  56. cond_match='test')
  57. def tearDown(self):
  58. super(SequenceTestCase, self).tearDown()
  59. sys.stdout = self._stdout
  60. def test_run(self):
  61. """
  62. Test packstack.instaler.core.sequences.Sequence run.
  63. """
  64. self.seq.run()
  65. contents = sys.stdout.getvalue()
  66. self.assertEqual(contents, '')
  67. self.seq.run(config={'test': 'test'}, step='2')
  68. contents = sys.stdout.getvalue()
  69. assert contents.startswith('Step 2')
  70. output = []
  71. self.steps.insert(0, {'title': 'Step 2'})
  72. for i in self.steps:
  73. output.append('%s\n' % utils.state_message(i['title'],
  74. 'DONE', 'green'))
  75. self.seq.run(config={'test': 'test'})
  76. contents = sys.stdout.getvalue()
  77. self.assertEqual(contents, ''.join(output))