Skip to content

Commit

Permalink
restore more of the example and test as an oci image
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Jan 25, 2025
1 parent 31fdb9a commit d6b38a0
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 11 deletions.
16 changes: 16 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,19 @@ crate.from_cargo(
],
)
use_repo(crate, "crate_index")

# For building test images with py_image_layer
bazel_dep(name = "container_structure_test", version = "1.19.1", dev_dependency = True)
bazel_dep(name = "rules_oci", version = "2.0.1", dev_dependency = True)

oci = use_extension("@rules_oci//oci:extensions.bzl", "oci", dev_dependency = True)
oci.pull(
name = "ubuntu",
digest = "sha256:80dd3c3b9c6cecb9f1667e9290b3bc61b78c2678c02cbdae5f0fea92cc6734ab",
image = "ubuntu",
platforms = [
"linux/arm64/v8",
"linux/amd64",
],
tag = "latest",
)
26 changes: 26 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,29 @@ crates_repository(
load("@crate_index//:defs.bzl", "crate_repositories")

crate_repositories()

load("@rules_oci//oci:dependencies.bzl", "rules_oci_dependencies")

rules_oci_dependencies()

load("@rules_oci//oci:repositories.bzl", "oci_register_toolchains")

oci_register_toolchains(name = "oci")

# You can pull your base images using oci_pull like this:
load("@rules_oci//oci:pull.bzl", "oci_pull")

oci_pull(
name = "ubuntu",
digest = "sha256:80dd3c3b9c6cecb9f1667e9290b3bc61b78c2678c02cbdae5f0fea92cc6734ab",
image = "ubuntu",
platforms = [
"linux/arm64/v8",
"linux/amd64",
],
tag = "latest",
)

load("@container_structure_test//:repositories.bzl", "container_structure_test_register_toolchain")

container_structure_test_register_toolchain(name = "cst")
14 changes: 14 additions & 0 deletions internal_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ def rules_py_internal_deps():
url = "/~https://github.com/bazelbuild/rules_python/releases/download/0.31.0/rules_python-0.31.0.tar.gz",
)

http_archive(
name = "rules_oci",
sha256 = "1bd16e455278d523f01326e0c3964cd64d7840a7e99cdd6e2617e59f698f3504",
strip_prefix = "rules_oci-2.2.0",
url = "/~https://github.com/bazel-contrib/rules_oci/releases/download/v2.2.0/rules_oci-v2.2.0.tar.gz",
)

http_archive(
name = "container_structure_test",
integrity = "sha256-TLs4LT1+3JcSn3n4MZbJXmAG2QY9ntuzOiMRupNyrTk=",
strip_prefix = "container-structure-test-1.19.3",
url = "/~https://github.com/GoogleContainerTools/container-structure-test/archive/refs/tags/v1.19.3.zip",
)

http_archive(
name = "rules_rust",
integrity = "sha256-heIBNyerJvsiq9/+SyrAwnotW2KWFnumPY9uExQPUfk=",
Expand Down
2 changes: 1 addition & 1 deletion py/tests/internal-deps/adder/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ py_library(
# This library contributes to the container test, testing we can pull in and use a library from another
# package in the repo.
visibility = [
"//py/tests/containers:__pkg__",
"//py/tests/internal-deps:__pkg__",
"//py/tests/py_image_layer:__pkg__",
],
)
35 changes: 33 additions & 2 deletions py/tests/py_image_layer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("@container_structure_test//:defs.bzl", "container_structure_test")
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_load")
load("//py:defs.bzl", "py_binary", "py_image_layer")
load("asserts.bzl", "assert_tar_listing")

Expand All @@ -9,10 +11,15 @@ platform(
],
)

# Case 1: Basic usage
py_binary(
name = "my_app_bin",
srcs = ["main.py"],
srcs = ["__main__.py"],
tags = ["manual"],
deps = [
"//py/tests/internal-deps/adder",
"//py/tests/py_image_layer/branding",
"@pypi_colorama//:pkg",
],
)

py_image_layer(
Expand All @@ -26,3 +33,27 @@ assert_tar_listing(
actual = [":my_app_layers"],
expected = ":my_app_layers.listing",
)

oci_image(
name = "image",
# This is defined by an oci.pull() call in /MODULE.bazel
base = "@ubuntu",
entrypoint = ["/{}/my_app_bin".format(package_name())],
tars = [":my_app_layers"],
)

# To build the image and load it into it into a local runtime:
# $ bazel run //py/tests/py_image_layer:image_load
# $ docker run --rm gcr.io/oci_python_hello_world:latest
oci_load(
name = "image_load",
image = ":image",
repo_tags = ["gcr.io/oci_python_hello_world:latest"],
)

container_structure_test(
name = "py_image_test",
configs = ["py_image_test.yaml"],
image = ":image",
tags = ["manual"],
)
7 changes: 7 additions & 0 deletions py/tests/py_image_layer/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from colorama import Fore, Style

from branding import get_branding
from adder.add import add

if __name__ == "__main__":
print(f"{Fore.GREEN}Hello {get_branding()} - {add(3, .14)}{Style.RESET_ALL}")
1 change: 0 additions & 1 deletion py/tests/py_image_layer/asserts.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ def assert_tar_listing(name, actual, expected):
in_file = actual_listing,
out_file = expected,
testonly = True,
tags = ["skip-on-bazel6"],
)
8 changes: 8 additions & 0 deletions py/tests/py_image_layer/branding/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@aspect_rules_py//py:defs.bzl", "py_library")

py_library(
name = "branding",
srcs = ["__init__.py"],
imports = [".."],
visibility = ["//py/tests/py_image_layer:__pkg__"],
)
2 changes: 2 additions & 0 deletions py/tests/py_image_layer/branding/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def get_branding():
return "rules_py"
1 change: 0 additions & 1 deletion py/tests/py_image_layer/main.py

This file was deleted.

42 changes: 36 additions & 6 deletions py/tests/py_image_layer/my_app_layers.listing
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,29 @@ drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app
-rwxr-xr-x 0 0 0 3697 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/python_toolchain_x86_64-unknown-linux-gnu/lib/python3.9/site-packages/setuptools/warnings.py
-rwxr-xr-x 0 0 0 8628 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/python_toolchain_x86_64-unknown-linux-gnu/lib/python3.9/site-packages/setuptools/wheel.py
-rwxr-xr-x 0 0 0 719 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/python_toolchain_x86_64-unknown-linux-gnu/lib/python3.9/site-packages/setuptools/windows_support.py
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/
-rwxr-xr-x 0 0 0 149 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/__init__.py
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama/
-rwxr-xr-x 0 0 0 266 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama/__init__.py
-rwxr-xr-x 0 0 0 2522 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama/ansi.py
-rwxr-xr-x 0 0 0 11128 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama/ansitowin32.py
-rwxr-xr-x 0 0 0 3325 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama/initialise.py
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama/tests/
-rwxr-xr-x 0 0 0 75 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama/tests/__init__.py
-rwxr-xr-x 0 0 0 2839 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama/tests/ansi_test.py
-rwxr-xr-x 0 0 0 10678 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama/tests/ansitowin32_test.py
-rwxr-xr-x 0 0 0 6741 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama/tests/initialise_test.py
-rwxr-xr-x 0 0 0 1866 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama/tests/isatty_test.py
-rwxr-xr-x 0 0 0 1079 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama/tests/utils.py
-rwxr-xr-x 0 0 0 3709 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama/tests/winterm_test.py
-rwxr-xr-x 0 0 0 6181 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama/win32.py
-rwxr-xr-x 0 0 0 7134 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama/winterm.py
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama-0.4.6.dist-info/
-rwxr-xr-x 0 0 0 42 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama-0.4.6.dist-info/INSTALLER
-rwxr-xr-x 0 0 0 17158 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama-0.4.6.dist-info/METADATA
-rwxr-xr-x 0 0 0 105 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama-0.4.6.dist-info/WHEEL
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama-0.4.6.dist-info/licenses/
-rwxr-xr-x 0 0 0 1491 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/site-packages/colorama-0.4.6.dist-info/licenses/LICENSE.txt
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/python_toolchain_x86_64-unknown-linux-gnu/
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/python_toolchain_x86_64-unknown-linux-gnu/bin/
-rwxr-xr-x 0 0 0 20960 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/python_toolchain_x86_64-unknown-linux-gnu/bin/python3
Expand Down Expand Up @@ -2438,20 +2461,27 @@ drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/
-rwxr-xr-x 0 0 0 2887 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin
-rwxr-xr-x 0 0 0 16 Jan 1 2023 ./py/tests/py_image_layer/main.py
-rwxr-xr-x 0 0 0 40 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.venv.pth
-rwxr-xr-x 0 0 0 2895 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin
-rwxr-xr-x 0 0 0 204 Jan 1 2023 ./py/tests/py_image_layer/__main__.py
-rwxr-xr-x 0 0 0 183 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.venv.pth
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/aspect_rules_py/
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/aspect_rules_py/py/
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/aspect_rules_py/py/tests/
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/aspect_rules_py/py/tests/py_image_layer/
-rwxr-xr-x 0 0 0 16 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/aspect_rules_py/py/tests/py_image_layer/main.py
-rwxr-xr-x 0 0 0 40 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/aspect_rules_py/py/tests/py_image_layer/my_app_bin.venv.pth
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/aspect_rules_py/py/tests/py_image_layer/branding/
-rwxr-xr-x 0 0 0 42 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/aspect_rules_py/py/tests/py_image_layer/branding/__init__.py
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/aspect_rules_py/py/tests/internal-deps/
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/aspect_rules_py/py/tests/internal-deps/adder/
-rwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/aspect_rules_py/py/tests/internal-deps/adder/__init__.py
-rwxr-xr-x 0 0 0 32 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/aspect_rules_py/py/tests/internal-deps/adder/add.py
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/pypi_colorama/
-rwxr-xr-x 0 0 0 204 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/aspect_rules_py/py/tests/py_image_layer/__main__.py
-rwxr-xr-x 0 0 0 183 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/aspect_rules_py/py/tests/py_image_layer/my_app_bin.venv.pth
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/bazel_tools/
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/bazel_tools/tools/
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/bazel_tools/tools/bash/
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/bazel_tools/tools/bash/runfiles/
-rwxr-xr-x 0 0 0 21622 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash
drwxr-xr-x 0 0 0 0 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/aspect_rules_py/py/tools/
-rwxr-xr-x 0 0 0 2887 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/aspect_rules_py/py/tests/py_image_layer/my_app_bin
-rwxr-xr-x 0 0 0 2895 Jan 1 2023 ./py/tests/py_image_layer/my_app_bin.runfiles/aspect_rules_py/py/tests/py_image_layer/my_app_bin
14 changes: 14 additions & 0 deletions py/tests/py_image_layer/py_image_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
schemaVersion: 2.0.0

fileExistenceTests:
- name: __main__ is present
path: /app/py/tests/py_image_layer/py_image.binary.runfiles/aspect_rules_py/py/tests/py_image_layer/__main__.py
- name: runfiles dependencies are present
path: /app/py/tests/py_image_layer/py_image.binary.runfiles/pypi_colorama/__init__.py
commandTests:
- name: can run binary
exitCode: 0
command: /usr/bin/python
args:
- /app/py/tests/py_image_layer/py_image.binary
expectedOutput: ["Hello rules_py - 3.14"]

0 comments on commit d6b38a0

Please sign in to comment.