Skip to content

Commit f1955fe

Browse files
committed
test cases run successfully on keploy
Signed-off-by: Pranav5255 <pranavmetil@gmail.com>
1 parent 18a7e31 commit f1955fe

21 files changed

Lines changed: 2744 additions & 23 deletions

crud-app-mongodb/README.md

Lines changed: 178 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ Always use `sudo -E env "PATH=$PATH"` with Keploy commands to preserve environme
184184

185185
# CRUD app using MongoDB
186186

187-
This repository contains a sample .NET 7 Web API that uses MongoDB for CRUD operations and integrates with [Keploy](https://keploy.io) to auto-generate and run test cases by recording real API traffic.
187+
This repository contains a sample .NET 8 Web API that uses MongoDB for CRUD operations and integrates with [Keploy](https://keploy.io) to auto-generate and run test cases by recording real API traffic.
188188

189189
---
190190

@@ -200,57 +200,212 @@ This repository contains a sample .NET 7 Web API that uses MongoDB for CRUD oper
200200

201201
### 1. Clone the Repository
202202

203-
``` bash
203+
```bash
204204
git clone https://github.com/Pranav5255/samples-mongodb.git
205205
cd samples-mongodb
206206
```
207207

208-
### 2. Configure MongoDB
209-
Update `appsettings.json` with your MongoDB connection string:
208+
### 2. Install Prerequisites
209+
210+
#### Install .NET 8 SDK and Runtime
211+
212+
```bash
213+
# Download and install Microsoft repository configuration
214+
wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
215+
sudo dpkg -i packages-microsoft-prod.deb
216+
rm packages-microsoft-prod.deb
217+
218+
# Update package lists
219+
sudo apt-get update
220+
221+
# Install .NET 8 Runtime and SDK
222+
sudo apt-get install -y dotnet-runtime-8.0 aspnetcore-runtime-8.0 dotnet-sdk-8.0
223+
224+
# Verify installation
225+
dotnet --version
226+
```
227+
228+
#### Install MongoDB
229+
230+
```bash
231+
# Import MongoDB public GPG key
232+
wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -
233+
234+
# Create list file for MongoDB
235+
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
236+
237+
# Update package database
238+
sudo apt-get update
239+
240+
# Install MongoDB
241+
sudo apt-get install -y mongodb-org
210242

211-
``` json
243+
# Start MongoDB
244+
sudo systemctl start mongod
245+
246+
# Enable MongoDB to start on boot
247+
sudo systemctl enable mongod
248+
249+
# Verify MongoDB is running
250+
sudo systemctl status mongod
251+
```
252+
253+
### 3. Configure MongoDB
254+
255+
Update `appsettings.json` with your MongoDB connection string:
256+
257+
```json
212258
{
213-
"ConnectionString": "mongodb://localhost:27017",
214-
"DatabaseName": "UserDb",
215-
"UsersCollectionName": "Users"
259+
"MongoDBSettings": {
260+
"ConnectionString": "mongodb://localhost:27017",
261+
"DatabaseName": "UserDb",
262+
"UsersCollectionName": "Users"
263+
}
216264
}
217265
```
218-
### 3. Run the .NET App
219-
``` bash
266+
267+
### 4. Run the .NET App
268+
269+
```bash
220270
dotnet run
221271
```
222272

223273
App runs by default at:
224-
``` arduino
274+
275+
```
225276
http://localhost:5067
226277
```
227278

228279
You can test endpoints like:
229-
``` http
280+
281+
```http
230282
GET /users
231283
POST /users
232284
GET /users/{id}
285+
PUT /users/{id}
286+
DELETE /users/{id}
287+
```
288+
289+
#### Test with cURL
290+
291+
```bash
292+
# Create a user
293+
curl -X POST "http://localhost:5067/users" \
294+
-H "Content-Type: application/json" \
295+
-d '{"name":"Pranav","age":24}'
296+
297+
# Get all users
298+
curl http://localhost:5067/users
233299
```
234300

301+
---
302+
235303
## 🐾 Generate Test Cases with Keploy
236-
### 1. Download Keploy
237-
``` powershell
238-
curl -O -L https://keploy.io/install.sh && source install.sh
304+
305+
### 1. Install Keploy
306+
307+
```bash
308+
# Download Keploy binary
309+
curl -L https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz -o keploy.tar.gz
310+
311+
# Extract it
312+
tar -xzf keploy.tar.gz
313+
314+
# Move to /usr/local/bin
315+
sudo mv keploy /usr/local/bin/
316+
317+
# Make it executable
318+
sudo chmod +x /usr/local/bin/keploy
319+
320+
# Clean up
321+
rm keploy.tar.gz
322+
323+
# Verify installation
324+
keploy version
239325
```
240326

241327
### 2. Record API Calls
242-
``` bash
243-
keploy.exe record --proxy-port 8080 --app-port 5067 --path ./keploy-tests
244-
```
245328

246-
Now send traffic to:
247-
``` bash
248-
http://localhost:8080/users
329+
**Important:** Keploy requires elevated permissions to use eBPF for intercepting network calls. You must run it with `sudo`:
330+
331+
```bash
332+
sudo -E env "PATH=$PATH" keploy record -c "dotnet run" --proxy-port 8080 --path ./keploy-tests
249333
```
250-
Use Postman, Swagger or cURL to generate traffic.
334+
335+
The application will start and Keploy will begin recording. Keep this terminal running.
251336

252337
### 3. Replay Tests
253338
After Recording:
254339
``` bash
255340
keploy.exe test --path ./keploy-tests
256-
```
341+
```
342+
343+
### 3. Generate Traffic
344+
345+
Open a **new terminal** and send traffic to your application:
346+
347+
```bash
348+
# Create a user
349+
curl -X POST "http://localhost:5067/users" \
350+
-H "Content-Type: application/json" \
351+
-d '{"name":"John","age":30}'
352+
353+
# Get all users
354+
curl http://localhost:5067/users
355+
356+
# Get user by ID (replace {id} with actual ID from response)
357+
curl http://localhost:5067/users/{id}
358+
```
359+
360+
Keploy will automatically capture these requests and responses as test cases in the `./keploy-tests` directory.
361+
362+
### 4. Stop Recording
363+
364+
Press `Ctrl+C` in the terminal where Keploy is running to stop recording.
365+
366+
### 5. Replay Tests
367+
368+
After recording, replay the captured test cases:
369+
370+
```bash
371+
sudo -E env "PATH=$PATH" keploy test -c "dotnet run" --path ./keploy-tests --delay 10
372+
```
373+
374+
The `--delay 10` flag gives the .NET application 10 seconds to start before running tests.
375+
376+
---
377+
378+
## 📝 Notes
379+
380+
- **Sudo Required**: Keploy uses eBPF (extended Berkeley Packet Filter) to intercept network traffic, which requires root privileges. Always run `keploy` commands with `sudo -E env "PATH=$PATH"` to preserve your environment variables.
381+
382+
- **Port Already in Use**: If you get an "address already in use" error, kill any existing dotnet processes:
383+
```bash
384+
pkill -9 dotnet
385+
```
386+
387+
- **MongoDB Connection**: Ensure MongoDB is running before starting the application:
388+
```bash
389+
sudo systemctl status mongod
390+
```
391+
392+
---
393+
394+
## 🔗 Resources
395+
396+
- [Keploy Documentation](https://keploy.io/docs)
397+
- [.NET 8 Documentation](https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8)
398+
- [MongoDB .NET Driver](https://www.mongodb.com/docs/drivers/csharp/)
399+
400+
---
401+
402+
## 🤝 Contributing
403+
404+
Contributions are welcome! Please feel free to submit a Pull Request.
405+
406+
---
407+
408+
## 📄 License
409+
410+
This project is licensed under the MIT License.
411+
212 KB
Loading
181 KB
Loading
220 KB
Loading
201 KB
Loading
162 KB
Loading
159 KB
Loading
172 KB
Loading
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
/reports/

0 commit comments

Comments
 (0)