def init_gerrit(self): """Run Gerrit from the war file and configure it.""" golden_ver_file = self._dir('gsite', 'golden_ver') if os.path.exists(self.gsite_dir): if not os.path.exists(golden_ver_file): golden_ver = '0' else: with open(golden_ver_file) as f: golden_ver = f.read().strip() if GOLDEN_SITE_VER != golden_ver: print("Existing golden site has version %s, removing..." % golden_ver) shutil.rmtree(self.gsite_dir) else: print("Golden site of version %s already exists" % GOLDEN_SITE_VER) return # We write out the ssh host key for gerrit's ssh server which # for undocumented reasons forces gerrit init to download the # bouncy castle libs which we need for ssh that works on # newer distros like ubuntu xenial. os.makedirs(self._dir('gsite', 'etc')) # create SSH host key host_key_file = self._dir('gsite', 'etc', 'ssh_host_rsa_key') utils.run_cmd('ssh-keygen', '-t', 'rsa', '-b', '4096', '-f', host_key_file, '-N', '') print("Creating a new golden site of version " + GOLDEN_SITE_VER) # initialize Gerrit utils.run_cmd('java', '-jar', self.gerrit_war, 'init', '-d', self.gsite_dir, '--batch', '--no-auto-start', '--install-plugin', 'download-commands') utils.run_cmd('java', '-jar', self.gerrit_war, 'reindex', '-d', self.gsite_dir) with open(golden_ver_file, 'w') as f: f.write(GOLDEN_SITE_VER) # create SSH public key key_file = self._dir('gsite', 'test_ssh_key') utils.run_cmd('ssh-keygen', '-t', 'rsa', '-b', '4096', '-f', key_file, '-N', '') with open(key_file + '.pub', 'rb') as pub_key_file: pub_key = pub_key_file.read() # create admin user in Gerrit database sql_query = """INSERT INTO ACCOUNTS (REGISTERED_ON) VALUES (NOW()); INSERT INTO ACCOUNT_GROUP_MEMBERS (ACCOUNT_ID, GROUP_ID) \ VALUES (0, 1); INSERT INTO ACCOUNT_EXTERNAL_IDS (ACCOUNT_ID, EXTERNAL_ID, PASSWORD) \ VALUES (0, 'username:test_user', 'test_pass'); INSERT INTO ACCOUNT_SSH_KEYS (SSH_PUBLIC_KEY, VALID) \ VALUES ('%s', 'Y')""" % pub_key.decode() utils.run_cmd('java', '-jar', self._dir('gsite', 'bin', 'gerrit.war'), 'gsql', '-d', self.gsite_dir, '-c', sql_query)
def init_gerrit(self): """Run Gerrit from the war file and configure it.""" golden_ver_file = self._dir('gsite', 'golden_ver') if os.path.exists(self.gsite_dir): if not os.path.exists(golden_ver_file): golden_ver = '0' else: with open(golden_ver_file) as f: golden_ver = f.read().strip() if GOLDEN_SITE_VER != golden_ver: print("Existing golden site has version %s, removing..." % golden_ver) shutil.rmtree(self.gsite_dir) else: print("Golden site of version %s already exists" % GOLDEN_SITE_VER) return # We write out the ssh host key for gerrit's ssh server which # for undocumented reasons forces gerrit init to download the # bouncy castle libs which we need for ssh that works on # newer distros like ubuntu xenial. os.makedirs(self._dir('gsite', 'etc')) # create SSH host key host_key_file = self._dir('gsite', 'etc', 'ssh_host_rsa_key') utils.run_cmd('ssh-keygen', '-t', 'rsa', '-b', '4096', '-m', 'PEM', '-f', host_key_file, '-N', '') print("Creating a new golden site of version " + GOLDEN_SITE_VER) # initialize Gerrit utils.run_cmd('java', '-jar', self.gerrit_war, 'init', '-d', self.gsite_dir, '--batch', '--no-auto-start', '--install-plugin', 'download-commands') utils.run_cmd('java', '-jar', self.gerrit_war, 'reindex', '-d', self.gsite_dir) with open(golden_ver_file, 'w') as f: f.write(GOLDEN_SITE_VER) # create SSH public key key_file = self._dir('gsite', 'test_ssh_key') utils.run_cmd('ssh-keygen', '-t', 'rsa', '-b', '4096', '-m', 'PEM', '-f', key_file, '-N', '') with open(key_file + '.pub', 'rb') as pub_key_file: pub_key = pub_key_file.read() # create admin user in Gerrit database sql_query = """INSERT INTO ACCOUNTS (REGISTERED_ON) VALUES (NOW()); INSERT INTO ACCOUNT_GROUP_MEMBERS (ACCOUNT_ID, GROUP_ID) \ VALUES (0, 1); INSERT INTO ACCOUNT_EXTERNAL_IDS (ACCOUNT_ID, EXTERNAL_ID, PASSWORD) \ VALUES (0, 'username:test_user', 'test_pass'); INSERT INTO ACCOUNT_SSH_KEYS (SSH_PUBLIC_KEY, VALID) \ VALUES ('%s', 'Y')""" % pub_key.decode() utils.run_cmd('java', '-jar', self._dir('gsite', 'bin', 'gerrit.war'), 'gsql', '-d', self.gsite_dir, '-c', sql_query)
def _run_gerrit(self): # create a copy of site dir shutil.copytree(self.gsite_dir, self.site_dir) self.addCleanup(shutil.rmtree, self.site_dir) # write config with open(self._dir('site', 'etc', 'gerrit.config'), 'w') as _conf: new_conf = utils.get_gerrit_conf(self.gerrit_port, self.gerrit_port + 10) _conf.write(new_conf) # start Gerrit gerrit_sh = self._dir('site', 'bin', 'gerrit.sh') utils.run_cmd(gerrit_sh, 'start') self.addCleanup(utils.run_cmd, gerrit_sh, 'stop')
def _run_gerrit(self): # create a copy of site dir shutil.copytree(self.gsite_dir, self.site_dir) self.addCleanup(shutil.rmtree, self.site_dir) # write config with open(self._dir('site', 'etc', 'gerrit.config'), 'w') as _conf: new_conf = utils.get_gerrit_conf(self.gerrit_port, self.gerrit_port + 1000) _conf.write(new_conf) # start Gerrit gerrit_sh = self._dir('site', 'bin', 'gerrit.sh') utils.run_cmd(gerrit_sh, 'start') self.addCleanup(utils.run_cmd, gerrit_sh, 'stop')
def _configure_ssh(self, ssh_addr, ssh_port): """Setup ssh and scp to run with special options.""" os.mkdir(self.ssh_dir) ssh_key = utils.run_cmd('ssh-keyscan', '-p', str(ssh_port), ssh_addr) utils.write_to_file(self._dir('ssh', 'known_hosts'), ssh_key.encode()) self.addCleanup(os.remove, self._dir('ssh', 'known_hosts')) # Attach known_hosts to test results if anything fails @self.addOnException def add_known_hosts(exc_info): known_hosts = self._dir('ssh', 'known_hosts') if os.path.exists(known_hosts): content.attach_file(self, known_hosts) else: self.addDetail('known_hosts', content.text_content('Not found')) for cmd in ('ssh', 'scp'): cmd_file = self._dir('ssh', cmd) s = '#!/bin/sh\n' \ '/usr/bin/%s -i %s -o UserKnownHostsFile=%s $@' % \ (cmd, self._dir('gsite', 'test_ssh_key'), self._dir('ssh', 'known_hosts')) utils.write_to_file(cmd_file, s.encode()) os.chmod(cmd_file, os.stat(cmd_file).st_mode | stat.S_IEXEC) os.environ['PATH'] = self.ssh_dir + os.pathsep + os.environ['PATH'] os.environ['GIT_SSH'] = self._dir('ssh', 'ssh')
def _configure_ssh(self, ssh_addr, ssh_port): """Setup ssh and scp to run with special options.""" os.mkdir(self.ssh_dir) ssh_key = utils.run_cmd('ssh-keyscan', '-p', str(ssh_port), ssh_addr) utils.write_to_file(self._dir('ssh', 'known_hosts'), ssh_key.encode()) self.addCleanup(os.remove, self._dir('ssh', 'known_hosts')) # Attach known_hosts to test results if anything fails self.attach_on_exception(self._dir('ssh', 'known_hosts')) for cmd in ('ssh', 'scp'): cmd_file = self._dir('ssh', cmd) s = '#!/bin/sh\n' \ '/usr/bin/%s -i %s -o UserKnownHostsFile=%s ' \ '-o IdentitiesOnly=yes ' \ '-o PasswordAuthentication=no $@' % \ (cmd, self._dir('gsite', 'test_ssh_key'), self._dir('ssh', 'known_hosts')) utils.write_to_file(cmd_file, s.encode()) os.chmod(cmd_file, os.stat(cmd_file).st_mode | stat.S_IEXEC) os.environ['PATH'] = self.ssh_dir + os.pathsep + os.environ['PATH'] os.environ['GIT_SSH'] = self._dir('ssh', 'ssh')
def _configure_ssh(self, ssh_addr, ssh_port): """Setup ssh and scp to run with special options.""" os.mkdir(self.ssh_dir) ssh_key = utils.run_cmd("ssh-keyscan", "-p", str(ssh_port), ssh_addr) utils.write_to_file(self._dir("ssh", "known_hosts"), ssh_key.encode()) self.addCleanup(os.remove, self._dir("ssh", "known_hosts")) # Attach known_hosts to test results if anything fails self.attach_on_exception(self._dir("ssh", "known_hosts")) for cmd in ("ssh", "scp"): cmd_file = self._dir("ssh", cmd) s = ( "#!/bin/sh\n" "/usr/bin/%s -i %s -o UserKnownHostsFile=%s " "-o IdentitiesOnly=yes " "-o PasswordAuthentication=no $@" % (cmd, self._dir("gsite", "test_ssh_key"), self._dir("ssh", "known_hosts")) ) utils.write_to_file(cmd_file, s.encode()) os.chmod(cmd_file, os.stat(cmd_file).st_mode | stat.S_IEXEC) os.environ["PATH"] = self.ssh_dir + os.pathsep + os.environ["PATH"] os.environ["GIT_SSH"] = self._dir("ssh", "ssh")
def _run_gerrit(self, ssh_addr, ssh_port, http_addr, http_port): # create a copy of site dir shutil.copytree(self.gsite_dir, self.site_dir) self.addCleanup(shutil.rmtree, self.site_dir) # write config with open(self._dir("site", "etc", "gerrit.config"), "w") as _conf: new_conf = utils.get_gerrit_conf(ssh_addr, ssh_port, http_addr, http_port) _conf.write(new_conf) # If test fails, attach Gerrit config and logs to the result self.attach_on_exception(self._dir("site", "etc", "gerrit.config")) for name in ["error_log", "sshd_log", "httpd_log"]: self.attach_on_exception(self._dir("site", "logs", name)) # start Gerrit gerrit_sh = self._dir("site", "bin", "gerrit.sh") utils.run_cmd(gerrit_sh, "start") self.addCleanup(utils.run_cmd, gerrit_sh, "stop")
def _run_gerrit(self, ssh_addr, ssh_port, http_addr, http_port): # create a copy of site dir shutil.copytree(self.gsite_dir, self.site_dir) self.addCleanup(shutil.rmtree, self.site_dir) # write config with open(self._dir('site', 'etc', 'gerrit.config'), 'w') as _conf: new_conf = utils.get_gerrit_conf( ssh_addr, ssh_port, http_addr, http_port) _conf.write(new_conf) # If test fails, attach Gerrit config and logs to the result self.attach_on_exception(self._dir('site', 'etc', 'gerrit.config')) for name in ['error_log', 'sshd_log', 'httpd_log']: self.attach_on_exception(self._dir('site', 'logs', name)) # start Gerrit gerrit_sh = self._dir('site', 'bin', 'gerrit.sh') utils.run_cmd(gerrit_sh, 'start') self.addCleanup(utils.run_cmd, gerrit_sh, 'stop')
def _run_gerrit(self, ssh_addr, ssh_port, http_addr, http_port): # create a copy of site dir shutil.copytree(self.gsite_dir, self.site_dir) self.addCleanup(shutil.rmtree, self.site_dir) # write config with open(self._dir('site', 'etc', 'gerrit.config'), 'w') as _conf: new_conf = utils.get_gerrit_conf(ssh_addr, ssh_port, http_addr, http_port) _conf.write(new_conf) # If test fails, attach Gerrit config and logs to the result self.attach_on_exception(self._dir('site', 'etc', 'gerrit.config')) for name in ['error_log', 'sshd_log', 'httpd_log']: self.attach_on_exception(self._dir('site', 'logs', name)) # start Gerrit gerrit_sh = self._dir('site', 'bin', 'gerrit.sh') utils.run_cmd(gerrit_sh, 'start') self.addCleanup(utils.run_cmd, gerrit_sh, 'stop')
def init_gerrit(self): """Run Gerrit from the war file and configure it.""" if os.path.exists(self.gsite_dir): return # initialize Gerrit utils.run_cmd('java', '-jar', self._dir('gerrit', 'gerrit.war'), 'init', '-d', self.gsite_dir, '--batch', '--no-auto-start') # create SSH public key key_file = self._dir('gsite', 'test_ssh_key') utils.run_cmd('ssh-keygen', '-t', 'rsa', '-b', '4096', '-f', key_file, '-N', '') with open(key_file + '.pub', 'rb') as pub_key_file: pub_key = pub_key_file.read() # create admin user in Gerrit database sql_query = """INSERT INTO ACCOUNTS (REGISTERED_ON) VALUES (NOW()); INSERT INTO ACCOUNT_GROUP_MEMBERS (ACCOUNT_ID, GROUP_ID) \ VALUES (0, 1); INSERT INTO ACCOUNT_EXTERNAL_IDS (ACCOUNT_ID, EXTERNAL_ID) \ VALUES (0, 'username:test_user'); INSERT INTO ACCOUNT_SSH_KEYS (SSH_PUBLIC_KEY, VALID) \ VALUES ('%s', 'Y')""" % pub_key.decode() utils.run_cmd('java', '-jar', self._dir('gsite', 'bin', 'gerrit.war'), 'gsql', '-d', self.gsite_dir, '-c', sql_query)
def init_gerrit(self): """Run Gerrit from the war file and configure it.""" if os.path.exists(self.gsite_dir): return # initialize Gerrit utils.run_cmd('java', '-jar', self._dir('gerrit', 'gerrit.war'), 'init', '-d', self.gsite_dir, '--batch', '--no-auto-start') # create SSH public key key_file = self._dir('gsite', 'test_ssh_key') utils.run_cmd('ssh-keygen', '-t', 'rsa', '-b', '4096', '-f', key_file, '-N', '') with open(key_file + '.pub', 'rb') as pub_key_file: pub_key = pub_key_file.read() # create admin user in Gerrit database sql_query = """INSERT INTO ACCOUNTS (REGISTERED_ON) VALUES (NOW()); INSERT INTO ACCOUNT_GROUP_MEMBERS (ACCOUNT_ID, GROUP_ID) \ VALUES (0, 1); INSERT INTO ACCOUNT_EXTERNAL_IDS (ACCOUNT_ID, EXTERNAL_ID, PASSWORD) \ VALUES (0, 'username:test_user', 'test_pass'); INSERT INTO ACCOUNT_SSH_KEYS (SSH_PUBLIC_KEY, VALID) \ VALUES ('%s', 'Y')""" % pub_key.decode() utils.run_cmd('java', '-jar', self._dir('gsite', 'bin', 'gerrit.war'), 'gsql', '-d', self.gsite_dir, '-c', sql_query)
def init_gerrit(self): """Run Gerrit from the war file and configure it.""" golden_ver_file = self._dir("gsite", "golden_ver") if os.path.exists(self.gsite_dir): if not os.path.exists(golden_ver_file): golden_ver = "0" else: with open(golden_ver_file) as f: golden_ver = f.read().strip() if GOLDEN_SITE_VER != golden_ver: print("Existing golden site has version %s, removing..." % golden_ver) shutil.rmtree(self.gsite_dir) else: print("Golden site of version %s already exists" % GOLDEN_SITE_VER) return print("Creating a new golden site of version " + GOLDEN_SITE_VER) # initialize Gerrit utils.run_cmd( "java", "-jar", self.gerrit_war, "init", "-d", self.gsite_dir, "--batch", "--no-auto-start", "--install-plugin", "download-commands", ) with open(golden_ver_file, "w") as f: f.write(GOLDEN_SITE_VER) # create SSH public key key_file = self._dir("gsite", "test_ssh_key") utils.run_cmd("ssh-keygen", "-t", "rsa", "-b", "4096", "-f", key_file, "-N", "") with open(key_file + ".pub", "rb") as pub_key_file: pub_key = pub_key_file.read() # create admin user in Gerrit database sql_query = ( """INSERT INTO ACCOUNTS (REGISTERED_ON) VALUES (NOW()); INSERT INTO ACCOUNT_GROUP_MEMBERS (ACCOUNT_ID, GROUP_ID) \ VALUES (0, 1); INSERT INTO ACCOUNT_EXTERNAL_IDS (ACCOUNT_ID, EXTERNAL_ID, PASSWORD) \ VALUES (0, 'username:test_user', 'test_pass'); INSERT INTO ACCOUNT_SSH_KEYS (SSH_PUBLIC_KEY, VALID) \ VALUES ('%s', 'Y')""" % pub_key.decode() ) utils.run_cmd( "java", "-jar", self._dir("gsite", "bin", "gerrit.war"), "gsql", "-d", self.gsite_dir, "-c", sql_query )
def init_gerrit(self): """Run Gerrit from the war file and configure it.""" golden_ver_file = self._dir('gsite', 'golden_ver') if os.path.exists(self.gsite_dir): if not os.path.exists(golden_ver_file): golden_ver = '0' else: with open(golden_ver_file) as f: golden_ver = f.read().strip() if GOLDEN_SITE_VER != golden_ver: print("Existing golden site has version %s, removing..." % golden_ver) shutil.rmtree(self.gsite_dir) else: print("Golden site of version %s already exists" % GOLDEN_SITE_VER) return print("Creating a new golden site of version " + GOLDEN_SITE_VER) # initialize Gerrit utils.run_cmd('java', '-jar', self.gerrit_war, 'init', '-d', self.gsite_dir, '--batch', '--no-auto-start', '--install-plugin', 'download-commands') with open(golden_ver_file, 'w') as f: f.write(GOLDEN_SITE_VER) # create SSH public key key_file = self._dir('gsite', 'test_ssh_key') utils.run_cmd('ssh-keygen', '-t', 'rsa', '-b', '4096', '-f', key_file, '-N', '') with open(key_file + '.pub', 'rb') as pub_key_file: pub_key = pub_key_file.read() # create admin user in Gerrit database sql_query = """INSERT INTO ACCOUNTS (REGISTERED_ON) VALUES (NOW()); INSERT INTO ACCOUNT_GROUP_MEMBERS (ACCOUNT_ID, GROUP_ID) \ VALUES (0, 1); INSERT INTO ACCOUNT_EXTERNAL_IDS (ACCOUNT_ID, EXTERNAL_ID, PASSWORD) \ VALUES (0, 'username:test_user', 'test_pass'); INSERT INTO ACCOUNT_SSH_KEYS (SSH_PUBLIC_KEY, VALID) \ VALUES ('%s', 'Y')""" % pub_key.decode() utils.run_cmd('java', '-jar', self._dir('gsite', 'bin', 'gerrit.war'), 'gsql', '-d', self.gsite_dir, '-c', sql_query)
def _configure_ssh(self): """Setup ssh and scp to run with special options.""" os.mkdir(self.ssh_dir) ssh_key = utils.run_cmd('ssh-keyscan', '-p', str(self.gerrit_port), 'localhost') utils.write_to_file(self._dir('ssh', 'known_hosts'), ssh_key.encode()) self.addCleanup(os.remove, self._dir('ssh', 'known_hosts')) for cmd in ('ssh', 'scp'): cmd_file = self._dir('ssh', cmd) s = '#!/bin/sh\n' \ '/usr/bin/%s -i %s -o UserKnownHostsFile=%s $@' % \ (cmd, self._dir('gsite', 'test_ssh_key'), self._dir('ssh', 'known_hosts')) utils.write_to_file(cmd_file, s.encode()) os.chmod(cmd_file, os.stat(cmd_file).st_mode | stat.S_IEXEC) os.environ['PATH'] = self.ssh_dir + os.pathsep + os.environ['PATH'] os.environ['GIT_SSH'] = self._dir('ssh', 'ssh')
def _run_git_review(self, *args, **kwargs): """Run git-review utility from source.""" git_review = utils.run_cmd('which', 'git-review') kwargs.setdefault('chdir', self.test_dir) return utils.run_cmd(git_review, *args, **kwargs)
def _run_gerrit_cli(self, command, *args): """SSH to gerrit Gerrit server and run command there.""" return utils.run_cmd('ssh', '-p', str(self.gerrit_port), 'test_user@' + self.gerrit_host, 'gerrit', command, *args)
def test_git_review_s_from_subdirectory(self): """Test git-review -s from subdirectory.""" self.reset_remote() utils.run_cmd('mkdir', 'subdirectory', chdir=self.test_dir) self._run_git_review( '-s', chdir=os.path.join(self.test_dir, 'subdirectory'))
def _run_gerrit_cli(self, command, *args): """SSH to gerrit Gerrit server and run command there.""" return utils.run_cmd( "ssh", "-p", str(self.gerrit_port), "test_user@" + self.gerrit_host, "gerrit", command, *args )
def list_test_ids(argv): res = utils.run_cmd(sys.executable, '-m', 'testtools.run', *argv[1:]) return res.split('\n')
def _run_git_review(self, *args, **kwargs): """Run git-review utility from source.""" git_review = utils.run_cmd('which', 'git-review') return utils.run_cmd(git_review, *args, chdir=self.test_dir, **kwargs)
def _run_git_review(self, *args, **kwargs): """Run git-review utility from source.""" git_review = utils.run_cmd("which", "git-review") kwargs.setdefault("chdir", self.test_dir) return utils.run_cmd(git_review, *args, **kwargs)