|
| 1 | +# Sample CRUD application |
| 2 | + |
| 3 | +This is a sample C# application that demonstrates the usage of Keploy with .NET and MongoDB. |
| 4 | + |
| 5 | +## Get Started with Keploy |
| 6 | + |
| 7 | +1. **Install Keploy** |
| 8 | +```bash |
| 9 | +curl -L https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz -o keploy.tar.gz |
| 10 | +tar -xzf keploy.tar.gz |
| 11 | +sudo mv keploy /usr/local/bin/ |
| 12 | +sudo chmod +x /usr/local/bin/keploy |
| 13 | +rm keploy.tar.gz |
| 14 | + |
| 15 | +# Verify installation |
| 16 | +keploy version |
| 17 | +``` |
| 18 | + |
| 19 | +2. **Setup application** |
| 20 | +```bash |
| 21 | +git clone https://github.com/keploy/samples-csharp.git |
| 22 | +cd samples-csharp/crud-app-mongodb |
| 23 | +``` |
| 24 | + |
| 25 | +## Prerequisites |
| 26 | + |
| 27 | +### Install .NET 8 SDK and Runtime |
| 28 | + |
| 29 | +```bash |
| 30 | +# Download and install Microsoft repository configuration |
| 31 | +wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb |
| 32 | +sudo dpkg -i packages-microsoft-prod.deb |
| 33 | +rm packages-microsoft-prod.deb |
| 34 | + |
| 35 | +# Update package lists |
| 36 | +sudo apt-get update |
| 37 | + |
| 38 | +# Install .NET 8 Runtime and SDK |
| 39 | +sudo apt-get install -y dotnet-runtime-8.0 aspnetcore-runtime-8.0 dotnet-sdk-8.0 |
| 40 | + |
| 41 | +# Verify installation |
| 42 | +dotnet --version |
| 43 | +``` |
| 44 | + |
| 45 | +### Install and Start MongoDB |
| 46 | + |
| 47 | +```bash |
| 48 | +# Import MongoDB public GPG key |
| 49 | +wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add - |
| 50 | + |
| 51 | +# Create list file for MongoDB |
| 52 | +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 |
| 53 | + |
| 54 | +# Update package database |
| 55 | +sudo apt-get update |
| 56 | + |
| 57 | +# Install MongoDB |
| 58 | +sudo apt-get install -y mongodb-org |
| 59 | + |
| 60 | +# Start MongoDB |
| 61 | +sudo systemctl start mongod |
| 62 | + |
| 63 | +# Enable MongoDB to start on boot |
| 64 | +sudo systemctl enable mongod |
| 65 | + |
| 66 | +# Verify MongoDB is running |
| 67 | +sudo systemctl status mongod |
| 68 | +``` |
| 69 | + |
| 70 | +### Configuration |
| 71 | + |
| 72 | +Update `appsettings.json` with your MongoDB connection string (default configuration works with local MongoDB): |
| 73 | + |
| 74 | +```json |
| 75 | +{ |
| 76 | + "MongoDBSettings": { |
| 77 | + "ConnectionString": "mongodb://localhost:27017", |
| 78 | + "DatabaseName": "UserDb", |
| 79 | + "UsersCollectionName": "Users" |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +## Running in Record Mode |
| 85 | + |
| 86 | +```bash |
| 87 | +sudo -E env "PATH=$PATH" keploy record -c "dotnet run" --proxy-port 8080 --path ./keploy-tests |
| 88 | +``` |
| 89 | + |
| 90 | +**Note:** Keploy requires elevated permissions (sudo) to use eBPF for intercepting network calls. |
| 91 | + |
| 92 | +Now, since we have our application up and running, let's perform a few cURL requests (in a new terminal): |
| 93 | + |
| 94 | +### 1. POST Requests |
| 95 | + |
| 96 | +```bash |
| 97 | +curl -X POST -H "Content-Type: application/json" -d '{"name":"Sarthak Shnygle","age":23}' http://localhost:5067/api/users |
| 98 | + |
| 99 | +curl -X POST -H "Content-Type: application/json" -d '{"name":"Gourav Kumar","age":22}' http://localhost:5067/api/users |
| 100 | +``` |
| 101 | + |
| 102 | +### 2. GET Request |
| 103 | + |
| 104 | +```bash |
| 105 | +curl http://localhost:5067/api/users |
| 106 | +``` |
| 107 | + |
| 108 | +### 3. GET Request by ID |
| 109 | + |
| 110 | +```bash |
| 111 | +# Replace {id} with actual ID from POST response |
| 112 | +curl http://localhost:5067/api/users/{id} |
| 113 | +``` |
| 114 | + |
| 115 | +### 4. DELETE Request |
| 116 | + |
| 117 | +```bash |
| 118 | +# Replace {id} with actual ID from POST response |
| 119 | +curl -X DELETE http://localhost:5067/api/users/{id} |
| 120 | +``` |
| 121 | + |
| 122 | +And voila, we have our test cases generated in the `./keploy-tests` directory! |
| 123 | + |
| 124 | + |
| 125 | +### Stop Recording |
| 126 | + |
| 127 | +Press `Ctrl+C` in the terminal where Keploy is running to stop recording. |
| 128 | + |
| 129 | +## Run the Test Cases |
| 130 | + |
| 131 | +Now let's run Keploy in test mode: |
| 132 | + |
| 133 | +```bash |
| 134 | +sudo -E env "PATH=$PATH" keploy test -c "dotnet run" --path ./keploy-tests --delay 10 |
| 135 | +``` |
| 136 | + |
| 137 | +The `--delay 10` flag gives the .NET application 10 seconds to start before running tests. |
| 138 | + |
| 139 | +Keploy will replay all recorded test cases and generate a test report in the `keploy-tests/reports` directory. |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +## Troubleshooting |
| 146 | + |
| 147 | +### Port Already in Use |
| 148 | + |
| 149 | +If you get an "address already in use" error: |
| 150 | + |
| 151 | +```bash |
| 152 | +# Kill existing dotnet processes |
| 153 | +pkill -9 dotnet |
| 154 | +``` |
| 155 | + |
| 156 | +### MongoDB Not Running |
| 157 | + |
| 158 | +Ensure MongoDB is running: |
| 159 | + |
| 160 | +```bash |
| 161 | +sudo systemctl status mongod |
| 162 | + |
| 163 | +# If not running, start it |
| 164 | +sudo systemctl start mongod |
| 165 | +``` |
| 166 | + |
| 167 | +### Permission Denied |
| 168 | + |
| 169 | +Always use `sudo -E env "PATH=$PATH"` with Keploy commands to preserve environment variables and provide necessary permissions for eBPF. |
| 170 | + |
| 171 | +## API Endpoints |
| 172 | + |
| 173 | +- `GET /api/users` - Get all users |
| 174 | +- `POST /api/users` - Create a new user |
| 175 | +- `GET /api/users/{id}` - Get user by ID |
| 176 | +- `PUT /api/users/{id}` - Update user by ID |
| 177 | +- `DELETE /api/users/{id}` - Delete user by ID |
| 178 | + |
| 179 | +## Resources |
| 180 | + |
| 181 | +- [Keploy Documentation](https://keploy.io/docs) |
| 182 | +- [.NET 8 Documentation](https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8) |
| 183 | +- [MongoDB .NET Driver](https://www.mongodb.com/docs/drivers/csharp/) |
0 commit comments