test_profile.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 profile style of config"""
  19. import unittest
  20. from mock import patch, MagicMock, Mock
  21. import gitbuildsys.conf
  22. from gitbuildsys.errors import ConfigError
  23. from test_config import Fixture
  24. from test_passwdx import FakeFile
  25. def get_profile():
  26. '''get current profile to test'''
  27. reload(gitbuildsys.conf)
  28. return gitbuildsys.conf.configmgr.get_current_profile()
  29. class ProfileStyleTest(unittest.TestCase):
  30. '''Test for profile oriented config'''
  31. @Fixture(home='profile.ini')
  32. def test_profile_api(self):
  33. 'test get obs api'
  34. self.assertEquals('https://api.tz/path', get_profile().obs.url)
  35. @Fixture(home='profile.ini')
  36. def test_api_inherit_auth(self):
  37. 'test api can inherit auto from parent profile section'
  38. self.assertEquals('https://Alice:secret@api.tz/path',
  39. get_profile().obs.url.full)
  40. @Fixture(home='profile_only_has_api.ini')
  41. def test_api_auth_can_be_overwrite(self):
  42. 'test api auth can be overwrite'
  43. self.assertEquals('https://Bob:classified@api.tz/path',
  44. get_profile().obs.url.full)
  45. @Fixture(home='profile.ini')
  46. def test_profile_repos_in_order(self):
  47. 'repos must be in same order as they are write in config'
  48. self.assertEquals(['https://repo/ia32/main',
  49. 'https://repo/ia32/non-oss',
  50. 'https://repo/ia32/base',
  51. '/local/path'],
  52. [i.url for i in get_profile().repos])
  53. @Fixture(home='profile.ini')
  54. def test_repo_inherit_auth(self):
  55. 'test repo can inherit auth from parent section'
  56. self.assertEquals('https://Alice:secret@repo/ia32/main',
  57. get_profile().repos[0].url.full)
  58. @Fixture(home='profile.ini')
  59. def test_repo_overwrite_auth(self):
  60. 'test repo auth can be overwrite'
  61. self.assertEquals('https://Bob:classified@repo/ia32/base',
  62. get_profile().repos[2].url.full)
  63. @Fixture(home='bug387_inherit_only_user.ini')
  64. def test_inherit_only_user(self):
  65. 'test inherit only user from parent'
  66. self.assertEquals('https://tester:secret@repo',
  67. get_profile().repos[0].url.full)
  68. self.assertEquals('https://tester:secret@obs',
  69. get_profile().obs.url.full)
  70. @Fixture(home='bug387_inherit_only_passwdx.ini')
  71. def test_inherit_only_passwdx(self):
  72. 'test inherit only password from parent'
  73. self.assertEquals('https://tester:secret@repo',
  74. get_profile().repos[0].url.full)
  75. self.assertEquals('https://tester:secret@obs',
  76. get_profile().obs.url.full)
  77. @Fixture(home='bug387_only_password_no_user.ini')
  78. def test_only_password_no_user(self):
  79. 'test only password no user'
  80. self.assertRaises(ConfigError, get_profile)
  81. @Fixture(home='bug387_inline_auth_has_the_highest_priority.ini')
  82. def test_inline_highest_priority(self):
  83. 'test inline auth has the highest priority'
  84. self.assertEquals('https://this:inline-pwd@obs',
  85. get_profile().obs.url.full)
  86. @Fixture(home='no_such_profile_section_name.ini')
  87. def test_no_such_profile(self):
  88. 'test profile name does not exist'
  89. self.assertRaises(ConfigError, get_profile)
  90. @Fixture(home='empty_profile.ini')
  91. def test_empty_profile(self):
  92. 'test get a empty profile'
  93. profile = get_profile()
  94. self.assertEquals(None, profile.obs)
  95. self.assertEquals([], profile.repos)
  96. @Fixture(home='profile.ini')
  97. def test_local_repo_need_not_auth(self):
  98. '''test local path needn't auth info'''
  99. self.assertEquals('/local/path', get_profile().repos[3].url.full)
  100. @Fixture(home='profile.ini')
  101. def test_obs_base_project(self):
  102. 'test read base project from conf'
  103. self.assertEquals('base', get_profile().obs.base)
  104. @Fixture(home='profile.ini')
  105. def test_obs_target_project(self):
  106. 'test read target project from conf'
  107. self.assertEquals('target', get_profile().obs.target)
  108. @patch('gitbuildsys.conf.open', MagicMock(), create=True)
  109. @patch('gitbuildsys.conf.os.rename', Mock())
  110. class SubcommandStyleTest(unittest.TestCase):
  111. '''test for subcommand oriented config'''
  112. @Fixture(home='subcommand.ini')
  113. def test_api(self):
  114. 'test obs api'
  115. self.assertEquals('https://api/build/server', get_profile().obs.url)
  116. @Fixture(home='subcommand.ini')
  117. def test_api_auth(self):
  118. 'test api auth'
  119. self.assertEquals('https://Alice:secret@api/build/server',
  120. get_profile().obs.url.full)
  121. @Fixture(home='subcommand.ini')
  122. def test_repos_in_order(self):
  123. 'repos list must be in the same order as they are write in config'
  124. self.assertEquals(['https://repo1/path',
  125. 'https://repo2/path',
  126. '/local/path/repo'],
  127. [i.url for i in get_profile().repos])
  128. @Fixture(home='subcommand.ini')
  129. def test_repo_auth(self):
  130. 'test repo auth'
  131. self.assertEquals('https://Alice:secret@repo1/path',
  132. get_profile().repos[0].url.full)
  133. @patch('gitbuildsys.conf.open', create=True)
  134. class ConvertTest(unittest.TestCase):
  135. 'Test convert subcommand to profile'
  136. @Fixture(home='subcommand.ini')
  137. def test_convert(self, fake_open):
  138. 'test convert'
  139. conf = FakeFile()
  140. fake_open.return_value = conf
  141. get_profile()
  142. self.assertEquals(conf.getvalue(), '''[general]
  143. profile = profile.current
  144. [obs.remotebuild]
  145. url = https://api/build/server
  146. user = Alice
  147. passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
  148. base_prj = Main
  149. target_prj = Target
  150. [repo.repo1]
  151. url = https://repo1/path
  152. user = Alice
  153. passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
  154. [repo.repo2]
  155. url = https://repo2/path
  156. user = Alice
  157. passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
  158. [repo.repo3]
  159. url = /local/path/repo
  160. [profile.current]
  161. obs = obs.remotebuild
  162. repos = repo.repo1, repo.repo2, repo.repo3
  163. ''')
  164. if __name__ == '__main__':
  165. unittest.main()