test_passwdx.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 setting passwdx back to config"""
  19. import unittest
  20. from StringIO import StringIO
  21. from mock import patch
  22. import gitbuildsys.conf
  23. from gitbuildsys.conf import BrainConfigParser
  24. from gitbuildsys.errors import ConfigError
  25. from test_config import Fixture
  26. class FakeFile(object):
  27. 'Fake file used to get updated config file'
  28. def __init__(self):
  29. self.buffer = StringIO()
  30. def write(self, data):
  31. 'write data into fake file'
  32. self.buffer.write(data)
  33. def close(self):
  34. 'do not close buffer, then call getvalue() to retrieve the content'
  35. def __exit__(self, *_args):
  36. 'mock with statement'
  37. def __enter__(self):
  38. 'mock with statement'
  39. return self
  40. def getvalue(self):
  41. 'get content of fake file'
  42. return self.buffer.getvalue()
  43. @patch('gitbuildsys.conf.open', create=True)
  44. class PasswdxTest(unittest.TestCase):
  45. 'Test for setting passwdx'
  46. @Fixture(home='plain_passwd.ini')
  47. def test_one_file(self, fake_open):
  48. 'test passwdx set back to one file'
  49. conf = FakeFile()
  50. fake_open.return_value = conf
  51. reload(gitbuildsys.conf)
  52. self.assertEquals('''[remotebuild]
  53. build_server = https://api
  54. passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
  55. [build]
  56. repo1.url = https://repo1
  57. repo1.passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
  58. ''', conf.getvalue())
  59. @Fixture(home='plain_passwd.ini', project='plain_passwd2.ini')
  60. def test_two_files(self, fake_open):
  61. 'test passwdx set back to two files'
  62. confs = [FakeFile(), FakeFile()]
  63. def side_effect(name, _mode):
  64. 'fake open'
  65. if name == '~/.gbs.conf':
  66. return confs[0]
  67. return confs[1]
  68. fake_open.side_effect = side_effect
  69. reload(gitbuildsys.conf)
  70. self.assertEquals('''[remotebuild]
  71. build_server = https://api
  72. passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
  73. [build]
  74. repo1.url = https://repo1
  75. repo1.passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
  76. ''', confs[0].getvalue())
  77. self.assertEquals('''[remotebuild]
  78. build_server = https://api
  79. user = test
  80. passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
  81. [build]
  82. repo1.url = https://repo1
  83. repo1.user = test
  84. repo1.passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
  85. ''', confs[1].getvalue())
  86. @Fixture(home='normal_passwdx.ini')
  87. def test_get_passwdx(self, _fake_open):
  88. 'test get decode passwd'
  89. reload(gitbuildsys.conf)
  90. pwd = gitbuildsys.conf.configmgr.get('passwd', 'remotebuild')
  91. self.assertEquals('secret', pwd)
  92. @Fixture(home='plain_passwd.ini')
  93. def test_get_passwd(self, fake_open):
  94. 'test get decode passwd'
  95. fake_open.return_value = FakeFile()
  96. reload(gitbuildsys.conf)
  97. pwd = gitbuildsys.conf.configmgr.get('passwd', 'remotebuild')
  98. self.assertEquals('secret', pwd)
  99. @Fixture(home='bad_passwdx.ini')
  100. def test_bad_passwdx(self, _fake_open):
  101. 'test bad passwdx'
  102. reload(gitbuildsys.conf)
  103. self.assertRaises(ConfigError, gitbuildsys.conf.configmgr.get,
  104. 'passwd', 'remotebuild')
  105. @Fixture(home='empty_passwdx.ini')
  106. def test_empty_passwdx(self, _fake_open):
  107. 'test empty passwdx'
  108. reload(gitbuildsys.conf)
  109. pwd = gitbuildsys.conf.configmgr.get('passwd', 'remotebuild')
  110. self.assertEquals('', pwd)
  111. @patch('gitbuildsys.conf.os.chmod')
  112. @patch('gitbuildsys.conf.open', create=True)
  113. class AutoGenerateTest(unittest.TestCase):
  114. 'test auto generation if no conf was found'
  115. @Fixture()
  116. def test_auto_generate_conf(self, fake_open, _fake_chmod):
  117. 'test auto generate conf should contain obs and repos'
  118. conf = FakeFile()
  119. fake_open.return_value = conf
  120. reload(gitbuildsys.conf)
  121. parser = BrainConfigParser()
  122. parser.readfp(StringIO(conf.getvalue()))
  123. name = parser.get('general', 'profile')
  124. obs = parser.get(name, 'obs')
  125. repos = parser.get(name, 'repos')
  126. self.assertTrue(parser.has_section(obs))
  127. for repo in repos.split(','):
  128. self.assertTrue(parser.has_section(repo.strip()))
  129. if __name__ == '__main__':
  130. unittest.main()