Skip to content

Commit

Permalink
Refactor and move testing-farm functions in testing_farm_util.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kwk committed Mar 28, 2024
1 parent b65618d commit edb447c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 78 deletions.
7 changes: 6 additions & 1 deletion snapshot_manager/snapshot_manager/github_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ def create_or_get_todays_github_issue(
return (issue, True)

def _create_labels(
self, prefix: str, color: str, labels: list[str] = [], force: bool = True
self,
prefix: str,
color: str,
labels: list[str] = [],
force: bool = True,
issue: github.Issue.Issue | None = None,
):
"""Iterates over the given labels and creates or edits each label in the list
with the given prefix and color."""
Expand Down
77 changes: 2 additions & 75 deletions snapshot_manager/snapshot_manager/snapshot_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def check_todays_builds(self):
)
if chroot in testing_farm_requests:
request_id = testing_farm_requests[chroot]
watch_result, artifacts_url = self.watch_testing_farm_request(
watch_result, artifacts_url = tf.watch_testing_farm_request(
request_id=request_id
)
if watch_result in [
Expand All @@ -180,7 +180,7 @@ def check_todays_builds(self):
)
else:
logging.info(f"chroot {chroot} has no tests associated yet.")
request_id = self.make_testing_farm_request(chroot=chroot)
request_id = tf.make_testing_farm_request(chroot=chroot)
testing_farm_requests[chroot] = request_id
issue.add_to_labels(f"in_testing/{chroot}")

Expand All @@ -206,76 +206,3 @@ def check_todays_builds(self):
logging.info("Cannot close issue yet.")

logging.info(f"Updated today's issue: {issue.html_url}")

def watch_testing_farm_request(
self, request_id: str
) -> tuple[tf.TestingFarmWatchResult, str]:
request_id = tf.sanitize_request_id(request_id=request_id)
cmd = f"testing-farm watch --no-wait --id {request_id}"
exit_code, stdout, stderr = util.run_cmd(cmd=cmd)
if exit_code != 0:
raise SystemError(
f"failed to watch 'testing-farm request': {cmd}\n\nstdout: {stdout}\n\nstderr: {stderr}"
)

watch_result, artifacts_url = tf.parse_for_watch_result(stdout)
if watch_result is None:
raise SystemError(
f"failed to watch 'testing-farm request': {cmd}\n\nstdout: {stdout}\n\nstderr: {stderr}"
)
return (watch_result, artifacts_url)

def make_testing_farm_request(self, chroot: str) -> str:
"""Runs a "testing-farm request" command and returns the request ID.
The request is made without waiting for the result.
It is the responsibility of the caller of this function to run "testing-farm watch --id <REQUEST_ID>",
where "<REQUEST_ID>" is the result of this function.
Depending on the chroot, we'll automatically select the proper testing-farm ranch for you.
For this to work you'll have to set the
TESTING_FARM_API_TOKEN_PUBLIC_RANCH and
TESTING_FARM_API_TOKEN_REDHAT_RANCH
environment variables. We'll then use one of them to set the TESTING_FARM_API_TOKEN
environment variable for the actual call to testing-farm.
Args:
chroot (str): The chroot that you want to run tests for.
Raises:
SystemError: When the testing-farm request failed
Returns:
str: Request ID
"""
logging.info(f"Kicking off new tests for chroot {chroot}.")

# TODO(kwk): Add testing-farm code here, something like this:
# TODO(kwk): Decide how if we want to wait for test results (probably not) and if not how we can check for the results later.
ranch = tf.select_ranch(chroot)
logging.info(f"Using testing-farm ranch: {ranch}")
if ranch == "public":
os.environ["TESTING_FARM_API_TOKEN"] = os.getenv(
"TESTING_FARM_API_TOKEN_PUBLIC_RANCH"
)
if ranch == "redhat":
os.environ["TESTING_FARM_API_TOKEN"] = os.getenv(
"TESTING_FARM_API_TOKEN_REDHAT_RANCH"
)
cmd = f"""testing-farm \
request \
--compose Fedora-latest \
--git-url {self.config.test_repo_url} \
--arch {util.chroot_arch(chroot)} \
--plan /tests/snapshot-gating \
--environment COPR_PROJECT={self.config.copr_projectname} \
--context distro={util.chroot_os(chroot)} \
--context arch=${util.chroot_arch(chroot)} \
--no-wait \
--context snapshot={self.config.yyyymmdd}"""
exit_code, stdout, stderr = util.run_cmd(cmd, timeout_secs=None)
if exit_code == 0:
return tf.parse_output_for_request_id(stdout)
raise SystemError(
f"failed to run 'testing-farm request': {cmd}\n\nstdout: {stdout}\n\nstderr: {stderr}"
)
75 changes: 73 additions & 2 deletions snapshot_manager/snapshot_manager/testing_farm_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,81 @@ def chroot_request_ids_to_html_comment(data: dict) -> str:
Example:
>>> chroot_request_ids_to_html_comment({"foo": "bar"})
'<!--TESTING_FARM:foo/bar-->'
>>> chroot_request_ids_to_html_comment({"foo": "bar", "hello": "world", "abra": "kadabra"})
'<!--TESTING_FARM:foo/bar--><!--TESTING_FARM:hello/world--><!--TESTING_FARM:abra/kadabra-->'
"""
res: list[str] = []
for key in data.keys():
res.append(f"<!--TESTING_FARM:{key}/{data[key]}-->")
return "".join(res)


def watch_testing_farm_request(request_id: str) -> tuple[TestingFarmWatchResult, str]:
request_id = sanitize_request_id(request_id=request_id)
cmd = f"testing-farm watch --no-wait --id {request_id}"
exit_code, stdout, stderr = util.run_cmd(cmd=cmd)
if exit_code != 0:
raise SystemError(
f"failed to watch 'testing-farm request': {cmd}\n\nstdout: {stdout}\n\nstderr: {stderr}"
)

watch_result, artifacts_url = parse_for_watch_result(stdout)
if watch_result is None:
raise SystemError(
f"failed to watch 'testing-farm request': {cmd}\n\nstdout: {stdout}\n\nstderr: {stderr}"
)
return (watch_result, artifacts_url)


def make_testing_farm_request(chroot: str) -> str:
"""Runs a "testing-farm request" command and returns the request ID.
The request is made without waiting for the result.
It is the responsibility of the caller of this function to run "testing-farm watch --id <REQUEST_ID>",
where "<REQUEST_ID>" is the result of this function.
Depending on the chroot, we'll automatically select the proper testing-farm ranch for you.
For this to work you'll have to set the
TESTING_FARM_API_TOKEN_PUBLIC_RANCH and
TESTING_FARM_API_TOKEN_REDHAT_RANCH
environment variables. We'll then use one of them to set the TESTING_FARM_API_TOKEN
environment variable for the actual call to testing-farm.
Args:
chroot (str): The chroot that you want to run tests for.
Raises:
SystemError: When the testing-farm request failed
Returns:
str: Request ID
"""
logging.info(f"Kicking off new tests for chroot {chroot}.")

ranch = select_ranch(chroot)
logging.info(f"Using testing-farm ranch: {ranch}")
if ranch == "public":
os.environ["TESTING_FARM_API_TOKEN"] = os.getenv(
"TESTING_FARM_API_TOKEN_PUBLIC_RANCH"
)
if ranch == "redhat":
os.environ["TESTING_FARM_API_TOKEN"] = os.getenv(
"TESTING_FARM_API_TOKEN_REDHAT_RANCH"
)
cmd = f"""testing-farm \
request \
--compose Fedora-latest \
--git-url {self.config.test_repo_url} \
--arch {util.chroot_arch(chroot)} \
--plan /tests/snapshot-gating \
--environment COPR_PROJECT={self.config.copr_projectname} \
--context distro={util.chroot_os(chroot)} \
--context arch=${util.chroot_arch(chroot)} \
--no-wait \
--context snapshot={self.config.yyyymmdd}"""
exit_code, stdout, stderr = util.run_cmd(cmd, timeout_secs=None)
if exit_code == 0:
return parse_output_for_request_id(stdout)
raise SystemError(
f"failed to run 'testing-farm request': {cmd}\n\nstdout: {stdout}\n\nstderr: {stderr}"
)

0 comments on commit edb447c

Please sign in to comment.