def test_getcwd(self, concurrent=False): """ Verify that the directory locking for concurrency is working """ cwd = pushd.Dir.getcwd() with pushd.Dir("/"): self.assertEqual(pushd.Dir.getcwd(), "/") with pushd.Dir("/dev"): self.assertEqual(pushd.Dir.getcwd(), "/dev") self.assertEqual(pushd.Dir.getcwd(), "/") self.assertEqual(pushd.Dir.getcwd(), cwd)
def test_chdir(self): """ Verify that when a Dir is created and used in a `with` context, the CWD changes from current to new and back after exiting the context """ cwd = os.getcwd() with pushd.Dir("/"): self.assertEqual(os.getcwd(), "/") with pushd.Dir("/dev"): self.assertEqual(os.getcwd(), "/dev") self.assertEqual(os.getcwd(), "/") self.assertEqual(os.getcwd(), cwd)
def test_getcwd(self, concurrent=False): """ Verify that the directory locking for concurrency is working """ cwd = os.getcwd() if not concurrent: self.assertEqual(pushd.Dir.getcwd(), cwd) else: # the initial value is not reliable when using multiple threads cwd = pushd.Dir.getcwd() with pushd.Dir("/"): self.assertEqual(pushd.Dir.getcwd(), "/") with pushd.Dir("/dev"): self.assertEqual(pushd.Dir.getcwd(), "/dev") self.assertEqual(pushd.Dir.getcwd(), "/") self.assertEqual(pushd.Dir.getcwd(), cwd)
def commit_and_push_bundle(self, commit_msg): """Try to commit and push bundle distgit repository if there were any content changes. :param string commit_msg: Commit message :return bool True if new changes were committed and pushed, False otherwise """ with pushd.Dir(self.bundle_clone_path): exectools.cmd_assert(["git", "add", "-A"]) rc, _, _ = exectools.cmd_gather( ["git", "diff-index", "--quiet", "HEAD"]) if rc == 0: self.runtime.logger.warning("Nothing new to commit.") return False exectools.cmd_assert('rhpkg {} commit -m "{}"'.format( self.rhpkg_opts, commit_msg)) _, is_shallow, _ = exectools.cmd_gather( ["git", "rev-parse", "--is-shallow-repository"]) if is_shallow.strip() == "true": exectools.cmd_assert(["git", "fetch", "--unshallow"], retries=3) cmd = f'rhpkg {self.rhpkg_opts} push' if not self.dry_run: exectools.cmd_assert(cmd) else: self.runtime.logger.warning("[DRY RUN] Would have run %s", cmd) return True
def checkout_repo(self, repo, commit_hash): """Checkout a repository to a particular commit hash :param string repo: The repository in which the checkout operation will be performed :param string commit_hash: The desired point to checkout the repository """ with pushd.Dir('{}/{}'.format(self.working_dir, repo)): exectools.cmd_assert('git checkout {}'.format(commit_hash))
def commit_and_push_metadata_repo(self): """Commit and push changes made on the metadata repository, using rhpkg """ with pushd.Dir('{}/{}'.format(self.working_dir, self.metadata_repo)): try: exectools.cmd_assert('git add .') user_option = '--user {} '.format(self.rhpkg_user) if self.rhpkg_user else '' exectools.cmd_assert('rhpkg {} {}commit -m "Update operator metadata"'.format(self.runtime.rhpkg_config, user_option)) exectools.cmd_assert('timeout 600 rhpkg {}push'.format(user_option), retries=3) return True except Exception: # The metadata repo might be already up to date, so we don't have anything new to commit return False
def clone_repo(self, repo, branch): """Clone a repository using rhpkg :param string repo: Name of the repository to be cloned :param string branch: Which branch of the repository should be cloned """ cmd = 'timeout 600 rhpkg ' cmd += self.runtime.rhpkg_config cmd += '--user {} '.format(self.rhpkg_user) if self.rhpkg_user else '' cmd += 'clone containers/{} --branch {}'.format(repo, branch) delete_repo = 'rm -rf {}/{}'.format(self.working_dir, repo) with pushd.Dir(self.working_dir): exectools.cmd_assert(cmd, retries=3, on_retry=delete_repo)
def commit_and_push_bundle(self, commit_msg): """Try to commit and push bundle distgit repository if there were any content changes. :param string commit_msg: Commit message :return bool True if new changes were committed and pushed, False otherwise """ with pushd.Dir(self.bundle_clone_path): try: exectools.cmd_assert('git add .') exectools.cmd_assert('rhpkg{}commit -m "{}"'.format( self.rhpkg_opts, commit_msg)) rc, out, err = exectools.cmd_gather('rhpkg{}push'.format( self.rhpkg_opts)) return True except Exception: return False # Bundle repository might be already up-to-date, nothing new to commit
def clone_repo(self, repo, branch): """Clone a repository using rhpkg :param string repo: Name of the repository to be cloned :param string branch: Which branch of the repository should be cloned """ def delete_and_clone(): self.delete_repo(repo) cmd = 'timeout 600 rhpkg ' cmd += '--user {} '.format( self.rhpkg_user) if self.rhpkg_user else '' cmd += 'clone containers/{} --branch {}'.format(repo, branch) return exectools.cmd_assert(cmd) with pushd.Dir(self.working_dir): exectools.retry(retries=3, task_f=delete_and_clone)
def build_metadata_container(self): """Build the metadata container using rhpkg :return: bool True if build succeeded, False otherwise :raise: Exception if command failed (rc != 0) """ with pushd.Dir('{}/{}'.format(self.working_dir, self.metadata_repo)): cmd = 'timeout 600 rhpkg {} {}container-build --nowait --target {}'.format( self.runtime.rhpkg_config, ('--user {} '.format( self.rhpkg_user) if self.rhpkg_user else ''), self.target) rc, stdout, stderr = exectools.cmd_gather(cmd) if rc != 0: raise Exception('{} failed! rc={} stdout={} stderr={}'.format( cmd, rc, stdout.strip(), stderr.strip())) return self.watch_brew_task( self.extract_brew_task_id(stdout.strip())) is None
def trigger_bundle_container_build(self): """Ask brew for a container-build of operator's bundle :return bool True if brew task was successfully created, False otherwise """ with pushd.Dir(self.bundle_clone_path): rc, out, err = exectools.cmd_gather( 'rhpkg{}container-build --nowait --target {}'.format( self.rhpkg_opts, self.target)) if rc != 0: msg = 'Unable to create brew task: rc={} out={} err={}'.format( rc, out, err) self.runtime.logger.info(msg) return False self.task_url = re.search(r'Task info:\s(.+)', out).group(1) self.task_id = re.search(r'Created task:\s(\d+)', out).group(1) return True
def trigger_bundle_container_build(self) -> Tuple[Optional[int], Optional[str]]: """Ask brew for a container-build of operator's bundle :return: (task_id, task_url) if brew task was successfully created, (None, None) otherwise """ if self.dry_run: self.runtime.logger.warning("[DRY RUN] Would have triggered bundle build.") return 12345, "https://brewweb.example.com/brew/taskinfo?taskID=12345" with pushd.Dir(self.bundle_clone_path): rc, out, err = exectools.cmd_gather( 'rhpkg {} container-build --nowait --target {}'.format(self.rhpkg_opts, self.target) ) if rc != 0: msg = 'Unable to create brew task: rc={} out={} err={}'.format(rc, out, err) self.runtime.logger.warning(msg) return False task_url = re.search(r'Task info:\s(.+)', out).group(1) task_id = int(re.search(r'Created task:\s(\d+)', out).group(1)) return task_id, task_url
def checkout_operator_to_build_commit(self): """Checkout clone of operator repository to specific commit used to build given operator NVR """ with pushd.Dir(self.operator_clone_path): exectools.cmd_assert('git checkout {}'.format( self.operator_build_commit))