Beispiel #1
0
 def get_client(self, cookie=None):
     if not cookie:
         try:
             basic = HTTPBasicAuth(self.conf['admin_user'], 'password')
             msg = '[%s] using direct basic auth to connect to gerrit'
             logger.debug(msg % self.service_name)
             g = GerritUtils(self.conf['url'] + 'api', auth=basic)
             return g
         except Exception as e:
             # if we can't get the admin credentials from the config,
             # let's not panic
             msg = ('[%s] simple auth raised error: %s, '
                    'going with SF cauth-based authentication')
             logger.debug(msg % (self.service_name, e))
         # Use an admin cookie
         if int(time.time()) - globals()['ADMIN_COOKIE_DATE'] > \
                 globals()['COOKIE_VALIDITY']:
             cookie = get_cookie(self._full_conf.auth['host'],
                                 self._full_conf.admin['name'],
                                 self._full_conf.admin['http_password'])
             globals()['ADMIN_COOKIE'] = cookie
             globals()['ADMIN_COOKIE_DATE'] = int(time.time())
         else:
             cookie = globals()['ADMIN_COOKIE']
     return GerritUtils(self.conf['url'], auth_cookie=cookie)
 def setUp(self):
     self.projects = []
     self.dirs_to_delete = []
     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.ju = JenkinsUtils()
     self.gu.add_pubkey(config.USERS[self.un]["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'])
     # Clone the config repo and make change to it
     # in order to test the new sample_project
     self.config_clone_dir = self.clone_as_admin("config")
     self.original_layout = file(os.path.join(
         self.config_clone_dir, "zuul/layout.yaml")).read()
     self.original_zuul_projects = file(os.path.join(
         self.config_clone_dir, "zuul/projects.yaml")).read()
     self.original_project = file(os.path.join(
         self.config_clone_dir, "jobs/projects.yaml")).read()
     # Put USER_2 as core for config project
     self.gu.add_group_member(config.USER_2, "config-core")
 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 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'
Beispiel #5
0
    def _prepare_review_submit_testing(self, data=None):
        pname = 'p_%s' % create_random_str()
        self.create_project(pname)
        gu = GerritUtils(
            config.GATEWAY_URL,
            auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
        k_index = gu.add_pubkey(config.USERS[config.ADMIN_USER]["pubkey"])
        self.assertTrue(gu.project_exists(pname))
        priv_key_path = set_private_key(
            config.USERS[config.ADMIN_USER]["privkey"])
        gitu = GerritGitUtils(config.ADMIN_USER, priv_key_path,
                              config.USERS[config.ADMIN_USER]['email'])
        url = "ssh://%s@%s:29418/%s" % (config.ADMIN_USER, config.GATEWAY_HOST,
                                        pname)
        clone_dir = gitu.clone(url, pname)
        self.dirs_to_delete.append(os.path.dirname(clone_dir))

        if not data:
            gitu.add_commit_and_publish(clone_dir, "master", "Test commit")
        else:
            file(os.path.join(clone_dir, "file"), 'w').write(data[1])
            gitu.add_commit_and_publish(clone_dir,
                                        "master",
                                        "Test commit",
                                        fnames=[data[0]])

        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, pname
Beispiel #6
0
 def setUp(self):
     super(TestProjectTestsWorkflow, self).setUp()
     self.projects = []
     self.dirs_to_delete = []
     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.ju = JenkinsUtils()
     self.gu.add_pubkey(config.USERS[self.un]["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'])
     # Clone the config repo and keep job/zuul config content
     self.config_clone_dir = self.clone_as_admin("config")
     self.original_zuul_projects = file(os.path.join(
         self.config_clone_dir, "zuul/projects.yaml")).read()
     self.original_project = file(os.path.join(
         self.config_clone_dir, "jobs/projects.yaml")).read()
     self.need_restore_config_repo = False
     # Put USER_2 as core for config project
     self.gu.add_group_member(config.USER_2, "config-core")
    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_create_public_project_as_user_clone_as_user(self):
     """ Create public project as user then clone as user
     """
     pname = 'p_%s' % create_random_str()
     # create the project as admin
     self.create_project(pname, config.USER_2)
     # add user2 ssh pubkey to user2
     gu = GerritUtils(
         'https://%s/' % config.GATEWAY_HOST,
         auth_cookie=config.USERS[config.USER_2]['auth_cookie'])
     gu.add_pubkey(config.USER_2_PUB_KEY)
     # prepare to clone
     priv_key_path = set_private_key(config.USER_2_PRIV_KEY)
     self.dirs_to_delete.append(os.path.dirname(priv_key_path))
     ggu = GerritGitUtils(config.USER_2,
                          priv_key_path,
                          config.USERS[config.USER_2]['email'])
     url = "ssh://%s@%s:29418/%s" % (config.USER_2,
                                     config.GATEWAY_HOST, pname)
     # clone
     clone_dir = ggu.clone(url, pname)
     self.dirs_to_delete.append(os.path.dirname(clone_dir))
     # Test that the clone is a success
     self.assertTrue(os.path.isdir(clone_dir))
     # Verify master own the .gitreview file
     self.assertTrue(os.path.isfile(os.path.join(clone_dir,
                                                 '.gitreview')))
 def test_create_public_project_as_user_clone_as_user(self):
     """ Create public project as user then clone as user
     """
     pname = 'p_%s' % create_random_str()
     # create the project as admin
     self.create_project(pname, config.USER_2)
     # add user2 ssh pubkey to user2
     gu = GerritUtils(
         config.GATEWAY_URL,
         auth_cookie=config.USERS[config.USER_2]['auth_cookie'])
     gu.add_pubkey(config.USER_2_PUB_KEY)
     # prepare to clone
     priv_key_path = set_private_key(config.USER_2_PRIV_KEY)
     self.dirs_to_delete.append(os.path.dirname(priv_key_path))
     ggu = GerritGitUtils(config.USER_2,
                          priv_key_path,
                          config.USERS[config.USER_2]['email'])
     url = "ssh://%s@%s:29418/%s" % (config.USER_2,
                                     config.GATEWAY_HOST, pname)
     # clone
     clone_dir = ggu.clone(url, pname)
     self.dirs_to_delete.append(os.path.dirname(clone_dir))
     # Test that the clone is a success
     self.assertTrue(os.path.isdir(clone_dir))
     # Verify master own the .gitreview file
     self.assertTrue(os.path.isfile(os.path.join(clone_dir,
                                                 '.gitreview')))
Beispiel #10
0
 def setUp(self):
     self.projects = []
     self.rm = RedmineUtils(
         config.GATEWAY_URL + "/redmine/",
         auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
     self.gu = GerritUtils(
         config.GATEWAY_URL,
         auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
Beispiel #11
0
 def setUp(self):
     super(TestProjectMembership, self).setUp()
     self.projects = []
     self.rm = get_issue_tracker_utils(
         auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
     self.gu = GerritUtils(
         config.GATEWAY_URL,
         auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
Beispiel #12
0
 def setUp(self):
     super(TestManageSF, self).setUp()
     self.projects = []
     self.dirs_to_delete = []
     self.rm = get_issue_tracker_utils(
         auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
     self.gu = GerritUtils(
         config.GATEWAY_URL,
         auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
 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_plugins_installed(self):
     """ Test if plugins are present
     """
     gu = GerritUtils(
         'https://%s/' % config.GATEWAY_HOST,
         auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
     plugins = gu.list_plugins()
     self.assertIn('download-commands', plugins)
     self.assertIn('gravatar-avatar-provider', plugins)
     self.assertIn('reviewers-by-blame', plugins)
Beispiel #15
0
 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)
Beispiel #16
0
    def setUp(self):
        super(TestResourcesWorkflow, self).setUp()
        priv_key_path = set_private_key(
            config.USERS[config.ADMIN_USER]["privkey"])
        self.gitu_admin = GerritGitUtils(
            config.ADMIN_USER, priv_key_path,
            config.USERS[config.ADMIN_USER]['email'])
        self.gu = GerritUtils(
            config.GATEWAY_URL,
            auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
        self.ju = JenkinsUtils()

        self.dirs_to_delete = []
Beispiel #17
0
    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)
Beispiel #18
0
 def setUp(self):
     super(TestGerritHooks, self).setUp()
     self.projects = []
     self.dirs_to_delete = []
     self.issues = []
     self.u = config.ADMIN_USER
     self.rm = get_issue_tracker_utils(
         auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
     self.gu = GerritUtils(config.GATEWAY_URL,
                           auth_cookie=config.USERS[self.u]['auth_cookie'])
     self.gu.add_pubkey(config.USERS[self.u]["pubkey"])
     priv_key_path = set_private_key(config.USERS[self.u]["privkey"])
     self.gitu = GerritGitUtils(self.u, priv_key_path,
                                config.USERS[self.u]['email'])
Beispiel #19
0
 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.gu = GerritUtils(
         'http://%s/' % config.GATEWAY_HOST,
         auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
     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 test_check_config_repo_exists(self):
        """ Validate config repo has been bootstraped
        """
        pname = "config"
        gu = GerritUtils(config.GATEWAY_URL, auth_cookie=config.USERS[config.ADMIN_USER]["auth_cookie"])
        self.assertTrue(gu.project_exists(pname))

        ggu = GerritGitUtils(config.ADMIN_USER, config.ADMIN_PRIV_KEY_PATH, config.USERS[config.ADMIN_USER]["email"])
        url = "ssh://%s@%s:29418/%s" % (config.ADMIN_USER, config.GATEWAY_HOST, pname)
        clone_dir = ggu.clone(url, pname)
        # Test that the clone is a success
        self.assertTrue(os.path.isdir(clone_dir))
        # Check if the clone dir has projects file
        self.assertTrue(os.path.isfile(os.path.join(clone_dir, "jobs/projects.yaml")))
Beispiel #21
0
 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.gu = GerritUtils(
         'http://%s/' % config.GATEWAY_HOST,
         auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
     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 setUp(self):
     self.projects = []
     self.rm = get_issue_tracker_utils(
         auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
     self.gu = GerritUtils(
         config.GATEWAY_URL,
         auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
 def setUp(self):
     self.projects = []
     self.dirs_to_delete = []
     self.rm = RedmineUtils(
         config.GATEWAY_URL + "/redmine/", auth_cookie=config.USERS[config.ADMIN_USER]["auth_cookie"]
     )
     self.gu = GerritUtils(config.GATEWAY_URL, auth_cookie=config.USERS[config.ADMIN_USER]["auth_cookie"])
Beispiel #24
0
 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.projects = []
     self.rm = RedmineUtils(
         config.REDMINE_URL,
         auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
     self.gu = GerritUtils(
         'https://%s/' % config.GATEWAY_HOST,
         auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
    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)
Beispiel #27
0
 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 test_check_config_repo_exists(self):
        pname = 'config'
        gu = GerritUtils(
            'https://%s/' % config.GATEWAY_HOST,
            auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
        self.assertTrue(gu.project_exists(pname))

        ggu = GerritGitUtils(config.ADMIN_USER,
                             config.ADMIN_PRIV_KEY_PATH,
                             config.USERS[config.ADMIN_USER]['email'])
        url = "ssh://%s@%s:29418/%s" % (config.ADMIN_USER,
                                        config.GATEWAY_HOST, pname)
        clone_dir = ggu.clone(url, pname)
        # Test that the clone is a success
        self.assertTrue(os.path.isdir(clone_dir))
        # Check if the clone dir has projects file
        self.assertTrue(os.path.isfile(os.path.join(clone_dir,
                                                    "jobs/projects.yaml")))
 def setUp(self):
     self.projects = []
     self.dirs_to_delete = []
     self.issues = []
     self.u = config.ADMIN_USER
     self.u2 = config.USER_2
     self.rm = RedmineUtils(
         config.GATEWAY_URL + "/redmine/",
         auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
     self.gu = GerritUtils(config.GATEWAY_URL,
                           auth_cookie=config.USERS[self.u]['auth_cookie'])
     self.gu2 = GerritUtils(
         config.GATEWAY_URL,
         auth_cookie=config.USERS[self.u2]['auth_cookie'])
     self.gu.add_pubkey(config.USERS[self.u]["pubkey"])
     priv_key_path = set_private_key(config.USERS[self.u]["privkey"])
     self.gitu = GerritGitUtils(self.u, priv_key_path,
                                config.USERS[self.u]['email'])
    def test_check_config_repo_exists(self):
        """ Validate config repo has been bootstraped
        """
        pname = 'config'
        gu = GerritUtils(
            config.GATEWAY_URL,
            auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
        self.assertTrue(gu.project_exists(pname))

        ggu = GerritGitUtils(config.ADMIN_USER, config.ADMIN_PRIV_KEY_PATH,
                             config.USERS[config.ADMIN_USER]['email'])
        url = "ssh://%s@%s:29418/%s" % (config.ADMIN_USER, config.GATEWAY_HOST,
                                        pname)
        clone_dir = ggu.clone(url, pname)
        # Test that the clone is a success
        self.assertTrue(os.path.isdir(clone_dir))
        # Check if the clone dir has projects file
        self.assertTrue(
            os.path.isfile(os.path.join(clone_dir, "jobs/projects.yaml")))
 def setUp(self):
     self.projects = []
     self.dirs_to_delete = []
     self.issues = []
     self.u = config.ADMIN_USER
     self.u2 = config.USER_2
     self.rm = get_issue_tracker_utils(
         auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
     self.gu = GerritUtils(
         config.GATEWAY_URL,
         auth_cookie=config.USERS[self.u]['auth_cookie'])
     self.gu2 = GerritUtils(
         config.GATEWAY_URL,
         auth_cookie=config.USERS[self.u2]['auth_cookie'])
     self.gu.add_pubkey(config.USERS[self.u]["pubkey"])
     priv_key_path = set_private_key(config.USERS[self.u]["privkey"])
     self.gitu = GerritGitUtils(self.u,
                                priv_key_path,
                                config.USERS[self.u]['email'])
 def setUp(self):
     self.projects = []
     self.dirs_to_delete = []
     self.issues = []
     self.u = config.ADMIN_USER
     self.u2 = config.USER_2
     self.rm = RedmineUtils(
         config.REDMINE_URL,
         auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
     self.gu = GerritUtils(
         'https://%s/' % config.GATEWAY_HOST,
         auth_cookie=config.USERS[self.u]['auth_cookie'])
     self.gu2 = GerritUtils(
         'https://%s/' % config.GATEWAY_HOST,
         auth_cookie=config.USERS[self.u2]['auth_cookie'])
     self.gu.add_pubkey(config.USERS[self.u]["pubkey"])
     priv_key_path = set_private_key(config.USERS[self.u]["privkey"])
     self.gitu = GerritGitUtils(self.u,
                                priv_key_path,
                                config.USERS[self.u]['email'])
Beispiel #33
0
    def setUp(self):
        super(TestProjectReplication, self).setUp()
        self.ru = ResourcesUtils()
        self.un = config.ADMIN_USER
        self.ju = JenkinsUtils()
        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 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')
Beispiel #35
0
 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.gu = GerritUtils(
         'http://%s/' % config.GATEWAY_HOST,
         auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
     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'])
Beispiel #36
0
    def test_check_add_automatic_reviewers(self):
        """ Test if reviewers-by-blame plugin works
        """
        data = "this\nis\na\ncouple\nof\nlines"
        change_id, gu, k1_index, pname = self._prepare_review_submit_testing(
            ('file', data))

        # Merge the change
        gu.submit_change_note(change_id, "current", "Code-Review", "2")
        gu.submit_change_note(change_id, "current", "Verified", "2")
        gu.submit_change_note(change_id, "current", "Workflow", "1")
        self.assertTrue(gu.submit_patch(change_id, "current"))

        gu2 = GerritUtils(
            config.GATEWAY_URL,
            auth_cookie=config.USERS[config.USER_2]['auth_cookie'])
        # Change the file we have commited with Admin user
        k2_index = gu2.add_pubkey(config.USERS[config.USER_2]["pubkey"])
        priv_key_path = set_private_key(config.USERS[config.USER_2]["privkey"])
        gitu2 = GerritGitUtils(config.USER_2, priv_key_path,
                               config.USERS[config.USER_2]['email'])
        url = "ssh://%s@%s:29418/%s" % (config.USER_2, config.GATEWAY_HOST,
                                        pname)
        clone_dir = gitu2.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))
        gitu2.add_commit_and_publish(clone_dir,
                                     "master",
                                     "Test commit",
                                     fnames=["file"])
        # Get the change id
        change_ids = gu2.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
        for retry in xrange(3):
            if len(gu2.get_reviewers(change_id)) > 0:
                break
            time.sleep(1)
        reviewers = gu2.get_reviewers(change_id)
        self.assertEqual(len(reviewers), 1)
        self.assertEqual(reviewers[0], config.ADMIN_USER)

        gu.del_pubkey(k1_index)
        gu2.del_pubkey(k2_index)
Beispiel #37
0
    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
Beispiel #38
0
    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 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'])

        # 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))
Beispiel #41
0
class SFchecker:
    """ This checker is only intended for testin
    SF backup/restore and update. It checks that the user
    data defined in resourses.yaml are present on the SF.

    Those data must have been provisioned by SFProvisioner.
    """
    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.gu = GerritUtils(
            'http://%s/' % config.GATEWAY_HOST,
            auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
        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 check_project(self, name):
        print " Check project %s exists ..." % name,
        if not self.gu.project_exists(name) or \
           (is_present('SFRedmine') and not self.rm.project_exists(name)):
            print "FAIL"
            exit(1)
        print "OK"

    def check_files_in_project(self, name, files):
        print " Check files(%s) exists in project ..." % ",".join(files),
        # TODO(fbo); use gateway host instead of gerrit host
        url = "ssh://%s@%s:29418/%s" % (config.ADMIN_USER, config.GATEWAY_HOST,
                                        name)
        clone_dir = self.ggu.clone(url, name, config_review=False)
        for f in files:
            if not os.path.isfile(os.path.join(clone_dir, f)):
                print "FAIL"
                exit(1)

    def check_issues_on_project(self, name, issues):
        print " Check that at least %s issues exists for that project ...," %\
            len(issues)
        current_issues = self.rm.get_issues_by_project(name)
        if len(current_issues) < len(issues):
            print "FAIL: expected %s, project has %s" % (len(issues),
                                                         len(current_issues))
            exit(1)
        print "OK"

    def check_jenkins_jobs(self, name, jobnames):
        print " Check that jenkins jobs(%s) exists ..." % ",".join(jobnames),
        for jobname in jobnames:
            if not '%s_%s' % (name, jobname) in self.ju.list_jobs():
                print "FAIL"
                exit(1)
        print "OK"

    def check_reviews_on_project(self, name, issues):
        reviews = [i for i in issues if i['review']]
        print " Check that at least %s reviews exists for that project ..." %\
            len(reviews),
        pending_reviews = self.ggu.list_open_reviews(name, config.GATEWAY_HOST)
        if not len(pending_reviews) >= len(reviews):
            print "FAIL"
            exit(1)
        print "OK"

    def check_pads(self, amount):
        pass

    def check_pasties(self, amount):
        pass

    def checker(self):
        for project in self.resources['projects']:
            print "Check user datas for %s" % project['name']
            self.check_project(project['name'])
            self.check_files_in_project(project['name'],
                                        [f['name'] for f in project['files']])
            if is_present('SFRedmine'):
                self.check_issues_on_project(project['name'],
                                             project['issues'])
            self.check_reviews_on_project(project['name'], project['issues'])
            self.check_jenkins_jobs(project['name'],
                                    [j['name'] for j in project['jobnames']])
        self.check_pads(2)
        self.check_pasties(2)
class TestGerritHooks(Base):
    """ Functional tests that validate Gerrit hooks.
    """
    @classmethod
    def setUpClass(cls):
        cls.msu = ManageSfUtils(config.GATEWAY_URL)

    @classmethod
    def tearDownClass(cls):
        pass

    def setUp(self):
        self.projects = []
        self.dirs_to_delete = []
        self.issues = []
        self.u = config.ADMIN_USER
        self.u2 = config.USER_2
        self.rm = RedmineUtils(
            config.GATEWAY_URL + "/redmine/",
            auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
        self.gu = GerritUtils(
            config.GATEWAY_URL,
            auth_cookie=config.USERS[self.u]['auth_cookie'])
        self.gu2 = GerritUtils(
            config.GATEWAY_URL,
            auth_cookie=config.USERS[self.u2]['auth_cookie'])
        self.gu.add_pubkey(config.USERS[self.u]["pubkey"])
        priv_key_path = set_private_key(config.USERS[self.u]["privkey"])
        self.gitu = GerritGitUtils(self.u,
                                   priv_key_path,
                                   config.USERS[self.u]['email'])

    def tearDown(self):
        for issue in self.issues:
            self.rm.delete_issue(issue)
        for name in self.projects:
            self.msu.deleteProject(name, self.u)
        for dirs in self.dirs_to_delete:
            shutil.rmtree(dirs)

    def create_project(self, name, user,
                       options=None):
        self.msu.createProject(name, user,
                               options)
        self.projects.append(name)

    def _test_update_issue_hooks(self, comment_template, status):
        """ A referenced issue in commit msg triggers the hook
        """
        pname = 'p_%s' % create_random_str()

        # Be sure the project does not exist
        self.msu.deleteProject(pname, self.u)

        # Create the project
        self.create_project(pname, self.u)
        # Put USER_2 as core for the project
        self.gu.add_group_member(self.u2, "%s-core" % pname)

        # Create an issue on the project
        issue_id = self.rm.create_issue(pname, "There is a problem")

        # Clone and commit something
        url = "ssh://%s@%s:29418/%s" % (self.u, config.GATEWAY_HOST,
                                        pname)
        clone_dir = self.gitu.clone(url, pname)
        cmt_msg = comment_template % issue_id
        self.gitu.add_commit_and_publish(clone_dir, 'master', cmt_msg)

        # Check issue status (Gerrit hook updates the issue to in progress)
        attempt = 0
        while True:
            if self.rm.test_issue_status(issue_id, 'In Progress'):
                break
            if attempt > 10:
                break
            time.sleep(1)
            attempt += 1
        self.assertTrue(self.rm.test_issue_status(issue_id, 'In Progress'))
        self._test_merging(pname, issue_id, status)

    def _test_merging(self, pname, issue_id, status):
        # Get the change id and merge the patch
        change_ids = self.gu.get_my_changes_for_project(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", "Workflow", "1")
        self.gu.submit_change_note(change_id, "current", "Verified", "2")
        self.gu2.submit_change_note(change_id, "current", "Code-Review", "2")
        self.assertTrue(self.gu.submit_patch(change_id, "current"))

        # Check issue status (Gerrit hook updates the issue to in progress)
        attempt = 0
        while True:
            if self.rm.test_issue_status(issue_id, status):
                break
            if attempt > 10:
                break
            time.sleep(1)
            attempt += 1
        self.assertTrue(self.rm.test_issue_status(issue_id, status))

    def test_gerrit_hook(self):
        """test various commit messages triggering a hook"""
        for template, final_status in TEST_MSGS:
            self._test_update_issue_hooks(template, final_status)
Beispiel #43
0
class TestUserdata(Base):
    @classmethod
    def setUpClass(cls):
        cls.msu = ManageSfUtils(config.GATEWAY_URL)

    @classmethod
    def tearDownClass(cls):
        pass

    def setUp(self):
        self.projects = []
        self.rm = RedmineUtils(
            config.GATEWAY_URL + "/redmine/",
            auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
        self.gu = GerritUtils(
            config.GATEWAY_URL,
            auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])

    def tearDown(self):
        for name in self.projects:
            self.msu.deleteProject(name, config.ADMIN_USER)

    def create_project(self, name, user, options=None, cookie=None):
        self.msu.createProject(name, user, options, cookie)
        self.projects.append(name)

    def verify_userdata_gerrit(self, login):
        # Now check that the correct data was stored in Gerrit
        data = self.gu.get_account(login)
        self.assertEqual(config.USERS[login]['lastname'], data.get('name'))
        self.assertEqual(config.USERS[login]['email'], data.get('email'))

    def verify_userdata_redmine(self, login):
        users = self.rm.r.user.filter(limit=10)
        user = [u for u in users if u.firstname == login][0]
        self.assertEqual(config.USERS[login]['lastname'], user.lastname)
        self.assertEqual(config.USERS[login]['email'], user.mail)

    def logout(self):
        url = config.GATEWAY_URL + '/auth/logout/'
        requests.get(url)

    def login(self, username, password, redirect='/'):
        url = config.GATEWAY_URL + "/auth/login"
        data = {'username': username, 'password': password, 'back': redirect}
        return requests.post(url, data=data)

    def test_login_redirect_to_jenkins(self):
        """ Verify the user creation and the login
        """
        self.logout()
        url = config.GATEWAY_URL + "jenkins/"
        quoted_url = urllib2.quote(url, safe='')
        response = self.login('user5', config.ADMIN_PASSWORD, quoted_url)

        self.assertEqual(url, response.url)
        # verify if user is created in gerrit and redmine
        self.verify_userdata_gerrit('user5')
        if is_present("SFRedmine"):
            self.verify_userdata_redmine('user5')

    @skipIfServiceMissing('SFRedmine')
    def test_login_redirect_to_redmine(self):
        """ Verify the redirect to redmine project page
        """
        self.logout()
        url = config.GATEWAY_URL + "redmine/projects"
        quoted_url = urllib2.quote(url, safe='')
        response = self.login('user5', config.ADMIN_PASSWORD, quoted_url)

        self.assertEqual(url, response.url)
        # verify if user is created in gerrit and redmine
        self.verify_userdata_gerrit('user5')
        self.verify_userdata_redmine('user5')

    def test_invalid_user_login(self):
        """ Try to login with an invalid user
        """
        self.logout()
        response = self.login('toto', 'nopass', '/')
        self.assertEqual(response.status_code, 401)

    def test_hook_user_login(self):
        """ Functional test when trying to login with service user
        """
        self.logout()
        response = self.login(config.HOOK_USER, config.HOOK_USER_PASSWORD, '/')
        self.assertTrue(response.status_code < 400)

    def test_create_local_user_and_login(self):
        """ Try to create a local user then login
        """
        try:
            self.msu.create_user('Flea', 'RHCP', '*****@*****.**')
        except NotImplementedError:
            skip("user management not supported in this version of managesf")
        self.logout()
        url = config.GATEWAY_URL + "/dashboard/"
        quoted_url = urllib2.quote(url, safe='')
        response = self.login('Flea', 'RHCP', quoted_url)
        self.assertEqual(url, response.url)

    @skipIfServiceMissing('SFRedmine')
    def test_nonmember_backlog_permissions(self):
        """Make sure project non members can see the backlog and add
        stories"""
        # default value, skip gracefully if it cannot be found
        try:
            non_member_role = self.rm.r.role.get(1)
            assert non_member_role.name == 'Non member'
        except:
            self.skipTest("Could not fetch non-member permissions")
        self.assertTrue('view_master_backlog' in non_member_role.permissions)
        self.assertTrue('create_stories' in non_member_role.permissions)

    def test_delete_user_in_backends_by_username(self):
        """ Delete a user previously registered user by username
        """
        # first, create a user and register it with services
        try:
            self.msu.create_user('bootsy', 'collins', '*****@*****.**')
        except NotImplementedError:
            skip("user management not supported in this version of managesf")
        self.logout()
        self.login('bootsy', 'collins', config.GATEWAY_URL)
        # make sure user is in redmine and gerrit
        self.assertEqual('*****@*****.**',
                         self.gu.get_account('bootsy').get('email'))
        if is_present("SFRedmine"):
            users = self.rm.r.user.filter(limit=20)
            users = [u for u in users if u.firstname == 'bootsy']
            self.assertEqual(1, len(users))
            user = users[0]
            self.assertEqual('*****@*****.**', user.mail)
        # now suppress it
        del_url = config.GATEWAY_URL + 'manage/services_users/?username=bootsy'
        # try with a a non-admin user, it should not work ...
        auth_cookie = get_cookie(config.GATEWAY_HOST, 'user5',
                                 config.ADMIN_PASSWORD)
        d = requests.delete(del_url, cookies={'auth_pubtkt': auth_cookie})
        self.assertEqual(401, int(d.status_code))
        # try with an admin ...
        auth_cookie = config.USERS[config.ADMIN_USER]['auth_cookie']
        d = requests.delete(del_url, cookies={'auth_pubtkt': auth_cookie})
        self.assertTrue(int(d.status_code) < 400, d.status_code)
        # make sure the user does not exist anymore
        self.assertEqual(False, self.gu.get_account('bootsy'))
        if is_present("SFRedmine"):
            users = self.rm.r.user.filter(limit=20)
            self.assertEqual(
                0, len([u for u in users if u.firstname == 'bootsy']))

    def test_delete_user_in_backends_by_email(self):
        """ Delete a user previously registered user by email
        """
        # first, create a user and register it with services
        try:
            self.msu.create_user('josh', 'homme', '*****@*****.**')
        except NotImplementedError:
            skip("user management not supported in this version of managesf")
        self.logout()
        self.login('josh', 'homme', config.GATEWAY_URL)
        # make sure user is in redmine and gerrit
        self.assertEqual('*****@*****.**',
                         self.gu.get_account('josh').get('email'))
        if is_present("SFRedmine"):
            users = self.rm.r.user.filter(limit=20)
            users = [u for u in users if u.firstname == 'josh']
            self.assertEqual(1, len(users))
            user = users[0]
            self.assertEqual('*****@*****.**', user.mail)
        # now suppress it
        del_url = config.GATEWAY_URL +\
            'manage/services_users/[email protected]'
        auth_cookie = config.USERS[config.ADMIN_USER]['auth_cookie']
        d = requests.delete(del_url, cookies={'auth_pubtkt': auth_cookie})
        self.assertTrue(int(d.status_code) < 400, d.status_code)
        # make sure the user does not exist anymore
        self.assertEqual(False, self.gu.get_account('josh'))
        if is_present("SFRedmine"):
            users = self.rm.r.user.filter(limit=20)
            self.assertEqual(0,
                             len([u for u in users if u.firstname == 'josh']))

    def test_delete_in_backend_and_recreate(self):
        """Make sure we can recreate a user but as a different one"""
        # first, create a user and register it with services
        try:
            self.msu.create_user('freddie', 'mercury', '*****@*****.**')
        except NotImplementedError:
            skip("user management not supported in this version of managesf")
        self.logout()
        self.login('freddie', 'mercury', config.GATEWAY_URL)
        gerrit_id = self.gu.get_account('freddie').get('_account_id')
        if is_present("SFRedmine"):
            users = self.rm.r.user.filter(limit=20)
            user = [u for u in users if u.firstname == 'freddie'][0]
            redmine_id = user.id
        del_url = config.GATEWAY_URL +\
            'manage/services_users/[email protected]'
        auth_cookie = config.USERS[config.ADMIN_USER]['auth_cookie']
        d = requests.delete(del_url, cookies={'auth_pubtkt': auth_cookie})
        self.assertTrue(int(d.status_code) < 400, d.status_code)
        self.assertEqual(False, self.gu.get_account('freddie'))
        if is_present("SFRedmine"):
            users = self.rm.r.user.filter(limit=20)
            self.assertEqual(
                0, len([u for u in users if u.firstname == 'freddie']))
        # recreate the user in the backends
        time.sleep(5)
        self.logout()
        self.login('freddie', 'mercury', config.GATEWAY_URL)
        new_gerrit_id = self.gu.get_account('freddie').get('_account_id')
        self.assertTrue(gerrit_id != new_gerrit_id)
        if is_present("SFRedmine"):
            users = self.rm.r.user.filter(limit=20)
            user = [u for u in users if u.firstname == 'freddie'][0]
            new_redmine_id = user.id
            self.assertTrue(redmine_id != new_redmine_id)
class TestProjectMembership(Base):
    """ Functional tests that validate adding or deleting
    users to project groups using managesf.
    """

    @classmethod
    def setUpClass(cls):
        cls.msu = ManageSfUtils(config.GATEWAY_URL)

    @classmethod
    def tearDownClass(cls):
        pass

    def setUp(self):
        self.projects = []
        self.rm = RedmineUtils(
            config.GATEWAY_URL + "/redmine/", auth_cookie=config.USERS[config.ADMIN_USER]["auth_cookie"]
        )
        self.gu = GerritUtils(config.GATEWAY_URL, auth_cookie=config.USERS[config.ADMIN_USER]["auth_cookie"])

    def tearDown(self):
        for name in self.projects:
            self.msu.deleteProject(name, config.ADMIN_USER)

    def create_project(self, name, user, options=None):
        self.msu.createProject(name, user, options)
        self.projects.append(name)

    def test_admin_manage_project_members(self):
        """ Test admin can add and delete users from all project groups
        """
        pname = "p_%s" % create_random_str()
        self.create_project(pname, config.ADMIN_USER)
        # Gerrit part
        self.assertTrue(self.gu.project_exists(pname))
        self.assertTrue(self.gu.group_exists("%s-ptl" % pname))
        self.assertTrue(self.gu.group_exists("%s-core" % pname))
        groups = "ptl-group core-group"
        # Add user2 to ptl and core groups
        self.msu.addUsertoProjectGroups(config.ADMIN_USER, pname, config.USER_2, groups)
        # Test if user2 exists in ptl and core groups
        self.assertTrue(self.gu.member_in_group(config.USER_2, "%s-ptl" % pname))
        self.assertTrue(self.gu.member_in_group(config.USER_2, "%s-core" % pname))
        # Redmine part
        self.assertTrue(self.rm.check_user_role(pname, config.USER_2, "Manager"))
        self.assertTrue(self.rm.check_user_role(pname, config.USER_2, "Developer"))

        # Delete user2 from project groups
        self.msu.deleteUserFromProjectGroups(config.ADMIN_USER, pname, config.USER_2)
        # Test if user exists in ptl and core groups
        self.assertFalse(self.gu.member_in_group(config.USER_2, "%s-ptl" % pname))
        self.assertFalse(self.gu.member_in_group(config.USER_2, "%s-core" % pname))
        # Redmine part
        self.assertFalse(self.rm.check_user_role(pname, config.USER_2, "Manager"))
        self.assertFalse(self.rm.check_user_role(pname, config.USER_2, "Developer"))

    def test_ptl_manage_project_members(self):
        """ Test ptl can add and delete users from all project groups
        """
        # Let user2 create the project, so he will be ptl for this project
        pname = "p_%s" % create_random_str()
        self.create_project(pname, config.USER_2)
        # Gerrit part
        self.assertTrue(self.gu.project_exists(pname))
        self.assertTrue(self.gu.group_exists("%s-ptl" % pname))
        self.assertTrue(self.gu.group_exists("%s-core" % pname))
        groups = "ptl-group core-group"

        # ptl should be ale to add users to all groups
        # so user2 should be able to add user3 to ptl and core groups
        self.msu.addUsertoProjectGroups(config.USER_2, pname, config.USER_3, groups)
        # Test if user3 exists in ptl and core groups
        self.assertTrue(self.gu.member_in_group(config.USER_3, "%s-ptl" % pname))
        self.assertTrue(self.gu.member_in_group(config.USER_3, "%s-core" % pname))
        # Redmine part
        self.assertTrue(self.rm.check_user_role(pname, config.USER_3, "Manager"))
        self.assertTrue(self.rm.check_user_role(pname, config.USER_3, "Developer"))

        # ptl should be able to remove users from all groups
        self.msu.deleteUserFromProjectGroups(config.USER_2, pname, config.USER_3)
        # user3 shouldn't exist in any group
        self.assertFalse(self.gu.member_in_group(config.USER_3, "%s-ptl" % pname))
        self.assertFalse(self.gu.member_in_group(config.USER_3, "%s-core" % pname))
        # Redmine part
        self.assertFalse(self.rm.check_user_role(pname, config.USER_3, "Manager"))
        self.assertFalse(self.rm.check_user_role(pname, config.USER_3, "Developer"))

    def test_core_manage_project_members(self):
        """ Test core can add and delete users to core group
        """
        # let admin create the project
        pname = "p_%s" % create_random_str()
        self.create_project(pname, config.ADMIN_USER)
        # Gerrit part
        self.assertTrue(self.gu.project_exists(pname))
        self.assertTrue(self.gu.group_exists("%s-ptl" % pname))
        self.assertTrue(self.gu.group_exists("%s-core" % pname))
        groups = "core-group"

        # Add user2 as core user
        self.msu.addUsertoProjectGroups(config.ADMIN_USER, pname, config.USER_2, groups)
        # Test if user2 exists in core group
        self.assertTrue(self.gu.member_in_group(config.USER_2, "%s-core" % pname))
        # Redmine part
        self.assertTrue(self.rm.check_user_role(pname, config.USER_2, "Developer"))

        groups = "core-group"
        # core should be ale to add users to only core group and not ptl group
        # so user2 should be able to add user3 to only core group and not
        # ptl group
        self.msu.addUsertoProjectGroups(config.USER_2, pname, config.USER_3, groups)
        # user3 should exist in core group
        self.assertTrue(self.gu.member_in_group(config.USER_3, "%s-core" % pname))
        # Redmine part
        self.assertTrue(self.rm.check_user_role(pname, config.USER_3, "Developer"))

        groups = "ptl-group"
        # core should not be allowed to add users to ptl group
        # so user2 should not be able to add user3 to ptl group
        self.msu.addUsertoProjectGroups(config.USER_2, pname, config.USER_3, groups)
        # user3 shouldn't exist in ptl group
        self.assertFalse(self.gu.member_in_group(config.USER_3, "%s-ptl" % pname))
        # Redmine part
        self.assertFalse(self.rm.check_user_role(pname, config.USER_3, "Manager"))

        # core should be able to remove users from core group
        group = "core-group"
        self.msu.deleteUserFromProjectGroups(config.USER_2, pname, config.USER_3, group)
        # user3 shouldn't exist in core group
        self.assertFalse(self.gu.member_in_group(config.USER_3, "%s-core" % pname))
        # Redmine part
        self.assertFalse(self.rm.check_user_role(pname, config.USER_3, "Developer"))

    def test_non_member_manage_project_members(self):
        """ Test non project members can add and delete users to core group
        """
        # Let admin create the project
        pname = "p_%s" % create_random_str()
        self.create_project(pname, config.ADMIN_USER)
        # Gerrit part
        self.assertTrue(self.gu.project_exists(pname))
        self.assertTrue(self.gu.group_exists("%s-ptl" % pname))
        self.assertTrue(self.gu.group_exists("%s-core" % pname))

        # non project meber can't add user to core group
        # user2 can't add user3 to core group
        groups = "core-group"
        self.msu.addUsertoProjectGroups(config.USER_2, pname, config.USER_3, groups)
        # user3 shouldn't exist in core group
        self.assertFalse(self.gu.member_in_group(config.USER_3, "%s-core" % pname))
        # Redmine part
        self.assertFalse(self.rm.check_user_role(pname, config.USER_3, "Developer"))

        groups = "ptl-group"
        # non project meber can't add usr to ptl group
        # user2 can't add user3 to ptl group
        self.msu.addUsertoProjectGroups(config.USER_2, pname, config.USER_3, groups)
        # user3 shouldn't exist in ptl group
        self.assertFalse(self.gu.member_in_group(config.USER_3, "%s-ptl" % pname))
        # Redmine part
        self.assertFalse(self.rm.check_user_role(pname, config.USER_3, "Manager"))

        # non project meber can't delete usr from any group.
        # Let admin add user3 to ptl and core groups,
        # then try to remove user3 from ptl and core groups by
        # user2 (i.e non member user)
        groups = "ptl-group core-group"
        self.msu.addUsertoProjectGroups(config.ADMIN_USER, pname, config.USER_3, groups)
        # non-admin user(user2) can't remove users from project groups
        self.msu.deleteUserFromProjectGroups(config.USER_2, pname, config.USER_3)
        # user3 should exist in ptl and core group
        self.assertTrue(self.gu.member_in_group(config.USER_3, "%s-ptl" % pname))
        self.assertTrue(self.gu.member_in_group(config.USER_3, "%s-core" % pname))
        # Redmine part
        self.assertTrue(self.rm.check_user_role(pname, config.USER_3, "Manager"))
        self.assertTrue(self.rm.check_user_role(pname, config.USER_3, "Developer"))

    def test_manage_project_members_for_dev_group(self):
        """ Add and Delete users from dev group by admin, ptl, core,
            dev and non members
        """
        pname = "p_%s" % create_random_str()
        options = {"private": ""}
        self.create_project(pname, config.ADMIN_USER, options=options)
        # Gerrit part
        self.assertTrue(self.gu.project_exists(pname))
        self.assertTrue(self.gu.group_exists("%s-ptl" % pname))
        self.assertTrue(self.gu.group_exists("%s-core" % pname))
        self.assertTrue(self.gu.group_exists("%s-dev" % pname))
        self.assertTrue(self.gu.member_in_group(config.ADMIN_USER, "%s-dev" % pname))
        # Redmine part
        self.assertTrue(self.rm.project_exists(pname))
        self.assertTrue(self.rm.check_user_role(pname, config.ADMIN_USER, "Developer"))

        # Admin should add user to dev group
        groups = "dev-group"
        self.msu.addUsertoProjectGroups(config.ADMIN_USER, pname, config.USER_3, groups)
        # Test if user3 exists in dev group
        self.assertTrue(self.gu.member_in_group(config.USER_3, "%s-dev" % pname))
        # Redmine part
        self.assertTrue(self.rm.check_user_role(pname, config.USER_3, "Developer"))

        # admin should be able to remove users from dev group
        self.msu.deleteUserFromProjectGroups(config.ADMIN_USER, pname, config.USER_3)
        # user3 shouldn't exist in dev group
        self.assertFalse(self.gu.member_in_group(config.USER_3, "%s-dev" % pname))
        # Redmine part
        self.assertFalse(self.rm.check_user_role(pname, config.USER_3, "Developer"))

        # ptl should add user to dev group
        # let admin add user2 as ptl
        groups = "ptl-group"
        self.msu.addUsertoProjectGroups(config.ADMIN_USER, pname, config.USER_2, groups)
        groups = "dev-group"
        self.msu.addUsertoProjectGroups(config.USER_2, pname, config.USER_3, groups)
        # Test if user3 exists in dev group
        self.assertTrue(self.gu.member_in_group(config.USER_3, "%s-dev" % pname))
        # Redmine part
        self.assertTrue(self.rm.check_user_role(pname, config.USER_3, "Developer"))

        # ptl should be able to remove users from dev group
        self.msu.deleteUserFromProjectGroups(config.USER_2, pname, config.USER_3)
        # user3 shouldn't exist in dev group
        self.assertFalse(self.gu.member_in_group(config.USER_3, "%s-dev" % pname))
        # Redmine part
        self.assertFalse(self.rm.check_user_role(pname, config.USER_3, "Developer"))
        # Remove user2 as ptl
        self.msu.deleteUserFromProjectGroups(config.ADMIN_USER, pname, config.USER_2)

        # core should add user to dev group
        # let admin add user2 as core
        groups = "core-group"
        self.msu.addUsertoProjectGroups(config.ADMIN_USER, pname, config.USER_2, groups)
        groups = "dev-group"
        self.msu.addUsertoProjectGroups(config.USER_2, pname, config.USER_3, groups)
        # Test if user3 exists in dev group
        self.assertTrue(self.gu.member_in_group(config.USER_3, "%s-dev" % pname))
        # Redmine part
        self.assertTrue(self.rm.check_user_role(pname, config.USER_3, "Developer"))

        # core should be able to remove users from dev group
        group = "dev-group"
        self.msu.deleteUserFromProjectGroups(config.USER_2, pname, config.USER_3, group)
        # user3 shouldn't exist in dev group
        self.assertFalse(self.gu.member_in_group(config.USER_3, "%s-dev" % pname))
        # Redmine part
        self.assertFalse(self.rm.check_user_role(pname, config.USER_3, "Developer"))
        # Remove user2 as core
        self.msu.deleteUserFromProjectGroups(config.ADMIN_USER, pname, config.USER_2)

        # let admin add user2 as developer
        groups = "dev-group"
        self.msu.addUsertoProjectGroups(config.ADMIN_USER, pname, config.USER_2, groups)
        # dev user should be able to add a new user to dev group
        groups = "dev-group"
        self.msu.addUsertoProjectGroups(config.USER_2, pname, config.USER_3, groups)
        # Test if user3 exists in dev group
        self.assertTrue(self.gu.member_in_group(config.USER_3, "%s-dev" % pname))
        # Redmine part
        self.assertTrue(self.rm.check_user_role(pname, config.USER_3, "Developer"))

        # developer should be able to remove users from dev group
        group = "dev-group"
        self.msu.deleteUserFromProjectGroups(config.USER_2, pname, config.USER_3, group)
        # user3 shouldn't exist in dev group
        self.assertFalse(self.gu.member_in_group(config.USER_3, "%s-dev" % pname))
        # Redmine part
        self.assertFalse(self.rm.check_user_role(pname, config.USER_3, "Developer"))
        # Remove user2 ifrom dev group
        self.msu.deleteUserFromProjectGroups(config.ADMIN_USER, pname, config.USER_2)
class TestGerritHooks(Base):
    """ Functional tests that validate Gerrit hooks.
    """
    @classmethod
    def setUpClass(cls):
        cls.msu = ManageSfUtils(config.GATEWAY_URL)

    @classmethod
    def tearDownClass(cls):
        pass

    def setUp(self):
        self.projects = []
        self.dirs_to_delete = []
        self.issues = []
        self.u = config.ADMIN_USER
        self.u2 = config.USER_2
        self.rm = RedmineUtils(
            config.GATEWAY_URL + "/redmine/",
            auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
        self.gu = GerritUtils(config.GATEWAY_URL,
                              auth_cookie=config.USERS[self.u]['auth_cookie'])
        self.gu2 = GerritUtils(
            config.GATEWAY_URL,
            auth_cookie=config.USERS[self.u2]['auth_cookie'])
        self.gu.add_pubkey(config.USERS[self.u]["pubkey"])
        priv_key_path = set_private_key(config.USERS[self.u]["privkey"])
        self.gitu = GerritGitUtils(self.u, priv_key_path,
                                   config.USERS[self.u]['email'])

    def tearDown(self):
        for issue in self.issues:
            self.rm.delete_issue(issue)
        for name in self.projects:
            self.msu.deleteProject(name, self.u)
        for dirs in self.dirs_to_delete:
            shutil.rmtree(dirs)

    def create_project(self, name, user, options=None):
        self.msu.createProject(name, user, options)
        self.projects.append(name)

    def _test_update_issue_hooks(self, comment_template, status):
        """ A referenced issue in commit msg triggers the hook
        """
        pname = 'p_%s' % create_random_str()

        # Be sure the project does not exist
        self.msu.deleteProject(pname, self.u)

        # Create the project
        self.create_project(pname, self.u)
        # Put USER_2 as core for the project
        self.gu.add_group_member(self.u2, "%s-core" % pname)

        # Create an issue on the project
        issue_id = self.rm.create_issue(pname, "There is a problem")

        # Clone and commit something
        url = "ssh://%s@%s:29418/%s" % (self.u, config.GATEWAY_HOST, pname)
        clone_dir = self.gitu.clone(url, pname)
        cmt_msg = comment_template % issue_id
        self.gitu.add_commit_and_publish(clone_dir, 'master', cmt_msg)

        # Check issue status (Gerrit hook updates the issue to in progress)
        attempt = 0
        while True:
            if self.rm.test_issue_status(issue_id, 'In Progress'):
                break
            if attempt > 10:
                break
            time.sleep(1)
            attempt += 1
        self.assertTrue(self.rm.test_issue_status(issue_id, 'In Progress'))
        self._test_merging(pname, issue_id, status)

    def _test_merging(self, pname, issue_id, status):
        # Get the change id and merge the patch
        change_ids = self.gu.get_my_changes_for_project(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", "Workflow", "1")
        self.gu.submit_change_note(change_id, "current", "Verified", "2")
        self.gu2.submit_change_note(change_id, "current", "Code-Review", "2")
        self.assertTrue(self.gu.submit_patch(change_id, "current"))

        # Check issue status (Gerrit hook updates the issue to in progress)
        attempt = 0
        while True:
            if self.rm.test_issue_status(issue_id, status):
                break
            if attempt > 10:
                break
            time.sleep(1)
            attempt += 1
        self.assertTrue(self.rm.test_issue_status(issue_id, status))

    def test_gerrit_hook(self):
        """test various commit messages triggering a hook"""
        for template, final_status in TEST_MSGS:
            self._test_update_issue_hooks(template, final_status)
Beispiel #46
0
class TestProjectReplication(Base):
    """ Functional tests to verify the gerrit replication feature
    """
    def setUp(self):
        super(TestProjectReplication, self).setUp()
        self.ru = ResourcesUtils()
        self.un = config.ADMIN_USER
        self.ju = JenkinsUtils()
        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):
        super(TestProjectReplication, self).tearDown()
        self.delete_config_section(self.un, self.pname)
        self.delete_mirror_repo(self.pname)
        self.ru.direct_delete_repo(self.pname)
        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):
        logger.info("Create repo to for testing replication %s" % name)
        self.ru.direct_create_repo(name)

    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):
        logger.info("Delete mirror repo created by the replication")
        mirror_path = '/var/lib/gerrit/tmp/%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):
        logger.info("Add the replication config section")
        host = '%s@%s' % (config.GERRIT_USER, config.GATEWAY_HOST)
        mirror_repo_path = '/var/lib/gerrit/tmp/\${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
        change_sha = self.gitu_admin.direct_push_branch(
            self.config_clone_dir, 'master')
        logger.info("Waiting for config-update on %s" % change_sha)
        self.ju.wait_for_config_update(change_sha)
        cmd = [
            'ssh',
            'gerrit.%s' % config.GATEWAY_HOST, 'grep', 'test_project',
            '/etc/gerrit/replication.config'
        ]
        logger.info("Wait for the replication config section to land")
        _, code = self.ssh_run_cmd(config.SERVICE_PRIV_KEY_PATH, 'root',
                                   config.GATEWAY_HOST, cmd)
        if code == 0:
            return
        raise Exception('replication.config file has not been updated (add)')

    def delete_config_section(self, user, project):
        logger.info("Remove the replication config section")
        url = "ssh://%s@%s:29418/config" % (self.un, config.GATEWAY_HOST)
        self.config_clone_dir = self.gitu_admin.clone(url,
                                                      'config',
                                                      config_review=True)
        sha = open("%s/.git/refs/heads/master" %
                   self.config_clone_dir).read().strip()
        path = os.path.join(self.config_clone_dir, 'gerrit/replication.config')
        call("git config -f %s --remove-section remote.test_project" % path,
             shell=True)
        change_sha = self.gitu_admin.add_commit_for_all_new_additions(
            self.config_clone_dir, "Remove replication test section")
        # The direct push will trigger the config-update job
        # as we commit through 29418
        if change_sha == sha:
            # Nothing have been changed/Nothing to publish
            return
        change_sha = self.gitu_admin.direct_push_branch(
            self.config_clone_dir, 'master')
        logger.info("Waiting for config-update on %s" % change_sha)
        self.ju.wait_for_config_update(change_sha)
        cmd = [
            'ssh',
            'gerrit.%s' % config.GATEWAY_HOST, 'grep', 'test_project',
            '/etc/gerrit/replication.config'
        ]
        _, code = self.ssh_run_cmd(config.SERVICE_PRIV_KEY_PATH, 'root',
                                   config.GATEWAY_HOST, cmd)
        if code != 0:
            return
        raise Exception('replication.config has not been updated (rm)')

    def mirror_clone_and_check_files(self, url, pname):
        for retry in xrange(50):
            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):
                logger.info("Files in the mirror repo: %s" % os.listdir(clone))
            if os.path.isdir(clone) and \
               os.path.isfile(os.path.join(clone, '.gitreview')):
                break
            else:
                time.sleep(3)
        self.assertTrue(os.path.exists(os.path.join(clone, '.gitreview')))

    def test_replication(self):
        """ Test gerrit replication for review process
        """
        # Create the project
        self.create_project(self.pname)

        # Be sure sftests.com host key is inside the known_hosts
        cmds = [[
            'ssh',
            'gerrit.%s' % config.GATEWAY_HOST, 'ssh-keyscan', 'sftests.com',
            '>', '/var/lib/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/var/lib/gerrit/tmp/" % (
            'root', config.GATEWAY_HOST)
        repo_url = self.managesf_repo_path + '%s.git' % self.pname
        logger.info("Wait for the replication to happen")
        self.mirror_clone_and_check_files(repo_url, self.pname)
class TestProjectTestsWorkflow(Base):
    """ Functional tests to verify the configuration of a project test
    """
    @classmethod
    def setUpClass(cls):
        cls.msu = ManageSfUtils(config.GATEWAY_URL)
        cls.sample_project_dir = \
            os.path.join(config.SF_TESTS_DIR, "sample_project/")

    @classmethod
    def tearDownClass(cls):
        pass

    def setUp(self):
        self.projects = []
        self.dirs_to_delete = []
        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.ju = JenkinsUtils()
        self.gu.add_pubkey(config.USERS[self.un]["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'])
        # Clone the config repo and make change to it
        # in order to test the new sample_project
        self.config_clone_dir = self.clone_as_admin("config")
        self.original_layout = file(os.path.join(
            self.config_clone_dir, "zuul/layout.yaml")).read()
        self.original_zuul_projects = file(os.path.join(
            self.config_clone_dir, "zuul/projects.yaml")).read()
        self.original_project = file(os.path.join(
            self.config_clone_dir, "jobs/projects.yaml")).read()
        # Put USER_2 as core for config project
        self.gu.add_group_member(config.USER_2, "config-core")

    def tearDown(self):
        self.restore_config_repo(self.original_layout,
                                 self.original_project,
                                 self.original_zuul_projects)
        for name in self.projects:
            self.msu.deleteProject(name,
                                   config.ADMIN_USER)
        for dirs in self.dirs_to_delete:
            shutil.rmtree(dirs)

    def assert_reviewer_approvals(self, change_id, value):
        approvals = {}
        for _ in range(90):
            approvals = self.gu.get_reviewer_approvals(change_id,
                                                       'jenkins')
            if approvals and approvals.get('Verified') == value:
                break
            time.sleep(1)
        self.assertEqual(value, approvals.get('Verified'))

    def clone_as_admin(self, pname):
        url = "ssh://%s@%s:29418/%s" % (self.un, config.GATEWAY_HOST,
                                        pname)
        clone_dir = self.gitu_admin.clone(url, pname)
        if os.path.dirname(clone_dir) not in self.dirs_to_delete:
            self.dirs_to_delete.append(os.path.dirname(clone_dir))
        return clone_dir

    def restore_config_repo(self, layout, project, zuul):
        file(os.path.join(
            self.config_clone_dir, "zuul/layout.yaml"), 'w').write(
            layout)
        file(os.path.join(
            self.config_clone_dir, "zuul/projects.yaml"), 'w').write(
            zuul)
        file(os.path.join(
            self.config_clone_dir, "jobs/projects.yaml"), 'w').write(
            project)
        self.commit_direct_push_as_admin(
            self.config_clone_dir,
            "Restore layout.yaml and projects.yaml")

    def commit_direct_push_as_admin(self, clone_dir, msg):
        # Stage, commit and direct push the additions on master
        self.gitu_admin.add_commit_for_all_new_additions(clone_dir, msg)
        self.gitu_admin.direct_push_branch(clone_dir, 'master')

    def push_review_as_admin(self, clone_dir, msg):
        # Stage, commit and direct push the additions on master
        self.gitu_admin.add_commit_for_all_new_additions(clone_dir, msg)
        self.gitu_admin.review_push_branch(clone_dir, 'master')

    def create_project(self, name, user,
                       options=None):
        self.msu.createProject(name, user,
                               options)
        self.projects.append(name)

    def test_check_project_test_workflow(self):
        """ Validate new project to test via zuul layout.yaml
        """
        # We want to create a project, provide project source
        # code with tests. We then configure zuul/jjb to handle the
        # run of the test cases. We then validate Gerrit has been
        # updated about the test results
        # We use the sample-project (that already exists)

        pname = 'test_workflow_%s' % create_random_str()
        # Be sure the project does not exist
        self.msu.deleteProject(pname,
                               config.ADMIN_USER)
        # Create it
        self.create_project(pname, config.ADMIN_USER)

        # Add the sample-project to the empty repository
        clone_dir = self.clone_as_admin(pname)
        copytree(self.sample_project_dir, clone_dir)
        self.commit_direct_push_as_admin(clone_dir, "Add the sample project")

        # Change to config/zuul/layout.yaml and jobs/projects.yaml
        # in order to test the new project
        ycontent = file(os.path.join(
            self.config_clone_dir, "zuul/projects.yaml")).read()
        file(os.path.join(
            self.config_clone_dir, "zuul/projects.yaml"), 'w').write(
            ycontent.replace("zuul-demo", pname),
        )
        ycontent2 = load(file(os.path.join(
            self.config_clone_dir, "jobs/projects.yaml")).read())
        sp2 = copy.deepcopy(
            [p for p in ycontent2 if 'project' in p and
                p['project']['name'] == 'zuul-demo'][0])
        sp2['project']['name'] = pname
        ycontent2.append(sp2)
        file(os.path.join(
            self.config_clone_dir, "jobs/projects.yaml"), 'w').write(
            dump(ycontent2))

        # Retrieve the previous build number for config-check
        last_success_build_num_ch = \
            self.ju.get_last_build_number("config-check",
                                          "lastSuccessfulBuild")
        # Retrieve the previous build number for config-update
        last_success_build_num_cu = \
            self.ju.get_last_build_number("config-update",
                                          "lastSuccessfulBuild")

        # Send review (config-check) will be triggered
        self.push_review_as_admin(
            self.config_clone_dir,
            "Add config definition in Zuul/JJB config for %s" % pname)

        # Wait for config-check to finish and verify the success
        self.ju.wait_till_job_completes("config-check",
                                        last_success_build_num_ch,
                                        "lastSuccessfulBuild")

        last_build_num_ch, last_success_build_num_ch = 0, 1
        attempt = 0
        while last_build_num_ch != last_success_build_num_ch:
            if attempt >= 90:
                break
            time.sleep(1)
            last_build_num_ch = \
                self.ju.get_last_build_number("config-check",
                                              "lastBuild")
            last_success_build_num_ch = \
                self.ju.get_last_build_number("config-check",
                                              "lastSuccessfulBuild")
            attempt += 1

        self.assertEqual(last_build_num_ch, last_success_build_num_ch)
        # let some time to Zuul to update the test result to Gerrit.
        time.sleep(2)

        # Get the change id
        change_ids = self.gu.get_my_changes_for_project("config")
        self.assertGreater(len(change_ids), 0)
        change_id = change_ids[0]

        # Check whether zuul sets verified to +1 after running the tests
        # let some time to Zuul to update the test result to Gerrit.
        self.assert_reviewer_approvals(change_id, '+1')

        # review the change
        self.gu2.submit_change_note(change_id, "current", "Code-Review", "2")
        self.gu2.submit_change_note(change_id, "current", "Workflow", "1")

        # now zuul processes gate pipeline and runs config-check job
        # Wait for config-check to finish and verify the success
        self.ju.wait_till_job_completes("config-check",
                                        last_success_build_num_ch,
                                        "lastSuccessfulBuild")

        last_build_num_ch, last_success_build_num_ch = 0, 1
        attempt = 0
        while last_build_num_ch != last_success_build_num_ch:
            if attempt >= 90:
                break
            time.sleep(1)
            last_build_num_ch = \
                self.ju.get_last_build_number("config-check",
                                              "lastBuild")
            last_success_build_num_ch = \
                self.ju.get_last_build_number("config-check",
                                              "lastSuccessfulBuild")
            attempt += 1

        self.assertEqual(last_build_num_ch, last_success_build_num_ch)

        # Check whether zuul sets verified to +2 after running the tests
        # let some time to Zuul to update the test result to Gerrit.
        self.assert_reviewer_approvals(change_id, '+2')

        # verify whether zuul merged the patch
        change = self.gu.get_change('config', 'master', change_id)
        change_status = change['status']
        attempt = 0
        while change_status != 'MERGED':
            if attempt >= 90:
                break
            time.sleep(1)
            change = self.gu.get_change('config', 'master', change_id)
            change_status = change['status']
            attempt += 1
        self.assertEqual(change_status, 'MERGED')

        # Test post pipe line
        # as the patch is merged, post pieline should run config-update job
        # Wait for config-update to finish and verify the success
        self.ju.wait_till_job_completes("config-update",
                                        last_success_build_num_cu,
                                        "lastSuccessfulBuild")
        last_build_num_cu = \
            self.ju.get_last_build_number("config-update",
                                          "lastBuild")
        last_success_build_num_cu = \
            self.ju.get_last_build_number("config-update",
                                          "lastSuccessfulBuild")
        self.assertEqual(last_build_num_cu, last_success_build_num_cu)

        # Retrieve the prev build number for pname-unit-tests
        # Retrieve the prev build number for pname-functional-tests
        last_success_build_num_sp_ut = \
            self.ju.get_last_build_number("%s-unit-tests" % pname,
                                          "lastSuccessfulBuild")
        last_success_build_num_sp_ft = \
            self.ju.get_last_build_number("%s-functional-tests" % pname,
                                          "lastSuccessfulBuild")
        # Test config-update
        # config-update should have created jobs for pname
        # Trigger tests on pname
        # Send a review and check tests has been run
        self.gitu_admin.add_commit_and_publish(
            clone_dir, 'master', "Add useless file",
            self.un)
        # Wait for pname-unit-tests to finish and verify the success
        self.ju.wait_till_job_completes("%s-unit-tests" % pname,
                                        last_success_build_num_sp_ut,
                                        "lastSuccessfulBuild")
        # Wait for pname-functional-tests to end and check the success
        self.ju.wait_till_job_completes("%s-functional-tests" % pname,
                                        last_success_build_num_sp_ft,
                                        "lastSuccessfulBuild")
        # Check the unit tests succeed
        last_build_num_sp_ut = \
            self.ju.get_last_build_number("%s-unit-tests" % pname,
                                          "lastBuild")
        last_success_build_num_sp_ut = \
            self.ju.get_last_build_number("%s-unit-tests" % pname,
                                          "lastSuccessfulBuild")
        self.assertEqual(last_build_num_sp_ut, last_success_build_num_sp_ut)
        # Check the functional tests succeed
        last_build_num_sp_ft = \
            self.ju.get_last_build_number("%s-functional-tests" % pname,
                                          "lastBuild")
        last_success_build_num_sp_ft = \
            self.ju.get_last_build_number("%s-functional-tests" % pname,
                                          "lastSuccessfulBuild")
        self.assertEqual(last_build_num_sp_ft, last_success_build_num_sp_ft)

        # Get the change id
        change_ids = self.gu.get_my_changes_for_project(pname)
        self.assertGreater(len(change_ids), 0)
        change_id = change_ids[0]

        # let some time to Zuul to update the test result to Gerrit.
        for i in range(90):
            if "jenkins" in self.gu.get_reviewers(change_id):
                break
            time.sleep(1)

        self.assert_reviewer_approvals(change_id, '+1')
class TestUserdata(Base):
    @classmethod
    def setUpClass(cls):
        cls.msu = ManageSfUtils(config.GATEWAY_URL)

    @classmethod
    def tearDownClass(cls):
        pass

    def setUp(self):
        self.projects = []
        self.rm = RedmineUtils(
            config.GATEWAY_URL + "/redmine/",
            auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
        self.gu = GerritUtils(
            config.GATEWAY_URL,
            auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])

    def tearDown(self):
        for name in self.projects:
            self.msu.deleteProject(name,
                                   config.ADMIN_USER)

    def create_project(self, name, user, options=None,
                       cookie=None):
        self.msu.createProject(name, user, options,
                               cookie)
        self.projects.append(name)

    def verify_userdata_gerrit(self, login):
        # Now check that the correct data was stored in Gerrit
        data = self.gu.get_account(login)
        self.assertEqual(config.USERS[login]['lastname'], data.get('name'))
        self.assertEqual(config.USERS[login]['email'], data.get('email'))

    def verify_userdata_redmine(self, login):
        users = self.rm.r.user.filter(limit=10)
        user = [u for u in users if u.firstname == login][0]
        self.assertEqual(config.USERS[login]['lastname'], user.lastname)
        self.assertEqual(config.USERS[login]['email'], user.mail)

    def logout(self):
        url = config.GATEWAY_URL + '/auth/logout/'
        requests.get(url)

    def login(self, username, password, redirect='/'):
        url = config.GATEWAY_URL + "/auth/login"
        data = {'username': username,
                'password': password,
                'back': redirect}
        return requests.post(url, data=data)

    def test_login_redirect_to_jenkins(self):
        """ Verify the user creation and the login
        """
        self.logout()
        url = config.GATEWAY_URL + "jenkins/"
        quoted_url = urllib2.quote(url, safe='')
        response = self.login('user5', config.ADMIN_PASSWORD, quoted_url)

        self.assertEqual(url, response.url)
        # verify if user is created in gerrit and redmine
        self.verify_userdata_gerrit('user5')
        self.verify_userdata_redmine('user5')

    def test_login_redirect_to_redmine(self):
        """ Verify the redirect to redmine project page
        """
        self.logout()
        url = config.GATEWAY_URL + "redmine/projects"
        quoted_url = urllib2.quote(url, safe='')
        response = self.login('user5', config.ADMIN_PASSWORD, quoted_url)

        self.assertEqual(url, response.url)
        # verify if user is created in gerrit and redmine
        self.verify_userdata_gerrit('user5')
        self.verify_userdata_redmine('user5')

    def test_invalid_user_login(self):
        """ Try to login with an invalid user
        """
        self.logout()
        response = self.login('toto', 'nopass', '/')
        self.assertEqual(response.status_code, 401)

    def test_hook_user_login(self):
        """ Functional test when trying to login with service user
        """
        self.logout()
        response = self.login(config.HOOK_USER,
                              config.HOOK_USER_PASSWORD,
                              '/')
        self.assertTrue(response.status_code < 400)

    def test_create_local_user_and_login(self):
        """ Try to create a local user then login
        """
        try:
            self.msu.create_user('Flea', 'RHCP', '*****@*****.**')
        except NotImplementedError:
            skip("user management not supported in this version of managesf")
        self.logout()
        url = config.GATEWAY_URL + "redmine/projects"
        quoted_url = urllib2.quote(url, safe='')
        response = self.login('Flea', 'RHCP', quoted_url)
        self.assertEqual(url, response.url)

    def test_nonmember_backlog_permissions(self):
        """Make sure project non members can see the backlog and add
        stories"""
        # default value, skip gracefully if it cannot be found
        try:
            non_member_role = self.rm.r.role.get(1)
            assert non_member_role.name == 'Non member'
        except:
            self.skipTest("Could not fetch non-member permissions")
        self.assertTrue('view_master_backlog' in non_member_role.permissions)
        self.assertTrue('create_stories' in non_member_role.permissions)
Beispiel #49
0
    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_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_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)
class TestUserdata(Base):
    @classmethod
    def setUpClass(cls):
        cls.msu = ManageSfUtils(config.GATEWAY_URL)

    @classmethod
    def tearDownClass(cls):
        pass

    def setUp(self):
        self.projects = []
        self.rm = get_issue_tracker_utils(
            auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
        self.gu = GerritUtils(
            config.GATEWAY_URL,
            auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])

    def tearDown(self):
        for name in self.projects:
            self.msu.deleteProject(name,
                                   config.ADMIN_USER)

    def create_project(self, name, user, options=None,
                       cookie=None):
        self.msu.createProject(name, user, options,
                               cookie)
        self.projects.append(name)

    def verify_userdata_gerrit(self, login):
        # Now check that the correct data was stored in Gerrit
        data = self.gu.get_account(login)
        self.assertEqual(config.USERS[login]['lastname'], data.get('name'))
        self.assertEqual(config.USERS[login]['email'], data.get('email'))

    def verify_userdata_issue_tracker(self, login):
        users = self.rm.active_users()
        user = [u for u in users if u[0] == login][0]
        self.assertEqual(login + " " + config.USERS[login]['lastname'],
                         user[2])
        self.assertEqual(config.USERS[login]['email'], user[1])

    def logout(self):
        url = config.GATEWAY_URL + '/auth/logout/'
        requests.get(url)

    def login(self, username, password, redirect='/'):
        url = config.GATEWAY_URL + "/auth/login"
        data = {'username': username,
                'password': password,
                'back': redirect}
        return requests.post(url, data=data)

    def test_login_redirect_to_jenkins(self):
        """ Verify the user creation and the login
        """
        self.logout()
        url = config.GATEWAY_URL + "/jenkins/"
        quoted_url = urllib2.quote(url, safe='')
        response = self.login('user5', config.ADMIN_PASSWORD, quoted_url)

        self.assertEqual(url, response.url)
        # verify if user is created in gerrit and tracker
        self.verify_userdata_gerrit('user5')
        if has_issue_tracker():
            self.verify_userdata_issue_tracker('user5')

    @skipIfIssueTrackerMissing()
    def test_login_redirect_to_issue_tracker(self):
        """ Verify the redirect to tracker project page
        """
        self.logout()
        url = self.rm.get_sf_projects_url()
        quoted_url = urllib2.quote(url, safe='')
        response = self.login('user5', config.ADMIN_PASSWORD, quoted_url)

        self.assertEqual(url, response.url)
        # verify if user is created in gerrit and issue tracker
        self.verify_userdata_gerrit('user5')
        self.verify_userdata_issue_tracker('user5')

    def test_invalid_user_login(self):
        """ Try to login with an invalid user
        """
        self.logout()
        response = self.login('toto', 'nopass', '/')
        self.assertEqual(response.status_code, 401)

    def test_hook_user_login(self):
        """ Functional test when trying to login with service user
        """
        self.logout()
        response = self.login(config.HOOK_USER,
                              config.HOOK_USER_PASSWORD,
                              '/')
        self.assertTrue(response.status_code < 400)

    def test_create_local_user_and_login(self):
        """ Try to create a local user then login
        """
        try:
            self.msu.create_user('Flea', 'RHCP', '*****@*****.**')
        except NotImplementedError:
            skip("user management not supported in this version of managesf")
        self.logout()
        url = config.GATEWAY_URL + "/dashboard/"
        quoted_url = urllib2.quote(url, safe='')
        response = self.login('Flea', 'RHCP', quoted_url)
        self.assertEqual(url, response.url)

    @skipIfIssueTrackerMissing()
    def test_nonmember_backlog_permissions(self):
        """Make sure project non members can see the backlog and add
        stories"""
        # default value, skip gracefully if it cannot be found
        try:
            non_member_role = self.rm.get_role(1)
            assert non_member_role.name == 'Non member'
        except:
            self.skipTest("Could not fetch non-member permissions")
        self.assertTrue('view_master_backlog' in non_member_role.permissions)
        self.assertTrue('create_stories' in non_member_role.permissions)

    def test_delete_user_in_backends_by_username(self):
        """ Delete a user previously registered user by username
        """
        # first, create a user and register it with services
        try:
            self.msu.create_user('bootsy', 'collins', '*****@*****.**')
        except NotImplementedError:
            skip("user management not supported in this version of managesf")
        self.logout()
        self.login('bootsy', 'collins', config.GATEWAY_URL)
        # make sure user is in redmine and gerrit
        self.assertEqual('*****@*****.**',
                         self.gu.get_account('bootsy').get('email'))
        if has_issue_tracker():
            users = self.rm.active_users()
            users = [u for u in users if u[0] == 'bootsy']
            self.assertEqual(1, len(users))
            user = users[0]
            self.assertEqual('*****@*****.**',
                             user[1])
        # now suppress it
        del_url = config.GATEWAY_URL +\
            '/manage/services_users/?username=bootsy'
        # try with a a non-admin user, it should not work ...
        auth_cookie = get_cookie(config.GATEWAY_HOST,
                                 'user5', config.ADMIN_PASSWORD)
        d = requests.delete(del_url,
                            cookies={'auth_pubtkt': auth_cookie})
        self.assertTrue(400 < int(d.status_code) < 500)
        # try with an admin ...
        auth_cookie = config.USERS[config.ADMIN_USER]['auth_cookie']
        d = requests.delete(del_url,
                            cookies={'auth_pubtkt': auth_cookie})
        self.assertTrue(int(d.status_code) < 400, d.status_code)
        # make sure the user does not exist anymore
        self.assertEqual(False,
                         self.gu.get_account('bootsy'))
        if has_issue_tracker():
            users = self.rm.active_users()
            self.assertEqual(0,
                             len([u for u in users
                                  if u[0] == 'bootsy']))

    def test_delete_user_in_backends_by_email(self):
        """ Delete a user previously registered user by email
        """
        # first, create a user and register it with services
        try:
            self.msu.create_user('josh', 'homme', '*****@*****.**')
        except NotImplementedError:
            skip("user management not supported in this version of managesf")
        self.logout()
        self.login('josh', 'homme', config.GATEWAY_URL)
        # make sure user is in redmine and gerrit
        self.assertEqual('*****@*****.**',
                         self.gu.get_account('josh').get('email'))
        if has_issue_tracker():
            users = self.rm.active_users()
            users = [u for u in users if u[0] == 'josh']
            self.assertEqual(1, len(users))
            user = users[0]
            self.assertEqual('*****@*****.**',
                             user[1])
        # now suppress it
        del_url = config.GATEWAY_URL +\
            '/manage/services_users/[email protected]'
        auth_cookie = config.USERS[config.ADMIN_USER]['auth_cookie']
        d = requests.delete(del_url,
                            cookies={'auth_pubtkt': auth_cookie})
        self.assertTrue(int(d.status_code) < 400, d.status_code)
        # make sure the user does not exist anymore
        self.assertEqual(False,
                         self.gu.get_account('josh'))
        if has_issue_tracker():
            users = self.rm.active_users()
            self.assertEqual(0,
                             len([u for u in users if u[0] == 'josh']))

    def test_delete_in_backend_and_recreate(self):
        """Make sure we can recreate a user but as a different one"""
        # first, create a user and register it with services
        try:
            self.msu.create_user('freddie', 'mercury', '*****@*****.**')
        except NotImplementedError:
            skip("user management not supported in this version of managesf")
        self.logout()
        self.login('freddie', 'mercury', config.GATEWAY_URL)
        gerrit_id = self.gu.get_account('freddie').get('_account_id')
        if has_issue_tracker():
            tracker_id = self.rm.get_user_id_by_username('freddie')
        del_url = config.GATEWAY_URL +\
            '/manage/services_users/[email protected]'
        auth_cookie = config.USERS[config.ADMIN_USER]['auth_cookie']
        d = requests.delete(del_url,
                            cookies={'auth_pubtkt': auth_cookie})
        self.assertTrue(int(d.status_code) < 400, d.status_code)
        self.assertEqual(False,
                         self.gu.get_account('freddie'))
        if has_issue_tracker():
            users = self.rm.active_users()
            self.assertEqual(0,
                             len([u for u in users
                                  if u[0] == 'freddie']))
        # recreate the user in the backends
        time.sleep(5)
        self.logout()
        self.login('freddie', 'mercury', config.GATEWAY_URL)
        new_gerrit_id = self.gu.get_account('freddie').get('_account_id')
        self.assertTrue(gerrit_id != new_gerrit_id)
        if has_issue_tracker():
            new_tracker_id = self.rm.get_user_id_by_username('freddie')
            self.assertTrue(tracker_id != new_tracker_id)
Beispiel #53
0
class SFchecker:
    """ This checker is only intended for testin
    SF backup/restore and update. It checks that the user
    data defined in resourses.yaml are present on the SF.

    Those data must have been provisioned by SFProvisioner.
    """
    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.gu = GerritUtils(
            'http://%s/' % config.GATEWAY_HOST,
            auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
        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 check_project(self, name):
        print " Check project %s exists ..." % name,
        if not self.gu.project_exists(name) or \
           (is_present('SFRedmine') and not self.rm.project_exists(name)):
            print "FAIL"
            exit(1)
        print "OK"

    def check_files_in_project(self, name, files):
        print " Check files(%s) exists in project ..." % ",".join(files),
        # TODO(fbo); use gateway host instead of gerrit host
        url = "ssh://%s@%s:29418/%s" % (config.ADMIN_USER,
                                        config.GATEWAY_HOST, name)
        clone_dir = self.ggu.clone(url, name, config_review=False)
        for f in files:
            if not os.path.isfile(os.path.join(clone_dir, f)):
                print "FAIL"
                exit(1)

    def check_issues_on_project(self, name, issues):
        print " Check that at least %s issues exists for that project ...," %\
            len(issues)
        current_issues = self.rm.get_issues_by_project(name)
        if len(current_issues) < len(issues):
            print "FAIL: expected %s, project has %s" % (
                len(issues), len(current_issues))
            exit(1)
        print "OK"

    def check_jenkins_jobs(self, name, jobnames):
        print " Check that jenkins jobs(%s) exists ..." % ",".join(jobnames),
        for jobname in jobnames:
            if not '%s_%s' % (name, jobname) in self.ju.list_jobs():
                print "FAIL"
                exit(1)
        print "OK"

    def check_reviews_on_project(self, name, issues):
        reviews = [i for i in issues if i['review']]
        print " Check that at least %s reviews exists for that project ..." %\
            len(reviews),
        pending_reviews = self.ggu.list_open_reviews(name, config.GATEWAY_HOST)
        if not len(pending_reviews) >= len(reviews):
            print "FAIL"
            exit(1)
        print "OK"

    def check_pads(self, amount):
        pass

    def check_pasties(self, amount):
        pass

    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 simple_login(self, user, password):
        """log as user"""
        return get_cookie(user, password)

    def check_users_list(self):
        print "Check that users are listable ...",
        users = [u['name'] for u in self.resources['users']]
        c = {'auth_pubtkt': config.USERS[config.ADMIN_USER]['auth_cookie']}
        url = 'http://%s/manage/project/membership/' % config.GATEWAY_HOST
        registered = requests.get(url,
                                  cookies=c).json()
        # usernames are in first position
        r_users = [u[0] for u in registered]
        if not set(users).issubset(set(r_users)):
            print "FAIL"
            exit(1)
        print "OK"

    def check_checksums(self):
        print "Check that expected file are there"
        checksum_list = yaml.load(file('/tmp/pc_checksums.yaml'))
        mismatch = False
        for f, checksum in checksum_list.items():
            c = self.compute_checksum(f)
            if c == checksum:
                print "Expected checksum (%s) for %s is OK." % (
                    checksum, f)
            else:
                print "Expected checksum (%s) for %s is WRONG (%s)." % (
                    checksum, f, c)
                print "New file is:"
                print "    %s" % self.read_file(f).replace("\n", "\n    ")
                mismatch = True
        if "checksum_warn_only" not in sys.argv and mismatch:
            sys.exit(1)

    def checker(self):
        self.check_checksums()
        self.check_users_list()
        for project in self.resources['projects']:
            print "Check user datas for %s" % project['name']
            self.check_project(project['name'])
            self.check_files_in_project(project['name'],
                                        [f['name'] for f in project['files']])
            if is_present('SFRedmine'):
                self.check_issues_on_project(project['name'],
                                             project['issues'])
            self.check_reviews_on_project(project['name'], project['issues'])
            self.check_jenkins_jobs(project['name'],
                                    [j['name'] for j in project['jobnames']])
        self.check_pads(2)
        self.check_pasties(2)
        for user in self.resources['local_users']:
            print "Check user %s can log in ..." % user['username'],
            if self.simple_login(user['username'],
                                 user['password']):
                print "OK"
            else:
                print "FAIL"
                exit(1)
class TestManageSF(Base):
    """ Functional tests that validate managesf features.
    Here we do basic verifications about project creation
    with managesf.
    """
    @classmethod
    def setUpClass(cls):
        cls.msu = ManageSfUtils(config.GATEWAY_URL)

    @classmethod
    def tearDownClass(cls):
        pass

    def setUp(self):
        self.projects = []
        self.dirs_to_delete = []
        self.rm = RedmineUtils(
            config.REDMINE_URL,
            auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
        self.gu = GerritUtils(
            'https://%s/' % config.GATEWAY_HOST,
            auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])

    def project_exists_ex(self, name, user):
        # Test here the project is "public"
        # ( Redmine API project detail does not return the private/public flag)
        rm = RedmineUtils(
            config.REDMINE_URL,
            auth_cookie=config.USERS[user]['auth_cookie'])
        try:
            return rm.project_exists(name)
        except Exception:
            return False

    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, user, options=None):
        self.msu.createProject(name, user, options)
        self.projects.append(name)

    def test_create_public_project_as_admin(self):
        """ Create public project on redmine and gerrit as admin
        """
        pname = 'p_%s' % create_random_str()
        self.create_project(pname, config.ADMIN_USER)
        # Gerrit part
        self.assertTrue(self.gu.project_exists(pname))
        self.assertTrue(self.gu.group_exists('%s-ptl' % pname))
        self.assertTrue(self.gu.group_exists('%s-core' % pname))
        # TODO(Project creator, as project owner, should only be in ptl group)
        self.assertTrue(
            self.gu.member_in_group(config.ADMIN_USER, '%s-ptl' % pname))
        self.assertTrue(
            self.gu.member_in_group(config.ADMIN_USER, '%s-core' % pname))
        # Redmine part
        self.assertTrue(self.rm.project_exists(pname))
        self.assertTrue(
            self.rm.check_user_role(pname, config.ADMIN_USER, 'Manager'))
        self.assertTrue(
            self.rm.check_user_role(pname, config.ADMIN_USER, 'Developer'))
        self.assertTrue(self.project_exists_ex(pname, config.USER_2))

    def test_create_private_project_as_admin(self):
        """ Create private project on redmine and gerrit as admin
        """
        pname = 'p_%s' % create_random_str()
        options = {"private": ""}
        self.create_project(pname, config.ADMIN_USER,
                            options=options)
        # Gerrit part
        self.assertTrue(self.gu.project_exists(pname))
        self.assertTrue(self.gu.group_exists('%s-ptl' % pname))
        self.assertTrue(self.gu.group_exists('%s-core' % pname))
        self.assertTrue(self.gu.group_exists('%s-dev' % pname))
        # TODO(Project creator, as project owner, should only be in ptl group)
        self.assertTrue(
            self.gu.member_in_group(config.ADMIN_USER, '%s-ptl' % pname))
        self.assertTrue(
            self.gu.member_in_group(config.ADMIN_USER, '%s-core' % pname))
        self.assertTrue(
            self.gu.member_in_group(config.ADMIN_USER, '%s-dev' % pname))
        # Redmine part
        self.assertTrue(self.rm.project_exists(pname))
        self.assertTrue(
            self.rm.check_user_role(pname, config.ADMIN_USER, 'Manager'))
        self.assertTrue(
            self.rm.check_user_role(pname, config.ADMIN_USER, 'Developer'))
        self.assertFalse(self.project_exists_ex(pname, config.USER_2))

    def test_delete_public_project_as_admin(self):
        """ Delete public project on redmine and gerrit as admin
        """
        pname = 'p_%s' % create_random_str()
        self.create_project(pname, config.ADMIN_USER)
        self.assertTrue(self.gu.project_exists(pname))
        self.assertTrue(self.rm.project_exists(pname))
        self.msu.deleteProject(pname, config.ADMIN_USER)
        self.assertFalse(self.gu.project_exists(pname))
        self.assertFalse(self.gu.group_exists('%s-ptl' % pname))
        self.assertFalse(self.rm.project_exists(pname))
        self.assertFalse(self.gu.group_exists('%s-core' % pname))
        self.projects.remove(pname)

    def test_create_public_project_as_user(self):
        """ Create public project on redmine and gerrit as user
        """
        pname = 'p_%s' % create_random_str()
        self.create_project(pname, config.USER_2)
        # Gerrit part
        self.assertTrue(self.gu.project_exists(pname))
        self.assertTrue(self.gu.group_exists('%s-ptl' % pname))
        self.assertTrue(self.gu.group_exists('%s-core' % pname))
        # TODO(Project creator, as project owner, should only be in ptl group)
        self.assertTrue(
            self.gu.member_in_group(config.ADMIN_USER, '%s-ptl' % pname))
        self.assertTrue(
            self.gu.member_in_group(config.ADMIN_USER, '%s-core' % pname))
        # Redmine part
        self.assertTrue(self.rm.project_exists(pname))
        self.assertTrue(self.project_exists_ex(pname, config.USER_2))
        self.assertTrue(
            self.rm.check_user_role(pname, config.USER_2, 'Manager'))
        self.assertTrue(
            self.rm.check_user_role(pname, config.USER_2, 'Developer'))
        self.assertTrue(self.project_exists_ex(pname, config.USER_3))

    def test_create_private_project_as_user(self):
        """ Create private project on redmine and gerrit as user
        """
        pname = 'p_%s' % create_random_str()
        options = {"private": ""}
        self.create_project(pname, config.USER_2,
                            options=options)
        # Gerrit part
        self.assertTrue(self.gu.project_exists(pname))
        self.assertTrue(self.gu.group_exists('%s-ptl' % pname))
        self.assertTrue(self.gu.group_exists('%s-core' % pname))
        self.assertTrue(self.gu.group_exists('%s-dev' % pname))
        # TODO(Project creator, as project owner, should only be in ptl group)
        self.assertTrue(
            self.gu.member_in_group(config.USER_2, '%s-ptl' % pname))
        self.assertTrue(
            self.gu.member_in_group(config.USER_2, '%s-core' % pname))
        self.assertTrue(
            self.gu.member_in_group(config.USER_2, '%s-dev' % pname))
        # Redmine part
        self.assertTrue(self.rm.project_exists(pname))
        self.assertTrue(self.project_exists_ex(pname, config.USER_2))
        self.assertTrue(
            self.rm.check_user_role(pname, config.USER_2, 'Manager'))
        self.assertTrue(
            self.rm.check_user_role(pname, config.USER_2, 'Developer'))
        self.assertFalse(self.project_exists_ex(pname, config.USER_3))

    def test_create_public_project_with_users_in_group(self):
        """ Create public project on redmine and gerrit with users in groups
        """
        pname = 'p_%s' % create_random_str()
        u2mail = config.USERS[config.USER_2]['email']
        u3mail = config.USERS[config.USER_3]['email']
        options = {"ptl-group": "",
                   "core-group": "%s,%s" % (u2mail, u3mail),
                   }
        self.create_project(pname, config.ADMIN_USER,
                            options=options)
        # Gerrit part
        self.assertTrue(self.gu.project_exists(pname))
        # TODO(Project creator, as project owner, should only be in ptl group)
        self.assertTrue(
            self.gu.member_in_group(config.ADMIN_USER, '%s-ptl' % pname))
        for user in (config.ADMIN_USER, config.USER_2, config.USER_3):
            self.assertTrue(self.gu.member_in_group(user, '%s-core' % pname))
        # Redmine part
        self.assertTrue(self.rm.project_exists(pname))
        self.assertTrue(
            self.rm.check_user_role(pname, config.ADMIN_USER, 'Manager'))
        for user in (config.ADMIN_USER, config.USER_2, config.USER_3):
            self.assertTrue(self.rm.check_user_role(pname, user, 'Developer'))

    def test_create_private_project_with_users_in_group(self):
        """ Create private project on redmine and gerrit with users in groups
        """
        pname = 'p_%s' % create_random_str()
        u2mail = config.USERS[config.USER_2]['email']
        u3mail = config.USERS[config.USER_3]['email']
        u4mail = config.USERS[config.USER_4]['email']
        options = {"private": "",
                   "ptl-group": "",
                   "core-group": "%s,%s" % (u2mail, u3mail),
                   "dev-group": "%s" % u4mail,
                   }
        self.create_project(pname, config.ADMIN_USER,
                            options=options)
        # Gerrit part
        self.assertTrue(self.gu.project_exists(pname))
        # TODO(Project creator, as project owner, should only be in ptl group)
        self.assertTrue(
            self.gu.member_in_group(config.ADMIN_USER, '%s-ptl' % pname))
        for user in (config.ADMIN_USER, config.USER_2, config.USER_3):
            self.assertTrue(self.gu.member_in_group(user, '%s-core' % pname))
        self.assertTrue(
            self.gu.member_in_group(config.USER_4, '%s-dev' % pname))
        # Redmine part
        # it should be visible to admin
        self.assertTrue(self.rm.project_exists(pname))
        self.assertTrue(
            self.rm.check_user_role(pname, config.ADMIN_USER, 'Manager'))
        for user in (config.ADMIN_USER, config.USER_2,
                     config.USER_3, config.USER_4):
            self.assertTrue(self.rm.check_user_role(pname, user, 'Developer'))

    def test_create_public_project_as_admin_clone_as_admin(self):
        """ Clone public project as admin and check content
        """
        pname = 'p_%s' % create_random_str()
        self.create_project(pname, config.ADMIN_USER)
        ggu = GerritGitUtils(config.ADMIN_USER,
                             config.ADMIN_PRIV_KEY_PATH,
                             config.USERS[config.ADMIN_USER]['email'])
        url = "ssh://%s@%s:29418/%s" % (config.ADMIN_USER,
                                        config.GATEWAY_HOST, pname)
        clone_dir = ggu.clone(url, pname)
        self.dirs_to_delete.append(os.path.dirname(clone_dir))
        # Test that the clone is a success
        self.assertTrue(os.path.isdir(clone_dir))
        # Verify master own the .gitreview file
        self.assertTrue(os.path.isfile(os.path.join(clone_dir,
                                                    '.gitreview')))
        # Verify meta/config branch own both group and ACLs config file
        ggu.fetch_meta_config(clone_dir)
        self.assertTrue(os.path.isfile(os.path.join(clone_dir,
                                                    'project.config')))
        self.assertTrue(os.path.isfile(os.path.join(clone_dir,
                                                    'groups')))
        # There is no group dev for a public project
        content = file(os.path.join(clone_dir, 'project.config')).read()
        self.assertFalse('%s-dev' % pname in content)
        content = file(os.path.join(clone_dir, 'groups')).read()
        self.assertFalse('%s-dev' % pname in content)

    def test_create_private_project_as_admin_clone_as_admin(self):
        """ Clone private project as admin and check content
        """
        pname = 'p_%s' % create_random_str()
        options = {"private": ""}
        self.create_project(pname, config.ADMIN_USER, options=options)
        ggu = GerritGitUtils(config.ADMIN_USER,
                             config.ADMIN_PRIV_KEY_PATH,
                             config.USERS[config.ADMIN_USER]['email'])
        url = "ssh://%s@%s:29418/%s" % (config.ADMIN_USER,
                                        config.GATEWAY_HOST, pname)
        clone_dir = ggu.clone(url, pname)
        self.dirs_to_delete.append(os.path.dirname(clone_dir))
        # Test that the clone is a success
        self.assertTrue(os.path.isdir(clone_dir))
        # Verify master own the .gitreview file
        self.assertTrue(os.path.isfile(os.path.join(clone_dir,
                                                    '.gitreview')))
        # Verify meta/config branch own both group and ACLs config file
        ggu.fetch_meta_config(clone_dir)
        self.assertTrue(os.path.isfile(os.path.join(clone_dir,
                                                    'project.config')))
        self.assertTrue(os.path.isfile(os.path.join(clone_dir,
                                                    'groups')))
        # There is a group dev for a private project
        content = file(os.path.join(clone_dir, 'project.config')).read()
        self.assertTrue('%s-dev' % pname in content)
        content = file(os.path.join(clone_dir, 'groups')).read()
        self.assertTrue('%s-dev' % pname in content)

    def test_create_public_project_as_admin_clone_as_user(self):
        """ Create public project as admin then clone as user
        """
        pname = 'p_%s' % create_random_str()
        # create the project as admin
        self.create_project(pname, config.ADMIN_USER)
        # add user2 ssh pubkey to user2
        gu = GerritUtils(
            'https://%s/' % config.GATEWAY_HOST,
            auth_cookie=config.USERS[config.USER_2]['auth_cookie'])
        gu.add_pubkey(config.USER_2_PUB_KEY)
        # prepare to clone
        priv_key_path = set_private_key(config.USER_2_PRIV_KEY)
        self.dirs_to_delete.append(os.path.dirname(priv_key_path))
        ggu = GerritGitUtils(config.USER_2,
                             priv_key_path,
                             config.USERS[config.USER_2]['email'])
        url = "ssh://%s@%s:29418/%s" % (config.USER_2,
                                        config.GATEWAY_HOST, pname)
        # clone
        clone_dir = ggu.clone(url, pname)
        self.dirs_to_delete.append(os.path.dirname(clone_dir))
        # Test that the clone is a success
        self.assertTrue(os.path.isdir(clone_dir))
        # Verify master own the .gitreview file
        self.assertTrue(os.path.isfile(os.path.join(clone_dir,
                                                    '.gitreview')))

    def test_create_public_project_as_user_clone_as_user(self):
        """ Create public project as user then clone as user
        """
        pname = 'p_%s' % create_random_str()
        # create the project as admin
        self.create_project(pname, config.USER_2)
        # add user2 ssh pubkey to user2
        gu = GerritUtils(
            'https://%s/' % config.GATEWAY_HOST,
            auth_cookie=config.USERS[config.USER_2]['auth_cookie'])
        gu.add_pubkey(config.USER_2_PUB_KEY)
        # prepare to clone
        priv_key_path = set_private_key(config.USER_2_PRIV_KEY)
        self.dirs_to_delete.append(os.path.dirname(priv_key_path))
        ggu = GerritGitUtils(config.USER_2,
                             priv_key_path,
                             config.USERS[config.USER_2]['email'])
        url = "ssh://%s@%s:29418/%s" % (config.USER_2,
                                        config.GATEWAY_HOST, pname)
        # clone
        clone_dir = ggu.clone(url, pname)
        self.dirs_to_delete.append(os.path.dirname(clone_dir))
        # Test that the clone is a success
        self.assertTrue(os.path.isdir(clone_dir))
        # Verify master own the .gitreview file
        self.assertTrue(os.path.isfile(os.path.join(clone_dir,
                                                    '.gitreview')))

    def test_upstream(self):
        """ Validate upstream feature of managesf
        """
        # Create a test upstream project
        pname_us = 'p_upstream'
        self.create_project(pname_us, config.ADMIN_USER)

        ggu_us = GerritGitUtils(config.ADMIN_USER,
                                config.ADMIN_PRIV_KEY_PATH,
                                config.USERS[config.ADMIN_USER]['email'])
        url = "ssh://%s@%s:29418/%s" % (config.ADMIN_USER,
                                        config.GATEWAY_HOST, pname_us)
        # clone
        us_clone_dir = ggu_us.clone(url, pname_us)
        self.dirs_to_delete.append(os.path.dirname(us_clone_dir))
        # Test that the clone is a success
        self.assertTrue(os.path.isdir(us_clone_dir))
        # push some test files to the upstream project
        us_files = [str(x) for x in range(1, 10)]
        for f in us_files:
            file(os.path.join(us_clone_dir, f), 'w').write(f)
            os.chmod(os.path.join(us_clone_dir, f), 0755)

        ggu_us.add_commit_in_branch(us_clone_dir, "master",
                                    commit="Adding files 1-10",
                                    files=us_files)
        ggu_us.direct_push_branch(us_clone_dir, "master")

        # No create a test project with upstream pointing to the above
        upstream_url = "ssh://%s@%s:29418/%s" % (
            config.ADMIN_USER, config.GATEWAY_HOST, pname_us)
        pname = 'p_%s' % create_random_str()
        # create the project as admin
        options = {"upstream": upstream_url}
        self.create_project(pname, config.ADMIN_USER, options=options)

        ggu = GerritGitUtils(config.ADMIN_USER,
                             config.ADMIN_PRIV_KEY_PATH,
                             config.USERS[config.ADMIN_USER]['email'])
        url = "ssh://%s@%s:29418/%s" % (config.ADMIN_USER,
                                        config.GATEWAY_HOST, pname)
        # clone
        clone_dir = ggu.clone(url, pname)
        self.dirs_to_delete.append(os.path.dirname(clone_dir))

        # Check if the files pushed in upstream project is present
        files = [f for f in os.listdir(clone_dir) if not f.startswith('.')]
        self.assertEqual(set(files), set(us_files))

    def test_delete_project_as_admin(self):
        """ Checking if admin can delete projects that are not owned by admin
        """
        pname = 'p_%s' % create_random_str()
        self.create_project(pname, config.USER_2)
        self.assertTrue(self.gu.project_exists(pname))
        self.assertTrue(self.rm.project_exists(pname))
        self.msu.deleteProject(pname, config.ADMIN_USER)
        self.assertFalse(self.gu.project_exists(pname))
        self.assertFalse(self.gu.group_exists('%s-ptl' % pname))
        self.assertFalse(self.rm.project_exists(pname))
        self.assertFalse(self.gu.group_exists('%s-core' % pname))
        self.projects.remove(pname)

    def test_list_active_members(self):
        """ Check the list of members as a list of tuples of emails and names
        """
        self.assertTrue(self.msu.list_active_members(config.USER_2))
Beispiel #55
0
class TestResourcesWorkflow(Base):
    def setUp(self):
        super(TestResourcesWorkflow, self).setUp()
        priv_key_path = set_private_key(
            config.USERS[config.ADMIN_USER]["privkey"])
        self.gitu_admin = GerritGitUtils(
            config.ADMIN_USER, priv_key_path,
            config.USERS[config.ADMIN_USER]['email'])
        self.gu = GerritUtils(
            config.GATEWAY_URL,
            auth_cookie=config.USERS[config.ADMIN_USER]['auth_cookie'])
        self.ju = JenkinsUtils()

        self.dirs_to_delete = []

    def tearDown(self):
        super(TestResourcesWorkflow, self).tearDown()
        for dirs in self.dirs_to_delete:
            shutil.rmtree(dirs)

    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

        devnull = open(os.devnull, 'wb')
        p = Popen(cmd, stdout=devnull, stderr=devnull)
        return p.communicate(), p.returncode

    def clone_as_admin(self, pname):
        url = "ssh://%s@%s:29418/%s" % (config.ADMIN_USER, config.GATEWAY_HOST,
                                        pname)
        clone_dir = self.gitu_admin.clone(url, pname)
        if os.path.dirname(clone_dir) not in self.dirs_to_delete:
            self.dirs_to_delete.append(os.path.dirname(clone_dir))
        return clone_dir

    def commit_direct_push_as_admin(self, clone_dir, msg):
        # Stage, commit and direct push the additions on master
        self.gitu_admin.add_commit_for_all_new_additions(clone_dir, msg)
        return self.gitu_admin.direct_push_branch(clone_dir, 'master')

    def set_resources_then_direct_push(self,
                                       fpath,
                                       resources=None,
                                       mode='add'):
        config_clone_dir = self.clone_as_admin("config")
        path = os.path.join(config_clone_dir, fpath)
        if mode == 'add':
            file(path, 'w').write(resources)
        elif mode == 'del':
            os.unlink(path)
        change_sha = self.commit_direct_push_as_admin(
            config_clone_dir, "Add new resources for functional tests")
        config_update_log = self.ju.wait_for_config_update(change_sha)
        self.assertIn("SUCCESS", config_update_log)

    def wait_for_jenkins_note(self, change_id):
        attempt = 0
        while "jenkins" not in self.gu.get_reviewers(change_id):
            if attempt >= 90:
                break
            time.sleep(1)
            attempt += 1

    def propose_resources_change_check_ci(self,
                                          fpath,
                                          resources=None,
                                          mode='add',
                                          expected_note=1,
                                          msg=None):

        config_clone_dir = self.clone_as_admin("config")
        path = os.path.join(config_clone_dir, fpath)
        if mode == 'add':
            file(path, 'w').write(resources)
        elif mode == 'del':
            os.unlink(path)

        if not msg:
            msg = "Validate resources"
        if mode == 'add':
            change_sha = self.gitu_admin.add_commit_and_publish(
                config_clone_dir, "master", msg, fnames=[path])
        if mode == 'del':
            change_sha = self.gitu_admin.add_commit_for_all_new_additions(
                config_clone_dir, msg, publish=True)

        change_nr = self.gu.get_change_number(change_sha)
        note = self.gu.wait_for_verify(change_nr)
        self.assertEqual(note, expected_note)

    def get_resources(self):
        gateau = config.USERS[config.ADMIN_USER]['auth_cookie']
        resp = requests.get("%s/manage/resources/" % config.GATEWAY_URL,
                            cookies={'auth_pubtkt': gateau})
        return resp.json()

    def test_validate_wrong_resource_workflow(self):
        """ Check resources - wrong model is detected by config-check """
        # This resource is not correct
        fpath = "resources/%s.yaml" % create_random_str()
        name = create_random_str()
        resources = """resources:
  groups:
    %s:
      unknown-key: value
      description: test for functional test
"""
        # Add the resource file with review then check CI
        resources = resources % name
        self.propose_resources_change_check_ci(fpath,
                                               resources=resources,
                                               mode='add',
                                               expected_note=-1)

    def test_validate_correct_resource_workflow(self):
        """ Check resources - good model is detected by config-check """
        # This resource is correct
        fpath = "resources/%s.yaml" % create_random_str()
        name = create_random_str()
        resources = """resources:
  groups:
    %s:
      description: test for functional test
      members:
        - [email protected]
"""
        # Add the resource file with review then check CI
        resources = resources % name
        self.propose_resources_change_check_ci(fpath,
                                               resources=resources,
                                               mode='add')

    def test_validate_resources_deletion(self):
        """ Check resources - deletions detected and authorized via flag """
        fpath = "resources/%s.yaml" % create_random_str()
        name = create_random_str()
        resources = """resources:
  groups:
    %s:
      description: test for functional test
      members: []
"""
        # Add the resources file w/o review
        resources = resources % name
        self.set_resources_then_direct_push(fpath,
                                            resources=resources,
                                            mode='add')

        # Remove the resource file via the review
        self.propose_resources_change_check_ci(fpath,
                                               mode='del',
                                               expected_note=-1)

        # Remove the resource file with "allow-delete" flag via the review
        shutil.rmtree(os.path.join(self.gitu_admin.tempdir, 'config'))
        msg = "Remove resource with flag\nsf-resources: allow-delete"
        self.propose_resources_change_check_ci(fpath, mode='del', msg=msg)

    @skipIfServiceMissing('storyboard')
    def test_CUD_project(self):
        """ Check resources - ops on project work as expected """
        sclient = SFStoryboard(config.GATEWAY_URL + "/storyboard_api",
                               config.USERS[config.USER_4]['auth_cookie'])
        fpath = "resources/%s.yaml" % create_random_str()
        name = create_random_str()
        resources = """resources:
  projects:
    %(pname)s:
      description: An awesome project
      issue-tracker: SFStoryboard
      source-repositories:
        - %(pname)s/%(r1name)s
  repos:
    %(pname)s/%(r1name)s:
      description: The server part
      acl: %(pname)s
  acls:
    %(pname)s:
      file: |
        [access "refs/*"]
          read = group Anonymous Users
"""
        tmpl_keys = {
            'pname': create_random_str(),
            'r1name': create_random_str()
        }

        resources = resources % tmpl_keys
        # Add the resources file w/o review
        self.set_resources_then_direct_push(fpath,
                                            resources=resources,
                                            mode='add')

        # Some checks to validate stuff have been created
        projects = [p.name for p in sclient.projects.get_all()]
        self.assertIn("%s/%s" % (tmpl_keys['pname'], tmpl_keys['r1name']),
                      projects)
        project_groups = [p.name for p in sclient.project_groups.get_all()]
        self.assertIn(tmpl_keys['pname'], project_groups)

        # Modify the project resource
        resources = """resources:
  projects:
    %(pname)s:
      description: An awesome project
      issue-tracker: SFStoryboard
      source-repositories:
        - %(pname)s/%(r1name)s
        - %(pname)s/%(r2name)s
  repos:
    %(pname)s/%(r1name)s:
      description: The server part
      acl: %(pname)s
    %(pname)s/%(r2name)s:
      description: The server part
      acl: %(pname)s
  acls:
    %(pname)s:
      file: |
        [access "refs/*"]
          read = group Anonymous Users
"""
        tmpl_keys.update({'r2name': create_random_str()})
        resources = resources % tmpl_keys
        self.set_resources_then_direct_push(fpath,
                                            resources=resources,
                                            mode='add')
        # Some checks to validate stuff have been updated
        projects = [p.name for p in sclient.projects.get_all()]
        for name in (tmpl_keys['r1name'], tmpl_keys['r2name']):
            self.assertIn("%s/%s" % (tmpl_keys['pname'], name), projects)
        project_groups = [p.name for p in sclient.project_groups.get_all()]
        self.assertIn(tmpl_keys['pname'], project_groups)

        # Del the resources file w/o review
        self.set_resources_then_direct_push(fpath, mode='del')

        # Check the project group has been deleted
        # Note the project (in storyboard) is not deleted
        # this is a current limitation of the API (01/13/2017)
        project_groups = [p.name for p in sclient.project_groups.get_all()]
        self.assertFalse(tmpl_keys['pname'] in project_groups)

    def test_CUD_group(self):
        """ Check resources - ops on group work as expected """
        fpath = "resources/%s.yaml" % create_random_str()
        name = create_random_str()
        resources = """resources:
  groups:
    %s:
      description: test for functional test
      members:
        - [email protected]
        - [email protected]
"""
        # Add the resources file w/o review
        resources = resources % name
        self.set_resources_then_direct_push(fpath,
                                            resources=resources,
                                            mode='add')
        # Check members on Gerrit
        gid = self.gu.get_group_id(name)
        members = [m['email'] for m in self.gu.get_group_members(gid)]
        self.assertIn("*****@*****.**", members)
        self.assertIn("*****@*****.**", members)
        # Modify resources Add/Remove members w/o review
        resources = """resources:
  groups:
    %s:
      description: test for functional test
      members:
        - [email protected]
        - [email protected]
"""
        resources = resources % name
        self.set_resources_then_direct_push(fpath,
                                            resources=resources,
                                            mode='add')
        # Check members on Gerrit
        gid = self.gu.get_group_id(name)
        members = [m['email'] for m in self.gu.get_group_members(gid)]
        self.assertIn("*****@*****.**", members)
        self.assertIn("*****@*****.**", members)
        self.assertNotIn("*****@*****.**", members)
        # Del the resources file w/o review
        self.set_resources_then_direct_push(fpath, mode='del')
        # Check the group has been deleted
        self.assertFalse(self.gu.get_group_id(name))

    def test_CD_repo(self):
        """ Check resources - ops on git repositories work as expected """
        fpath = "resources/%s.yaml" % create_random_str()
        name = create_random_str()
        resources = """resources:
  repos:
    %s:
      description: test for functional test
      default-branch: br1
      branches:
        br1: HEAD
        br2: HEAD
        master: '0'
"""
        # Add the resources file w/o review
        resources = resources % name
        self.set_resources_then_direct_push(fpath,
                                            resources=resources,
                                            mode='add')
        # Check the project has been created
        self.assertTrue(self.gu.project_exists(name))
        # Check branches
        branches = self.gu.g.get('/projects/%s/branches/' % name)
        for wref in ("HEAD", "br1", "br2"):
            found = False
            for ref in branches:
                if found:
                    continue
                if ref['ref'].endswith(wref):
                    found = True
                    if ref['ref'] == 'HEAD' and ref['revision'] != "br1":
                        raise Exception("Wrong default branch")
            if not found:
                raise Exception("Requested branch %s not found" % wref)
        # Del the resources file w/o review
        self.set_resources_then_direct_push(fpath, mode='del')
        # Check the project has been deleted
        self.assertFalse(self.gu.project_exists(name))

    def test_CRUD_resources(self):
        """ Check resources - bulk ops on resources work as expected """
        fpath = "resources/%s.yaml" % create_random_str()
        tmpl_keys = {
            'pname': create_random_str(),
            'r1name': create_random_str(),
            'r2name': create_random_str(),
            'aname': create_random_str(),
            'g1name': create_random_str(),
            'g2name': create_random_str()
        }
        resources = """resources:
  projects:
    %(pname)s:
      description: An awesome project
      contacts:
        - [email protected]
      source-repositories:
        - %(pname)s/%(r1name)s
        - %(pname)s/%(r2name)s
      website: http://ichiban-cloud.io
      documentation: http://ichiban-cloud.io/docs
      issue-tracker-url: http://ichiban-cloud.bugtrackers.io
  repos:
    %(pname)s/%(r1name)s:
      description: The server part
      acl: %(aname)s
    %(pname)s/%(r2name)s:
      description: The client part
      acl: %(aname)s
  acls:
    %(aname)s:
      file: |
        [access "refs/*"]
          read = group Anonymous Users
          read = group %(pname)s/%(g2name)s
          owner = group %(pname)s/%(g1name)s
        [access "refs/heads/*"]
          label-Code-Review = -2..+2 group %(pname)s/%(g2name)s
          label-Verified = -2..+2 group %(pname)s/%(g1name)s
          label-Workflow = -1..+1 group %(pname)s/%(g2name)s
          submit = group %(pname)s/%(g2name)s
          read = group Anonymous Users
          read = group %(pname)s/%(g2name)s
        [access "refs/meta/config"]
          read = group %(pname)s/%(g2name)s
        [receive]
          requireChangeId = true
        [submit]
          mergeContent = false
          action = fast forward only
      groups:
        - %(pname)s/%(g1name)s
        - %(pname)s/%(g2name)s
  groups:
    %(pname)s/%(g1name)s:
      members:
        - [email protected]
    %(pname)s/%(g2name)s:
      members:
        - [email protected]
        - [email protected]
"""
        # Add the resources file w/o review
        resources = resources % tmpl_keys
        self.set_resources_then_direct_push(fpath,
                                            resources=resources,
                                            mode='add')
        # Check resources have been created
        self.assertTrue(
            self.gu.project_exists(
                os.path.join(tmpl_keys['pname'], tmpl_keys['r1name'])))
        self.assertTrue(
            self.gu.project_exists(
                os.path.join(tmpl_keys['pname'], tmpl_keys['r2name'])))
        gid = self.gu.get_group_id(
            os.path.join(tmpl_keys['pname'], tmpl_keys['g1name']))
        members = [m['email'] for m in self.gu.get_group_members(gid)]
        self.assertEqual(len(members), 1)
        self.assertIn("*****@*****.**", members)
        gid2 = self.gu.get_group_id(
            os.path.join(tmpl_keys['pname'], tmpl_keys['g2name']))
        members = [m['email'] for m in self.gu.get_group_members(gid2)]
        self.assertEqual(len(members), 2)
        self.assertIn("*****@*****.**", members)
        self.assertIn("*****@*****.**", members)
        # Verify ACLs have been written for both repo
        for r in ('r1name', 'r2name'):
            rname = os.path.join(tmpl_keys['pname'], tmpl_keys[r])
            acl = self.gu.g.get('access/?project=%s' % rname)
            self.assertIn(
                gid2, acl[rname]['local']['refs/heads/*']['permissions']
                ['submit']['rules'].keys())
        # Verify the resources endpoint know about what we pushed
        res = self.get_resources()
        self.assertIn(tmpl_keys['pname'], res['resources']['projects'].keys())
        self.assertIn(tmpl_keys['aname'], res['resources']['acls'].keys())
        self.assertIn(os.path.join(tmpl_keys['pname'], tmpl_keys['g1name']),
                      res['resources']['groups'].keys())
        self.assertIn(os.path.join(tmpl_keys['pname'], tmpl_keys['g2name']),
                      res['resources']['groups'].keys())
        self.assertIn(os.path.join(tmpl_keys['pname'], tmpl_keys['r1name']),
                      res['resources']['repos'].keys())
        self.assertIn(os.path.join(tmpl_keys['pname'], tmpl_keys['r2name']),
                      res['resources']['repos'].keys())
        # Modify the ACL to verify repos ACL are updated
        resources = re.sub(
            'submit = group .*', 'submit = group %s' %
            os.path.join(tmpl_keys['pname'], tmpl_keys['g1name']), resources)
        self.set_resources_then_direct_push(fpath,
                                            resources=resources,
                                            mode='add')
        # Verify ACLs have been updated for both repo
        for r in ('r1name', 'r2name'):
            rname = os.path.join(tmpl_keys['pname'], tmpl_keys[r])
            acl = self.gu.g.get('access/?project=%s' % rname)
            self.assertIn(
                gid, acl[rname]['local']['refs/heads/*']['permissions']
                ['submit']['rules'].keys())
        # Now let's remove all that awesome resources
        self.set_resources_then_direct_push(fpath, mode='del')
        # Check resources have been deleted
        self.assertFalse(
            self.gu.project_exists(
                os.path.join(tmpl_keys['pname'], tmpl_keys['r1name'])))
        self.assertFalse(
            self.gu.project_exists(
                os.path.join(tmpl_keys['pname'], tmpl_keys['r2name'])))
        self.assertFalse(
            self.gu.get_group_id(
                os.path.join(tmpl_keys['pname'], tmpl_keys['g1name'])))
        self.assertFalse(
            self.gu.get_group_id(
                os.path.join(tmpl_keys['pname'], tmpl_keys['g2name'])))
        res = self.get_resources()
        projects = res['resources'].get('projects', {})
        acls = res['resources'].get('acls', {})
        groups = res['resources'].get('groups', {})
        repos = res['resources'].get('repos', {})
        self.assertNotIn(tmpl_keys['pname'], projects.keys())
        self.assertNotIn(tmpl_keys['aname'], acls.keys())
        self.assertNotIn(os.path.join(tmpl_keys['pname'], tmpl_keys['g1name']),
                         groups.keys())
        self.assertNotIn(os.path.join(tmpl_keys['pname'], tmpl_keys['g2name']),
                         groups.keys())
        self.assertNotIn(os.path.join(tmpl_keys['pname'], tmpl_keys['r1name']),
                         repos.keys())
        self.assertNotIn(os.path.join(tmpl_keys['pname'], tmpl_keys['r2name']),
                         repos.keys())

    def test_GET_resources(self):
        """ Check resources - GET resources works as expected"""
        cookies = dict(auth_pubtkt=config.USERS[config.USER_1]['auth_cookie'])
        ret = requests.get("%s/manage/resources/" % config.GATEWAY_URL,
                           cookies=cookies)
        self.assertIn('resources', ret.json())

    def test_GET_missing_resources(self):
        """ Check resources - GET missing resources works as expected"""
        token = config.USERS[config.ADMIN_USER]['auth_cookie']
        prev = "resources: {}"
        new = """resources:
  groups:
    %(gname)s:
      description: A test group
      members: ['*****@*****.**']
"""
        group_name = create_random_str()
        data = {'prev': prev, 'new': new % {'gname': group_name}}
        # Direct PUT resources bypassing the config repo workflow
        requests.put("%s/manage/resources/" % config.GATEWAY_URL,
                     json=data,
                     cookies={'auth_pubtkt': token})
        # Verify managesf detects diff and propose a re-sync resource struct
        ret = requests.get("%s/manage/resources/?get_missing_"
                           "resources=true" % config.GATEWAY_URL,
                           cookies={'auth_pubtkt': token})
        logs, resources = ret.json()
        self.assertListEqual(logs, [])
        self.assertIn(group_name, resources['resources']['groups'])
        # Call the resources.sh script on managesf node to propose
        # a review on the config repo to re-sync with the reality
        cmd = [
            '/usr/local/bin/resources.sh', 'get_missing_resources', 'submit'
        ]
        self.ssh_run_cmd(config.SERVICE_PRIV_KEY_PATH, 'root',
                         config.GATEWAY_HOST, cmd)
        # Get change id of the submitted review
        search_string = "Propose missing resources to the config repo"
        r = requests.get('%s/r/changes/?q=%s' %
                         (config.GATEWAY_URL, search_string))
        lastid = 0
        for r in json.loads(r.content[4:]):
            if r['_number'] > lastid:
                lastid = r['_number']
        self.assertEqual(self.gu.wait_for_verify(lastid), 1)
        # Check flag "sf-resources: skip-apply" in the commit msg
        change = self.gu.g.get(
            'changes/?q=%s&o=CURRENT_REVISION&o=CURRENT_COMMIT' % lastid)[0]
        revision = change["current_revision"]
        commit = change['revisions'][revision]["commit"]
        self.assertEqual(commit["message"].split('\n')[0],
                         'Propose missing resources to the config repo')
        self.assertTrue(commit["message"].find('sf-resources: skip-apply') > 0)
        # Approve the change and wait for the +2
        self.gu.submit_change_note(change['id'], "current", "Code-Review", "2")
        self.gu.submit_change_note(change['id'], "current", "Workflow", "1")
        # Check config-update return a success
        # The flag sf-resources: skip-apply should be detected
        # by the config update. Then missing resources won't
        # by concidered new and the resources apply will be skipped.
        # This tests (checking config-update succeed) confirm
        # resource apply have been skipped if not managesf resources
        # apply would have return 409 error making config-update failed too.
        # If not True then we cannot concider config-update succeed
        config_update_log = self.ju.wait_for_config_update(revision)
        self.assertIn("Skip resources apply.", config_update_log)
        self.assertIn("SUCCESS", config_update_log)
        # Checking again missing resources  must return nothing
        ret = requests.get("%s/manage/resources/?get_missing_"
                           "resources=true" % config.GATEWAY_URL,
                           cookies={'auth_pubtkt': token})
        logs, resources = ret.json()
        self.assertListEqual(logs, [])
        self.assertEqual(len(resources['resources']), 0)
    def test_check_zuul_operations(self):
        """ Test if zuul verifies project correctly through zuul-demo project
        """
        # zuul-demo - test project used exclusively to test zuul installation
        # The necessary project descriptions are already declared in Jenkins
        # and zuul

        pname = 'demo/zuul-demo'

        self.create_project(pname, config.ADMIN_USER)
        un = config.ADMIN_USER
        gu = GerritUtils(
            config.GATEWAY_URL,
            auth_cookie=config.USERS[un]['auth_cookie'])
        ju = JenkinsUtils()
        k_index = gu.add_pubkey(config.USERS[un]["pubkey"])
        # Gerrit part
        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))

        last_fail_build_num_ft = \
            ju.get_last_build_number("zuul-demo-functional-tests",
                                     "lastFailedBuild")
        last_fail_build_num_ut = \
            ju.get_last_build_number("zuul-demo-unit-tests",
                                     "lastFailedBuild")
        last_succeed_build_num_ft = \
            ju.get_last_build_number("zuul-demo-functional-tests",
                                     "lastSuccessfulBuild")
        last_succeed_build_num_ut = \
            ju.get_last_build_number("zuul-demo-unit-tests",
                                     "lastSuccessfulBuild")

        gitu.add_commit_and_publish(clone_dir, "master", "Test commit")

        change_ids = gu.get_my_changes_for_project(pname)
        self.assertGreater(len(change_ids), 0)
        change_id = change_ids[0]

        # Give some time for jenkins to work
        ju.wait_till_job_completes("zuul-demo-functional-tests",
                                   last_fail_build_num_ft, "lastFailedBuild")
        ju.wait_till_job_completes("zuul-demo-unit-tests",
                                   last_fail_build_num_ut, "lastFailedBuild")

        attempt = 0
        while "jenkins" not in gu.get_reviewers(change_id):
            if attempt >= 90:
                break
            time.sleep(1)
            attempt += 1

        attempt = 0
        while gu.get_reviewer_approvals(change_id, 'jenkins')['Verified'] \
                != '-1':
            if attempt >= 90:
                break
            time.sleep(1)
            attempt += 1

        self.assertEqual(
            gu.get_reviewer_approvals(change_id, 'jenkins')['Verified'], '-1')

        # Add the test case files and resubmit for review
        data = "echo Working"
        files = ["run_functional-tests.sh", "run_tests.sh"]

        for f in 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=files)

        # Give some time for jenkins to work
        ju.wait_till_job_completes("zuul-demo-functional-tests",
                                   last_succeed_build_num_ft,
                                   "lastSuccessfulBuild")
        ju.wait_till_job_completes("zuul-demo-unit-tests",
                                   last_succeed_build_num_ut,
                                   "lastSuccessfulBuild")

        attempt = 0
        while "jenkins" not in gu.get_reviewers(change_id):
            if attempt >= 90:
                break
            time.sleep(1)
            attempt += 1

        attempt = 0
        while gu.get_reviewer_approvals(change_id, 'jenkins')['Verified'] \
                != '+1':
            if attempt >= 90:
                break
            time.sleep(1)
            attempt += 1

        self.assertEqual(
            gu.get_reviewer_approvals(change_id, 'jenkins')['Verified'], '+1')

        gu.del_pubkey(k_index)
Beispiel #57
0
def get_group_id(sf_url, username, password, grpname):
    c = GerritUtils('http://' + sf_url,
                    auth_cookie=get_cookie(sf_url, username, password))
    return c.get_group_id(grpname)
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)