Skip to content

Commit

Permalink
Update test scripts. Add docker support to automate the procedure.
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuwch committed May 16, 2017
1 parent a584002 commit 94103c3
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 38 deletions.
15 changes: 0 additions & 15 deletions test/README

This file was deleted.

60 changes: 60 additions & 0 deletions test/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
The test utilize docker to run test automatically. The test script will start a docker image containing unrealcv binary automatically.


1. Install dependencies first

:code:`pip install -r requirements.txt`

2. Test whether the docker image can be successfully run.

:code:`python docker_util.py`

The binary will be started by docker by default. It is also possible to download and run the binary directly.

3. Run all python test

Test for all supported python distributions

:code:`tox .`

Run the test for your python

:code:`pytest .`

:code:`pytest --no-docker` if you want to run the virtual environment by yourself, for example in windows.

Show more diagnositic information, :code:`pytest -s .`


Files
=====
- :code:`connection_test.py`
Whether the basic connection is successful
- :code:`benchmark_report.py`
Report the speed
- :code:`test_config.py`

- Utility code
- :code:`docker_util.py`


Included tests
==============
- basic connection


The development of UnrealCV is supported by a set of test to ensure the correctness. To run the test for each components, `cd` to this folder and run `py.test -x`, the `-x` parameter will stop when encounter the first error. Use `py.test -v` if you prefer a verbose output.

1. `client`

Test the correctness of UnrealCV client. The test is done with a dummy test server which mimics the function of unrealcv server.

2. `ipc`

Test the connection between the client and the plugin. Test the data exchange, throughput, framerate, etc. No unrealcv commands is involved. Make sure the plugin is stable by itself.

3. `command`

Test the correctness of unrealcv commands. If you implemented a new command, add a test to here. Organize by the hierarchical structure of the unrealcv commands.

Make sure files have been cleared before running the test.
14 changes: 14 additions & 0 deletions test/camera_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from unrealcv import client
from conftest import env
import cv2

def test_gt(env):
client.connect()
gt_types = ['lit', 'depth', 'normal', 'object_mask']
for v in gt_types:
res = client.request('vget /camera/0/{type} output/{type}.png'.format(type=v))
print(res)
im = cv2.imread(res)
print(im.shape)

# Make sure the dimension is right
28 changes: 28 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'''
Configuration file for pytest
'''
import pytest
import os, time
from docker_util import DockerRunner

def pytest_addoption(parser):
parser.addoption('--no-docker', action='store_true', help='Do not run docker fixture')


@pytest.fixture(scope='module')
def env(request):
'''
Return docker instance if use docker
Return an empty environment if --no-docker is set
'''
if request.config.getoption('--no-docker'):
yield None
else:
docker_cmd = '/home/unrealcv/LinuxNoEditor/RealisticRendering/Binaries/Linux/RealisticRendering'
volumes = [(os.path.abspath('output'), '/home/unrealcv/LinuxNoEditor/RealisticRendering/Binaries/Linux/output')]
# The path needs to be absolute
runner = DockerRunner('qiuwch/rr', volumes)
runner.start(docker_cmd)
time.sleep(2)
yield runner
runner.stop()
16 changes: 16 additions & 0 deletions test/connection_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from unrealcv import client
# from docker_util import docker_runner
import pytest
from conftest import env

def test_connection(env):
client.connect()
res = client.request('vget /unrealcv/status')
# assert res == 'ok'

def test_viewmode(env):
viewmodes = ['lit', 'depth', 'object_mask', 'normal']
for viewmode in viewmodes:
cmd = 'vset /viewmode {viewmode}'.format(viewmode = viewmode)
res = client.request(cmd)
assert res == 'ok'
80 changes: 80 additions & 0 deletions test/docker_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

'''
Author: Fangwei Zhong, Weichao Qiu
Utility scripts to start the docker image
'''
import os, re, time
import docker
import pytest

host_cmd = 'nvidia-docker run -d -it \
--env="DISPLAY=:0.0" \
--volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
-p 9000:9000 \
{volume_args} \
{image_name}'
host_cmd = re.sub('[ ]+', ' ', host_cmd)
docker_cmd = '/home/unrealcv/LinuxNoEditor/RealisticRendering/Binaries/Linux/RealisticRendering'


class DockerRunner():
def __init__(self, image_name, volumes = None):
'''
image_name: the docker image to run
volumes: volume mapping
'''
self.docker_client = docker.from_env()
self.check_image(image_name)

if volumes:
volume_args = []
for (k, v) in volumes:
volume_args.append('-v %s:%s' % (k, v))
volume_args = ' '.join(volume_args)
else:
volume_args = ''

self.host_cmd = host_cmd.format(image_name = image_name, volume_args = volume_args)

def start(self, docker_cmd = docker_cmd):
cmd = '%s %s' % (self.host_cmd, docker_cmd)
os.system(cmd)
time.sleep(2)
print('It is equivalent to run: %s' % cmd)

def get_ip(self):
containers = self.docker_client.containers.list()
return containers[0].attrs['NetworkSettings']['Networks']['bridge']['IPAddress']

def stop(self):
containers = self.docker_client.containers.list()
containers[0].remove(force = True)
time.sleep(2)

def check_image(self, image_name):
images = self.docker_client.images.list()

# Check the existence of image
found_img = False
for i in range(len(images)):
if images[i].tags.count(image_name) > 0:
found_img = True
# Download image

if found_img == False:
print 'Can not find images, downloading'
self.docker_client.images.pull(image_name)
else:
print 'Found images'


def test_docker_runner():
volumes = []
runner = DockerRunner('qiuwch/rr', volumes)
runner.start(docker_cmd)

volumes = [(os.path.abspath('output'), '/home/unrealcv/LinuxNoEditor/RealisticRendering/Binaries/Linux/output')]
runner = DockerRunner('qiuwch/rr', volumes)

if __name__ == '__main__':
test_docker_runner()
26 changes: 26 additions & 0 deletions test/object_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'''
Test object related functions
'''
from unrealcv import client
import cv2
from common import isok, iserror
from conftest import env

# def test_seg_mask(docker_runner):
# client.connect()
#
# client.request('vrun setres 640x480')
# f1 = client.request('vget /camera/0/lit')
# f2 = client.request('vget /camera/0/object_mask')
# assert (not iserror(f1)) and (not iserror(f2))
# im = cv2.imread(f1)
# seg = cv2.imread(f2)
# assert(im.shape[:2] == seg.shape[:2]) # Make sure the width and height the same.

def test_object_list(env):
client.connect()
obj_ids = client.request('vget /objects').split(' ')

for obj_id in obj_ids:
color = client.request('vget /object/%s/color' % obj_id)
print color
2 changes: 2 additions & 0 deletions test/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest
docker
3 changes: 3 additions & 0 deletions test/rr_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'''
Test to verify the RealisticRendering environment.
'''
23 changes: 0 additions & 23 deletions test/testcfg.py

This file was deleted.

0 comments on commit 94103c3

Please sign in to comment.