Commit ad0d14ce authored by Gabriele Scivoletto's avatar Gabriele Scivoletto
Browse files

added possibility to run tests in Docker container, along with Readme

parent a218dcec
Loading
Loading
Loading
Loading
Loading

docker/Dockerfile

0 → 100644
+21 −0
Original line number Diff line number Diff line
##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

docker/Readme.md

0 → 100755
+47 −0
Original line number Diff line number Diff line
# 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 <robot_test_file_from_root>
```

### 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

docker/build_img.sh

0 → 100755
+6 −0
Original line number Diff line number Diff line
#!/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

docker/run_tests.sh

0 → 100755
+31 −0
Original line number Diff line number Diff line
#!/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}"

libraries/Server.py

0 → 100755
+107 −0
Original line number Diff line number Diff line
#!/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