Unverified Commit d6702dfa authored by Kelly Guo's avatar Kelly Guo Committed by GitHub

Updates some more tests to pytest (#543)

# Description

Updates a few missing tests that are still on unittest to pytest.

## Type of change

<!-- As you go through the list, delete the ones that are not
applicable. -->

- Bug fix (non-breaking change which fixes an issue)


## Checklist

- [x] I have run the [`pre-commit` checks](https://pre-commit.com/) with
`./isaaclab.sh --format`
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have updated the changelog and the corresponding version in the
extension's `config/extension.toml` file
- [ ] I have added my name to the `CONTRIBUTORS.md` or my name already
exists there

<!--
As you go through the checklist above, you can mark something as done by
putting an x character in it

For example,
- [x] I have done this task
- [ ] I have not done this task
-->
parent 7ac5ad53
......@@ -5,70 +5,60 @@
import os
import subprocess
import unittest
from pathlib import Path
class TestDocker(unittest.TestCase):
"""Test starting and stopping of the docker container with both currently supported profiles and with and without
a suffix. This assumes that docker is installed and configured correctly so that the user can use the docker
commands from the current shell."""
def start_stop_docker(self, profile, suffix):
"""Test starting and stopping docker profile with suffix."""
environ = os.environ
context_dir = Path(__file__).resolve().parent.parent
# generate parameters for the arguments
if suffix != "":
container_name = f"isaac-lab-{profile}-{suffix}"
suffix_args = ["--suffix", suffix]
else:
container_name = f"isaac-lab-{profile}"
suffix_args = []
run_kwargs = {
"check": False,
"capture_output": True,
"text": True,
"cwd": context_dir,
"env": environ,
}
# start the container
docker_start = subprocess.run(["python", "container.py", "start", profile] + suffix_args, **run_kwargs)
self.assertEqual(docker_start.returncode, 0)
# verify that the container is running
docker_running_true = subprocess.run(["docker", "ps"], **run_kwargs)
self.assertEqual(docker_running_true.returncode, 0)
self.assertIn(container_name, docker_running_true.stdout)
# stop the container
docker_stop = subprocess.run(["python", "container.py", "stop", profile] + suffix_args, **run_kwargs)
self.assertEqual(docker_stop.returncode, 0)
# verify that the container has stopped
docker_running_false = subprocess.run(["docker", "ps"], **run_kwargs)
self.assertEqual(docker_running_false.returncode, 0)
self.assertNotIn(container_name, docker_running_false.stdout)
def test_docker_base(self):
"""Test starting and stopping docker base."""
self.start_stop_docker("base", "")
def test_docker_base_suffix(self):
"""Test starting and stopping docker base with a test suffix."""
self.start_stop_docker("base", "test")
def test_docker_ros2(self):
"""Test starting and stopping docker ros2."""
self.start_stop_docker("ros2", "")
def test_docker_ros2_suffix(self):
"""Test starting and stopping docker ros2 with a test suffix."""
self.start_stop_docker("ros2", "test")
if __name__ == "__main__":
unittest.main(verbosity=2, exit=True)
import pytest
def start_stop_docker(profile, suffix):
"""Test starting and stopping docker profile with suffix."""
environ = os.environ
context_dir = Path(__file__).resolve().parent.parent
# generate parameters for the arguments
if suffix != "":
container_name = f"isaac-lab-{profile}-{suffix}"
suffix_args = ["--suffix", suffix]
else:
container_name = f"isaac-lab-{profile}"
suffix_args = []
run_kwargs = {
"check": False,
"capture_output": True,
"text": True,
"cwd": context_dir,
"env": environ,
}
# start the container
docker_start = subprocess.run(["python", "container.py", "start", profile] + suffix_args, **run_kwargs)
assert docker_start.returncode == 0
# verify that the container is running
docker_running_true = subprocess.run(["docker", "ps"], **run_kwargs)
assert docker_running_true.returncode == 0
assert container_name in docker_running_true.stdout
# stop the container
docker_stop = subprocess.run(["python", "container.py", "stop", profile] + suffix_args, **run_kwargs)
assert docker_stop.returncode == 0
# verify that the container has stopped
docker_running_false = subprocess.run(["docker", "ps"], **run_kwargs)
assert docker_running_false.returncode == 0
assert container_name not in docker_running_false.stdout
@pytest.mark.parametrize(
"profile,suffix",
[
("base", ""),
("base", "test"),
("ros2", ""),
("ros2", "test"),
],
)
def test_docker_profiles(profile, suffix):
"""Test starting and stopping docker profiles with and without suffixes."""
start_stop_docker(profile, suffix)
......@@ -3,6 +3,7 @@
#
# SPDX-License-Identifier: BSD-3-Clause
"""
This script checks the functionality of scale randomization.
"""
......@@ -20,9 +21,9 @@ simulation_app = app_launcher.app
"""Rest everything follows."""
import torch
import unittest
import omni.usd
import pytest
from pxr import Sdf
import isaaclab.envs.mdp as mdp
......@@ -278,80 +279,73 @@ class CubeEnvCfg(ManagerBasedEnvCfg):
self.sim.render_interval = self.decimation
class TestScaleRandomization(unittest.TestCase):
"""Test for scale randomization."""
"""
Tests
"""
def test_scale_randomization(self):
"""Test scale randomization for cube environment."""
for device in ["cpu", "cuda"]:
with self.subTest(device=device):
# create a new stage
omni.usd.get_context().new_stage()
# set the device
env_cfg = CubeEnvCfg()
env_cfg.sim.device = device
# setup base environment
env = ManagerBasedEnv(cfg=env_cfg)
# setup target position commands
target_position = torch.rand(env.num_envs, 3, device=env.device) * 2
target_position[:, 2] += 2.0
# offset all targets so that they move to the world origin
target_position -= env.scene.env_origins
# test to make sure all assets in the scene are created
all_prim_paths = sim_utils.find_matching_prim_paths("/World/envs/env_.*/cube.*/.*")
self.assertEqual(len(all_prim_paths), (env.num_envs * 2))
# test to make sure randomized values are truly random
applied_scaling_randomization = set()
prim_paths = sim_utils.find_matching_prim_paths("/World/envs/env_.*/cube1")
# get the stage
stage = omni.usd.get_context().get_stage()
# check if the scale values are truly random
for i in range(3):
prim_spec = Sdf.CreatePrimInLayer(stage.GetRootLayer(), prim_paths[i])
scale_spec = prim_spec.GetAttributeAtPath(prim_paths[i] + ".xformOp:scale")
if scale_spec.default in applied_scaling_randomization:
raise ValueError(
"Detected repeat in applied scale values - indication scaling randomization is not working."
)
applied_scaling_randomization.add(scale_spec.default)
# test to make sure that fixed values are assigned correctly
prim_paths = sim_utils.find_matching_prim_paths("/World/envs/env_.*/cube2")
for i in range(3):
prim_spec = Sdf.CreatePrimInLayer(stage.GetRootLayer(), prim_paths[i])
scale_spec = prim_spec.GetAttributeAtPath(prim_paths[i] + ".xformOp:scale")
self.assertEqual(tuple(scale_spec.default), (1.0, 1.0, 1.0))
# simulate physics
with torch.inference_mode():
for count in range(200):
# reset every few steps to check nothing breaks
if count % 100 == 0:
env.reset()
# step the environment
env.step(target_position)
env.close()
def test_scale_randomization_failure_replicate_physics(self):
"""Test scale randomization failure when replicate physics is set to True."""
# create a new stage
omni.usd.get_context().new_stage()
# set the arguments
cfg_failure = CubeEnvCfg()
cfg_failure.scene.replicate_physics = True
# run the test
with self.assertRaises(RuntimeError):
env = ManagerBasedEnv(cfg_failure)
env.close()
@pytest.mark.parametrize("device", ["cpu", "cuda"])
def test_scale_randomization(device):
"""Test scale randomization for cube environment."""
# create a new stage
omni.usd.get_context().new_stage()
# set the device
env_cfg = CubeEnvCfg()
env_cfg.sim.device = device
# setup base environment
env = ManagerBasedEnv(cfg=env_cfg)
# setup target position commands
target_position = torch.rand(env.num_envs, 3, device=env.device) * 2
target_position[:, 2] += 2.0
# offset all targets so that they move to the world origin
target_position -= env.scene.env_origins
# test to make sure all assets in the scene are created
all_prim_paths = sim_utils.find_matching_prim_paths("/World/envs/env_.*/cube.*/.*")
assert len(all_prim_paths) == (env.num_envs * 2)
# test to make sure randomized values are truly random
applied_scaling_randomization = set()
prim_paths = sim_utils.find_matching_prim_paths("/World/envs/env_.*/cube1")
# get the stage
stage = omni.usd.get_context().get_stage()
# check if the scale values are truly random
for i in range(3):
prim_spec = Sdf.CreatePrimInLayer(stage.GetRootLayer(), prim_paths[i])
scale_spec = prim_spec.GetAttributeAtPath(prim_paths[i] + ".xformOp:scale")
if scale_spec.default in applied_scaling_randomization:
raise ValueError(
"Detected repeat in applied scale values - indication scaling randomization is not working."
)
applied_scaling_randomization.add(scale_spec.default)
# test to make sure that fixed values are assigned correctly
prim_paths = sim_utils.find_matching_prim_paths("/World/envs/env_.*/cube2")
for i in range(3):
prim_spec = Sdf.CreatePrimInLayer(stage.GetRootLayer(), prim_paths[i])
scale_spec = prim_spec.GetAttributeAtPath(prim_paths[i] + ".xformOp:scale")
assert tuple(scale_spec.default) == (1.0, 1.0, 1.0)
# simulate physics
with torch.inference_mode():
for count in range(200):
# reset every few steps to check nothing breaks
if count % 100 == 0:
env.reset()
# step the environment
env.step(target_position)
env.close()
def test_scale_randomization_failure_replicate_physics():
"""Test scale randomization failure when replicate physics is set to True."""
# create a new stage
omni.usd.get_context().new_stage()
# set the arguments
cfg_failure = CubeEnvCfg()
cfg_failure.scene.replicate_physics = True
# run the test
with pytest.raises(RuntimeError):
env = ManagerBasedEnv(cfg_failure)
env.close()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment