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

feat: adding dynamic scenario and object creation tests for CARLA #227

Merged
merged 43 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
3e77edc
feat: [wip] adding dynamic scenario tests for CARLA
Apr 1, 2024
5c3e14d
fix: attempting to add some stateful behaviors
Apr 1, 2024
a07539f
feat: adding throttle and brake dynamic scenario tests
Apr 3, 2024
1ee7686
feat: apply formatting for dynamic scenarios
Apr 3, 2024
440fc87
feat: adding object creation tests
Apr 3, 2024
4a67237
fix: reformatting CarlaSimulator import
Apr 3, 2024
9ff068c
fix: fixing BusStop error and adding all objects test
Apr 3, 2024
1c472e1
fix: removing typo
Apr 3, 2024
89783f1
fix: adding compileScenic inline for testing files
Apr 3, 2024
d0c29cd
fix: adding old blueprint names
Apr 4, 2024
3e55ee0
fix: adding syntax error fix
Apr 4, 2024
7dfeca6
fix: adding reworked examples with blueprint
Apr 10, 2024
97bb8cd
fix: adding test cases using pytest parameterize
Apr 11, 2024
edfeb42
fix: attempting to rework some logic
Apr 11, 2024
b0fe215
fix: removing blueprint tests
Apr 11, 2024
4edf6c2
fix: applying black and isort
Apr 11, 2024
cbfbffa
fix: updating tests with specific position
Apr 11, 2024
d9ae994
Merge branch 'main' of github.com:BerkeleyLearnVerify/Scenic into aba…
Apr 11, 2024
cefecc4
fix: editing pyproject.toml to separate carla related tests
Apr 11, 2024
18521b0
fix: add env variable for CARLA path and blueprint tests with manual …
Apr 12, 2024
643ef35
fix: correcting environment references
Apr 12, 2024
9cd90f7
fix: reformatting file with black
Apr 12, 2024
a9f7ccf
fix: adding generic CARLA version to install
Apr 12, 2024
68d52d0
fix: CARLA not supported with python 3.11
Apr 12, 2024
cf2f3e6
feat: adding flaky
Apr 13, 2024
095e4c2
fix: moving flaky to .[test]
Apr 15, 2024
be5acdb
fix: adding linux and windows check for CARLA pip installation
Apr 25, 2024
c574c2b
fix: adding suggested fixes, testing fixes soon
May 7, 2024
ce0a8ac
fix: adding fixes
May 7, 2024
c670fbf
fix: resolving merge conflicts and autoformatting
May 7, 2024
628b105
fix: attempting to ignore all tests dependent on carla package in tes…
May 7, 2024
d110462
fix: adding launchCarlaServer to getCarlaSimulator
May 8, 2024
79f3394
fix: adding reformatting
May 8, 2024
bf96da7
fix: dividing dynamic tests into test_actions.py file
May 8, 2024
92c9c1c
fix: reverting test/conftest.py to original settings
May 8, 2024
7e1031d
fix: blueprints getCarlaSimulator reference
May 8, 2024
05e6ff0
fix: reworking CARLA simulator code
abanuelo Jun 28, 2024
291cf09
fix: removing unnecessary pytest skips in blueprints file
abanuelo Jun 28, 2024
47c79f9
fix: further paramterizing blueprint tests and adding package scope f…
abanuelo Jul 10, 2024
0ae01d3
test: making changes to throttle and brake tests, will check if working
abanuelo Jul 10, 2024
f6ab847
fix: addressing remaining revisions
abanuelo Jul 11, 2024
c1f6351
fix: addressing more round of comments
abanuelo Jul 11, 2024
c21239b
fix: addressing more comments
abanuelo Jul 11, 2024
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ test-full = [ # like 'test' but adds dependencies for optional features
"scenic[test]", # all dependencies from 'test' extra above
"scenic[guideways]", # for running guideways modules
"astor >= 0.8.1",
'carla >= 0.9.12; python_version <= "3.8" and (platform_system == "Linux" or platform_system == "Windows")',
'carla >= 0.9.12; python_version <= "3.10" and (platform_system == "Linux" or platform_system == "Windows")',
"dill",
"exceptiongroup",
"inflect ~= 5.5",
Expand Down
2 changes: 1 addition & 1 deletion src/scenic/simulators/carla/model.scenic
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ class Chair(Prop):


class BusStop(Prop):
blueprint: Uniform(*blueprints.busStopsModels)
blueprint: Uniform(*blueprints.busStopModels)


class Advertisement(Prop):
Expand Down
3 changes: 1 addition & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import os.path
from pathlib import Path
import re
import subprocess
import sys

import pytest
Expand Down Expand Up @@ -41,7 +40,7 @@ def manager():
return manager


@pytest.fixture
@pytest.fixture(scope="session")
def getAssetPath():
base = Path(__file__).parent.parent / "assets"

Expand Down
120 changes: 120 additions & 0 deletions tests/simulators/carla/test_actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import os
from pathlib import Path
import signal
import socket
import subprocess
import time

import pytest

try:
import carla

from scenic.simulators.carla import CarlaSimulator

Check warning on line 13 in tests/simulators/carla/test_actions.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_actions.py#L13

Added line #L13 was not covered by tests
except ModuleNotFoundError:
pytest.skip("carla package not installed", allow_module_level=True)

from tests.utils import compileScenic, sampleScene

Check warning on line 17 in tests/simulators/carla/test_actions.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_actions.py#L17

Added line #L17 was not covered by tests


def checkCarlaPath():
CARLA_ROOT = os.environ.get("CARLA_ROOT")
if not CARLA_ROOT:
pytest.skip("CARLA_ROOT env variable not set.")
return CARLA_ROOT

Check warning on line 24 in tests/simulators/carla/test_actions.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_actions.py#L20-L24

Added lines #L20 - L24 were not covered by tests


def isCarlaServerRunning(host="localhost", port=2000):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.settimeout(1)
try:
sock.connect((host, port))
return True
except (socket.timeout, ConnectionRefusedError):
return False

Check warning on line 34 in tests/simulators/carla/test_actions.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_actions.py#L27-L34

Added lines #L27 - L34 were not covered by tests


@pytest.fixture(scope="package")
def getCarlaSimulator(getAssetPath):
carla_process = None
if not isCarlaServerRunning():
CARLA_ROOT = checkCarlaPath()
carla_process = subprocess.Popen(

Check warning on line 42 in tests/simulators/carla/test_actions.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_actions.py#L37-L42

Added lines #L37 - L42 were not covered by tests
f"bash {CARLA_ROOT}/CarlaUE4.sh -RenderOffScreen", shell=True
)

for _ in range(30):
if isCarlaServerRunning():
break
time.sleep(1)

Check warning on line 49 in tests/simulators/carla/test_actions.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_actions.py#L46-L49

Added lines #L46 - L49 were not covered by tests

# Extra 5 seconds to ensure server startup
time.sleep(5)

Check warning on line 52 in tests/simulators/carla/test_actions.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_actions.py#L52

Added line #L52 was not covered by tests

base = getAssetPath("maps/CARLA")

Check warning on line 54 in tests/simulators/carla/test_actions.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_actions.py#L54

Added line #L54 was not covered by tests

def _getCarlaSimulator(town):
path = os.path.join(base, f"{town}.xodr")
simulator = CarlaSimulator(map_path=path, carla_map=town)
return simulator, town, path

Check warning on line 59 in tests/simulators/carla/test_actions.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_actions.py#L56-L59

Added lines #L56 - L59 were not covered by tests

yield _getCarlaSimulator

Check warning on line 61 in tests/simulators/carla/test_actions.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_actions.py#L61

Added line #L61 was not covered by tests

if carla_process:
subprocess.run("killall -9 CarlaUE4-Linux-Shipping", shell=True)

Check warning on line 64 in tests/simulators/carla/test_actions.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_actions.py#L63-L64

Added lines #L63 - L64 were not covered by tests


def test_throttle(getCarlaSimulator):
simulator, town, mapPath = getCarlaSimulator("Town01")
code = f"""

Check warning on line 69 in tests/simulators/carla/test_actions.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_actions.py#L67-L69

Added lines #L67 - L69 were not covered by tests
param map = r'{mapPath}'
param carla_map = '{town}'
param time_step = 1.0/10

model scenic.simulators.carla.model

behavior DriveWithThrottle():
while True:
take SetThrottleAction(1)

ego = new Car at (369, -326), with behavior DriveWithThrottle
record ego.speed as CarSpeed
terminate after 5 steps
"""
scenario = compileScenic(code, mode2D=True)
scene = sampleScene(scenario)
simulation = simulator.simulate(scene)
records = simulation.result.records["CarSpeed"]
assert records[len(records) // 2][1] < records[-1][1]

Check warning on line 88 in tests/simulators/carla/test_actions.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_actions.py#L84-L88

Added lines #L84 - L88 were not covered by tests


def test_brake(getCarlaSimulator):
simulator, town, mapPath = getCarlaSimulator("Town01")
code = f"""

Check warning on line 93 in tests/simulators/carla/test_actions.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_actions.py#L91-L93

Added lines #L91 - L93 were not covered by tests
param map = r'{mapPath}'
param carla_map = '{town}'
param time_step = 1.0/10

model scenic.simulators.carla.model

behavior DriveWithThrottle():
while True:
take SetThrottleAction(1)

behavior Brake():
while True:
take SetThrottleAction(0), SetBrakeAction(1)

behavior DriveThenBrake():
do DriveWithThrottle() for 2 steps
do Brake() for 4 steps

ego = new Car at (369, -326), with behavior DriveThenBrake
record final ego.speed as CarSpeed
terminate after 6 steps
"""
scenario = compileScenic(code, mode2D=True)
scene = sampleScene(scenario)
simulation = simulator.simulate(scene)
finalSpeed = simulation.result.records["CarSpeed"]
assert finalSpeed == pytest.approx(0.0, abs=1e-1)

Check warning on line 120 in tests/simulators/carla/test_actions.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_actions.py#L116-L120

Added lines #L116 - L120 were not covered by tests
98 changes: 98 additions & 0 deletions tests/simulators/carla/test_blueprints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import pytest

try:
import carla
except ModuleNotFoundError:
pytest.skip("carla package not installed", allow_module_level=True)

from test_actions import getCarlaSimulator

Check warning on line 8 in tests/simulators/carla/test_blueprints.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_blueprints.py#L8

Added line #L8 was not covered by tests

from scenic.simulators.carla.blueprints import (

Check warning on line 10 in tests/simulators/carla/test_blueprints.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_blueprints.py#L10

Added line #L10 was not covered by tests
advertisementModels,
atmModels,
barrelModels,
barrierModels,
benchModels,
bicycleModels,
boxModels,
busStopModels,
carModels,
caseModels,
chairModels,
coneModels,
containerModels,
creasedboxModels,
debrisModels,
garbageModels,
gnomeModels,
ironplateModels,
kioskModels,
mailboxModels,
motorcycleModels,
plantpotModels,
tableModels,
trafficwarningModels,
trashModels,
truckModels,
vendingMachineModels,
walkerModels,
)
from tests.utils import compileScenic, sampleScene

Check warning on line 40 in tests/simulators/carla/test_blueprints.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_blueprints.py#L40

Added line #L40 was not covered by tests


def model_blueprint(simulator, mapPath, town, modelType, modelName):
code = f"""

Check warning on line 44 in tests/simulators/carla/test_blueprints.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_blueprints.py#L43-L44

Added lines #L43 - L44 were not covered by tests
param map = r'{mapPath}'
param carla_map = '{town}'
param time_step = 1.0/10

model scenic.simulators.carla.model
ego = new {modelType} with blueprint '{modelName}'
terminate after 1 steps
"""
scenario = compileScenic(code, mode2D=True)
scene = sampleScene(scenario)
simulation = simulator.simulate(scene)
obj = simulation.objects[0]
assert obj.blueprint == modelName

Check warning on line 57 in tests/simulators/carla/test_blueprints.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_blueprints.py#L53-L57

Added lines #L53 - L57 were not covered by tests


model_data = {

Check warning on line 60 in tests/simulators/carla/test_blueprints.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_blueprints.py#L60

Added line #L60 was not covered by tests
"Car": carModels,
"Bicycle": bicycleModels,
"Motorcycle": motorcycleModels,
"Truck": truckModels,
"Trash": trashModels,
"Cone": coneModels,
"Debris": debrisModels,
"VendingMachine": vendingMachineModels,
"Chair": chairModels,
"BusStop": busStopModels,
"Advertisement": advertisementModels,
"Garbage": garbageModels,
"Container": containerModels,
"Table": tableModels,
"Barrier": barrierModels,
"PlantPot": plantpotModels,
"Mailbox": mailboxModels,
"Gnome": gnomeModels,
"CreasedBox": creasedboxModels,
"Case": caseModels,
"Box": boxModels,
"Bench": benchModels,
"Barrel": barrelModels,
"ATM": atmModels,
"Kiosk": kioskModels,
"IronPlate": ironplateModels,
"TrafficWarning": trafficwarningModels,
"Pedestrian": walkerModels,
}


@pytest.mark.parametrize(

Check warning on line 92 in tests/simulators/carla/test_blueprints.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_blueprints.py#L92

Added line #L92 was not covered by tests
"modelType, modelName",
[(type, name) for type, names in model_data.items() for name in names],
)
def test_model_blueprints(getCarlaSimulator, modelType, modelName):
simulator, town, mapPath = getCarlaSimulator("Town01")
model_blueprint(simulator, mapPath, town, modelType, modelName)

Check warning on line 98 in tests/simulators/carla/test_blueprints.py

View check run for this annotation

Codecov / codecov/patch

tests/simulators/carla/test_blueprints.py#L96-L98

Added lines #L96 - L98 were not covered by tests
Loading