test_config.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/python -tt
  2. # vim: ai ts=4 sts=4 et sw=4
  3. #
  4. # Copyright (c) 2012 Intel, Inc.
  5. #
  6. # This program is free software; you can redistribute it and/or modify it
  7. # under the terms of the GNU General Public License as published by the Free
  8. # Software Foundation; version 2 of the License
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. # for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc., 59
  17. # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. """Functional tests for GBS config"""
  19. import os
  20. import unittest
  21. from mock import patch
  22. from gitbuildsys.errors import ConfigError
  23. import gitbuildsys.conf
  24. FILE_DIRNAME = os.path.dirname(os.path.abspath(__file__))
  25. class Fixture(object):
  26. '''test fixture for testing config'''
  27. PATH = os.path.join(FILE_DIRNAME, 'testdata', 'ini')
  28. ETC = '/etc/gbs.conf'
  29. HOME = '~/.gbs.conf'
  30. PROJECT = '.gbs.conf'
  31. def __init__(self, etc=None, home=None, project=None):
  32. self.fake_files = {self.ETC: etc,
  33. self.HOME: home,
  34. self.PROJECT: project,
  35. }
  36. self.real_exists = os.path.exists
  37. self.real_abspath = os.path.abspath
  38. self.real_expanduser = os.path.expanduser
  39. def fake_exists(self, path):
  40. '''return True if corresponding fixture specified'''
  41. return bool(self.fake_files[path]) if path in self.fake_files \
  42. else self.real_exists(path)
  43. def fake_abspath(self, path):
  44. '''return itself if it's match fixture name'''
  45. return path if path in self.fake_files else self.real_abspath(path)
  46. def fake_expanduser(self, path):
  47. '''return itself if it's match fixture name'''
  48. return path if path in self.fake_files else self.real_expanduser(path)
  49. def fake_open(self, name, *args):
  50. '''open corresponding fixture file and return'''
  51. return open(os.path.join(self.PATH, self.fake_files[name])) \
  52. if name in self.fake_files \
  53. else open(name, *args)
  54. def __call__(self, func):
  55. '''decorator to setup fixtures'''
  56. patchers = [
  57. patch('gitbuildsys.conf.os.path.exists', self.fake_exists),
  58. patch('gitbuildsys.conf.os.path.expanduser', self.fake_expanduser),
  59. patch('gitbuildsys.conf.os.path.abspath', self.fake_abspath),
  60. patch('ConfigParser.open', self.fake_open, create=True),
  61. ]
  62. for patcher in patchers:
  63. func = patcher(func)
  64. return func
  65. class ConfigGettingTest(unittest.TestCase):
  66. '''TestCase for config'''
  67. @staticmethod
  68. def get(section, option):
  69. '''get section.option from config'''
  70. # configmgr is a global variable, reload to recreate it
  71. # otherwise fixtures only take effect in the first time
  72. reload(gitbuildsys.conf)
  73. return gitbuildsys.conf.configmgr.get(option, section)
  74. @staticmethod
  75. def add_conf(fpath):
  76. '''get section.option from config'''
  77. # configmgr is a global variable, reload to recreate it
  78. # otherwise fixtures only take effect in the first time
  79. reload(gitbuildsys.conf)
  80. return gitbuildsys.conf.configmgr.add_conf(fpath)
  81. @Fixture(project='project1.ini')
  82. def test_no_such_section(self):
  83. '''test no such section'''
  84. self.assertRaises(ConfigError,
  85. self.get, 'not_exists_section', 'key')
  86. @Fixture(project='project1.ini')
  87. def test_no_such_option(self):
  88. '''test no such option'''
  89. self.assertRaises(ConfigError,
  90. self.get, 'section', 'not_exists_option')
  91. @Fixture(project='project1.ini')
  92. def test_simple_get(self):
  93. '''get value when one config file provides'''
  94. self.assertEqual('projv2', self.get('section', 'proj_only_key'))
  95. @Fixture(home='home1.ini', project='project1.ini')
  96. def test_inherit(self):
  97. '''value can be inherit from two levels'''
  98. self.assertEqual('homev2', self.get('section', 'home_only_key'))
  99. @Fixture(home='home1.ini', project='project1.ini')
  100. def test_overwrite(self):
  101. '''value can be overwrite if name is the same'''
  102. self.assertEqual('projv1', self.get('section', 'common_key'))
  103. @Fixture(home='home1.ini')
  104. def test_default_value(self):
  105. 'test get hardcode default value '
  106. self.assertEquals('/var/tmp', self.get('general', 'tmpdir'))
  107. @Fixture(home='without_section_header.ini')
  108. def test_invalid_ini(self):
  109. 'test invalid ini'
  110. self.assertRaises(ConfigError, reload, gitbuildsys.conf)
  111. @Fixture(home='invalid_continuation_line.ini')
  112. def test_invalid_continuation_line(self):
  113. 'test invalid cointinuation line'
  114. self.assertRaises(ConfigError, reload, gitbuildsys.conf)
  115. @Fixture(home='interpolation.ini')
  116. def test_interpolation(self):
  117. 'test interpolation is supported'
  118. self.assertEquals('abc/def', self.get('remote', 'target'))
  119. @Fixture(home='home1.ini')
  120. def test_addconf(self):
  121. '''value can be inherit from two levels'''
  122. self.add_conf(os.path.join(FILE_DIRNAME, 'testdata', 'ini', 'project1.ini'))
  123. self.assertEqual('homev2', self.get('section', 'home_only_key'))
  124. if __name__ == '__main__':
  125. unittest.main()