def setUp(self): self.msu = ManageSfUtils(config.GATEWAY_URL) self.un = config.ADMIN_USER self.gu = GerritUtils(config.GATEWAY_URL, auth_cookie=config.USERS[self.un]['auth_cookie']) self.gu2 = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[config.USER_2]['auth_cookie']) self.k_idx = self.gu2.add_pubkey(config.USERS[config.USER_2]["pubkey"]) priv_key_path = set_private_key(config.USERS[self.un]["privkey"]) self.gitu_admin = GerritGitUtils(self.un, priv_key_path, config.USERS[self.un]['email']) # Configuration to access mirror repo present in managesf self.managesf_repo_path = "ssh://%s@%s/home/gerrit/git/" % ( config.GERRIT_USER, config.GATEWAY_HOST) # prepare environment for git clone on mirror repo self.mt = Tool() self.mt_tempdir = tempfile.mkdtemp() priv_key = file(config.GERRIT_SERVICE_PRIV_KEY_PATH, 'r').read() priv_key_path = os.path.join(self.mt_tempdir, 'user.priv') file(priv_key_path, 'w').write(priv_key) os.chmod(priv_key_path, stat.S_IREAD | stat.S_IWRITE) ssh_wrapper = "ssh -o StrictHostKeyChecking=no -i %s \"$@\"" % ( priv_key_path) wrapper_path = os.path.join(self.mt_tempdir, 'ssh_wrapper.sh') file(wrapper_path, 'w').write(ssh_wrapper) os.chmod(wrapper_path, stat.S_IRWXU) self.mt.env['GIT_SSH'] = wrapper_path self.pname = 'test-replication'
def setUp(self): super(TestManageSF, self).setUp() self.projects = [] self.dirs_to_delete = [] self.ru = ResourcesUtils() self.msu = ManageSfUtils(config.GATEWAY_URL) self.gu = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
class TestGerritGitWeb(Base): """ Functional tests that validate some GitWeb access behaviors. """ def setUp(self): super(TestGerritGitWeb, self).setUp() self.projects = [] self.clone_dirs = [] self.dirs_to_delete = [] self.msu = ManageSfUtils(config.GATEWAY_URL) def tearDown(self): super(TestGerritGitWeb, self).tearDown() for name in self.projects: self.msu.deleteProject(name, config.ADMIN_USER) for dirs in self.dirs_to_delete: shutil.rmtree(dirs) def create_project(self, name, options=None): self.msu.createProject(name, config.ADMIN_USER, options=options) self.projects.append(name) def test_gitweb_access(self): """ Test if gitweb access works correctly """ pub_pname = 'p_public_%s' % create_random_str() self.create_project(pub_pname) priv_pname = 'p_private_%s' % create_random_str() self.create_project(priv_pname, options={"private": ""}) # Test anonymous access to a public repo url = "%s/r/gitweb?p=%s.git" % (config.GATEWAY_URL, pub_pname) expected_title = "%s.git/summary" % pub_pname resp = requests.get(url) self.assertTrue(resp.url.endswith('/r/gitweb?p=%s.git' % pub_pname)) self.assertTrue(expected_title in resp.text) # Test anonymous access to a private repo url = "%s/r/gitweb?p=%s.git" % (config.GATEWAY_URL, priv_pname) expected_title = "%s.git/summary" % priv_pname resp = requests.get(url) self.assertTrue("auth/login" in resp.url) self.assertFalse(expected_title in resp.text) # Test authenticated access to a private repo resp = requests.get( url, cookies=dict( auth_pubtkt=config.USERS[config.USER_1]['auth_cookie'])) self.assertTrue(resp.url.endswith('/r/gitweb?p=%s.git' % priv_pname)) self.assertTrue(expected_title in resp.text)
def __init__(self): with open("%s/resources.yaml" % pwd, 'r') as rsc: self.resources = yaml.load(rsc) config.USERS[config.ADMIN_USER]['auth_cookie'] = get_cookie( config.ADMIN_USER, config.USERS[config.ADMIN_USER]['password']) self.msu = ManageSfUtils(config.GATEWAY_URL) self.ggu = GerritGitUtils(config.ADMIN_USER, config.ADMIN_PRIV_KEY_PATH, config.USERS[config.ADMIN_USER]['email']) self.ju = JenkinsUtils() self.rm = RedmineUtils( config.GATEWAY_URL + "/redmine/", auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
def __init__(self): with open("%s/resources.yaml" % pwd, 'r') as rsc: self.resources = yaml.load(rsc) config.USERS[config.ADMIN_USER]['auth_cookie'] = get_cookie( config.ADMIN_USER, config.USERS[config.ADMIN_USER]['password']) self.msu = ManageSfUtils(config.GATEWAY_URL) self.ru = ResourcesUtils() self.ggu = GerritGitUtils(config.ADMIN_USER, config.ADMIN_PRIV_KEY_PATH, config.USERS[config.ADMIN_USER]['email']) self.stb_client = SFStoryboard( config.GATEWAY_URL + "/storyboard_api", config.USERS[config.ADMIN_USER]['auth_cookie'])
def setUpClass(cls): cls.msu = ManageSfUtils(config.GATEWAY_URL) cls.rm = get_issue_tracker_utils( auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie']) cls.gu = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
def setUp(self): self.msu = ManageSfUtils(config.GATEWAY_URL) self.un = config.ADMIN_USER self.gu = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[self.un]['auth_cookie']) self.gu2 = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[config.USER_2]['auth_cookie']) self.k_idx = self.gu2.add_pubkey(config.USERS[config.USER_2]["pubkey"]) priv_key_path = set_private_key(config.USERS[self.un]["privkey"]) self.gitu_admin = GerritGitUtils(self.un, priv_key_path, config.USERS[self.un]['email']) # Configuration to access mirror repo present in mysql self.msql_repo_path = "ssh://%s@%s/%s" \ % (config.GERRIT_USER, config.GATEWAY_HOST, 'home/gerrit/site_path/git/') # prepare environment for git clone on mirror repo self.mt = Tool() self.mt_tempdir = tempfile.mkdtemp() priv_key = file(config.GERRIT_SERVICE_PRIV_KEY_PATH, 'r').read() priv_key_path = os.path.join(self.mt_tempdir, 'user.priv') file(priv_key_path, 'w').write(priv_key) os.chmod(priv_key_path, stat.S_IREAD | stat.S_IWRITE) ssh_wrapper = "ssh -o StrictHostKeyChecking=no -i " \ "%s \"$@\"" % priv_key_path wrapper_path = os.path.join(self.mt_tempdir, 'ssh_wrapper.sh') file(wrapper_path, 'w').write(ssh_wrapper) os.chmod(wrapper_path, stat.S_IRWXU) self.mt.env['GIT_SSH'] = wrapper_path self.pname = 'test-replication'
def test_gerrit_api_accessible(self): """ Test if Gerrit API is accessible on gateway hosts """ m = ManageSfUtils(config.GATEWAY_URL) url = config.GATEWAY_URL + "/api/" a = GerritUtils(url) a.g.url = "%s/" % a.g.url.rstrip('a/') self.assertRaises(HTTPError, a.get_account, config.USER_1) api_passwd = m.create_gerrit_api_password(config.USER_1) auth = HTTPBasicAuth(config.USER_1, api_passwd) a = GerritUtils(url, auth=auth) self.assertTrue(a.get_account(config.USER_1)) m.delete_gerrit_api_password(config.USER_1) a = GerritUtils(url, auth=auth) self.assertRaises(HTTPError, a.get_account, config.USER_1) a = GerritUtils(url) a.g.url = "%s/" % a.g.url.rstrip('a/') self.assertRaises(HTTPError, a.get_account, 'john')
def __init__(self): with open('resources.yaml', 'r') as rsc: self.resources = yaml.load(rsc) config.USERS[config.ADMIN_USER]['auth_cookie'] = get_cookie( config.ADMIN_USER, config.USERS[config.ADMIN_USER]['password']) self.msu = ManageSfUtils(config.GATEWAY_URL) self.ggu = GerritGitUtils(config.ADMIN_USER, config.ADMIN_PRIV_KEY_PATH, config.USERS[config.ADMIN_USER]['email']) self.ju = JenkinsUtils() self.rm = RedmineUtils( config.REDMINE_URL, auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
class TestManageSF(Base): """ Functional tests that validate managesf features. """ def setUp(self): super(TestManageSF, self).setUp() self.projects = [] self.dirs_to_delete = [] self.ru = ResourcesUtils() self.msu = ManageSfUtils(config.GATEWAY_URL) self.gu = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie']) def tearDown(self): super(TestManageSF, self).tearDown() for name in self.projects: self.ru.direct_delete_repo(name) for dirs in self.dirs_to_delete: shutil.rmtree(dirs) def create_project(self, name): self.ru.direct_create_repo(name) self.projects.append(name) def test_api_key_auth_with_sfmanager(self): """Test the api key auth workflow""" user2_cookies = dict( auth_pubtkt=config.USERS[config.USER_2]['auth_cookie']) url = "https://%s%s" % (config.GATEWAY_HOST, "/auth/apikey/") create_key = requests.post(url, cookies=user2_cookies) self.assertIn(create_key.status_code, (201, 409)) key = requests.get(url, cookies=user2_cookies).json().get('api_key') # call a simple command that needs authentication cmd = "sfmanager --url %s --auth-server-url " \ "%s --api-key %s sf_user list" % (config.GATEWAY_URL, config.GATEWAY_URL, key) users = self.msu.exe(cmd) self.assertTrue(config.USER_2 in users, "'%s' returned %s" % (cmd, users))
def setUp(self): self.msu = ManageSfUtils(config.GATEWAY_URL) self.un = config.ADMIN_USER self.gu = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[self.un]['auth_cookie']) self.gu2 = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[config.USER_2]['auth_cookie']) self.k_idx = self.gu2.add_pubkey(config.USERS[config.USER_2]["pubkey"]) priv_key_path = set_private_key(config.USERS[self.un]["privkey"]) self.gitu_admin = GerritGitUtils(self.un, priv_key_path, config.USERS[self.un]['email']) # Prepare environment for git clone on mirror repo self.mt = Tool() self.mt_tempdir = tempfile.mkdtemp() # Copy the service private key in a flat file priv_key = file(config.SERVICE_PRIV_KEY_PATH, 'r').read() priv_key_path = os.path.join(self.mt_tempdir, 'user.priv') file(priv_key_path, 'w').write(priv_key) os.chmod(priv_key_path, stat.S_IREAD | stat.S_IWRITE) # Prepare the ssh wrapper script ssh_wrapper = "ssh -o StrictHostKeyChecking=no -i %s \"$@\"" % ( priv_key_path) wrapper_path = os.path.join(self.mt_tempdir, 'ssh_wrapper.sh') file(wrapper_path, 'w').write(ssh_wrapper) os.chmod(wrapper_path, stat.S_IRWXU) # Set the wrapper as GIT_SSH env variable self.mt.env['GIT_SSH'] = wrapper_path self.config_clone_dir = None # Project we are going to configure the replication for self.pname = 'test/replication' # Remove artifacts of previous run if any self.delete_config_section(self.un, self.pname) self.delete_mirror_repo(self.pname)
class TestProjectReplication(Base): """ Functional tests to verify the gerrit replication feature """ def setUp(self): self.msu = ManageSfUtils(config.GATEWAY_URL) self.un = config.ADMIN_USER self.gu = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[self.un]['auth_cookie']) self.gu2 = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[config.USER_2]['auth_cookie']) self.k_idx = self.gu2.add_pubkey(config.USERS[config.USER_2]["pubkey"]) priv_key_path = set_private_key(config.USERS[self.un]["privkey"]) self.gitu_admin = GerritGitUtils(self.un, priv_key_path, config.USERS[self.un]['email']) # Configuration to access mirror repo present in mysql self.msql_repo_path = "ssh://%s@%s/%s" \ % (config.GERRIT_USER, config.GATEWAY_HOST, 'home/gerrit/site_path/git/') # prepare environment for git clone on mirror repo self.mt = Tool() self.mt_tempdir = tempfile.mkdtemp() priv_key = file(config.GERRIT_SERVICE_PRIV_KEY_PATH, 'r').read() priv_key_path = os.path.join(self.mt_tempdir, 'user.priv') file(priv_key_path, 'w').write(priv_key) os.chmod(priv_key_path, stat.S_IREAD | stat.S_IWRITE) ssh_wrapper = "ssh -o StrictHostKeyChecking=no -i " \ "%s \"$@\"" % priv_key_path wrapper_path = os.path.join(self.mt_tempdir, 'ssh_wrapper.sh') file(wrapper_path, 'w').write(ssh_wrapper) os.chmod(wrapper_path, stat.S_IRWXU) self.mt.env['GIT_SSH'] = wrapper_path self.pname = 'test-replication' def tearDown(self): self.deleteConfigSection(self.un, self.pname) self.deleteMirrorRepo(self.pname) self.msu.deleteProject(self.pname, self.un) self.gu2.del_pubkey(self.k_idx) # Can't use GerritGitUtils.clone as not sure when source uri repo in mysql # be ready.(i.e gerrit is taking time to create the mirror repo in mysql # node) So this clone may succeed or fail, we don't need 'git review -s' # and other review commands in clone method def clone(self, uri, target): self.assertTrue(uri.startswith('ssh://')) cmd = "git clone %s %s" % (uri, target) self.mt.exe(cmd, self.mt_tempdir) clone = os.path.join(self.mt_tempdir, target) return clone def create_project(self, name, user, options=None): self.msu.createProject(name, user, options) def ssh_run_cmd(self, sshkey_priv_path, user, host, subcmd): host = '%s@%s' % (user, host) sshcmd = ['ssh', '-o', 'LogLevel=ERROR', '-o', 'StrictHostKeyChecking=no', '-o', 'UserKnownHostsFile=/dev/null', '-i', sshkey_priv_path, host] cmd = sshcmd + subcmd p = Popen(cmd, stdout=PIPE) return p.communicate() def deleteMirrorRepo(self, name): sshkey_priv_path = config.GERRIT_SERVICE_PRIV_KEY_PATH user = '******' host = config.GATEWAY_HOST mirror_path = '/home/gerrit/site_path/git/%s.git' % name cmd = ['rm', '-rf', mirror_path] self.ssh_run_cmd(sshkey_priv_path, user, host, cmd) def createConfigSection(self, user, project): # Section name will be node name and the project section = 'mysql_%s' % project host = '%s@%s' % (config.GERRIT_USER, config.GATEWAY_HOST) mirror_repo_path = '/home/gerrit/site_path/git/\${name}.git' url = '%s:%s' % (host, mirror_repo_path) self.msu.replicationModifyConfig(user, 'add', section, 'projects', project) self.msu.replicationModifyConfig(user, 'add', section, 'url', url) push = '+refs/heads/*:refs/heads/*' self.msu.replicationModifyConfig(user, 'add', section, 'push', push) push = '+refs/tags/*:refs/tags/*' self.msu.replicationModifyConfig(user, 'add', section, 'push', push) def deleteConfigSection(self, user, project): # section name will be node name and the project section = 'managesf_%s' % project self.msu.replicationModifyConfig(user, 'remove-section', section) def mirror_clone_and_check_files(self, url, pname, us_files): retries = 0 files = [] while True: clone = self.clone(url, pname) # clone may fail, as mirror repo is not yet ready(i.e gerrit not # yet replicated the project) if os.path.isdir(clone): files = [f for f in os.listdir(clone) if not f.startswith('.')] shutil.rmtree(clone) if us_files and files: break elif retries > 30: break else: time.sleep(3) retries += 1 if us_files: for f in us_files: self.assertIn(f, files) self.assertTrue((len(us_files) < len(files))) def test_replication(self): """ Test gerrit replication for review process """ # Be sure the project, mirror repo, project in config don't exist self.deleteMirrorRepo(self.pname) self.deleteConfigSection(self.un, self.pname) self.msu.deleteProject(self.pname, self.un) # Create the project self.create_project(self.pname, self.un) # Create new section for this project in replication.config self.createConfigSection(self.un, self.pname) # Force gerrit to read its known_hosts file. The only # way to do that is by restarting gerrit. The Puppet Gerrit # manifest will restart gerrit if a new entry in known_hosts_gerrit # is discovered. # This may take some time (gerrit in some condition take long # to be fully up) call("ssh [email protected] systemctl restart gerrit", shell=True) call("ssh [email protected] /root/wait4gerrit.sh", shell=True) # Clone the project and submit it for review priv_key_path = set_private_key(config.USERS[self.un]["privkey"]) gitu = GerritGitUtils(self.un, priv_key_path, config.USERS[self.un]['email']) url = "ssh://%s@%s:29418/%s" % (self.un, config.GATEWAY_HOST, self.pname) clone_dir = gitu.clone(url, self.pname) gitu.add_commit_and_publish(clone_dir, "master", "Test commit") # Add 2 files and resubmit for review data = "echo Working" us_files = ["run_functional-tests.sh", "run_tests.sh"] for f in us_files: file(os.path.join(clone_dir, f), 'w').write(data) os.chmod(os.path.join(clone_dir, f), 0755) gitu.add_commit_and_publish(clone_dir, "master", None, fnames=us_files) # Review the patch and merge it change_ids = self.gu.get_my_changes_for_project(self.pname) self.assertGreater(len(change_ids), 0) change_id = change_ids[0] self.gu.submit_change_note(change_id, "current", "Code-Review", "2") self.gu.submit_change_note(change_id, "current", "Verified", "2") self.gu.submit_change_note(change_id, "current", "Workflow", "1") # Put USER_2 as core for config project grp_name = '%s-core' % self.pname self.gu.add_group_member(config.USER_2, grp_name) self.gu2.submit_change_note(change_id, "current", "Code-Review", "2") self.assertTrue(self.gu.submit_patch(change_id, "current")) shutil.rmtree(clone_dir) # Verify if gerrit automatically triggered replication # Mirror repo(in mysql node) should have these latest changes # Clone the mirror repo(from mysql) and check for the 2 files msql_repo_url = self.msql_repo_path + '%s.git' % self.pname self.mirror_clone_and_check_files(msql_repo_url, self.pname, us_files)
def setUp(self): self.projects = [] self.clone_dirs = [] self.dirs_to_delete = [] self.msu = ManageSfUtils(config.GATEWAY_URL)
class TestGerrit(Base): """ Functional tests that validate some gerrit behaviors. """ @classmethod def setUpClass(cls): pass @classmethod def tearDownClass(cls): pass def setUp(self): self.projects = [] self.clone_dirs = [] self.dirs_to_delete = [] self.msu = ManageSfUtils(config.GATEWAY_URL) def tearDown(self): for name in self.projects: self.msu.deleteProject(name, config.ADMIN_USER) for dirs in self.dirs_to_delete: shutil.rmtree(dirs) def create_project(self, name, options=None): self.msu.createProject(name, config.ADMIN_USER, options=options) self.projects.append(name) def test_review_labels(self): """ Test if list of review labels are as expected """ pname = 'p_%s' % create_random_str() self.create_project(pname) un = config.ADMIN_USER gu = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[un]['auth_cookie']) k_index = gu.add_pubkey(config.USERS[un]["pubkey"]) self.assertTrue(gu.project_exists(pname)) priv_key_path = set_private_key(config.USERS[un]["privkey"]) gitu = GerritGitUtils(un, priv_key_path, config.USERS[un]['email']) url = "ssh://%s@%s:29418/%s" % (un, config.GATEWAY_HOST, pname) clone_dir = gitu.clone(url, pname) self.dirs_to_delete.append(os.path.dirname(clone_dir)) gitu.add_commit_and_publish(clone_dir, "master", "Test commit") change_ids = gu.get_my_changes_for_project(pname) self.assertEqual(len(change_ids), 1) change_id = change_ids[0] labels = gu.get_labels_list_for_change(change_id) self.assertIn('Workflow', labels) self.assertIn('Code-Review', labels) self.assertIn('Verified', labels) self.assertEqual(len(labels.keys()), 3) gu.del_pubkey(k_index) def _prepare_review_submit_testing(self, project_options=None): if project_options is None: u2mail = config.USERS[config.USER_2]['email'] project_options = {'core-group': u2mail} pname = 'p_%s' % create_random_str() self.create_project(pname, project_options) un = config.ADMIN_USER gu = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[un]['auth_cookie']) k_index = gu.add_pubkey(config.USERS[un]["pubkey"]) self.assertTrue(gu.project_exists(pname)) priv_key_path = set_private_key(config.USERS[un]["privkey"]) gitu = GerritGitUtils(un, priv_key_path, config.USERS[un]['email']) url = "ssh://%s@%s:29418/%s" % (un, config.GATEWAY_HOST, pname) clone_dir = gitu.clone(url, pname) self.dirs_to_delete.append(os.path.dirname(clone_dir)) gitu.add_commit_and_publish(clone_dir, "master", "Test commit") change_ids = gu.get_my_changes_for_project(pname) self.assertEqual(len(change_ids), 1) change_id = change_ids[0] return change_id, gu, k_index def test_review_submit_approval(self): """ Test submit criteria - CR(+2s), V(+1), W(+1) """ change_id, gu, k_index = self._prepare_review_submit_testing() gu.submit_change_note(change_id, "current", "Code-Review", "1") self.assertFalse(gu.submit_patch(change_id, "current")) gu.submit_change_note(change_id, "current", "Verified", "2") self.assertFalse(gu.submit_patch(change_id, "current")) gu.submit_change_note(change_id, "current", "Workflow", "1") self.assertFalse(gu.submit_patch(change_id, "current")) gu.submit_change_note(change_id, "current", "Code-Review", "2") self.assertTrue(gu.submit_patch(change_id, "current")) gu.del_pubkey(k_index) def test_plugins_installed(self): """ Test if plugins are present """ gu = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie']) plugins = gu.list_plugins() self.assertIn('download-commands', plugins) self.assertIn('avatars-gravatar', plugins) self.assertIn('reviewers-by-blame', plugins) def test_check_download_commands(self): """ Test if download commands plugin works """ pname = 'p_%s' % create_random_str() self.create_project(pname) un = config.ADMIN_USER gu = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[un]['auth_cookie']) self.assertTrue(gu.project_exists(pname)) k_index = gu.add_pubkey(config.USERS[un]["pubkey"]) priv_key_path = set_private_key(config.USERS[un]["privkey"]) gitu = GerritGitUtils(un, priv_key_path, config.USERS[un]['email']) url = "ssh://%s@%s:29418/%s" % (un, config.GATEWAY_HOST, pname) clone_dir = gitu.clone(url, pname) self.dirs_to_delete.append(os.path.dirname(clone_dir)) gitu.add_commit_and_publish(clone_dir, "master", "Test commit") change_ids = gu.get_my_changes_for_project(pname) self.assertEqual(len(change_ids), 1) change_id = change_ids[0] resp = gu.get_change_last_patchset(change_id) self.assertIn("current_revision", resp) self.assertIn("revisions", resp) current_rev = resp["current_revision"] fetch = resp["revisions"][current_rev]["fetch"] self.assertGreater(fetch.keys(), 0) # disable and check if the fetch has anything gu.e_d_plugin("download-commands", 'disable') resp = gu.get_change_last_patchset(change_id) fetch = resp["revisions"][current_rev]["fetch"] self.assertEqual(len(fetch.keys()), 0) # enable the plugin and check if the fetch information is valid gu.e_d_plugin("download-commands", 'enable') resp = gu.get_change_last_patchset(change_id) fetch = resp["revisions"][current_rev]["fetch"] self.assertGreater(len(fetch.keys()), 0) gu.del_pubkey(k_index) def test_check_add_automatic_reviewers(self): """ Test if reviewers-by-blame plugin works """ pname = 'p_%s' % create_random_str() u2mail = config.USERS[config.USER_2]['email'] options = {'core-group': u2mail} self.create_project(pname, options) first_u = config.ADMIN_USER gu_first_u = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[first_u]['auth_cookie']) self.assertTrue(gu_first_u.project_exists(pname)) # Push data in the create project as Admin user k1_index = gu_first_u.add_pubkey(config.USERS[first_u]["pubkey"]) priv_key_path = set_private_key(config.USERS[first_u]["privkey"]) gitu = GerritGitUtils(first_u, priv_key_path, config.USERS[first_u]['email']) url = "ssh://%s@%s:29418/%s" % (first_u, config.GATEWAY_HOST, pname) clone_dir = gitu.clone(url, pname) self.dirs_to_delete.append(os.path.dirname(clone_dir)) data = ['this', 'is', 'a', 'couple', 'of', 'lines'] clone_dir = gitu.clone(url, pname) file(os.path.join(clone_dir, "file"), 'w').write("\n".join(data)) gitu.add_commit_and_publish(clone_dir, "master", "Test commit", fnames=["file"]) # Get the change id change_ids = gu_first_u.get_my_changes_for_project(pname) self.assertEqual(len(change_ids), 1) change_id = change_ids[0] # Merge the change gu_first_u.submit_change_note(change_id, "current", "Code-Review", "2") gu_first_u.submit_change_note(change_id, "current", "Verified", "2") gu_first_u.submit_change_note(change_id, "current", "Workflow", "1") second_u = config.USER_2 gu_second_u = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[second_u]['auth_cookie']) self.assertTrue(gu_first_u.submit_patch(change_id, "current")) # Change the file we have commited with Admin user k2_index = gu_second_u.add_pubkey(config.USERS[second_u]["pubkey"]) priv_key_path = set_private_key(config.USERS[second_u]["privkey"]) gitu = GerritGitUtils(second_u, priv_key_path, config.USERS[second_u]['email']) url = "ssh://%s@%s:29418/%s" % (second_u, config.GATEWAY_HOST, pname) clone_dir = gitu.clone(url, pname) self.dirs_to_delete.append(os.path.dirname(clone_dir)) data = ['this', 'is', 'some', 'lines'] file(os.path.join(clone_dir, "file"), 'w').write("\n".join(data)) gitu.add_commit_and_publish(clone_dir, "master", "Test commit", fnames=["file"]) # Get the change id change_ids = gu_second_u.get_my_changes_for_project(pname) self.assertEqual(len(change_ids), 1) change_id = change_ids[0] # Verify first_u has been automatically added to reviewers attempts = 0 while True: if len(gu_second_u.get_reviewers(change_id)) > 0 or attempts >= 3: break attempts += 1 time.sleep(1) reviewers = gu_second_u.get_reviewers(change_id) self.assertGreaterEqual(len(reviewers), 1) self.assertTrue(first_u in reviewers) gu_first_u.del_pubkey(k1_index) gu_second_u.del_pubkey(k2_index) def test_gerrit_version(self): """ Test if correct Gerrit version is running """ url = config.GATEWAY_URL + "/r/config/server/version" resp = requests.get( url, cookies=dict( auth_pubtkt=config.USERS[config.USER_1]['auth_cookie'])) self.assertTrue('"2.11.5"' in resp.text)
def setUpClass(cls): cls.msu = ManageSfUtils(config.GATEWAY_URL) cls.sample_project_dir = \ os.path.join(config.SF_TESTS_DIR, "sample_project/")
class SFProvisioner(object): """ This provider is only intended for testing SF backup/restore and update. It provisions some user datas in a SF installation based on a resourses.yaml file. Later those data can be checked by its friend the SFChecker. Provisioned data should remain really simple. """ def __init__(self): with open('resources.yaml', 'r') as rsc: self.resources = yaml.load(rsc) config.USERS[config.ADMIN_USER]['auth_cookie'] = get_cookie( config.ADMIN_USER, config.USERS[config.ADMIN_USER]['password']) self.msu = ManageSfUtils(config.GATEWAY_URL) self.ggu = GerritGitUtils(config.ADMIN_USER, config.ADMIN_PRIV_KEY_PATH, config.USERS[config.ADMIN_USER]['email']) self.ju = JenkinsUtils() self.rm = RedmineUtils( config.REDMINE_URL, auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie']) def create_project(self, name): print " Creating project %s ..." % name self.msu.createProject(name, config.ADMIN_USER) def push_files_in_project(self, name, files): print " Add files(%s) in a commit ..." % ",".join(files) # TODO(fbo); use gateway host instead of gerrit host self.url = "ssh://%s@%s:29418/%s" % (config.ADMIN_USER, config.GATEWAY_HOST, name) clone_dir = self.ggu.clone(self.url, name, config_review=False) self.clone_dir = clone_dir for f in files: file(os.path.join(clone_dir, f), 'w').write('data') self.ggu.git_add(clone_dir, (f,)) self.ggu.add_commit_for_all_new_additions(clone_dir) self.ggu.direct_push_branch(clone_dir, 'master') def create_issues_on_project(self, name, issues): print " Create %s issue(s) for that project ..." % len(issues) for i in issues: issue = self.rm.create_issue(name, i['name']) yield issue, i['review'] def create_jenkins_jobs(self, name, jobnames): print " Create Jenkins jobs(%s) ..." % ",".join(jobnames) for jobname in jobnames: self.ju.create_job("%s_%s" % (name, jobname)) def create_pads(self, amount): # TODO pass def create_pasties(self, amount): # TODO pass def create_review(self, project, issue): """Very basic review creator for statistics and restore tests purposes.""" self.ggu.config_review(self.clone_dir) self.ggu.add_commit_in_branch(self.clone_dir, 'branch_' + issue, commit='test\n\nBug: %s' % issue) self.ggu.review_push_branch(self.clone_dir, 'branch_' + issue) def provision(self): for project in self.resources['projects']: print "Create user datas for %s" % project['name'] self.create_project(project['name']) self.push_files_in_project(project['name'], [f['name'] for f in project['files']]) for i, review in self.create_issues_on_project(project['name'], project['issues']): if review: print "Create review for bug %i in %s" % (i, project['name']) self.create_review(project['name'], str(i)) self.create_jenkins_jobs(project['name'], [j['name'] for j in project['jobnames']]) self.create_pads(2) self.create_pasties(2)
def setUp(self): super(TestGerritGitWeb, self).setUp() self.projects = [] self.clone_dirs = [] self.dirs_to_delete = [] self.msu = ManageSfUtils(config.GATEWAY_URL)
class SFProvisioner(object): """ This provider is only intended for testing SF backup/restore and update. It provisions some user datas in a SF installation based on a resourses.yaml file. Later those data can be checked by its friend the SFChecker. Provisioned data should remain really simple. """ def __init__(self): with open("%s/resources.yaml" % pwd, 'r') as rsc: self.resources = yaml.load(rsc) config.USERS[config.ADMIN_USER]['auth_cookie'] = get_cookie( config.ADMIN_USER, config.USERS[config.ADMIN_USER]['password']) self.msu = ManageSfUtils(config.GATEWAY_URL) self.ru = ResourcesUtils() self.ggu = GerritGitUtils(config.ADMIN_USER, config.ADMIN_PRIV_KEY_PATH, config.USERS[config.ADMIN_USER]['email']) self.stb_client = SFStoryboard( config.GATEWAY_URL + "/storyboard_api", config.USERS[config.ADMIN_USER]['auth_cookie']) def create_resources(self): print " Creating resources ..." if cmp_version(os.environ.get("PROVISIONED_VERSION", "0.0"), "2.4.0"): # Remove review-dashboard for p in self.resources['resources']['projects'].values(): del p['review-dashboard'] self.ru.create_resources("provisioner", {'resources': self.resources['resources']}) # Create review for the first few repositories for project in self.resources['resources']['repos'].keys()[:3]: self.clone_project(project) self.create_review(project, "Test review for %s" % project) def create_project(self, name): print " Creating project %s ..." % name self.ru.create_repo(name) def clone_project(self, name): # TODO(fbo); use gateway host instead of gerrit host self.url = "ssh://%s@%s:29418/%s" % (config.ADMIN_USER, config.GATEWAY_HOST, name) self.clone_dir = self.ggu.clone(self.url, name, config_review=False) def push_files_in_project(self, name, files): print " Add files(%s) in a commit ..." % ",".join(files) self.clone_project(name) for f in files: file(os.path.join(self.clone_dir, f), 'w').write('data') self.ggu.git_add(self.clone_dir, (f, )) self.ggu.add_commit_for_all_new_additions(self.clone_dir) self.ggu.direct_push_branch(self.clone_dir, 'master') def create_storyboard_issue(self, name, issue_name): project = self.stb_client.projects.get(name) story = self.stb_client.stories.create(title=issue_name) task = self.stb_client.tasks.create(story_id=story.id, project_id=project.id, title=issue_name) return task.id, story.id def create_issues_on_project(self, name, issues): print " Create %s issue(s) for that project ..." % len(issues) for i in issues: if is_present('storyboard'): issue = self.create_storyboard_issue(name, i['name']) else: issue = (random.randint(1, 100), random.randint(1, 100)) yield issue, i['review'] def create_pads(self, amount): # TODO pass def create_pasties(self, amount): # TODO pass def simple_login(self, user, password): """log as user to make the user listable""" get_cookie(user, password) def create_review(self, project, commit_message, branch='master'): """Very basic review creator for statistics and restore tests purposes.""" self.ggu.config_review(self.clone_dir) self.ggu.add_commit_in_branch(self.clone_dir, branch, commit=commit_message) self.ggu.review_push_branch(self.clone_dir, branch) def create_review_for_issue(self, project, issue): self.create_review( project, 'test\n\nTask: #%s\nStory: #%s' % (issue[0], issue[1]), 'branch_%s' % str(issue[0])) def create_local_user(self, username, password, email): self.msu.create_user(username, password, email) def command(self, cmd): return ssh_run_cmd(os.path.expanduser("~/.ssh/id_rsa"), "root", config.GATEWAY_HOST, shlex.split(cmd)) def compute_checksum(self, f): out = self.command("md5sum %s" % f)[0] if out: return out.split()[0] def read_file(self, f): return self.command("cat %s" % f)[0] def provision(self): for cmd in self.resources['commands']: print "Execute command %s" % cmd['cmd'] print self.command(cmd['cmd']) checksum_list = {} for checksum in self.resources['checksum']: print "Compute checksum for file %s" % checksum['file'] checksum_list[checksum['file']] = self.compute_checksum( checksum['file']) checksum_list['content_' + checksum['file']] = self.read_file( checksum['file']) yaml.dump(checksum_list, file('pc_checksums.yaml', 'w'), default_flow_style=False) for user in self.resources['local_users']: print "Create local user %s" % user['username'] self.create_local_user(user['username'], user['password'], user['email']) self.simple_login(user['username'], user['password']) for u in self.resources['users']: print "log in as %s" % u['name'] self.simple_login(u['name'], config.USERS[u['name']]['password']) for project in self.resources['projects']: print "Create user datas for %s" % project['name'] self.create_project(project['name']) self.push_files_in_project(project['name'], [f['name'] for f in project['files']]) for i, review in self.create_issues_on_project( project['name'], project['issues']): if review: print "Create review for bug %s in %s" % (i, project['name']) self.create_review_for_issue(project['name'], i) self.create_resources() self.create_pads(2) self.create_pasties(2)
class SFProvisioner(object): """ This provider is only intended for testing SF backup/restore and update. It provisions some user datas in a SF installation based on a resourses.yaml file. Later those data can be checked by its friend the SFChecker. Provisioned data should remain really simple. """ def __init__(self): with open("%s/resources.yaml" % pwd, 'r') as rsc: self.resources = yaml.load(rsc) config.USERS[config.ADMIN_USER]['auth_cookie'] = get_cookie( config.ADMIN_USER, config.USERS[config.ADMIN_USER]['password']) self.msu = ManageSfUtils(config.GATEWAY_URL) self.ggu = GerritGitUtils(config.ADMIN_USER, config.ADMIN_PRIV_KEY_PATH, config.USERS[config.ADMIN_USER]['email']) self.ju = JenkinsUtils() self.rm = RedmineUtils( config.GATEWAY_URL + "/redmine/", auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie']) def create_project(self, name): print " Creating project %s ..." % name self.msu.createProject(name, config.ADMIN_USER) def push_files_in_project(self, name, files): print " Add files(%s) in a commit ..." % ",".join(files) # TODO(fbo); use gateway host instead of gerrit host self.url = "ssh://%s@%s:29418/%s" % (config.ADMIN_USER, config.GATEWAY_HOST, name) clone_dir = self.ggu.clone(self.url, name, config_review=False) self.clone_dir = clone_dir for f in files: file(os.path.join(clone_dir, f), 'w').write('data') self.ggu.git_add(clone_dir, (f,)) self.ggu.add_commit_for_all_new_additions(clone_dir) self.ggu.direct_push_branch(clone_dir, 'master') def create_issues_on_project(self, name, issues): print " Create %s issue(s) for that project ..." % len(issues) for i in issues: if is_present('SFRedmine'): issue = self.rm.create_issue(name, i['name']) else: issue = random.randint(1,100) yield issue, i['review'] def create_jenkins_jobs(self, name, jobnames): print " Create Jenkins jobs(%s) ..." % ",".join(jobnames) for jobname in jobnames: self.ju.create_job("%s_%s" % (name, jobname)) def create_pads(self, amount): # TODO pass def create_pasties(self, amount): # TODO pass def simple_login(self, user): """log as user to make the user listable""" get_cookie(user, config.USERS[user]['password']) def create_review(self, project, issue): """Very basic review creator for statistics and restore tests purposes.""" self.ggu.config_review(self.clone_dir) self.ggu.add_commit_in_branch(self.clone_dir, 'branch_' + issue, commit='test\n\nBug: %s' % issue) self.ggu.review_push_branch(self.clone_dir, 'branch_' + issue) def create_local_user(self, username, password, email): self.msu.create_user(username, password, email) def command(self, cmd): return ssh_run_cmd(os.path.expanduser("~/.ssh/id_rsa"), "root", config.GATEWAY_HOST, shlex.split(cmd)) def compute_checksum(self, f): out = self.command("md5sum %s" % f)[0] if out: return out.split()[0] def provision(self): for cmd in self.resources['commands']: print "Execute command %s" % cmd['cmd'] print self.command(cmd['cmd']) checksum_list = {} for checksum in self.resources['checksum'] : print "Compute checksum for file %s" % checksum['file'] checksum_list[checksum['file']] = self.compute_checksum( checksum['file']) yaml.dump(checksum_list, file('/tmp/pc_checksums.yaml', 'w')) for user in self.resources['local_users']: print "Create local user %s" % user['username'] self.create_local_user(user['username'], user['password'], user['email']) for u in self.resources['users']: print "log in as %s" % u['name'] self.simple_login(u['name']) for project in self.resources['projects']: print "Create user datas for %s" % project['name'] self.create_project(project['name']) self.push_files_in_project(project['name'], [f['name'] for f in project['files']]) for i, review in self.create_issues_on_project(project['name'], project['issues']): if review: print "Create review for bug %i in %s" % (i, project['name']) self.create_review(project['name'], str(i)) self.create_jenkins_jobs(project['name'], [j['name'] for j in project['jobnames']]) self.create_pads(2) self.create_pasties(2)
def setUpClass(cls): cls.msu = ManageSfUtils(config.GATEWAY_URL)
class TestProjectReplication(Base): """ Functional tests to verify the gerrit replication feature """ def setUp(self): self.msu = ManageSfUtils(config.GATEWAY_URL) self.un = config.ADMIN_USER self.gu = GerritUtils(config.GATEWAY_URL, auth_cookie=config.USERS[self.un]['auth_cookie']) self.gu2 = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[config.USER_2]['auth_cookie']) self.k_idx = self.gu2.add_pubkey(config.USERS[config.USER_2]["pubkey"]) priv_key_path = set_private_key(config.USERS[self.un]["privkey"]) self.gitu_admin = GerritGitUtils(self.un, priv_key_path, config.USERS[self.un]['email']) # Configuration to access mirror repo present in managesf self.managesf_repo_path = "ssh://%s@%s/home/gerrit/git/" % ( config.GERRIT_USER, config.GATEWAY_HOST) # prepare environment for git clone on mirror repo self.mt = Tool() self.mt_tempdir = tempfile.mkdtemp() priv_key = file(config.GERRIT_SERVICE_PRIV_KEY_PATH, 'r').read() priv_key_path = os.path.join(self.mt_tempdir, 'user.priv') file(priv_key_path, 'w').write(priv_key) os.chmod(priv_key_path, stat.S_IREAD | stat.S_IWRITE) ssh_wrapper = "ssh -o StrictHostKeyChecking=no -i %s \"$@\"" % ( priv_key_path) wrapper_path = os.path.join(self.mt_tempdir, 'ssh_wrapper.sh') file(wrapper_path, 'w').write(ssh_wrapper) os.chmod(wrapper_path, stat.S_IRWXU) self.mt.env['GIT_SSH'] = wrapper_path self.pname = 'test-replication' def tearDown(self): self.deleteConfigSection(self.un, self.pname) self.deleteMirrorRepo(self.pname) self.msu.deleteProject(self.pname, self.un) self.gu2.del_pubkey(self.k_idx) # Can't use GerritGitUtils.clone as not sure when source uri repo # be ready.(i.e gerrit is taking time to create the mirror repo in managesf # node) So this clone may succeed or fail, we don't need 'git review -s' # and other review commands in clone method def clone(self, uri, target): self.assertTrue(uri.startswith('ssh://')) cmd = "git clone %s %s" % (uri, target) self.mt.exe(cmd, self.mt_tempdir) clone = os.path.join(self.mt_tempdir, target) return clone def create_project(self, name, user, options=None): self.msu.createProject(name, user, options) def ssh_run_cmd(self, sshkey_priv_path, user, host, subcmd): host = '%s@%s' % (user, host) sshcmd = [ 'ssh', '-o', 'LogLevel=ERROR', '-o', 'StrictHostKeyChecking=no', '-o', 'UserKnownHostsFile=/dev/null', '-i', sshkey_priv_path, host ] cmd = sshcmd + subcmd p = Popen(cmd, stdout=PIPE) return p.communicate() def deleteMirrorRepo(self, name): sshkey_priv_path = config.GERRIT_SERVICE_PRIV_KEY_PATH user = '******' host = config.GATEWAY_HOST mirror_path = '/home/gerrit/git/%s.git' % name cmd = ['rm', '-rf', mirror_path] self.ssh_run_cmd(sshkey_priv_path, user, host, cmd) def createConfigSection(self, user, project): # Section name will be node name and the project section = 'managesf_%s' % project host = '%s@%s' % (config.GERRIT_USER, config.GATEWAY_HOST) mirror_repo_path = '/home/gerrit/git/\${name}.git' url = '%s:%s' % (host, mirror_repo_path) self.msu.replicationModifyConfig(user, 'add', section, 'projects', project) self.msu.replicationModifyConfig(user, 'add', section, 'url', url) push = '+refs/heads/*:refs/heads/*' self.msu.replicationModifyConfig(user, 'add', section, 'push', push) push = '+refs/tags/*:refs/tags/*' self.msu.replicationModifyConfig(user, 'add', section, 'push', push) def deleteConfigSection(self, user, project): # section name will be node name and the project section = 'managesf_%s' % project self.msu.replicationModifyConfig(user, 'remove', section) def mirror_clone_and_check_files(self, url, pname, us_files): retries = 0 files = [] while True: clone = self.clone(url, pname) # clone may fail, as mirror repo is not yet ready(i.e gerrit not # yet replicated the project) if os.path.isdir(clone): files = [f for f in os.listdir(clone) if not f.startswith('.')] shutil.rmtree(clone) if us_files and files: break elif retries > 30: break else: time.sleep(3) retries += 1 if us_files: for f in us_files: self.assertIn(f, files) self.assertTrue((len(us_files) < len(files))) def test_replication(self): """ Test gerrit replication for review process """ # Be sure the project, mirror repo, project in config don't exist self.deleteMirrorRepo(self.pname) self.deleteConfigSection(self.un, self.pname) self.msu.deleteProject(self.pname, self.un) # Create the project self.create_project(self.pname, self.un) # Create new section for this project in replication.config self.createConfigSection(self.un, self.pname) # Force gerrit to read its known_hosts file. The only # way to do that is by restarting gerrit. The Puppet Gerrit # manifest will restart gerrit if a new entry in known_hosts_gerrit # is discovered. # This may take some time (gerrit in some condition take long # to be fully up) call("ssh %s ssh gerrit systemctl restart gerrit" % config.GATEWAY_HOST, shell=True) call("ssh %s ssh gerrit /root/wait4gerrit.sh" % config.GATEWAY_HOST, shell=True) # Clone the project and submit it for review priv_key_path = set_private_key(config.USERS[self.un]["privkey"]) gitu = GerritGitUtils(self.un, priv_key_path, config.USERS[self.un]['email']) url = "ssh://%s@%s:29418/%s" % (self.un, config.GATEWAY_HOST, self.pname) clone_dir = gitu.clone(url, self.pname) gitu.add_commit_and_publish(clone_dir, "master", "Test commit") # Add 2 files and resubmit for review data = "echo Working" us_files = ["run_functional-tests.sh", "run_tests.sh"] for f in us_files: file(os.path.join(clone_dir, f), 'w').write(data) os.chmod(os.path.join(clone_dir, f), 0755) gitu.add_commit_and_publish(clone_dir, "master", None, fnames=us_files) # Review the patch and merge it change_ids = self.gu.get_my_changes_for_project(self.pname) self.assertGreater(len(change_ids), 0) change_id = change_ids[0] self.gu.submit_change_note(change_id, "current", "Code-Review", "2") self.gu.submit_change_note(change_id, "current", "Verified", "2") self.gu.submit_change_note(change_id, "current", "Workflow", "1") # Put USER_2 as core for config project grp_name = '%s-core' % self.pname self.gu.add_group_member(config.USER_2, grp_name) self.gu2.submit_change_note(change_id, "current", "Code-Review", "2") self.assertTrue(self.gu.submit_patch(change_id, "current")) shutil.rmtree(clone_dir) # Verify if gerrit automatically triggered replication repo_url = self.managesf_repo_path + '%s.git' % self.pname self.mirror_clone_and_check_files(repo_url, self.pname, us_files)
class TestProjectReplication(Base): """ Functional tests to verify the gerrit replication feature """ def setUp(self): self.msu = ManageSfUtils(config.GATEWAY_URL) self.un = config.ADMIN_USER self.gu = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[self.un]['auth_cookie']) self.gu2 = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[config.USER_2]['auth_cookie']) self.k_idx = self.gu2.add_pubkey(config.USERS[config.USER_2]["pubkey"]) priv_key_path = set_private_key(config.USERS[self.un]["privkey"]) self.gitu_admin = GerritGitUtils(self.un, priv_key_path, config.USERS[self.un]['email']) # Prepare environment for git clone on mirror repo self.mt = Tool() self.mt_tempdir = tempfile.mkdtemp() # Copy the service private key in a flat file priv_key = file(config.SERVICE_PRIV_KEY_PATH, 'r').read() priv_key_path = os.path.join(self.mt_tempdir, 'user.priv') file(priv_key_path, 'w').write(priv_key) os.chmod(priv_key_path, stat.S_IREAD | stat.S_IWRITE) # Prepare the ssh wrapper script ssh_wrapper = "ssh -o StrictHostKeyChecking=no -i %s \"$@\"" % ( priv_key_path) wrapper_path = os.path.join(self.mt_tempdir, 'ssh_wrapper.sh') file(wrapper_path, 'w').write(ssh_wrapper) os.chmod(wrapper_path, stat.S_IRWXU) # Set the wrapper as GIT_SSH env variable self.mt.env['GIT_SSH'] = wrapper_path self.config_clone_dir = None # Project we are going to configure the replication for self.pname = 'test/replication' # Remove artifacts of previous run if any self.delete_config_section(self.un, self.pname) self.delete_mirror_repo(self.pname) def tearDown(self): self.delete_config_section(self.un, self.pname) self.delete_mirror_repo(self.pname) self.msu.deleteProject(self.pname, self.un) self.gu2.del_pubkey(self.k_idx) def clone(self, uri, target): self.assertTrue(uri.startswith('ssh://')) cmd = "git clone %s %s" % (uri, target) clone = os.path.join(self.mt_tempdir, target) if os.path.isdir(clone): shutil.rmtree(clone) self.mt.exe(cmd, self.mt_tempdir) return clone def create_project(self, name, user, options=None): self.msu.createProject(name, user, options) def ssh_run_cmd(self, sshkey_priv_path, user, host, subcmd): host = '%s@%s' % (user, host) sshcmd = ['ssh', '-o', 'LogLevel=ERROR', '-o', 'StrictHostKeyChecking=no', '-o', 'UserKnownHostsFile=/dev/null', '-i', sshkey_priv_path, host] cmd = sshcmd + subcmd p = Popen(cmd, stdout=PIPE) return p.communicate(), p.returncode def delete_mirror_repo(self, name): mirror_path = '/home/gerrit/git/%s.git' % name cmd = ['ssh', 'gerrit.%s' % config.GATEWAY_HOST, 'rm', '-rf', mirror_path] self.ssh_run_cmd(config.SERVICE_PRIV_KEY_PATH, 'root', config.GATEWAY_HOST, cmd) def create_config_section(self, project): host = '%s@%s' % (config.GERRIT_USER, config.GATEWAY_HOST) mirror_repo_path = '/home/gerrit/git/\${name}.git' url = '%s:%s' % (host, mirror_repo_path) path = os.path.join(self.config_clone_dir, 'gerrit/replication.config') call("git config -f %s --remove-section remote.test_project" % path, shell=True) call("git config -f %s --add remote.test_project.projects %s" % (path, project), shell=True) call("git config -f %s --add remote.test_project.url %s" % (path, url), shell=True) self.gitu_admin.add_commit_for_all_new_additions( self.config_clone_dir, "Add replication test section") # The direct push will trigger the config-update job # as we commit through 29418 self.gitu_admin.direct_push_branch(self.config_clone_dir, 'master') attempts = 0 cmd = ['ssh', 'gerrit.%s' % config.GATEWAY_HOST, 'grep', 'test_project', '/home/gerrit/site_path/etc/replication.config'] while attempts < 30: out, code = self.ssh_run_cmd(config.SERVICE_PRIV_KEY_PATH, 'root', config.GATEWAY_HOST, cmd) if code == 0: return attempts += 1 time.sleep(2) raise Exception('replication.config file has not been updated (add)') def delete_config_section(self, user, project): url = "ssh://%s@%s:29418/config" % (self.un, config.GATEWAY_HOST) self.config_clone_dir = self.gitu_admin.clone( url, 'config', config_review=True) path = os.path.join(self.config_clone_dir, 'gerrit/replication.config') call("git config -f %s --remove-section remote.test_project" % path, shell=True) try: self.gitu_admin.add_commit_for_all_new_additions( self.config_clone_dir, "Remove replication test section") except CalledProcessError: # We fail if nothing to re-initialized return # The direct push will trigger the config-update job # as we commit through 29418 self.gitu_admin.direct_push_branch(self.config_clone_dir, 'master') attempts = 0 cmd = ['ssh', 'gerrit.%s' % config.GATEWAY_HOST, 'grep', 'test_project', '/home/gerrit/site_path/etc/replication.config'] while attempts < 30: out, code = self.ssh_run_cmd(config.SERVICE_PRIV_KEY_PATH, 'root', config.GATEWAY_HOST, cmd) if code != 0: return attempts += 1 time.sleep(2) raise Exception('replication.config has not been updated (rm)') def mirror_clone_and_check_files(self, url, pname): retries = 0 while True: clone = self.clone(url, pname) # clone may fail, as mirror repo is not yet ready(i.e gerrit not # yet replicated the project) if os.path.isdir(clone) and \ os.path.isfile(os.path.join(clone, '.gitreview')): return True elif retries > 50: break else: time.sleep(3) retries += 1 return False def test_replication(self): """ Test gerrit replication for review process """ # Create the project self.create_project(self.pname, self.un) # Be sure sftests.com host key is inside the known_hosts cmds = [['ssh', 'gerrit.%s' % config.GATEWAY_HOST, 'ssh-keyscan', 'sftests.com', '>', '/home/gerrit/.ssh/known_hosts']] for cmd in cmds: self.ssh_run_cmd(config.SERVICE_PRIV_KEY_PATH, 'root', config.GATEWAY_HOST, cmd) # Create new section for this project in replication.config self.create_config_section(self.pname) # Verify if gerrit replicated the repo self.managesf_repo_path = "ssh://%s@%s/home/gerrit/git/" % ( 'root', config.GATEWAY_HOST) repo_url = self.managesf_repo_path + '%s.git' % self.pname self.assertTrue(self.mirror_clone_and_check_files(repo_url, self.pname))
class TestGerrit(Base): """ Functional tests that validate some gerrit behaviors. """ @classmethod def setUpClass(cls): pass @classmethod def tearDownClass(cls): pass def setUp(self): self.projects = [] self.clone_dirs = [] self.dirs_to_delete = [] self.msu = ManageSfUtils(config.GATEWAY_URL) def tearDown(self): for name in self.projects: self.msu.deleteProject(name, config.ADMIN_USER) for dirs in self.dirs_to_delete: shutil.rmtree(dirs) def create_project(self, name, options=None): self.msu.createProject(name, config.ADMIN_USER, options=options) self.projects.append(name) def test_review_labels(self): """ Test if list of review labels are as expected """ pname = 'p_%s' % create_random_str() self.create_project(pname) un = config.ADMIN_USER gu = GerritUtils(config.GATEWAY_URL, auth_cookie=config.USERS[un]['auth_cookie']) k_index = gu.add_pubkey(config.USERS[un]["pubkey"]) self.assertTrue(gu.project_exists(pname)) priv_key_path = set_private_key(config.USERS[un]["privkey"]) gitu = GerritGitUtils(un, priv_key_path, config.USERS[un]['email']) url = "ssh://%s@%s:29418/%s" % (un, config.GATEWAY_HOST, pname) clone_dir = gitu.clone(url, pname) self.dirs_to_delete.append(os.path.dirname(clone_dir)) gitu.add_commit_and_publish(clone_dir, "master", "Test commit") change_ids = gu.get_my_changes_for_project(pname) self.assertEqual(len(change_ids), 1) change_id = change_ids[0] labels = gu.get_labels_list_for_change(change_id) self.assertIn('Workflow', labels) self.assertIn('Code-Review', labels) self.assertIn('Verified', labels) self.assertEqual(len(labels.keys()), 3) gu.del_pubkey(k_index) def _prepare_review_submit_testing(self, project_options=None): if project_options is None: u2mail = config.USERS[config.USER_2]['email'] project_options = {'core-group': u2mail} pname = 'p_%s' % create_random_str() self.create_project(pname, project_options) un = config.ADMIN_USER gu = GerritUtils(config.GATEWAY_URL, auth_cookie=config.USERS[un]['auth_cookie']) k_index = gu.add_pubkey(config.USERS[un]["pubkey"]) self.assertTrue(gu.project_exists(pname)) priv_key_path = set_private_key(config.USERS[un]["privkey"]) gitu = GerritGitUtils(un, priv_key_path, config.USERS[un]['email']) url = "ssh://%s@%s:29418/%s" % (un, config.GATEWAY_HOST, pname) clone_dir = gitu.clone(url, pname) self.dirs_to_delete.append(os.path.dirname(clone_dir)) gitu.add_commit_and_publish(clone_dir, "master", "Test commit") change_ids = gu.get_my_changes_for_project(pname) self.assertEqual(len(change_ids), 1) change_id = change_ids[0] return change_id, gu, k_index def test_review_submit_approval(self): """ Test submit criteria - CR(+2s), V(+1), W(+1) """ change_id, gu, k_index = self._prepare_review_submit_testing() gu.submit_change_note(change_id, "current", "Code-Review", "1") self.assertFalse(gu.submit_patch(change_id, "current")) gu.submit_change_note(change_id, "current", "Verified", "2") self.assertFalse(gu.submit_patch(change_id, "current")) gu.submit_change_note(change_id, "current", "Workflow", "1") self.assertFalse(gu.submit_patch(change_id, "current")) gu.submit_change_note(change_id, "current", "Code-Review", "2") self.assertTrue(gu.submit_patch(change_id, "current")) gu.del_pubkey(k_index) def test_plugins_installed(self): """ Test if plugins are present """ gu = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie']) plugins = gu.list_plugins() self.assertIn('download-commands', plugins) self.assertIn('avatars-gravatar', plugins) self.assertIn('reviewers-by-blame', plugins) def test_check_download_commands(self): """ Test if download commands plugin works """ pname = 'p_%s' % create_random_str() self.create_project(pname) un = config.ADMIN_USER gu = GerritUtils(config.GATEWAY_URL, auth_cookie=config.USERS[un]['auth_cookie']) self.assertTrue(gu.project_exists(pname)) k_index = gu.add_pubkey(config.USERS[un]["pubkey"]) priv_key_path = set_private_key(config.USERS[un]["privkey"]) gitu = GerritGitUtils(un, priv_key_path, config.USERS[un]['email']) url = "ssh://%s@%s:29418/%s" % (un, config.GATEWAY_HOST, pname) clone_dir = gitu.clone(url, pname) self.dirs_to_delete.append(os.path.dirname(clone_dir)) gitu.add_commit_and_publish(clone_dir, "master", "Test commit") change_ids = gu.get_my_changes_for_project(pname) self.assertEqual(len(change_ids), 1) change_id = change_ids[0] resp = gu.get_change_last_patchset(change_id) self.assertIn("current_revision", resp) self.assertIn("revisions", resp) current_rev = resp["current_revision"] fetch = resp["revisions"][current_rev]["fetch"] self.assertGreater(fetch.keys(), 0) # disable and check if the fetch has anything gu.e_d_plugin("download-commands", 'disable') resp = gu.get_change_last_patchset(change_id) fetch = resp["revisions"][current_rev]["fetch"] self.assertEqual(len(fetch.keys()), 0) # enable the plugin and check if the fetch information is valid gu.e_d_plugin("download-commands", 'enable') resp = gu.get_change_last_patchset(change_id) fetch = resp["revisions"][current_rev]["fetch"] self.assertGreater(len(fetch.keys()), 0) gu.del_pubkey(k_index) def test_check_add_automatic_reviewers(self): """ Test if reviewers-by-blame plugin works """ pname = 'p_%s' % create_random_str() u2mail = config.USERS[config.USER_2]['email'] options = {'core-group': u2mail} self.create_project(pname, options) first_u = config.ADMIN_USER gu_first_u = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[first_u]['auth_cookie']) self.assertTrue(gu_first_u.project_exists(pname)) # Push data in the create project as Admin user k1_index = gu_first_u.add_pubkey(config.USERS[first_u]["pubkey"]) priv_key_path = set_private_key(config.USERS[first_u]["privkey"]) gitu = GerritGitUtils(first_u, priv_key_path, config.USERS[first_u]['email']) url = "ssh://%s@%s:29418/%s" % (first_u, config.GATEWAY_HOST, pname) clone_dir = gitu.clone(url, pname) self.dirs_to_delete.append(os.path.dirname(clone_dir)) data = ['this', 'is', 'a', 'couple', 'of', 'lines'] clone_dir = gitu.clone(url, pname) file(os.path.join(clone_dir, "file"), 'w').write("\n".join(data)) gitu.add_commit_and_publish(clone_dir, "master", "Test commit", fnames=["file"]) # Get the change id change_ids = gu_first_u.get_my_changes_for_project(pname) self.assertEqual(len(change_ids), 1) change_id = change_ids[0] # Merge the change gu_first_u.submit_change_note(change_id, "current", "Code-Review", "2") gu_first_u.submit_change_note(change_id, "current", "Verified", "2") gu_first_u.submit_change_note(change_id, "current", "Workflow", "1") second_u = config.USER_2 gu_second_u = GerritUtils( config.GATEWAY_URL, auth_cookie=config.USERS[second_u]['auth_cookie']) self.assertTrue(gu_first_u.submit_patch(change_id, "current")) # Change the file we have commited with Admin user k2_index = gu_second_u.add_pubkey(config.USERS[second_u]["pubkey"]) priv_key_path = set_private_key(config.USERS[second_u]["privkey"]) gitu = GerritGitUtils(second_u, priv_key_path, config.USERS[second_u]['email']) url = "ssh://%s@%s:29418/%s" % (second_u, config.GATEWAY_HOST, pname) clone_dir = gitu.clone(url, pname) self.dirs_to_delete.append(os.path.dirname(clone_dir)) data = ['this', 'is', 'some', 'lines'] file(os.path.join(clone_dir, "file"), 'w').write("\n".join(data)) gitu.add_commit_and_publish(clone_dir, "master", "Test commit", fnames=["file"]) # Get the change id change_ids = gu_second_u.get_my_changes_for_project(pname) self.assertEqual(len(change_ids), 1) change_id = change_ids[0] # Verify first_u has been automatically added to reviewers attempts = 0 while True: if len(gu_second_u.get_reviewers(change_id)) > 0 or attempts >= 3: break attempts += 1 time.sleep(1) reviewers = gu_second_u.get_reviewers(change_id) self.assertGreaterEqual(len(reviewers), 1) self.assertTrue(first_u in reviewers) gu_first_u.del_pubkey(k1_index) gu_second_u.del_pubkey(k2_index) def test_gerrit_version(self): """ Test if correct Gerrit version is running """ url = config.GATEWAY_URL + "/r/config/server/version" resp = requests.get( url, cookies=dict( auth_pubtkt=config.USERS[config.USER_1]['auth_cookie'])) self.assertTrue('"2.11.5"' in resp.text)
class SFProvisioner(object): """ This provider is only intended for testing SF backup/restore and update. It provisions some user datas in a SF installation based on a resourses.yaml file. Later those data can be checked by its friend the SFChecker. Provisioned data should remain really simple. """ def __init__(self): with open("%s/resources.yaml" % pwd, 'r') as rsc: self.resources = yaml.load(rsc) config.USERS[config.ADMIN_USER]['auth_cookie'] = get_cookie( config.ADMIN_USER, config.USERS[config.ADMIN_USER]['password']) self.msu = ManageSfUtils(config.GATEWAY_URL) self.ggu = GerritGitUtils(config.ADMIN_USER, config.ADMIN_PRIV_KEY_PATH, config.USERS[config.ADMIN_USER]['email']) self.ju = JenkinsUtils() self.rm = RedmineUtils( config.GATEWAY_URL + "/redmine/", auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie']) def create_project(self, name): print " Creating project %s ..." % name self.msu.createProject(name, config.ADMIN_USER) def push_files_in_project(self, name, files): print " Add files(%s) in a commit ..." % ",".join(files) # TODO(fbo); use gateway host instead of gerrit host self.url = "ssh://%s@%s:29418/%s" % (config.ADMIN_USER, config.GATEWAY_HOST, name) clone_dir = self.ggu.clone(self.url, name, config_review=False) self.clone_dir = clone_dir for f in files: file(os.path.join(clone_dir, f), 'w').write('data') self.ggu.git_add(clone_dir, (f, )) self.ggu.add_commit_for_all_new_additions(clone_dir) self.ggu.direct_push_branch(clone_dir, 'master') def create_issues_on_project(self, name, issues): print " Create %s issue(s) for that project ..." % len(issues) for i in issues: issue = self.rm.create_issue(name, i['name']) yield issue, i['review'] def create_jenkins_jobs(self, name, jobnames): print " Create Jenkins jobs(%s) ..." % ",".join(jobnames) for jobname in jobnames: self.ju.create_job("%s_%s" % (name, jobname)) def create_pads(self, amount): # TODO pass def create_pasties(self, amount): # TODO pass def create_review(self, project, issue): """Very basic review creator for statistics and restore tests purposes.""" self.ggu.config_review(self.clone_dir) self.ggu.add_commit_in_branch(self.clone_dir, 'branch_' + issue, commit='test\n\nBug: %s' % issue) self.ggu.review_push_branch(self.clone_dir, 'branch_' + issue) def provision(self): for project in self.resources['projects']: print "Create user datas for %s" % project['name'] self.create_project(project['name']) self.push_files_in_project(project['name'], [f['name'] for f in project['files']]) for i, review in self.create_issues_on_project( project['name'], project['issues']): if review: print "Create review for bug %i in %s" % (i, project['name']) self.create_review(project['name'], str(i)) self.create_jenkins_jobs(project['name'], [j['name'] for j in project['jobnames']]) self.create_pads(2) self.create_pasties(2)
def setUpClass(cls): cls.projects = [] cls.clone_dirs = [] cls.dirs_to_delete = [] cls.msu = ManageSfUtils(config.GATEWAY_URL)