Sample Library App (Flask + Redis)
Introduction
🪄 Dive into the world of Student CRUD Apps and see how seamlessly Keploy integrated with Flask and Redis. Buckle up, it's gonna be a fun ride! 🎢
🛠️ Platform-Specific Requirements for Keploy
Below is a table summarizing the tools needed for both native and Docker installations of Keploy on MacOS, Windows, and Linux:
Operating System | Without Docker | Docker Installation | Prerequisites |
---|---|---|---|
MacOS | - Docker Desktop version must be 4.25.2 or above - For running Keploy on MacOS natively, refer to Guide | ||
Windows | - Use WSL wsl --install - Windows 10 version 2004 and higher (Build 19041 and higher) or Windows 11 | ||
Linux | Linux kernel 5.15 or higher |
On MacOS and Windows, additional tools are required for Keploy due to the lack of native eBPF support.
Keploy Installation
Quick Installation Using CLI
Let's get started by setting up the Keploy alias with this command:
curl --silent -O -L https://keploy.io/install.sh && source install.sh
You should see something like this:
▓██▓▄
▓▓▓▓██▓█▓▄
████████▓▒
▀▓▓███▄ ▄▄ ▄ ▌
▄▌▌▓▓████▄ ██ ▓█▀ ▄▌▀▄ ▓▓▌▄ ▓█ ▄▌▓▓▌▄ ▌▌ ▓
▓█████████▌▓▓ ██▓█▄ ▓█▄▓▓ ▐█▌ ██ ▓█ █▌ ██ █▌ █▓
▓▓▓▓▀▀▀▀▓▓▓▓▓▓▌ ██ █▓ ▓▌▄▄ ▐█▓▄▓█▀ █▓█ ▀█▄▄█▀ █▓█
▓▌ ▐█▌ █▌
▓
Keploy CLI
Available Commands:
example Example to record and test via keploy
config --generate generate the keploy configuration file
record record the keploy testcases from the API calls
test run the recorded testcases and execute assertions
update Update Keploy
Flags:
--debug Run in debug mode
-h, --help help for keploy
-v, --version version for keploy
Use "keploy [command] --help" for more information about a command.
🎉 Wohoo! You are all set to use Keploy.
Other Installation Methods
Install using Docker
Downloading and running Keploy in Docker
On macOS
Note : Keploy is not supported natively on MacOS, so you can follow the below method to run with docker
-
Open up a terminal window.
-
Create a bridge network in Docker using the following docker network create command:
docker network create keploy-network
- Run the following command to start the Keploy container:
alias keploy="docker run --name keploy-v2 -p 16789:16789 --network keploy-network --privileged --pid=host -v $(pwd):$(pwd) -w $(pwd) -v /sys/fs/cgroup:/sys/fs/cgroup -v /sys/kernel/debug:/sys/kernel/debug -v /sys/fs/bpf:/sys/fs/bpf -v /var/run/docker.sock:/var/run/docker.sock --rm ghcr.io/keploy/keploy"
Downloading and running Keploy in Native
Downloading and running Keploy in Native
Prequisites:
- Linux Kernel version 5.15 or higher
- Run
uname -a
to verify the system architecture. - In case of Windows, use WSL with Ubuntu 20.04 LTS or higher.
Downloading and running Keploy On WSL/Linux AMD
On WSL/Linux AMD
- Open the terminal Session.
- Run the following command to download and install Keploy:
curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz" | tar xz --overwrite -C /tmp
sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin/keploy
On WSL/Linux ARM
- Open the terminal Session
- Run the following command to download and install Keploy:
curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_arm64.tar.gz" | tar xz --overwrite -C /tmp
sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin/keploy
Note: Keploy is not supported on MacOS natively.
Setting up the Docker Desktop for WSL 2
- Install Docker Desktop for Windows from here.
When developing on Windows with Docker Desktop and WSL 2, it's crucial to configure Docker Desktop to allow WSL 2 distributions to access the Docker daemon. This setup enables seamless integration between your Windows environment, WSL 2 Linux distros, and Docker.
By default, Docker Desktop may not be configured to work with all WSL 2 distros out of the box. Proper configuration ensures that you can run Docker commands from within your WSL 2 environment, allowing for a more native Linux development experience while leveraging the power of Windows.
This setup is essential for Keploy to function correctly in a WSL 2 environment, as it needs to interact with the Docker daemon to manage containers and networks effectively. For detailed instructions on how to configure
Docker Desktop
for WSL 2, please refer to the official Docker documentation.
With Arkade
With Arkade
- Installing Arkade
# Note: you can also run without `sudo` and move the binary yourself
curl -sLS https://get.arkade.dev | sudo sh
arkade --help
ark --help # a handy alias
# Windows users with Git Bash
curl -sLS https://get.arkade.dev | sh
- Install Keploy
arkade get keploy
Or you can also download specific version of Keploy using the following command:
arkade get keploy@2.2.0-alpha23
Get Started! 🎬
Clone the application 🧪
git clone https://github.com/keploy/samples-python.git && cd samples-python/flask-redis
Installation Keploy
Depending on your OS, choose your adventure:
We are going to run the application the following way
Using Docker Compose 🐳
We will be using Docker compose to run the application as well as Mongo on Docker container.
Lights, Camera, Record! 🎥
Capture the test-cases-
keploy record -c "docker compose up" --container-name "flask-web" --buildDelay 50
🔥Make some API calls. Postman, Hoppscotch or even curl - take your pick!
Let's make URLs short and sweet:
Generate testcases
To generate testcases we just need to make some API calls.
- Make a POST request:
curl -X POST http://localhost:5000/books/ \
-H "Content-Type: application/json" \
-d '{"title": "1984", "author": "George Orwell"}'
- Make a GET request:
curl -X GET "http://localhost:5000/books/?page=1&limit=10"
- Make a PUT request:
curl -X PUT http://localhost:5000/books/1 \
-H "Content-Type: application/json" \
-d '{"title": "1984 - Updated", "author": "George Orwell"}'
- Make a DELETE request:
curl -X DELETE http://localhost:5000/books/1
And once you are done, you can stop the recording and give yourself a pat on the back! With that simple spell, you've conjured up a test case with a mock! Explore the keploy directory and you'll discover your handiwork in tests
directory and mocks.yml
.
version: api.keploy.io/v1beta1
kind: Redis
name: mock-0
spec:
metadata:
type: config
redisrequests:
- origin: client
message:
- type: string
data: "*4\r\n$6\r\nCLIENT\r\n$7\r\nSETINFO\r\n$8\r\nLIB-NAME\r\n$8\r\nredis-py\r\n"
redisresponses:
- origin: server
message:
- type: string
data: "+OK\r\n"
reqtimestampmock: 2024-08-02T22:15:10.6084523Z
restimestampmock: 2024-08-02T22:15:10.608930466Z
---
Time to run the testcases
keploy test -c 'sudo docker compose up' --containerName "flask-web" --delay 10
You can also check the test summary from your cli
<=========================================>
COMPLETE TESTRUN SUMMARY.
Total tests: 11
Total test passed: 10
Total test failed: 1
Total time taken: "15.13 s"
Test Suite Name Total Test Passed Failed Time Taken
"test-set-0" 6 6 0 "5.06 s"
"test-set-1" 1 1 0 "5.02 s"
"test-set-2" 4 3 1 "5.04 s"
<=========================================>
Some errors you may run into ?
- While running the application you might have some ports up and running that you are trying to access again. This would throw a EBPF error
You can check the ports from the below command and
sudo lsof -p
If you want to check which process is using a specific port (e.g., port 5000), use:
sudo lsof -i :5000
Once you have identified the PID of the process using the port you need, you can terminate the process with the kill command:
sudo kill -9 <PID>
- ERROR: for redis 'ContainerConfig'
The KeyError: 'ContainerConfig' issue you're encountering with Docker Compose is indicative of a problem with the Docker Compose file or its version compatibility.
Here’s how you can address and troubleshoot this error:
sudo apt-get update
sudo apt-get install docker-compose
Check your compose file's permissions
ls -l docker-compose.yml
Re-run the record or test command from above