SECTCRE = SectionPattern()
- def read(self, filenames):
- """Limit the read() only support one input file. It's enough for
- current case.
- If the input list has multiple values, use the last one.
- """
-
- if not isinstance(filenames, basestring) and len(filenames) > 1:
- msger.warning('Will not support multiple config files, '
- 'only read in the last one.')
- filenames = filenames[-1:]
-
- return SafeConfigParser.read(self, filenames)
+ def read_one(self, filename):
+ """only support one input file"""
+ return SafeConfigParser.read(self, filename)
def _read(self, fptr, fname):
"""Parse a sectioned setup file.
for fpath in fpaths:
cfgparser = BrainConfigParser()
try:
- cfgparser.read(fpath)
+ cfgparser.read_one(fpath)
except MissingSectionHeaderError, err:
raise errors.ConfigError('config file error:%s' % err)
self._cfgparsers.append(cfgparser)
raise errors.ConfigError('no opt: %s in section %s' \
% (opt, str(section)))
- def check_opt(self, opt, section='general'):
- if section in self.DEFAULTS and \
- opt in self.DEFAULTS[section]:
- return True
- else:
- return False
-
def options(self, section='general'):
'merge and return options of certain section from multi-levels'
sect_found = False
def get(self, opt, section='general'):
'get item value. return plain text of password if item is passwd'
if opt == 'passwd':
- opt = 'passwdx'
- val = self._get(opt, section)
- if val:
- try:
- return decode_passwdx(val)
- except (TypeError, IOError), err:
- raise errors.ConfigError('passwdx:%s' % err)
- else:
- return val
+ val = self._get('passwdx', section)
+ try:
+ return decode_passwdx(val)
+ except (TypeError, IOError), err:
+ raise errors.ConfigError('passwdx:%s' % err)
else:
return self._get(opt, section)
- def set(self, opt, val, section='general'):
- if opt.endswith('passwd'):
- val = encode_passwd(val)
- opt += 'x'
-
- for cfgparser in self._cfgparsers:
- if cfgparser.has_option(section, opt):
- return cfgparser.set(section, opt, val)
-
- # Option not found, add a new key to the first cfg file that has
- # the section
- for cfgparser in self._cfgparsers:
- if cfgparser.has_section(section):
- return cfgparser.set(section, opt, val)
-
- raise errors.ConfigError('invalid section %s' % (section))
-
def update(self):
'update changed values into files on disk'
for cfgparser in self._cfgparsers:
"""Functional tests for GBS config"""
import os
-import functools
import unittest
from mock import patch
-from gitbuildsys import errors
+from gitbuildsys.errors import ConfigError
import gitbuildsys.conf
patch('gitbuildsys.conf.os.path.abspath', self.fake_abspath),
patch('ConfigParser.open', self.fake_open, create=True),
]
-
- @functools.wraps(func)
- def wrapper(*args, **kw):
- '''setup mock objects and tear down them finally'''
- for patcher in reversed(patchers):
- patcher.start()
-
- # configmgr is a global variable, reload to recreate it
- # otherwise fixtures only take effect in the first time
- reload(gitbuildsys.conf)
-
- try:
- return func(*args, **kw)
- except:
- raise
- finally:
- for patcher in patchers:
- patcher.stop()
-
- return wrapper
+ for patcher in patchers:
+ func = patcher(func)
+ return func
class ConfigGettingTest(unittest.TestCase):
@staticmethod
def get(section, option):
'''get section.option from config'''
+ # configmgr is a global variable, reload to recreate it
+ # otherwise fixtures only take effect in the first time
+ reload(gitbuildsys.conf)
return gitbuildsys.conf.configmgr.get(option, section)
@Fixture(project='project1.ini')
def test_no_such_section(self):
'''test no such section'''
- self.assertRaises(errors.ConfigError,
+ self.assertRaises(ConfigError,
self.get, 'not_exists_section', 'key')
@Fixture(project='project1.ini')
def test_no_such_option(self):
'''test no such option'''
- self.assertRaises(errors.ConfigError,
+ self.assertRaises(ConfigError,
self.get, 'section', 'not_exists_option')
@Fixture(project='project1.ini')
@Fixture(project='project1.ini')
def test_no_such_named_section(self):
'''test no such section'''
- self.assertRaises(errors.ConfigError,
+ self.assertRaises(ConfigError,
self.get, ('profile', 'NOT_EXISTS'), 'key')
@Fixture(project='project1.ini')
def test_no_such_option_in_named_section(self):
'''test no such section'''
- self.assertRaises(errors.ConfigError,
+ self.assertRaises(ConfigError,
self.get, ('profile', 'rsa'), 'not_exists_option')
+ @Fixture(home='home1.ini')
+ def test_default_value(self):
+ 'test get hardcode default value '
+ self.assertEquals('/var/tmp', self.get('general', 'tmpdir'))
+
+ @Fixture(home='without_section_header.ini')
+ def test_invalid_ini(self):
+ 'test invalid ini'
+ self.assertRaises(ConfigError, reload, gitbuildsys.conf)
+
if __name__ == '__main__':
unittest.main()
\ No newline at end of file
--- /dev/null
+#!/usr/bin/python -tt
+# vim: ai ts=4 sts=4 et sw=4
+#
+# Copyright (c) 2012 Intel, Inc.
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the Free
+# Software Foundation; version 2 of the License
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+# for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc., 59
+# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+"""Functional tests for setting passwdx back to config"""
+import unittest
+from StringIO import StringIO
+
+from mock import patch
+
+from test_config import Fixture
+import gitbuildsys.conf
+from gitbuildsys.errors import ConfigError
+
+
+class FakeFile(object):
+ 'Fake file used to get updated config file'
+
+ def __init__(self):
+ self.buffer = StringIO()
+
+ def write(self, data):
+ 'write data into fake file'
+ self.buffer.write(data)
+
+ def close(self):
+ 'do not close buffer, then call getvalue() to retrieve the content'
+
+ def getvalue(self):
+ 'get content of fake file'
+ return self.buffer.getvalue()
+
+
+@patch('gitbuildsys.conf.open', create=True)
+class PasswdxTest(unittest.TestCase):
+ 'Test for setting passwdx'
+
+ @Fixture(home='plain_passwd.ini')
+ def test_one_file(self, fake_open):
+ 'test passwdx set back to one file'
+ conf = FakeFile()
+ fake_open.return_value = conf
+
+ reload(gitbuildsys.conf)
+
+ self.assertEquals('''[remotebuild]
+build_server = https://api
+passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
+
+[build]
+repo1.url = https://repo1
+repo1.passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
+''', conf.getvalue())
+
+
+ @Fixture(home='plain_passwd.ini', project='plain_passwd2.ini')
+ def test_two_files(self, fake_open):
+ 'test passwdx set back to two files'
+ confs = [FakeFile(), FakeFile()]
+ bak = confs[:]
+ fake_open.side_effect = lambda *args, **kw: bak.pop()
+
+ reload(gitbuildsys.conf)
+
+ self.assertEquals('''[remotebuild]
+build_server = https://api
+passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
+
+[build]
+repo1.url = https://repo1
+repo1.passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
+''', confs[0].getvalue())
+
+ self.assertEquals('''[remotebuild]
+build_server = https://api
+user = test
+passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
+
+[build]
+repo1.url = https://repo1
+repo1.user = test
+repo1.passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
+''', confs[1].getvalue())
+
+
+ @Fixture(home='normal_passwdx.ini')
+ def test_get_passwdx(self, _fake_open):
+ 'test get decode passwd'
+ reload(gitbuildsys.conf)
+
+ pwd = gitbuildsys.conf.configmgr.get('passwd', 'remotebuild')
+ self.assertEquals('secret', pwd)
+
+ @Fixture(home='plain_passwd.ini')
+ def test_get_passwd(self, _fake_open):
+ 'test get decode passwd'
+ reload(gitbuildsys.conf)
+
+ pwd = gitbuildsys.conf.configmgr.get('passwd', 'remotebuild')
+ self.assertEquals('secret', pwd)
+
+ @Fixture(home='bad_passwdx.ini')
+ def test_bad_passwdx(self, _fake_open):
+ 'test bad passwdx'
+ reload(gitbuildsys.conf)
+
+ self.assertRaises(ConfigError, gitbuildsys.conf.configmgr.get,
+ 'passwd', 'remotebuild')
+
+ @Fixture(home='empty_passwdx.ini')
+ def test_empty_passwdx(self, _fake_open):
+ 'test empty passwdx'
+ reload(gitbuildsys.conf)
+
+ pwd = gitbuildsys.conf.configmgr.get('passwd', 'remotebuild')
+ self.assertEquals('', pwd)
+
+
+if __name__ == '__main__':
+ unittest.main()
\ No newline at end of file
def get_profile():
'''get current profile to test'''
+ reload(gitbuildsys.conf)
return gitbuildsys.conf.configmgr.get_current_profile()
--- /dev/null
+[remotebuild]
+build_server = https://api
+passwdx = secret
\ No newline at end of file
--- /dev/null
+[remotebuild]
+build_server = https://api
+passwdx =
\ No newline at end of file
--- /dev/null
+[remotebuild]
+build_server = https://api
+passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
\ No newline at end of file
--- /dev/null
+[remotebuild]
+build_server = https://api
+passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
\ No newline at end of file
--- /dev/null
+[remotebuild]
+build_server = https://api
+passwd = secret
+
+[build]
+repo1.url = https://repo1
+repo1.passwd = secret
--- /dev/null
+[remotebuild]
+build_server = https://api
+user = test
+passwd = secret
+
+[build]
+repo1.url = https://repo1
+repo1.user = test
+repo1.passwd = secret
\ No newline at end of file
--- /dev/null
+key = value
\ No newline at end of file