Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: testing #91

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# ignoring e2e MODULE.bazel.lock for the time being
# see https://github.com/bazelbuild/bazel/issues/20369
e2e/smoke/MODULE.bazel.lock
bazel-*
.bazelrc.user
.idea/
Expand Down
13 changes: 11 additions & 2 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,20 @@ load("@bullseye//:packages.bzl", "bullseye_packages")

bullseye_packages()

# bazel run @apt_security//:lock
deb_index(
name = "bullseye_nolock",
manifest = "//examples/debian_snapshot:bullseye_nolock.yaml",
nolock = True,
)

load("@bullseye_nolock//:packages.bzl", "bullseye_nolock_packages")

bullseye_nolock_packages()

deb_index(
name = "apt_security",
# lock = "//examples/debian_snapshot_security:security.lock.json",
manifest = "//examples/debian_snapshot_security:security.yaml",
nolock = True,
)

load("@apt_security//:packages.bzl", "apt_security_packages")
Expand Down
9 changes: 7 additions & 2 deletions apt/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ def _distroless_extension(module_ctx):
install.resolve_transitive,
)

# buildifier: disable=print
print("\nNo lockfile was given, please run `bazel run @%s//:lock` to create the lockfile." % install.name)
if not install.nolock:
# buildifier: disable=print
print("\nNo lockfile was given, please run `bazel run @%s//:lock` to create the lockfile." % install.name)
else:
lockf = lockfile.from_json(module_ctx, module_ctx.read(install.lock))

Expand Down Expand Up @@ -65,6 +66,10 @@ def _distroless_extension(module_ctx):
install = tag_class(attrs = {
"name": attr.string(doc = "Name of the generated repository"),
"lock": attr.label(doc = """The lock file to use for the index."""),
"nolock": attr.bool(
doc = """If you explicitly want to run without a lock, set it to True to avoid the DEBUG messages.""",
default = False,
),
"manifest": attr.label(doc = """The file used to generate the lock file"""),
"resolve_transitive": attr.bool(
doc = """Whether dependencies of dependencies should be resolved and added to the lockfile.""",
Expand Down
14 changes: 11 additions & 3 deletions apt/index.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def deb_index(
name,
manifest,
lock = None,
nolock = False,
package_template = None,
resolve_transitive = True):
"""A convience repository macro for apt rules.
Expand All @@ -20,9 +21,13 @@ def deb_index(

deb_index(
name = "bullseye",
# For the initial setup, the lockfile attribute can be omitted and generated by running
# For the initial setup, the lockfile attribute can be omitted and
# generated by running
# bazel run @bullseye//:lock
# This will generate the lock.json file next to the manifest file by replacing `.yaml` with `.lock.json`
# This will generate the lock.json file next to the manifest file by
# replacing `.yaml` with `.lock.json`.
# If you explicitly want to run without a lock, set nolock to True to
# avoid the DEBUG messages
lock = "//examples/apt:bullseye.lock.json",
manifest = "//examples/apt:bullseye.yaml",
)
Expand Down Expand Up @@ -57,6 +62,8 @@ def deb_index(

While we strongly encourage users to check in the generated lockfile, it's not always possible because Debian repositories are rolling by default. Therefore, a lockfile generated today might not work later if the upstream repository removes or publishes a new version of a package.

If you explicitly want to run without a lock, set the `nolock` attribute to True to avoid the DEBUG messages.

#### Snapshot repositories

##### Debian
Expand All @@ -73,6 +80,7 @@ def deb_index(
name: name of the repository
manifest: label to a `manifest.yaml`
lock: label to a `lock.json`
nolock: bool, set to True if you explicitly want to run without a lock and avoid the DEBUG messages.
package_template: (EXPERIMENTAL!) a template file for generated BUILD files.
Available template replacement keys are: `{target_name}`, `{deps}`, `{urls}`, `{name}`, `{arch}`, `{sha256}`, `{repo_name}`
resolve_transitive: whether dependencies of dependencies should be resolved and added to the lockfile.
Expand All @@ -83,7 +91,7 @@ def deb_index(
resolve_transitive = resolve_transitive,
)

if not lock:
if not lock and not nolock:
# buildifier: disable=print
print("\nNo lockfile was given, please run `bazel run @%s//:lock` to create the lockfile." % name)

Expand Down
12 changes: 7 additions & 5 deletions apt/private/resolve.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ if [ -e $BUILD_WORKSPACE_DIRECTORY/WORKSPACE ]; then
wksp_file="WORKSPACE"
elif [ -e $BUILD_WORKSPACE_DIRECTORY/WORKSPACE.bazel ]; then
wksp_file="WORKSPACE.bazel"
elif [ -e $BUILD_WORKSPACE_DIRECTORY/MODULE.bazel ]; then
wksp_file="MODULE.bazel"
else
echo>&2 "Error: neither WORKSPACE nor WORKSPACE.bazel file was found"
echo>&2 "Error: no MODULE.bazel or WORKSPACE file was found"
exit 1
fi

Expand Down Expand Up @@ -151,10 +153,10 @@ def _deb_resolve_impl(rctx):
rctx.file(
"copy.sh",
_COPY_SH_TMPL.format(
# TODO: don't assume the canonical -> apparent repo mapping character, as it might change
# https://bazelbuild.slack.com/archives/C014RARENH0/p1719237766005439
# https://github.com/bazelbuild/bazel/issues/22865
name = rctx.name.split("~")[-1],
# NOTE: the split("~") is needed when we run bazel from another
# directory, e.g. when running e2e tests we change dir to e2e/smoke
# and then rctx.name is 'rules_distroless~~apt~bullseye'
name = rctx.name.split("~")[-1].replace("_resolve", ""),
label = locklabel,
workspace_relative_path = (("%s/" % locklabel.package) if locklabel.package else "") + locklabel.name,
),
Expand Down
12 changes: 4 additions & 8 deletions apt/tests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
load(":package_resolution_test.bzl", "version_depends_test")
load(":version_test.bzl", "version_compare_test", "version_parse_test", "version_sort_test")
load(":package_resolution_test.bzl", "package_resolution_tests")
load(":version_test.bzl", "version_tests")

version_compare_test(name = "version_compare_test")
package_resolution_tests()

version_parse_test(name = "version_parse_test")

version_depends_test(name = "version_depends")

version_sort_test(name = "version_sort_test")
version_tests()
106 changes: 60 additions & 46 deletions apt/tests/package_resolution_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,73 @@
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
load("//apt/private:package_resolution.bzl", "package_resolution")

_TEST_SUITE_PREFIX = "package_resolution/"

def _parse_depends_test(ctx):
env = unittest.begin(ctx)
asserts.equals(
env,
[

parameters = {
" | ".join([
"libc6 (>= 2.2.1), default-mta",
"mail-transport-agent",
]): [
{"name": "libc6", "version": (">=", "2.2.1"), "arch": None},
[{"name": "default-mta", "version": None, "arch": None}, {"name": "mail-transport-agent", "version": None, "arch": None}],
[
{"name": "default-mta", "version": None, "arch": None},
{"name": "mail-transport-agent", "version": None, "arch": None},
],
],
package_resolution.parse_depends("libc6 (>= 2.2.1), default-mta | mail-transport-agent"),
)

asserts.equals(
env,
[
{"name": "libluajit5.1-dev", "version": None, "arch": ["i386", "amd64", "kfreebsd-i386", "armel", "armhf", "powerpc", "mips"]},
{"name": "liblua5.1-dev", "version": None, "arch": ["hurd-i386", "ia64", "kfreebsd-amd64", "s390x", "sparc"]},
", ".join([
"libluajit5.1-dev [i386 amd64 powerpc mips]",
"liblua5.1-dev [hurd-i386 ia64 s390x sparc]",
]): [
{
"name": "libluajit5.1-dev",
"version": None,
"arch": ["i386", "amd64", "powerpc", "mips"],
},
{
"name": "liblua5.1-dev",
"version": None,
"arch": ["hurd-i386", "ia64", "s390x", "sparc"],
},
],
package_resolution.parse_depends("libluajit5.1-dev [i386 amd64 kfreebsd-i386 armel armhf powerpc mips], liblua5.1-dev [hurd-i386 ia64 kfreebsd-amd64 s390x sparc]"),
)

asserts.equals(
env,
[
" | ".join([
"emacs",
"emacsen, make, debianutils (>= 1.7)",
]): [
[
{"name": "emacs", "version": None, "arch": None},
{"name": "emacsen", "version": None, "arch": None},
],
{"name": "make", "version": None, "arch": None},
{"name": "debianutils", "version": (">=", "1.7"), "arch": None},
],
package_resolution.parse_depends("emacs | emacsen, make, debianutils (>= 1.7)"),
)

asserts.equals(
env,
[
{"name": "libcap-dev", "version": None, "arch": ["!kfreebsd-i386", "!kfreebsd-amd64", "!hurd-i386"]},
", ".join([
"libcap-dev [!kfreebsd-i386 !hurd-i386]",
"autoconf",
"debhelper (>> 5.0.0)",
"file",
"libc6 (>= 2.7-1)",
"libpaper1",
"psutils",
]): [
{"name": "libcap-dev", "version": None, "arch": ["!kfreebsd-i386", "!hurd-i386"]},
{"name": "autoconf", "version": None, "arch": None},
{"name": "debhelper", "version": (">>", "5.0.0"), "arch": None},
{"name": "file", "version": None, "arch": None},
{"name": "libc6", "version": (">=", "2.7-1"), "arch": None},
{"name": "libpaper1", "version": None, "arch": None},
{"name": "psutils", "version": None, "arch": None},
],
package_resolution.parse_depends("libcap-dev [!kfreebsd-i386 !kfreebsd-amd64 !hurd-i386], autoconf, debhelper (>> 5.0.0), file, libc6 (>= 2.7-1), libpaper1, psutils"),
)

asserts.equals(
env,
[
"python3:any": [
{"name": "python3", "version": None, "arch": ["any"]},
],
package_resolution.parse_depends("python3:any"),
)

asserts.equals(
env,
[
" | ".join([
"gcc-i686-linux-gnu (>= 4:10.2)",
"gcc:i386, g++-i686-linux-gnu (>= 4:10.2)",
"g++:i386, dpkg-cross",
]): [
[
{"name": "gcc-i686-linux-gnu", "version": (">=", "4:10.2"), "arch": None},
{"name": "gcc", "version": None, "arch": ["i386"]},
Expand All @@ -71,12 +80,11 @@ def _parse_depends_test(ctx):
],
{"name": "dpkg-cross", "version": None, "arch": None},
],
package_resolution.parse_depends("gcc-i686-linux-gnu (>= 4:10.2) | gcc:i386, g++-i686-linux-gnu (>= 4:10.2) | g++:i386, dpkg-cross"),
)

asserts.equals(
env,
[
" | ".join([
"gcc-x86-64-linux-gnu (>= 4:10.2)",
"gcc:amd64, g++-x86-64-linux-gnu (>= 4:10.2)",
"g++:amd64, dpkg-cross",
]): [
[
{"name": "gcc-x86-64-linux-gnu", "version": (">=", "4:10.2"), "arch": None},
{"name": "gcc", "version": None, "arch": ["amd64"]},
Expand All @@ -87,9 +95,15 @@ def _parse_depends_test(ctx):
],
{"name": "dpkg-cross", "version": None, "arch": None},
],
package_resolution.parse_depends("gcc-x86-64-linux-gnu (>= 4:10.2) | gcc:amd64, g++-x86-64-linux-gnu (>= 4:10.2) | g++:amd64, dpkg-cross"),
)
}

for deps, expected in parameters.items():
actual = package_resolution.parse_depends(deps)
asserts.equals(env, expected, actual)

return unittest.end(env)

version_depends_test = unittest.make(_parse_depends_test)
parse_depends_test = unittest.make(_parse_depends_test)

def package_resolution_tests():
parse_depends_test(name = _TEST_SUITE_PREFIX + "parse_depends")
Loading