From ad0d14cee5e601807564c1e34eda038bb0b86cc3 Mon Sep 17 00:00:00 2001 From: Gabriele Scivoletto Date: Tue, 11 Jun 2024 12:34:14 +0200 Subject: [PATCH] added possibility to run tests in Docker container, along with Readme --- docker/Dockerfile | 21 +++++++++ docker/Readme.md | 47 +++++++++++++++++++ docker/build_img.sh | 6 +++ docker/run_tests.sh | 31 +++++++++++++ libraries/Server.py | 107 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 212 insertions(+) create mode 100644 docker/Dockerfile create mode 100755 docker/Readme.md create mode 100755 docker/build_img.sh create mode 100755 docker/run_tests.sh create mode 100755 libraries/Server.py diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..905e813 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,21 @@ +##Docker file containing the environment for running robot3.1 based on python3.9 +## As example, MEC016 test suite are used +##How to run this container. + +## Build the image using this docker file, make sure the server side is up and running and eventually run the tests. + +FROM python:3.10-slim + +RUN addgroup robot && adduser -D -G robot -h /home/robot robot +WORKDIR /home/robot + +# Install system packages +RUN apt-get update && apt-get install -y \ + build-essential \ + libssl-dev \ + libffi-dev + +COPY requirements.txt . +RUN pip install -r requirements.txt + +COPY libraries libraries diff --git a/docker/Readme.md b/docker/Readme.md new file mode 100755 index 0000000..8b01320 --- /dev/null +++ b/docker/Readme.md @@ -0,0 +1,47 @@ +# Guide to Run Robot Tests in a Docker Container + +This guide provides instructions on how to build a Docker image and run Robot Framework tests in a Docker container. + +## Prerequisites + +- Docker must be installed and running on your system. + +## Steps + +### 1. Build the Docker Image + +To build the Docker image, navigate to the root directory of your project and run the following script: + +```sh +$ ./docker/build_img.sh +``` + +### 2. Run the Test + +To execute a Robot Framework test, use the following script and specify the path to the test file relative to the root directory: + +```sh +$ ./docker/run_test.sh +``` + +### Example + +Here is an example of how to run a specific test file: + +```sh +$ ./docker/run_test.sh MEC029/SRV/FAIS/PlatFixedAcessInfo.robot +``` + +## Notes + +- Ensure that the specified test file path is correct and accessible from the root directory. + +## Troubleshooting + +- If you encounter any issues with Docker, ensure that your Docker service is running and that you have the necessary permissions to execute Docker commands. +- Verify that the test file exists and that the path is correct. + +## Additional Resources + +- [Docker Documentation](https://docs.docker.com/) +- [Robot Framework Documentation](https://robotframework.org/) \ No newline at end of file diff --git a/docker/build_img.sh b/docker/build_img.sh new file mode 100755 index 0000000..a44aad6 --- /dev/null +++ b/docker/build_img.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +script_dir_path=$(dirname "$(realpath "$0")") +dockerfile_path="${script_dir_path}/Dockerfile" +docker build -f "${dockerfile_path}" "${script_dir_path}/.." -t ttf-robot-img:latest + diff --git a/docker/run_tests.sh b/docker/run_tests.sh new file mode 100755 index 0000000..f4b7a5b --- /dev/null +++ b/docker/run_tests.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# Function to display usage message +usage() { + echo "Usage: $0 testname" + exit 1 +} + +if [ "$#" -ne 1 ]; then + usage +fi + +testname=$1 + +if [ -f "$testname" ]; then + full_dir_path=$(dirname "$(realpath "$testname")") + echo "Running the test: $(realpath "$testname")" +else + echo "The file '$testname' does not exist." + exit 1 +fi + +script_dir_path=$(dirname "$(realpath "$0")") +root_path="${script_dir_path}/../" + +docker run --rm -it --name etsi-ttf-robot \ + -v "${root_path}":/home/robot \ + --workdir "/home/robot/" \ + -e TEST_FILE="${testname}" \ + ttf-robot-img:latest \ + python -m robot -X "${TEST_FILE}" diff --git a/libraries/Server.py b/libraries/Server.py new file mode 100755 index 0000000..f802415 --- /dev/null +++ b/libraries/Server.py @@ -0,0 +1,107 @@ +#!/usr/bin/python3 + +from http.server import BaseHTTPRequestHandler, HTTPServer +import json + +# Library version +__version__ = '0.0.1' + +class Server ( object ): + + ROBOT_LIBRARY_VERSION = '0.0.1' + + def spawn_web_server (self, host, port, timeout, method, endpoint, resp_body): + + class GET_Server(BaseHTTPRequestHandler): + + def __call__(self, *args, **kwargs): + """Handle a request.""" + super().__init__(*args, **kwargs) + + def __init__(self, endpoint, resp_body): + self.resp_body = resp_body + self.endpoint = endpoint + + def do_GET(self): + self.send_response(200) + self.send_header('Content-Type', 'application/json') + self.end_headers() + if self.path == self.endpoint: + self.wfile.write(json.dumps(self.resp_body).encode(encoding='utf_8')) + else: + self.wfile.write(json.dumps("wrong endpoint").encode(encoding='utf_8')) + + class POST_Server(BaseHTTPRequestHandler): + + def __call__(self, *args, **kwargs): + """Handle a request.""" + super().__init__(*args, **kwargs) + + def __init__(self, endpoint, resp_body): + self.resp_body = resp_body + self.endpoint = endpoint + + def do_POST(self): + self.send_response(200) + self.send_header('Content-Type', 'application/json') + self.end_headers() + if self.path == self.endpoint: + self.wfile.write(json.dumps(self.resp_body).encode(encoding='utf_8')) + else: + self.wfile.write(json.dumps("wrong endpoint").encode(encoding='utf_8')) + + class PUT_Server(BaseHTTPRequestHandler): + + def __call__(self, *args, **kwargs): + """Handle a request.""" + super().__init__(*args, **kwargs) + + def __init__(self, endpoint, resp_body): + self.resp_body = resp_body + self.endpoint = endpoint + + def do_PUT(self): + self.send_response(200) + self.send_header('Content-Type', 'application/json') + self.end_headers() + if self.path == self.endpoint: + self.wfile.write(json.dumps(self.resp_body).encode(encoding='utf_8')) + else: + self.wfile.write(json.dumps("wrong endpoint").encode(encoding='utf_8')) + + class DELETE_Server(BaseHTTPRequestHandler): + + def __call__(self, *args, **kwargs): + """Handle a request.""" + super().__init__(*args, **kwargs) + + def __init__(self, endpoint, resp_body): + self.resp_body = resp_body + self.endpoint = endpoint + + def do_DELETE(self): + self.send_response(200) + self.send_header('Content-Type', 'application/json') + self.end_headers() + if self.path == self.endpoint: + self.wfile.write(json.dumps(self.resp_body).encode(encoding='utf_8')) + else: + self.wfile.write(json.dumps("wrong endpoint").encode(encoding='utf_8')) + + if method == "GET": + self.handler = GET_Server(endpoint, resp_body) + elif method == "POST": + self.handler = POST_Server(endpoint, resp_body) + elif method == "PUT": + self.handler = PUT_Server(endpoint, resp_body) + elif method == "DELETE": + self.handler = DELETE_Server(endpoint, resp_body) + else: + print("Error, unknown endpoint") + exit(1) + + self.app = HTTPServer((host, int(port)), self.handler) + self.app.timeout = int(timeout) + + self.app.handle_request() + self.app.server_close() \ No newline at end of file -- GitLab