def install_git_review_file(self, **kwargs): logs = [] name = kwargs['name'] gitreview_template = """[gerrit] host=%(gerrit-host)s port=%(gerrit-host-port)s project=%(name)s defaultbranch=master """ paths = {} content = gitreview_template % ( { 'gerrit-host': self.conf.gerrit['top_domain'], 'gerrit-host-port': self.conf.gerrit['ssh_port'], 'name': name }) paths['.gitreview'] = content # Clone the master branch and push the .gitreview file try: r = utils.GerritRepo(name, self.conf) r.clone() r.push_master(paths) except Exception, e: logs.append(str(e))
def test_push_master(self): gr = utils.GerritRepo('p1', self.conf) with patch.object(gr, '_exec') as ex: with patch.object(gr, 'add_file') as af: gr.push_master({'f1': 'contentf1', 'f2': 'contentf2'}) self.assertEqual(2, len(af.mock_calls)) self.assertEqual(5, len(ex.mock_calls))
def install_acl(self, **kwargs): logs = [] name = kwargs['name'] description = kwargs['description'] acl_id = kwargs['acl'] self._set_client() group_names = set([]) acl_data = "" # Add default groups implicitly for default_group in DEFAULT_GROUPS: group_names.add(default_group) # If the string is not empty (default empty) if acl_id: # Fetch the ACL acl_data = self.new['resources']['acls'][acl_id]['file'] acl_group_ids = set( self.new['resources']['acls'][acl_id]['groups']) # Fetch groups name for group_id in acl_group_ids: gname = self.new['resources']['groups'][group_id]['name'] group_names.add(gname) else: acl_data = """[access] inheritFrom = All-Projects [project] description = No description provided""" # Fill a groups file groups_file = """# UUID Group Name global:Registered-Users\tRegistered Users""" for group in group_names: gid = self.client.get_group_id(group) groups_file += "\n%s\t%s" % (gid, group) # Overwrite the description if given in the ACL file if 'description =' in acl_data: acl_data = re.sub('description =.*', 'description = %s' % description, acl_data) else: acl_data += """[project] description = %s """ acl_data = acl_data % description # Clone the meta/config branch and push the ACL try: r = utils.GerritRepo(name, self.conf) r.clone() paths = {} paths['project.config'] = acl_data paths['groups'] = groups_file r.push_config(paths) except Exception, e: logs.append(str(e))
def test_add_file(self): gr = utils.GerritRepo('p1', self.conf) with patch.object(gr, '_exec') as ex: gr.add_file('thefile', 'thecontent') p = os.path.join(gr.infos['localcopy_path'], 'thefile') self.assertTrue(os.path.isfile(p)) self.assertEqual('thecontent', file(p).read()) self.assertEqual('git add thefile', ex.mock_calls[0][1][0])
def test_clone(self): gr = utils.GerritRepo('p1', self.conf) with patch.object(gr, '_exec') as ex: gr.clone() self.assertEqual( 'git clone ssh://[email protected]:2929/p1 %s' % gr.infos['localcopy_path'], ex.mock_calls[0][1][0])
def test_review_changes(self): gr = utils.GerritRepo('p1', self.conf) with patch.object(gr, '_exec') as ex: gr.review_changes('this is a test') self.assertEqual(3, len(ex.mock_calls)) self.assertTrue(ex.mock_calls[0][1][0].startswith( 'ssh-agent bash -c')) self.assertTrue(ex.mock_calls[1][1][0].startswith('git commit -a')) self.assertEqual('git review', ex.mock_calls[2][1][0])
class GitRepositoryOps(object): def __init__(self, conf, new): self.conf = conf self.new = new self.client = None def _set_client(self): if not self.client: gerrit = SoftwareFactoryGerrit(self.conf) self.client = gerrit.get_client() def get_all(self): logs = [] gitrepos = {} acls = {} self._set_client() try: repos = self.client.get_projects() if repos is False: logs.append("Repo list: err API returned HTTP 404/409") except Exception, e: logs.append("Repo list: err API returned %s" % e) for name in repos: gitrepos[name] = {} r = utils.GerritRepo(name, self.conf) # Remove the project section when it only contains description remove_project_section = False acl_path = r.get_raw_acls() acl_groups = set() c = GitConfigParser(acl_path) c.read() for section_name in c.sections(): for k, v in c.items(section_name): if section_name == 'project': if k == 'description': if len(c.items(section_name)) == 1: remove_project_section = True gitrepos[name]['description'] = v continue r = re.search('group (.*)', v) if r: acl_groups.add(r.groups()[0].strip()) _acl = file(acl_path).read() acl = "" # Clean the ACL file to avoid issue at YAML multiline # serialization. Remove the description and as a good # practice description should never appears in a ACL rtype # TODO(fbo): extra_validation of acl must deny the description for l in _acl.splitlines(): if remove_project_section and l.find('[project]') != -1: continue if l.find('description') != -1: continue acl += l.replace('\t', ' ').rstrip() + '\n' m = hashlib.md5() m.update(acl) acl_id = m.hexdigest() gitrepos[name]['name'] = name gitrepos[name]['acl'] = acl_id acls[acl_id] = {} acls[acl_id]['file'] = acl acls[acl_id]['groups'] = acl_groups acls[acl_id]['groups'] -= set(DEFAULT_GROUPS) acls[acl_id]['groups'] -= set(('Registered Users', )) acls[acl_id]['groups'] = list(acls[acl_id]['groups']) return logs, {'repos': gitrepos, 'acls': acls}
def test_push_master_from_git_remote(self): gr = utils.GerritRepo('p1', self.conf) with patch.object(gr, '_exec') as ex: gr.push_master_from_git_remote('git://tests.dom/git/oldp1.git') self.assertEqual(5, len(ex.mock_calls))
def test_exec(self): gr = utils.GerritRepo('p1', self.conf) gr._exec('touch f') self.assertTrue(os.path.isfile(os.path.join(gr.infos['localcopy_path'], 'f')))
def test_init(self): gr = utils.GerritRepo('p1', self.conf) self.assertTrue(gr.infos['localcopy_path'].endswith('p1')) self.assertTrue(os.path.isfile(gr.env['GIT_SSH'])) self.assertTrue(gr.env['GIT_COMMITTER_NAME']) self.assertTrue(gr.env['GIT_COMMITTER_EMAIL'])