Skip to content

Commit

Permalink
Added Offline Mode E2E tests (#210)
Browse files Browse the repository at this point in the history
* Added Offline Mode E2E tests

* Added dependency
  • Loading branch information
danyi1212 authored Nov 6, 2024
1 parent 3dc4a05 commit fe01da5
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ WORKDIR /app
RUN addgroup -S permit -g 1001
RUN adduser -S -s /bin/bash -u 1000 -G permit -h /home/permit permit

# create backup directory
RUN mkdir -p /app/backup && chmod -R 777 /app/backup

# install linux libraries necessary to compile some python packages
RUN apk update && \
apk add --no-cache bash build-base libffi-dev libressl-dev musl-dev zlib-dev gcompat
Expand Down
3 changes: 3 additions & 0 deletions test_offline_mode/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PDP_API_KEY=<YOUR_API_KEY>
PDP_CONTROL_PLANE=https://permitio.api.stg.permit.io
PDP_ENABLE_OFFLINE_MODE=True
7 changes: 7 additions & 0 deletions test_offline_mode/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM python:alpine

RUN pip install --upgrade pip && pip install permit colorlog

COPY checker.py /checker.py

CMD ["python", "/checker.py"]
33 changes: 33 additions & 0 deletions test_offline_mode/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# E2E tests for PDP Offline Mode

### Create Permit Environment

Login to Permit and create a new environment with the following objects:

* Resource 'file' with action 'create'
* Role 'admin' with permission to create 'file'
* User 'user-1' with role 'admin'

Copy the `.env.example` file to `.env` and update the values with the environment details.

### Prepare repo for building PDP image

This would download the Custom OPA and FactDB source code.

```bash
VERSION=<my-local-version> make run-prepare
```
Replace `<my-local-version>` with the version you want to use for the PDP image

### Run the tests

```bash
docker compose up
```


### What does it do

1. Start an online PDP with `PDP_ENABLE_OFFLINE_MODE=True` and connect the `/app/backup` to a volume.
2. Start another offline PDP that is also connected to the same volume.
3. Run a tester that run `permit.check("user-1", "create", "file")` on the online PDP and the offline PDP.
80 changes: 80 additions & 0 deletions test_offline_mode/checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import asyncio
import logging.config
import os

import aiohttp
from permit import Permit, PermitConfig

logging.config.dictConfig(
{
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"color": {
"()": "colorlog.ColoredFormatter",
"format": "%(log_color)s[%(asctime)s.%(msecs)03d] %(levelname)s - "
"%(name)s:%(lineno)d | %(message)s",
"datefmt": "%H:%M:%S",
"log_colors": {
"DEBUG": "white",
"INFO": "green",
"WARNING": "yellow",
"ERROR": "red",
"CRITICAL": "red,bg_white",
},
},
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"formatter": "color",
},
},
"root": {
"handlers": ["console"],
"level": "INFO",
},
}
)
logger = logging.getLogger(__name__)


async def main():
pdp_url = os.environ["PDP_URL"]
logger.info("Starting PDP checker against: %s", pdp_url)
permit = Permit(
PermitConfig(
token=os.environ["PDP_API_KEY"],
api_url=os.environ["PDP_CONTROL_PLANE"],
pdp=pdp_url,
)
)
async with aiohttp.ClientSession(base_url=pdp_url) as client:
while True:
try:
resp = await client.get("/healthy")
if resp.status == 200:
logger.info("PDP is healthy")
break
else:
logger.warning("PDP is not healthy")
except Exception as e:
logger.exception(f"Error: {e}")

await asyncio.sleep(1)

while True:
try:
result = await permit.check("user-1", "create", "file")
if result:
logger.info("Passed")
else:
logger.warning("Failed")
except Exception as e:
logger.exception(f"Error: {e}")

await asyncio.sleep(1)


if __name__ == "__main__":
asyncio.run(main())
67 changes: 67 additions & 0 deletions test_offline_mode/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
volumes:
backup:

networks:
internet:
driver: bridge
no-internet:
driver: bridge
internal: true

services:
online-pdp:
container_name: online-pdp
build:
context: ..
dockerfile: Dockerfile
volumes:
- backup:/app/backup:rw
networks:
- internet
env_file: .env
healthcheck:
test: "wget --no-verbose --tries=1 --spider http://127.0.0.1:7000/healthy || exit 1"

offline-pdp:
container_name: offline-pdp
build:
context: ..
dockerfile: Dockerfile
volumes:
- backup:/app/backup:rw
networks:
- no-internet
env_file: .env
healthcheck:
test: "wget --no-verbose --tries=1 --spider http://127.0.0.1:7000/healthy || exit 1"
depends_on:
online-pdp:
condition: service_healthy

online-tester:
container_name: online-tester
build:
context: .
dockerfile: Dockerfile
networks:
- internet
env_file: .env
environment:
- PDP_URL=http://online-pdp:7000
depends_on:
online-pdp:
condition: service_healthy

offline-tester:
container_name: offline-tester
build:
context: .
dockerfile: Dockerfile
networks:
- no-internet
env_file: .env
environment:
- PDP_URL=http://offline-pdp:7000
depends_on:
offline-pdp:
condition: service_healthy

0 comments on commit fe01da5

Please sign in to comment.