Skip to content

Commit

Permalink
Merge pull request #22 from chip-csg/csg/feature/add-sample-client
Browse files Browse the repository at this point in the history
Add the test_client sample program
  • Loading branch information
wahajsyed authored May 28, 2021
2 parents c446da4 + 7e154c2 commit 2ac9e00
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/controller/python/test_client/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**/__pycache__
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
README.md
78 changes: 78 additions & 0 deletions src/controller/python/test_client/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# getting base image for ubuntu
FROM ubuntu:20.10

# To override
ENV TZ=America/Los_Angeles
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Executed during Image build
USER root
RUN apt-get update

# Install Dependencies for setting up CHIP environment
RUN apt-get install -y apt-utils \
git \
gcc \
g++ \
python \
pkg-config \
libssl-dev \
libdbus-1-dev \
libglib2.0-dev \
libavahi-client-dev \
libgirepository1.0-dev \
ninja-build \
python3-venv \
python3-dev \
python3-pip \
unzip

# Additional Dependencies for Chip device controller
RUN apt-get install -y autotools-dev \
automake \
libusb-dev \
libudev-dev \
libical-dev \
libreadline-dev \
libdbus-glib-1-dev \
libcairo2-dev \
libtool \
bluez \
bluetooth \
python3-gi \
python-is-python3 \
python3 \
pi-bluetooth \
avahi-daemon \
libavahi-client-dev \
build-essential \
protobuf-compiler \
wpasupplicant \
wireless-tools \
rfkill \
libcairo2-dev \
python3-widgetsnbextension \
python3-testresources \
isc-dhcp-client \
avahi-utils \
iproute2 \
iputils-ping

ADD requirements.txt .
RUN pip3 install -r requirements.txt --no-binary :all

# Wheel needs to be in the same dir as the dockerfile
ADD chip-0.0-cp37-abi3-linux_aarch64.whl .
RUN pip3 install chip-0.0-cp37-abi3-linux_aarch64.whl
WORKDIR /chip_tool

# Switching to a non-root user, please refer to https://aka.ms/vscode-docker-python-user-rights
RUN useradd appuser && chown -R appuser /chip_tool
USER appuser

# Set PYTHONPATH
ENV PYTHONPATH="/usr/bin/python3"

# Executed when you start/launch a container
# CMD bash
CMD ["chip-device-ctrl"]
21 changes: 21 additions & 0 deletions src/controller/python/test_client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Python client using xmlrpc and docker
Sample client program using XML-RPC to do RPC across multiple containers running chip stack.


**Setup:**
1. Install the pre-requisites mentioned [here](https://github.com/chip-csg/connectedhomeip/blob/master/docs/BUILDING.md) for linux and RaPi like so:

`sudo apt-get install git gcc g++ python pkg-config libssl-dev libdbus-1-dev \
libglib2.0-dev libavahi-client-dev ninja-build python3-venv python3-dev \
python3-pip unzip libgirepository1.0-dev libcairo2-dev`

`sudo apt-get install pi-bluetooth`
2. **Reboot** your RaPi after installing the pre-requisites.
3. Build the connectedhomeip repo or the certification fork repo by running the following scripts in the scripts directory:
To just build the container:`./scripts/build_container.sh`
To build both the sdk and the container: `./source scripts/build_sdk_container.sh`
4. Edit the rpc_client.py file and add your rpc calls, i/o validation.
5. Run the python script using `python3 rpc_client.py`

**Note:**
- The script may emit warnings if the previous run did not cleanup the container properly.
Binary file not shown.
4 changes: 4 additions & 0 deletions src/controller/python/test_client/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# To ensure app dependencies are ported from your virtual environment/host machine into your container, run 'pip freeze > requirements.txt' in the terminal to overwrite this file
pycairo==1.20.0
six==1.14.0
PyGObject==3.36.0
101 changes: 101 additions & 0 deletions src/controller/python/test_client/rpc_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import xmlrpc.client
import docker
import time
from docker.models.containers import Container
import asyncio
from asyncio import TimeoutError, wait_for
from typing import Optional

client = docker.from_env()

def destroy_container(container: Container) -> None:
container.kill()
container.remove()

async def container_running(container: Container) -> None:
sleep_interval = 0.2

while True:
# Check if the container is running, then sleep for 0.1 sec
if client.containers.get(container.id).status == "running":
return

# Sleep first to give container some time
await asyncio.sleep(sleep_interval)

async def container_ready(container: Container) -> None:
# Wait for the container for start running
try:
await wait_for(
container_running(container), 5 #seconds
)
except TimeoutError as e:
print("Container timed out")
destroy_container(container)
raise e

#Create docker containers
def run_new_container(docker_image_tag: str, port: int) -> Container:
# Create containers
try:
# Note: These options are different from test harness, because the network topology is different
# Test Harness has dedicated network on which we can find containers by hostname
# This container is launched from the host directly and is bridged, hence we can easily use ports to connect
mount_volumes = {"/var/run/dbus" : {'bind': '/var/run/dbus', 'mode': 'rw'}}
return client.containers.run(
docker_image_tag, detach=True, ports={5000:port}, volumes=mount_volumes, privileged=True
)
except Exception as error:
print(
"Error ocurred while creating a container from image " + str(error)
)

def get_shortened_id(self, container: Container) -> str:
# Docker containers are addressable by first 12 chars of the container id
# https://github.com/docker/docker-py/issues/2660
return container.id[:12]

async def create_container(docker_image_tag: str, port: int) -> Container:
container = run_new_container(docker_image_tag=docker_image_tag, port=port)
if container is None:
print("Unable to run container: " + docker_image_tag)
await container_ready(container)
print("Container running for " + docker_image_tag)

return container

async def main():
try:
container_1 = await create_container(docker_image_tag="chip-test", port=5050)
except AttributeError as e:
for container in client.containers.list():
for tag in container.image.tags:
if tag == "chip-test:latest":
print("WARNING:Container already running, killing the container, please try running the script again")
destroy_container(container)
exit()

print("List of containers: " + str(client.containers.list()))

# Wait for the controller to start the rpc server
time.sleep(1)

# Create an RPC server
server_1 = xmlrpc.client.ServerProxy("http://localhost:5050/")


# Invoke RPCs
try:
print("Calling RPCs")
print("echo_alive Response:" + server_1.echo_alive("Test"))
except:
destroy_container(container_1)

# Cleanup
destroy_container(container_1)


if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export CHIP_SDK_ROOT=../../../..
export TEST_CLIENT_ROOT=./
source $CHIP_SDK_ROOT/scripts/activate.sh
4 changes: 4 additions & 0 deletions src/controller/python/test_client/scripts/build_container.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#! /usr/bin/env bash

# Build docker image
docker build -t chip-test ./
14 changes: 14 additions & 0 deletions src/controller/python/test_client/scripts/build_sdk_container.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#! /usr/bin/env bash

# Skip creating environment if it already exists
if [[ -z "${CHIP_SDK_ROOT}" ]]; then
source ./scripts/activate_environment.sh
fi

# Build CHIP SDK
gn gen $CHIP_SDK_ROOT/out/debug
ninja -C $CHIP_SDK_ROOT/out/debug
cp $CHIP_SDK_ROOT/out/debug/controller/python/*.whl $TEST_CLIENT_ROOT/

# Build Docker image
$TEST_CLIENT_ROOT/scripts/build_container.sh

0 comments on commit 2ac9e00

Please sign in to comment.