Skip to content

Commit

Permalink
Ensure all Bazel projects use same .bazelrc ROS 2 testing setup
Browse files Browse the repository at this point in the history
Move drake_ros...bazel_ros_testing to bazel_ros2_rules...bazel_ros_env
  • Loading branch information
EricCousineau-TRI committed Sep 25, 2023
1 parent 266b5b0 commit dfed0a8
Show file tree
Hide file tree
Showing 18 changed files with 106 additions and 60 deletions.
8 changes: 5 additions & 3 deletions bazel_ros2_rules/ros2/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ COMMON_FILES_MANIFEST = [
"ros_py.bzl",
"rosidl.bzl",
"_calculate_rosidl_capitalization.bzl",
"resources/bazel_ros_env/bazel_ros_env.py",
"resources/bazel_ros_env/package.BUILD.bazel",
"resources/cmake_tools/__init__.py",
"resources/cmake_tools/file_api.py",
"resources/cmake_tools/packages.py",
Expand All @@ -18,14 +20,14 @@ COMMON_FILES_MANIFEST = [
"resources/rmw_isolation/rmw_isolation.cc",
"resources/rmw_isolation/rmw_isolation.h",
"resources/rmw_isolation/rmw_isolation.py",
"resources/rmw_isolation/test/rmw_isolation_test.py",
"resources/rmw_isolation/test/rmw_isolation_test.cc",
"resources/rmw_isolation/test/rmw_isolation_test.py",
"resources/ros2bzl/__init__.py",
"resources/ros2bzl/resources.py",
"resources/ros2bzl/sandboxing.py",
"resources/ros2bzl/scraping/__init__.py",
"resources/ros2bzl/scraping/ament_cmake.py",
"resources/ros2bzl/scraping/ament_python.py",
"resources/ros2bzl/scraping/__init__.py",
"resources/ros2bzl/scraping/metadata.py",
"resources/ros2bzl/scraping/properties.py",
"resources/ros2bzl/scraping/system.py",
Expand All @@ -38,8 +40,8 @@ COMMON_FILES_MANIFEST = [
"resources/templates/package_cc_library.bazel.tpl",
"resources/templates/package_interfaces_filegroup.bazel.tpl",
"resources/templates/package_meta_py_library.bazel.tpl",
"resources/templates/package_py_library.bazel.tpl",
"resources/templates/package_py_library_with_cc_libs.bazel.tpl",
"resources/templates/package_py_library.bazel.tpl",
"resources/templates/package_share_filegroup.bazel.tpl",
"resources/templates/prologue.bazel",
"resources/templates/run.bash.in",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# TODO(eric.cousineau): Hoist this to rmw_isolation / bazel_ros2_rules.
# TODO(#303): Move RMW + ROS isolation to a location accessible by CMake as
# well.

import functools
import os
Expand All @@ -7,15 +8,6 @@
from rules_python.python.runfiles import runfiles


@functools.lru_cache
def _runfiles():
return runfiles.Create()


def Rlocation(path):
return _runfiles().Rlocation(path)


def make_unique_ros_isolation_env(
*,
unique_identifier=None,
Expand All @@ -30,14 +22,18 @@ def make_unique_ros_isolation_env(
Warning:
scratch_directory should generally be unique to prevent collisions
among environments intended to be distinct.
For more info, see:
https://github.com/RobotLocomotion/drake-ros/blob/main/bazel_ros2_rules/ros2/resources/rmw_isolation/rmw_isolation.py
""" # noqa

if unique_identifier is None:
# A PID should be unique for a single machine.
unique_identifier = str(os.getpid())
is_bazel_test = "TEST_TMPDIR" in environ
# A PID should be unique for a single machine if we are not
# running inside `bazel test`. This may or may not be true
# inside of `bazel test - it is explicitly undefined.
# https://bazel.build/reference/test-encyclopedia#initial-conditions
if is_bazel_test:
unique_identifier = os.environ["TEST_TMPDIR"]
else:
unique_identifier = str(os.getpid())

if temp_dir is None:
temp_dir = environ.get("TEST_TMPDIR", "/tmp")
Expand All @@ -62,14 +58,15 @@ def make_unique_ros_isolation_env(
return env


def make_bazel_runfiles_env():
env = dict(_runfiles().EnvVars())
return env
@functools.lru_cache
def _runfiles():
return runfiles.Create()


def maybe_make_test_ros_isolation_env():
env = dict()
if "TEST_TMPDIR" in os.environ:
# Only isolate when testing.
env.update(make_unique_ros_isolation_env())
def Rlocation(path):
return _runfiles().Rlocation(path)


def make_bazel_runfiles_env():
env = dict(_runfiles().EnvVars())
return env
13 changes: 13 additions & 0 deletions bazel_ros2_rules/ros2/resources/bazel_ros_env/package.BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- python -*-
# vi: set ft=python :

py_library(
name = "bazel_ros_env_py",
srcs = ["bazel_ros_env.py"],
visibility = ["//visibility:public"],
imports = ["."],
deps = [
"@ros2//resources/rmw_isolation:rmw_isolation_py",
"@rules_python//python/runfiles",
],
)
11 changes: 11 additions & 0 deletions default.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@ build --action_env=ROS_HOME=/tmp/.ros
# Default test options.
test --test_output=errors
test --test_summary=terse

# Prevent ROS 2 messages from leaking into or outside of tests.
# At present, this error message is not fully traceable. As of 2022-11-03, it
# appears as follows:
# "failed to initialize rcl: ROS_DOMAIN_ID is not an integral number".
# See https://github.com/RobotLocomotion/drake-ros/issues/187 for possible
# improvements.
build --test_env=ROS_DOMAIN_ID=INVALID_PLEASE_USE_RMW_ISOLATION
test --test_env=ROS_HOME=/does_not_exist/instead_please_reconfigure_in_test
# Disable ROS communication with other machines
test --test_env=ROS_LOCALHOST_ONLY=1
6 changes: 3 additions & 3 deletions drake_ros/drake_ros/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ ros_py_test(
"@ros2//:rclpy_py",
"@ros2//:std_msgs_py",
"@ros2//:test_msgs_py",
"@ros2//resources/rmw_isolation:rmw_isolation_py",
"@ros2//resources/bazel_ros_env:bazel_ros_env_py",
],
)

Expand All @@ -91,7 +91,7 @@ ros_py_test(
"@drake//bindings/pydrake",
"@ros2//:rclpy_py",
"@ros2//:tf2_ros_py_py",
"@ros2//resources/rmw_isolation:rmw_isolation_py",
"@ros2//resources/bazel_ros_env:bazel_ros_env_py",
],
)

Expand All @@ -104,7 +104,7 @@ ros_py_test(
"@drake//bindings/pydrake",
"@ros2//:rclpy_py",
"@ros2//:visualization_msgs_py",
"@ros2//resources/rmw_isolation:rmw_isolation_py",
"@ros2//resources/bazel_ros_env:bazel_ros_env_py",
],
)

Expand Down
11 changes: 7 additions & 4 deletions drake_ros/drake_ros/test/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@


def isolate_if_using_bazel():
if 'TEST_TMPDIR' in os.environ:
# This package can only be imported when using bazel_ros2_rules
from rmw_isolation import isolate_rmw_by_path
isolate_rmw_by_path(os.environ['TEST_TMPDIR'])
# Do not require `make_unique_ros_isolation_env` module for CMake.
# TODO(eric.cousineau): Expose this to CMake in better location..
try:
from bazel_ros_env import make_unique_ros_isolation_env
os.environ.update(make_unique_ros_isolation_env())
except ImportError:
assert "TEST_TMPDIR" not in os.environ


@pytest.fixture
Expand Down
12 changes: 12 additions & 0 deletions drake_ros/drake_ros/test/tf2_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import math
import sys

Expand Down Expand Up @@ -27,6 +28,16 @@
import tf2_ros


def isolate_if_using_bazel():
# Do not require `make_unique_ros_isolation_env` module for CMake.
# TODO(eric.cousineau): Expose this to CMake in better location..
try:
from bazel_ros_env import make_unique_ros_isolation_env
os.environ.update(make_unique_ros_isolation_env())
except ImportError:
assert "TEST_TMPDIR" not in os.environ


def test_nominal_case():
drake_ros.core.init()

Expand Down Expand Up @@ -122,4 +133,5 @@ def test_nominal_case():


if __name__ == '__main__':
isolate_if_using_bazel()
sys.exit(pytest.main(sys.argv))
12 changes: 12 additions & 0 deletions drake_ros/drake_ros/test/viz_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import random
import string
import sys
Expand All @@ -21,6 +22,16 @@
from drake_ros.viz import RvizVisualizer


def isolate_if_using_bazel():
# Do not require `make_unique_ros_isolation_env` module for CMake.
# TODO(eric.cousineau): Expose this to CMake in better location..
try:
from bazel_ros_env import make_unique_ros_isolation_env
os.environ.update(make_unique_ros_isolation_env())
except ImportError:
assert "TEST_TMPDIR" not in os.environ


class ManagedSubscription:
def __init__(
self,
Expand Down Expand Up @@ -169,4 +180,5 @@ def test_receive_visual_marker_array():


if __name__ == '__main__':
isolate_if_using_bazel()
sys.exit(pytest.main(sys.argv))
11 changes: 0 additions & 11 deletions drake_ros_examples/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,3 @@ py_test(
"@drake_ros_repo//:drake_ros_py",
],
)

py_library(
name = "bazel_ros_testing_py",
testonly = 1,
srcs = ["test/bazel_ros_testing.py"],
visibility = ["//:__subpackages__"],
deps = [
"@ros2//resources/rmw_isolation:rmw_isolation_py",
"@rules_python//python/runfiles",
],
)
2 changes: 1 addition & 1 deletion drake_ros_examples/examples/hydroelastic/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ ros_py_test(
],
main = "test/hydroelastic_test.py",
deps = [
"//:bazel_ros_testing_py",
"@ros2//resources/bazel_ros_env:bazel_ros_env_py",
],
)
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import os
from subprocess import run

from drake_ros_examples.test.bazel_ros_testing import (
from bazel_ros_env import (
Rlocation,
make_bazel_runfiles_env,
maybe_make_test_ros_isolation_env,
make_unique_ros_isolation_env,
)


def make_env():
env = dict(os.environ)
env.update(make_bazel_runfiles_env())
env.update(maybe_make_test_ros_isolation_env())
env.update(make_unique_ros_isolation_env())
return env


Expand Down
2 changes: 1 addition & 1 deletion drake_ros_examples/examples/iiwa_manipulator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ ros_py_test(
],
main = "test/iiwa_manipulator_test.py",
deps = [
"//:bazel_ros_testing_py",
"@ros2//resources/bazel_ros_env:bazel_ros_env_py",
],
)
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import os
from subprocess import run

from drake_ros_examples.test.bazel_ros_testing import (
from bazel_ros_env import (
Rlocation,
make_bazel_runfiles_env,
maybe_make_test_ros_isolation_env,
make_unique_ros_isolation_env,
)


def make_env():
env = dict(os.environ)
env.update(make_bazel_runfiles_env())
env.update(maybe_make_test_ros_isolation_env())
env.update(make_unique_ros_isolation_env())
return env


Expand Down
2 changes: 1 addition & 1 deletion drake_ros_examples/examples/multirobot/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ ros_py_test(
],
main = "test/multirobot_test.py",
deps = [
"//:bazel_ros_testing_py",
"@ros2//resources/bazel_ros_env:bazel_ros_env_py",
],
)
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import os
from subprocess import run

from drake_ros_examples.test.bazel_ros_testing import (
from bazel_ros_env import (
Rlocation,
make_bazel_runfiles_env,
maybe_make_test_ros_isolation_env,
make_unique_ros_isolation_env,
)


def make_env():
env = dict(os.environ)
env.update(make_bazel_runfiles_env())
env.update(maybe_make_test_ros_isolation_env())
env.update(make_unique_ros_isolation_env())
return env


Expand Down
2 changes: 1 addition & 1 deletion drake_ros_examples/examples/rs_flip_flop/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ ros_py_test(
],
main = "test/rs_flip_flop_test.py",
deps = [
"//:bazel_ros_testing_py",
"@ros2//resources/bazel_ros_env:bazel_ros_env_py",
],
)
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import os
from subprocess import run

from drake_ros_examples.test.bazel_ros_testing import (
from bazel_ros_env import (
Rlocation,
make_bazel_runfiles_env,
maybe_make_test_ros_isolation_env,
make_unique_ros_isolation_env,
)


def make_env():
env = dict(os.environ)
env.update(make_bazel_runfiles_env())
env.update(maybe_make_test_ros_isolation_env())
env.update(make_unique_ros_isolation_env())
return env


Expand Down
7 changes: 7 additions & 0 deletions ros2_example_bazel_installed/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ http_archive(
],
)

http_archive(
name = "rules_python",
sha256 = "0a8003b044294d7840ac7d9d73eef05d6ceb682d7516781a4ec62eeb34702578", # noqa
strip_prefix = "rules_python-0.24.0",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.24.0/rules_python-0.24.0.tar.gz", # noqa
)

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()
Expand Down

0 comments on commit dfed0a8

Please sign in to comment.