Skip to content

Commit

Permalink
test: move useful functions from setup-osbuild-repo to imgtestlib
Browse files Browse the repository at this point in the history
  • Loading branch information
achilleas-k committed Dec 19, 2023
1 parent 293eb0c commit b48a61a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
31 changes: 31 additions & 0 deletions test/scripts/imgtestlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

REGISTRY = "registry.gitlab.com/redhat/services/products/image-builder/ci/images"

SCHUTZFILE = "Schutzfile"
OS_RELEASE_FILE = "/etc/os-release"

# ostree containers are pushed to the CI registry to be reused by dependants
OSTREE_CONTAINERS = [
Expand Down Expand Up @@ -254,3 +256,32 @@ def clargs():
help="architecture to generate configs for (defaults to host architecture)")

return parser


def read_osrelease():
"""Read Operating System Information from `os-release`
This creates a dictionary with information describing the running operating system. It reads the information from
the path array provided as `paths`. The first available file takes precedence. It must be formatted according to
the rules in `os-release(5)`.
"""
osrelease = {}

with open(OS_RELEASE_FILE, encoding="utf8") as orf:
for line in orf:
line = line.strip()
if not line:
continue
if line[0] == "#":
continue
key, value = line.split("=", 1)
osrelease[key] = value.strip('"')

return osrelease


def get_osbuild_commit(distro_version):
with open(SCHUTZFILE, encoding="utf-8") as schutzfile:
data = json.load(schutzfile)

return data.get(distro_version, {}).get("dependencies", {}).get("osbuild", {}).get("commit", None)
40 changes: 5 additions & 35 deletions test/scripts/setup-osbuild-repo
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
#
# Add a repository configuration to install the osbuild rpm at a specific
# commit if specified.
import json
import os

SCHUTZFILE = "Schutzfile"
OS_RELEASE_FILE = "/etc/os-release"
REPO_FILE = "/etc/yum.repos.d/osbuild.repo"
import imgtestlib as testlib

REPO_TEMPLATE = """
[osbuild]
Expand All @@ -18,49 +15,22 @@ gpgcheck=0
priority=10
"""


def read_osrelease(path):
"""Read Operating System Information from `os-release`
This creates a dictionary with information describing the running operating system. It reads the information from
the path array provided as `paths`. The first available file takes precedence. It must be formatted according to
the rules in `os-release(5)`.
"""
osrelease = {}

with open(path, encoding="utf8") as f:
for line in f:
line = line.strip()
if not line:
continue
if line[0] == "#":
continue
key, value = line.split("=", 1)
osrelease[key] = value.strip('"')

return osrelease


def get_osbuild_commit(distro_version):
with open(SCHUTZFILE) as schutzfile:
data = json.load(schutzfile)

return data.get(distro_version, {}).get("dependencies", {}).get("osbuild", {}).get("commit", None)
REPO_FILE = "/etc/yum.repos.d/osbuild.repo"


def write_repo(commit, distro_version):
arch = os.uname().machine
repo_path = f"osbuild/{distro_version}/{arch}/{commit}"
print(f"Setting up dnf repository for {commit} ({repo_path})")
with open("/etc/yum.repos.d/osbuild.repo", "w") as repofile:
with open("/etc/yum.repos.d/osbuild.repo", "w", endoding="utf-8") as repofile:
repofile.write(REPO_TEMPLATE.format(commit=commit, repo_path=repo_path))


def main():
osrelease = read_osrelease(OS_RELEASE_FILE)
osrelease = testlib.read_osrelease()

distro_version = osrelease["ID"] + "-" + osrelease["VERSION_ID"]
commit_id = get_osbuild_commit(distro_version)
commit_id = testlib.get_osbuild_commit(distro_version)
if not commit_id:
print("No commit ID defined for osbuild")
return
Expand Down

0 comments on commit b48a61a

Please sign in to comment.