Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.

Commit

Permalink
squad-create-reproducer-from-testrun: Download reproducers by testrun
Browse files Browse the repository at this point in the history
Add support for downloading reproducers by testrun ID in the
squad-create-reproducers script. This allows the user to provide the
--testrun parameter to download the build or test reproducer from a
specific testrun, using the get_reproducer_from_testrun function from
squadutilslib.

The --local flag can be provided to fetch TuxRun reproducer, otherwise
the TuxPlan reproducer will be downloaded by default.

Signed-off-by: Katie Worton <[email protected]>
  • Loading branch information
katieworton authored and roxell committed Dec 11, 2023
1 parent c602628 commit 09ef6cd
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 32 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,25 @@ options:
The number of builds to fetch when searching for a reproducer.
```

### `squad-create-reproducer-from-testrun`: Get a reproducer for a given TestRun ID.

This script fetches the build or test reproducer for a given TestRun ID.

```
usage: squad-create-reproducer-from-testrun [-h] --testrun TESTRUN [--debug]
[--filename FILENAME] [--local]
Provide a SQUAD TestRun ID to download the build or test reproducer for that TestRun. The
reproducer will be printed to the terminal and written to a file.
optional arguments:
-h, --help show this help message and exit
--testrun TESTRUN The TestRun ID of the build or test to fetch the reproducer for.
--debug Display debug messages.
--filename FILENAME Name for the reproducer file, 'reproducer' by default.
--local Fetch a TuxRun or TuxMake reproducer rather than a TuxPlan reproducer.
```

### `squad-create-skipfile-reproducers`: Creating skipfile reproducers

The `squad-create-skipfile-reproducers` script can be used to create TuxRun or
Expand Down
97 changes: 97 additions & 0 deletions squad-create-reproducer-from-testrun
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim: set ts=4
#
# Copyright 2023-present Linaro Limited
#
# SPDX-License-Identifier: MIT


from argparse import ArgumentParser
from logging import INFO, basicConfig, getLogger
from os import chmod, getenv
from stat import S_IRUSR, S_IWUSR, S_IXUSR
from sys import exit

from squad_client.core.api import SquadApi

from squadutilslib import (
ReproducerNotFound,
get_reproducer_from_testrun,
)

squad_host_url = "https://qa-reports.linaro.org/"
SquadApi.configure(cache=3600, url=getenv("SQUAD_HOST", squad_host_url))

basicConfig(level=INFO)
logger = getLogger(__name__)


def parse_args(raw_args):
parser = ArgumentParser(
description="Provide a SQUAD TestRun ID to download the build or test reproducer for that TestRun."
+ " The reproducer will be printed to the terminal and written to a file."
)

parser.add_argument(
"--testrun",
required=True,
help="The TestRun ID of the build or test to fetch the reproducer for.",
)

# Optional arguments:
parser.add_argument(
"--debug",
required=False,
action="store_true",
default=False,
help="Display debug messages.",
)

parser.add_argument(
"--filename",
required=False,
help="Name for the reproducer file, 'reproducer' by default.",
)

parser.add_argument(
"--local",
required=False,
action="store_true",
default=False,
help="Fetch a TuxRun or TuxMake reproducer rather than a TuxPlan reproducer.",
)

return parser.parse_args(raw_args)


def run(raw_args=None):
args = parse_args(raw_args)

# If filename was not provided, set filename to "reproducer"
if not args.filename:
filename = "reproducer"
else:
filename = args.filename

try:
reproducer = get_reproducer_from_testrun(args.testrun, filename, args.local)

except ReproducerNotFound as e:
logger.error(f"No reproducer could be found for TestRun {args.testrun}")
logger.error(f"{e}")
return -1

# If a filename was provided, don't print the reproducer to console
if not args.filename:
print(reproducer)

if args.local:
# Make the script executable
chmod(filename, S_IXUSR | S_IRUSR | S_IWUSR)

logger.info(f"file created: {filename}")


if __name__ == "__main__":
exit(run())
41 changes: 9 additions & 32 deletions squadutilslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,32 +58,18 @@ def get_file(path, filename=None):
raise Exception(f"Path {path} not found")


def get_reproducer_from_testrun(testrun_id, filename, plan=False, local=False):
def get_reproducer_from_testrun(testrun_id, filename, local=False):
"""Given a testrun, download its reproducer."""
testrun = TestRun(testrun_id)
is_test_reproducer = None
reproducer = None

if local and plan:
logger.error("Error: not valid to request both plan=True and local=True.")
raise ReproducerNotFound

# If there is a download_url try to treat it as a build
if testrun.metadata.download_url:
try:
download_file = testrun.metadata.download_url + "/tux_plan.yaml"
if local:
reproducer_file = get_file(
testrun.metadata.download_url + "/tuxmake_reproducer.sh", filename
)
elif plan:
reproducer_file = get_file(
testrun.metadata.download_url + "/tux_plan.yaml", filename
)
else:
reproducer_file = get_file(
testrun.metadata.download_url + "/tuxsuite_reproducer.sh", filename
)
is_test_reproducer = False
download_file = testrun.metadata.download_url + "/tuxmake_reproducer.sh"
reproducer_file = get_file(download_file, filename)
with open(reproducer_file) as f:
reproducer = f.read()
except HTTPError:
Expand All @@ -92,26 +78,17 @@ def get_reproducer_from_testrun(testrun_id, filename, plan=False, local=False):
if not reproducer:
# If no build reproducer was found, treat it as a test
try:
download_file = testrun.metadata.job_url + "/tux_plan"
if local:
reproducer_file = get_file(
testrun.metadata.job_url + "/reproducer", filename
)
elif plan:
reproducer_file = get_file(
testrun.metadata.job_url + "/tux_plan", filename
)
else:
reproducer_file = get_file(
testrun.metadata.job_url + "/tuxsuite_reproducer", filename
)
is_test_reproducer = True
download_file = testrun.metadata.job_url + "/reproducer"
reproducer_file = get_file(download_file, filename)
with open(reproducer_file) as f:
reproducer = f.read()
except HTTPError:
logger.error("No build or test reproducer found.")
raise ReproducerNotFound

return reproducer, is_test_reproducer
return reproducer


def filter_projects(projects, pattern):
Expand Down Expand Up @@ -249,7 +226,7 @@ def get_reproducer(
# In theory there should only be one of those
logger.debug(f"Testrun id: {testrun.id}")

reproducer, is_test_reproducer = get_reproducer_from_testrun(
reproducer = get_reproducer_from_testrun(
testrun_id=testrun.id, filename=filename, plan=False, local=local
)
return (
Expand Down

0 comments on commit 09ef6cd

Please sign in to comment.