Skip to content

Commit

Permalink
set up GH actions for pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinran committed Dec 8, 2021
1 parent 228bbeb commit b411e28
Show file tree
Hide file tree
Showing 38 changed files with 580 additions and 458 deletions.
4 changes: 4 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[flake8]
ignore = E226,E302,E401,E501 # https://pycodestyle.pycqa.org/en/latest/intro.html#error-codes
max-line-length = 120
max-complexity = 10
22 changes: 22 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: test

on: [pull_request, push, workflow_dispatch]

concurrency:
group: test-${{github.ref}}
cancel-in-progress: true

jobs:
test:
runs-on: "ubuntu-latest"
# To push new image: docker build . -t merlinran/acorn_docker --platform linux/amd64 && docker push merlinran/acorn_docker
container: docker://merlinran/acorn_docker:latest
strategy:
fail-fast: true

steps:
- name: Checkout source
uses: actions/checkout@v2

- name: Run tests
run: pytest
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ RUN apk add tmux vim mercurial
RUN python3 -m pip install adafruit-circuitpython-mcp230xx coloredlogs
RUN apk add iw
RUN apk add raspberrypi; exit 0 # Only succeeds on raspberry pi but not needed otherwise.
RUN python3 -m pip install pytest
34 changes: 6 additions & 28 deletions docker-compose-simulation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,15 @@ version: "3.7"

services:
acorn_vehicle:
build: .
container_name: acorn_vehicle
image: acorn_docker:1.0
working_dir: /home/pi
extends:
file: docker-compose-vehicle.yml
service: acorn_vehicle
volumes:
- /dev:/dev
- .:/home/pi/
- ./.bashrc:/home/pi/.bashrc
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- /usr/local/bin:/usr/local/bin
privileged: true
command: sh -c /home/pi/vehicle/autolaunch_vehicle_sim.sh
restart: unless-stopped
network_mode: "host"
ipc: host
acorn_server:
build: .
container_name: acorn_server
image: acorn_docker:1.0
ports:
- 80:80
- 5570:5570
- 6379:6379
- 6799:6799
working_dir: /acorn/server
environment:
- PYTHONPATH=/acorn/vehicle
volumes:
- .:/acorn
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
privileged: true
command: sh -c /acorn/server/autolaunch_server.sh
extends:
file: docker-compose-server.yml
service: acorn_server
13 changes: 13 additions & 0 deletions docker-compose-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: "3.7"

services:
acorn_vehicle_test:
extends:
file: docker-compose-simulation.yml
service: acorn_vehicle
command: sleep infinity
# acorn_server:
# extends:
# file: docker-compose-simulation.yml
# service: acorn_server
# command: sleep infinity
20 changes: 20 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[tool.pytest.ini_options]
minversion = "6.0"
addopts = "-ra -q"
log_cli_level = "DEBUG"
testpaths = [
"vehicle/tests",
"server/tests",
]
filterwarnings = [
"ignore::DeprecationWarning:evdev.*:",
]

[tool.black]
line-length = 120
target-version = ['py36', 'py37', 'py38']
include = '\.pyi?$'

[tool.autopep8]
max-line-length = 120
ignore = "E501"
4 changes: 2 additions & 2 deletions run_docker_simulation.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
docker-compose -f docker-compose-simulation.yml down
docker-compose -f docker-compose-simulation.yml up --remove-orphans -d
docker-compose -f docker-compose-simulation.yml down --remove-orphans
docker-compose -f docker-compose-simulation.yml up -d
2 changes: 2 additions & 0 deletions run_docker_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
docker-compose -f docker-compose-test.yml up --remove-orphans -d
docker exec -it acorn_vehicle pytest --log-cli-level DEBUG
1 change: 1 addition & 0 deletions server/autolaunch_server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ tmux new-session -d -s "redis" sh redis-command.sh&
tmux new-session -d -s "zmq" python3 zmq_server_pirate.py&
tmux new-session -d -s "zmq_ppq" python3 zmq_ppqueue.py&
tmux new-session -d -s "web" python3 server.py&
sleep 5 # wait for Redis to up
tmux new-session -d -s "system_manager" python3 system_manager.py&
trap : TERM INT; (while true; do sleep 1000; done) & wait
4 changes: 2 additions & 2 deletions server/redis_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"""

import pickle
from master_process import RobotCommand
from model import RobotCommand


def get_energy_segment_key(robot_key):
Expand Down Expand Up @@ -99,7 +99,7 @@ def get_dense_path(redis_client=None, robot_key=None):
key = get_energy_segment_key(robot_key)
list_length = redis_client.llen(key)
path = []
for idx in range(list_length-1, 0, -1):
for idx in range(list_length - 1, 0, -1):
segment = pickle.loads(redis_client.lindex(key, idx))
# print(segment.subsampled_points)
try:
Expand Down
2 changes: 1 addition & 1 deletion server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import pickle
import datetime
import redis_utils
from master_process import RobotCommand
from model import RobotCommand
break
except Exception as e:
print(e)
Expand Down
2 changes: 1 addition & 1 deletion server/system_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import pickle
import redis
import redis_utils
from remote_control_process import CONTROL_ONLINE
from model import CONTROL_ONLINE
from zmq_server_pirate import REDIS_PORT
from server import active_site

Expand Down
Empty file added server/tests/__init__.py
Empty file.
10 changes: 5 additions & 5 deletions server/zmq_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ def launch_zmq_server():
pid = os.fork()
if pid > 0:
# Exit first parent
#os.waitid(os.P_PID, pid, os.WEXITED)
# os.waitid(os.P_PID, pid, os.WEXITED)
return
# sys.exit(0)
except OSError as e:
except OSError:
sys.exit(1)

# Decouple from parent environment
Expand All @@ -75,12 +75,12 @@ def launch_zmq_server():
pid = os.fork()
if pid > 0:
sys.exit(0)
except OSError as e:
except OSError:
sys.exit(1)

print(cwd)
proc = subprocess.Popen(ZMQ_CMD, cwd=cwd, stdin=None,
stdout=None, stderr=None, close_fds=True, shell=False)
_ = subprocess.Popen(ZMQ_CMD, cwd=cwd, stdin=None,
stdout=None, stderr=None, close_fds=True, shell=False)

while True:
time.sleep(0.001)
Expand Down
6 changes: 2 additions & 4 deletions server/zmq_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import redis
import psutil
import redis_utils
from master_process import RobotCommand
from model import RobotCommand
from master_process import _CMD_WRITE_KEY, _CMD_READ_KEY, _CMD_UPDATE_ROBOT, _CMD_ROBOT_COMMAND
from master_process import _CMD_ACK, _CMD_READ_KEY_REPLY, _CMD_READ_PATH_KEY

Expand Down Expand Up @@ -112,9 +112,7 @@ def run(self):
connection_active = False
worker = self.context.socket(zmq.DEALER)
worker.connect('inproc://backend')
r = redis.Redis(
host='localhost',
port=6379)
r = redis.Redis(host='localhost', port=6379)
tprint('Worker started')
while True:
if (connection_active and time.time() - self.last_active_time > _ALLOWED_ACTIVITY_LAPSE_SEC):
Expand Down
3 changes: 1 addition & 2 deletions server/zmq_server_pirate.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
import zmq_server

# keep the two imported to keep pickle working
# TODO: avoid this by moving the class defs to a separate module.
from master_process import Robot, RobotCommand
from model import Robot, RobotCommand

REDIS_PORT = 6379

Expand Down
2 changes: 1 addition & 1 deletion stop_docker_simulation.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
docker-compose -f docker-compose-simulation.yml down
docker-compose -f docker-compose-simulation.yml down --remove-orphans
4 changes: 2 additions & 2 deletions vehicle/autolaunch_vehicle_sim.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ echo $PWD
echo Sleep 5...
sleep 5
echo Start Sessions...
tmux new-session -d -s "main" sh /home/pi/vehicle/start_main_sim.sh&
tmux new-session -d -s "motors" sh /home/pi/vehicle/start_motors_sim.sh&
tmux new-session -d -s "main" sh /home/pi/vehicle/start_main.sh --sim &
tmux new-session -d -s "motors" sh /home/pi/vehicle/start_motors.sh --simulated_hardware &
echo Started. Begin infinite loop.
trap : TERM INT; (while true; do sleep 1000; done) & wait
20 changes: 10 additions & 10 deletions vehicle/gps_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ def find_closest_pt_on_line(pt1, pt2, pt3):
x1, y1 = pt1
x2, y2 = pt2
x3, y3 = pt3
dx, dy = x2-x1, y2-y1
det = dx*dx + dy*dy
a = (dy*(y3-y1)+dx*(x3-x1))/det
return GpsPoint(x1+a*dx, y1+a*dy)
dx, dy = x2 - x1, y2 - y1
det = dx * dx + dy * dy
a = (dy * (y3 - y1) + dx * (x3 - x1)) / det
return GpsPoint(x1 + a * dx, y1 + a * dy)


def get_heading(start_pt, second_pt):
Expand All @@ -120,7 +120,7 @@ def project_point(point, bearing, distance_meters):
# Convert to geopy point for calculation.
point = geopy.Point(point.lat, point.lon)

d = geopy.distance.VincentyDistance(kilometers=distance_meters/1000.0)
d = geopy.distance.geodesic(kilometers=distance_meters / 1000.0)

# Use the `destination` method with a bearing of bearing degrees
geo_point = d.destination(point=point, bearing=bearing)
Expand All @@ -139,7 +139,7 @@ def get_closest_points_at_distance(point_index, distance_meters, path):
while True:
if index_down > 0:
index_down -= 1
if index_up < len(path)-1:
if index_up < len(path) - 1:
index_up += 1
dist_up = get_distance(path[point_index], path[index_up])
this_err_up = math.fabs(distance_meters - dist_up)
Expand All @@ -151,7 +151,7 @@ def get_closest_points_at_distance(point_index, distance_meters, path):
if math.fabs(distance_meters - dist_down) < min_err_down:
best_index_down = index_down
min_err_down = this_err_down
if index_down == 0 and index_up == len(path)-1:
if index_down == 0 and index_up == len(path) - 1:
return path[best_index_up], path[best_index_down]


Expand All @@ -162,7 +162,7 @@ def get_approx_distance_point_from_line(point, line_pt1, line_pt2):
p3 = np.asarray((point.lat, point.lon))

# https://stackoverflow.com/questions/39840030/distance-between-point-and-a-line-from-two-points#
d1 = np.cross(p2-p1, p1-p3) / np.linalg.norm(p2-p1) * -1
d1 = np.cross(p2 - p1, p1 - p3) / np.linalg.norm(p2 - p1) * -1
return d1 * _GPS_DISTANCE_SCALAR


Expand Down Expand Up @@ -192,7 +192,7 @@ def offset_row(row, distance, direction, copy_data=False, make_dict=True):

def interpolate_points(points, num_points):
total_distance = get_distance(points[0], points[1])
increment_distance = total_distance/num_points
increment_distance = total_distance / num_points
heading = get_heading(points[0], points[1])
last_point = points[0]
interpolated_points = [points[0]]
Expand Down Expand Up @@ -235,7 +235,7 @@ def chain_rows(row_list, starting_point, starting_direction, connection_type, fo

if idx + 1 >= len(row_list):
return row_chain
next_row = row_list[idx+1]
next_row = row_list[idx + 1]
angle_reverse = starting_direction
if get_distance(row[-1], next_row[0]) > get_distance(row[-1], next_row[-1]):
next_row.reverse()
Expand Down
Loading

0 comments on commit b411e28

Please sign in to comment.