diff --git a/MODULE.bazel b/MODULE.bazel index 967dad27..8315d8d4 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -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", +) diff --git a/WORKSPACE b/WORKSPACE index cb5bed37..af575f13 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -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") diff --git a/internal_deps.bzl b/internal_deps.bzl index e597c9f4..51748eb6 100644 --- a/internal_deps.bzl +++ b/internal_deps.bzl @@ -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=", diff --git a/py/tests/internal-deps/adder/BUILD.bazel b/py/tests/internal-deps/adder/BUILD.bazel index c8fd66e0..0609c9c8 100644 --- a/py/tests/internal-deps/adder/BUILD.bazel +++ b/py/tests/internal-deps/adder/BUILD.bazel @@ -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__", ], ) diff --git a/py/tests/py_image_layer/BUILD.bazel b/py/tests/py_image_layer/BUILD.bazel index 914b964d..09519f93 100644 --- a/py/tests/py_image_layer/BUILD.bazel +++ b/py/tests/py_image_layer/BUILD.bazel @@ -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") @@ -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( @@ -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"], +) diff --git a/py/tests/py_image_layer/__main__.py b/py/tests/py_image_layer/__main__.py new file mode 100644 index 00000000..ebfaf15f --- /dev/null +++ b/py/tests/py_image_layer/__main__.py @@ -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}") diff --git a/py/tests/py_image_layer/asserts.bzl b/py/tests/py_image_layer/asserts.bzl index 3b484bb9..f443d9b5 100644 --- a/py/tests/py_image_layer/asserts.bzl +++ b/py/tests/py_image_layer/asserts.bzl @@ -19,5 +19,4 @@ def assert_tar_listing(name, actual, expected): in_file = actual_listing, out_file = expected, testonly = True, - tags = ["skip-on-bazel6"], ) diff --git a/py/tests/py_image_layer/branding/BUILD.bazel b/py/tests/py_image_layer/branding/BUILD.bazel new file mode 100644 index 00000000..e8fa0100 --- /dev/null +++ b/py/tests/py_image_layer/branding/BUILD.bazel @@ -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__"], +) diff --git a/py/tests/py_image_layer/branding/__init__.py b/py/tests/py_image_layer/branding/__init__.py new file mode 100644 index 00000000..39fe20c8 --- /dev/null +++ b/py/tests/py_image_layer/branding/__init__.py @@ -0,0 +1,2 @@ +def get_branding(): + return "rules_py" diff --git a/py/tests/py_image_layer/main.py b/py/tests/py_image_layer/main.py deleted file mode 100644 index e8ac73ad..00000000 --- a/py/tests/py_image_layer/main.py +++ /dev/null @@ -1 +0,0 @@ -print("Hello!!") \ No newline at end of file diff --git a/py/tests/py_image_layer/my_app_layers.listing b/py/tests/py_image_layer/my_app_layers.listing index 7b02781a..d4c3c362 100644 --- a/py/tests/py_image_layer/my_app_layers.listing +++ b/py/tests/py_image_layer/my_app_layers.listing @@ -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 @@ -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 diff --git a/py/tests/py_image_layer/py_image_test.yaml b/py/tests/py_image_layer/py_image_test.yaml new file mode 100644 index 00000000..ad2205bb --- /dev/null +++ b/py/tests/py_image_layer/py_image_test.yaml @@ -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"]