Repo or OBS section can inherit only user or password from profile.
authorHuang Hao <hao.h.huang@intel.com>
Fri, 21 Sep 2012 06:39:53 +0000 (14:39 +0800)
committerZhang Qiang <qiang.z.zhang@intel.com>
Fri, 21 Sep 2012 13:09:25 +0000 (21:09 +0800)
- Fix #387, add test cases for this.
- inline user/password have the hight priority.

Change-Id: I6a8d8f6d81d012596e7383d65279060d9cfac0c9

gitbuildsys/conf.py
gitbuildsys/safe_url.py
tests/test_profile.py
tests/test_safe_url.py
tests/testdata/ini/bug387_inherit_only_passwdx.ini [new file with mode: 0644]
tests/testdata/ini/bug387_inherit_only_user.ini [new file with mode: 0644]
tests/testdata/ini/bug387_inline_auth_has_the_highest_priority.ini [new file with mode: 0644]
tests/testdata/ini/bug387_only_password_no_user.ini [new file with mode: 0644]

index a65ed79..0bb3449 100644 (file)
@@ -24,6 +24,7 @@ import os
 import ast
 import base64
 import shutil
+from collections import namedtuple
 from ConfigParser import SafeConfigParser, NoSectionError, \
                          MissingSectionHeaderError, Error
 
@@ -372,7 +373,8 @@ url = http://download.tizen.org/snapshots/trunk/common/latest/repos/main/ia32/pa
         else:
             return self._get(opt, section)
 
-    def update(self, cfgparsers):
+    @staticmethod
+    def update(cfgparsers):
         'update changed values into files on disk'
         for cfgparser in cfgparsers:
             try:
@@ -381,16 +383,25 @@ url = http://download.tizen.org/snapshots/trunk/common/latest/repos/main/ia32/pa
                 msger.warning('update config file error: %s' % err)
 
 
+URL = namedtuple('URL', 'url user password')
+
+
 class OBSConf(object):
     'Config items related to obs section'
 
     def __init__(self, parent, name, url, base, target):
         self.parent = parent
         self.name = name
-        self.url = url
         self.base = base
         self.target = target
 
+        user = url.user or parent.common_user
+        password = url.password or parent.common_password
+        try:
+            self.url = SafeURL(url.url, user, password)
+        except ValueError, err:
+            raise errors.ConfigError('%s for %s' % (str(err), url.url))
+
     def dump(self, fhandler):
         'dump ini to file object'
         parser = BrainConfigParser()
@@ -419,7 +430,13 @@ class RepoConf(object):
     def __init__(self, parent, name, url):
         self.parent = parent
         self.name = name
-        self.url = url
+
+        user = url.user or parent.common_user
+        password = url.password or parent.common_password
+        try:
+            self.url = SafeURL(url.url, user, password)
+        except ValueError, err:
+            raise errors.ConfigError('%s for %s' % (str(err), url.url))
 
     def dump(self, fhandler):
         'dump ini to file object'
@@ -447,22 +464,12 @@ class Profile(object):
         self.repos = []
         self.obs = None
 
-    def _update_url(self, url):
-        'update url by common auth info'
-        if not url.user:
-            url.user = self.common_user
-        if not url.passwd:
-            url.passwd = self.common_password
-        return url
-
     def add_repo(self, repoconf):
         '''add a repo to repo list of the profile'''
-        self._update_url(repoconf.url)
         self.repos.append(repoconf)
 
     def set_obs(self, obsconf):
         '''set OBS api of the profile'''
-        self._update_url(obsconf.url)
         self.obs = obsconf
 
     def dump(self, fhandler):
@@ -546,10 +553,7 @@ class BizConfigManager(ConfigMgr):
         url = self.get('url', section_id)
         user = self.get_optional_item(section_id, 'user')
         password = self.get_optional_item(section_id, 'passwd')
-        try:
-            return SafeURL(url, user, password)
-        except ValueError, err:
-            raise errors.ConfigError('%s for %s' % (str(err), url))
+        return URL(url, user, password)
 
     def _build_profile_by_name(self, name):
         '''return profile object by a given section'''
@@ -636,11 +640,7 @@ class BizConfigManager(ConfigMgr):
         if addr:
             user = self.get_optional_item(sec, 'user')
             password = self.get_optional_item(sec, 'passwd')
-
-            try:
-                url = SafeURL(addr, user, password)
-            except ValueError, err:
-                raise errors.ConfigError('%s for %s' % (str(err), addr))
+            url = URL(addr, user, password)
 
             obsconf = OBSConf(profile, 'obs.%s' % sec, url,
                               self.get_optional_item('remotebuild', 'base_prj'),
@@ -651,10 +651,7 @@ class BizConfigManager(ConfigMgr):
         for key, item in repos:
             if 'url' not in item:
                 raise errors.ConfigError("URL is not specified for %s" % key)
-            try:
-                url = SafeURL(item['url'], item.get('user'), item.get('passwd'))
-            except ValueError, err:
-                raise errors.ConfigError('%s for %s' % (str(err), item['url']))
+            url = URL(item['url'], item.get('user'), item.get('passwd'))
 
             repoconf = RepoConf(profile, 'repo.%s' % key, url)
             profile.add_repo(repoconf)
index 9e9acfe..c1a9f6c 100644 (file)
@@ -99,12 +99,8 @@ class SafeURL(str):
     @staticmethod
     def _check_userinfo(user_inline, passwd_inline, user, passwd):
         '''returns the valid user and passwd'''
-
-        if user_inline and user or passwd_inline and passwd:
-            raise ValueError('Auth info specified twice')
-
-        user = user or user_inline
-        passwd = passwd or passwd_inline
+        user = user_inline or user
+        passwd = passwd_inline or passwd
 
         if not user and passwd:
             raise ValueError('No user is specified only password')
index c7cf863..7d2719f 100644 (file)
@@ -73,6 +73,33 @@ class ProfileStyleTest(unittest.TestCase):
         self.assertEquals('https://Bob:classified@repo/ia32/base',
                           get_profile().repos[2].url.full)
 
+    @Fixture(home='bug387_inherit_only_user.ini')
+    def test_inherit_only_user(self):
+        'test inherit only user from parent'
+        self.assertEquals('https://tester:secret@repo',
+                          get_profile().repos[0].url.full)
+        self.assertEquals('https://tester:secret@obs',
+                          get_profile().obs.url.full)
+
+    @Fixture(home='bug387_inherit_only_passwdx.ini')
+    def test_inherit_only_passwdx(self):
+        'test inherit only password from parent'
+        self.assertEquals('https://tester:secret@repo',
+                          get_profile().repos[0].url.full)
+        self.assertEquals('https://tester:secret@obs',
+                          get_profile().obs.url.full)
+
+    @Fixture(home='bug387_only_password_no_user.ini')
+    def test_only_password_no_user(self):
+        'test only password no user'
+        self.assertRaises(ConfigError, get_profile)
+
+    @Fixture(home='bug387_inline_auth_has_the_highest_priority.ini')
+    def test_inline_highest_priority(self):
+        'test inline auth has the highest priority'
+        self.assertEquals('https://this:inline-pwd@obs',
+                          get_profile().obs.url.full)
+
     @Fixture(home='no_such_profile_section_name.ini')
     def test_no_such_profile(self):
         'test profile name does not exist'
index 4308835..26ea8b1 100644 (file)
@@ -26,15 +26,6 @@ from gitbuildsys.safe_url import SafeURL
 class SafeURLTest(unittest.TestCase):
     '''Test SafeURL class'''
 
-    def test_duplicated_user(self):
-        '''raise ValueError if specify user twice'''
-        self.assertRaises(ValueError, SafeURL, 'http://Alice@server', 'Bob')
-
-    def test_duplicated_password(self):
-        '''raise ValueError if specify passwd twice'''
-        self.assertRaises(ValueError, SafeURL,
-                          'http://Alice:pp@server', None, 'password')
-
     def test_passwd_no_user(self):
         '''raise ValueError if only given password'''
         self.assertRaises(ValueError, SafeURL, 'http://:password@server')
diff --git a/tests/testdata/ini/bug387_inherit_only_passwdx.ini b/tests/testdata/ini/bug387_inherit_only_passwdx.ini
new file mode 100644 (file)
index 0000000..fbfe399
--- /dev/null
@@ -0,0 +1,16 @@
+[general]
+profile = profile.test
+
+[profile.test]
+repos = repo.test
+obs = obs.test
+
+passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
+
+[repo.test]
+url = https://repo
+user = tester
+
+[obs.test]
+url = https://obs
+user = tester
\ No newline at end of file
diff --git a/tests/testdata/ini/bug387_inherit_only_user.ini b/tests/testdata/ini/bug387_inherit_only_user.ini
new file mode 100644 (file)
index 0000000..e5bb6d8
--- /dev/null
@@ -0,0 +1,16 @@
+[general]
+profile = profile.test
+
+[profile.test]
+repos = repo.test
+obs = obs.test
+
+user = tester
+
+[repo.test]
+url = https://repo
+passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
+
+[obs.test]
+url = https://obs
+passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
diff --git a/tests/testdata/ini/bug387_inline_auth_has_the_highest_priority.ini b/tests/testdata/ini/bug387_inline_auth_has_the_highest_priority.ini
new file mode 100644 (file)
index 0000000..478a04a
--- /dev/null
@@ -0,0 +1,10 @@
+[general]
+profile = profile.test
+
+[profile.test]
+obs = obs.test
+
+[obs.test]
+url = https://this:inline-pwd@obs
+user = tester
+passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
diff --git a/tests/testdata/ini/bug387_only_password_no_user.ini b/tests/testdata/ini/bug387_only_password_no_user.ini
new file mode 100644 (file)
index 0000000..a8d63c8
--- /dev/null
@@ -0,0 +1,13 @@
+[general]
+profile = profile.test
+
+[profile.test]
+repos = repo.test
+obs = obs.test
+passwdx = QlpoOTFBWSZTWYfNdxYAAAIBgAoAHAAgADDNAMNEA24u5IpwoSEPmu4s
+
+[repo.test]
+url = https://repo
+
+[obs.test]
+url = https://obs