Add Toxiproxy

This commit is contained in:
agatha 2023-09-08 15:50:26 -04:00
parent be35b049c1
commit df080f2f77
5 changed files with 139 additions and 10 deletions

View File

@ -2,25 +2,26 @@
This is a mock API playground used for developing robust API wrappers and This is a mock API playground used for developing robust API wrappers and
other HTTP-related code. other HTTP-related code.
The API is created and served using [Mockoon](https://mockoon.com). Latency,
timeouts, and outages are introduced at random with
[Toxiproxy](https://github.com/Shopify/toxiproxy).
## Requirements ## Requirements
Requires either [Mockoon Desktop](https://mockcoon.com/download) or - Docker Compose
[Mockoon CLI](https://hub.docker.com/r/mockoon/cli).
## Features ## Features
- JSON responses - JSON responses
- Randomized HTTP responses - Randomized HTTP responses
- Randomized timeouts, latency, and outages
## Usage ## Usage
Import the environment file [mockoon-env.json](mockoon-env.json) into Mockcoon Desktop and click "Start Server".
For a more simple deployment, use Docker Compose:
```shell ```shell
docker compose up docker compose up
``` ```
Requests will be served at http://localhost:3000 unless another port is specified Requests with random faults will be served at http://localhost:18080. The API
in the Mockcoon environment settings or the [docker-compose.yml](docker-compose.yml) file. is accessible without Toxiproxy at http://localhost:3000, but will stil throw
some bad response codes at random.
## Routes ## Routes

View File

@ -5,5 +5,17 @@ services:
ports: ports:
- 3000:3000 - 3000:3000
volumes: volumes:
- ./mockoon-env.json:/data:ro - ./mockoon/mockoon-env.json:/data:ro
command: ["--data", "data"] command: ["--data", "data"]
toxiproxy:
image: ghcr.io/shopify/toxiproxy:2.6.0
ports:
- 8474:8474
- 18080:18080
depends_on:
- mockoon
toxifier:
build:
context: ./toxiproxy
depends_on:
- toxiproxy

View File

@ -4,7 +4,7 @@
"name": "API Playground", "name": "API Playground",
"endpointPrefix": "", "endpointPrefix": "",
"latency": 0, "latency": 0,
"port": 3002, "port": 3000,
"hostname": "", "hostname": "",
"folders": [ "folders": [
{ {

10
toxiproxy/Dockerfile Normal file
View File

@ -0,0 +1,10 @@
FROM alpine:latest
RUN apk update
RUN apk upgrade
RUN apk add curl bash
WORKDIR /tmp
COPY toxify.sh /tmp/toxify.sh
CMD ["/bin/bash", "toxify.sh"]

106
toxiproxy/toxify.sh Normal file
View File

@ -0,0 +1,106 @@
#!/bin/bash
SERVICE_NAME=mockapi_dev
TIMEOUT_ENABLED=0
LATENCY_ENABLED=0
OUTAGE_ENABLED=0
# Toxic functions
enable_timeout() {
if (( TIMEOUT_ENABLED == 0 )); then
echo "enabling timeouts..."
curl -s -X POST http://toxiproxy:8474/proxies/$SERVICE_NAME/toxics -d'{
"type": "timeout",
"name": "timeouts",
"attributes": {"timeout": 7000}
}' > /dev/null
TIMEOUT_ENABLED=1
fi
}
disable_timeout() {
if (( TIMEOUT_ENABLED == 1 )); then
echo "disabling timeouts..."
curl -s -X DELETE http://toxiproxy:8474/proxies/$SERVICE_NAME/toxics/timeouts > /dev/null
TIMEOUT_ENABLED=0
fi
}
enable_latency() {
if (( LATENCY_ENABLED == 0 )); then
echo "enabling latency..."
curl -s -X POST http://toxiproxy:8474/proxies/$SERVICE_NAME/toxics -d'{
"type": "latency",
"name": "lag",
"attributes": {"latency": 3000, "jitter": 2000}
}' > /dev/null
LATENCY_ENABLED=1
fi
}
disable_latency() {
if (( LATENCY_ENABLED == 1 )); then
echo "disabling latency..."
curl -s -X DELETE http://toxiproxy:8474/proxies/$SERVICE_NAME/toxics/lag > /dev/null
LATENCY_ENABLED=0
fi
}
enable_outage() {
if (( OUTAGE_ENABLED == 0 )); then
echo "enabling outage..."
curl -s -X POST http://toxiproxy:8474/proxies/$SERVICE_NAME -d'{
"enabled": false
}' > /dev/null
OUTAGE_ENABLED=1
fi
}
disable_outage() {
if (( OUTAGE_ENABLED == 1 )); then
echo "disabling outage..."
curl -s -X POST http://toxiproxy:8474/proxies/$SERVICE_NAME -d'{
"enabled": true
}' > /dev/null
OUTAGE_ENABLED=0
fi
}
# Sleep to allow toxiproxy to start listening
sleep 5
# Add upstream
curl -s -X POST -d "{\"name\": \"$SERVICE_NAME\", \"listen\": \"toxiproxy:18080\", \"upstream\": \"mockoon:3000\"}" http://toxiproxy:8474/proxies > /dev/null
# infinite loop
while true
do
# Disable all toxics from previous iteration
disable_timeout
disable_latency
disable_outage
# Randomly choose a new toxic or as per your request, do nothing (50% chance)
case $(( RANDOM % 6 )) in
0)
enable_timeout
;;
1)
enable_latency
;;
2)
enable_outage
;;
3) # Do nothing
;;
4) # Do nothing
;;
5) # Do nothing
;;
esac
# Sleep for 30 seconds before the next iteration.
sleep 30
done