test_puppet.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 os
  17. from unittest import TestCase
  18. from ..test_base import PackstackTestCaseMixin
  19. from packstack.installer.exceptions import PuppetError
  20. from packstack.modules.puppet import validate_logfile
  21. class PuppetTestCase(PackstackTestCaseMixin, TestCase):
  22. def test_validate_logfile(self):
  23. """Test packstack.modules.validate_logfile."""
  24. filename = os.path.join(self.tempdir, "puppet.log")
  25. # test valid run
  26. with open(filename, "w") as fp:
  27. fp.write("Everything went ok")
  28. validate_logfile(filename)
  29. # test invalid run
  30. with open(filename, "w") as fp:
  31. fp.write("No matching value for selector param 'Fedora' ...")
  32. self.assertRaises(PuppetError, validate_logfile, filename)
  33. # test run with error exception
  34. with open(filename, "w") as fp:
  35. err = ("err: Could not prefetch database_grant provider 'mysql': "
  36. "Execution of '/usr/bin/mysql --defaults-file=/root/.my.cnf"
  37. " mysql -Be describe user' returned 1: Could not open "
  38. "required defaults file: /root/.my.cnf")
  39. fp.write(err)
  40. validate_logfile(filename)
  41. # test surrogate
  42. with open(filename, "w") as fp:
  43. err = ("err: /Stage[main]/Vswitch::Ovs/Package[openvswitch]/ensure"
  44. ": change from absent to present failed: Execution of "
  45. "'/usr/bin/yum -d 0 -e 0 -y install openvswitch' returned "
  46. "1: Error: Nothing to do")
  47. fp.write(err)
  48. self.assertRaises(PuppetError, validate_logfile, filename)
  49. try:
  50. validate_logfile(filename)
  51. except PuppetError as ex:
  52. ex_msg = str(ex)
  53. sr_msg = ("Package openvswitch has not been found in enabled Yum "
  54. "repos")
  55. assert sr_msg in ex_msg