Skip to content

Commit

Permalink
refactor: add code coverage steps in echo & mux sql sample application (
Browse files Browse the repository at this point in the history
#104)

* Create keploy.yml

* Update keploy.yml

* Update keploy.yml

* Update test-1.yaml

* Update mocks.yaml

* refactor: add code coverage

Signed-off-by: Animesh Pathak <[email protected]>

* refactor: add code coverage

Signed-off-by: Animesh Pathak <[email protected]>

* Delete .github/workflows/keploy.yml

* feat: added graceful shutdown for go coverage in gin-redis

Signed-off-by: Pranshu Srivastava <[email protected]>

---------

Signed-off-by: Animesh Pathak <[email protected]>
Signed-off-by: Pranshu Srivastava <[email protected]>
Co-authored-by: Pranshu Srivastava <[email protected]>
  • Loading branch information
Sonichigo and PranshuSrivastava authored Jun 14, 2024
1 parent 3fae48e commit 1f31169
Show file tree
Hide file tree
Showing 11 changed files with 535 additions and 461 deletions.
5 changes: 3 additions & 2 deletions echo-sql/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
postgres-data/
keploy
echo-psql-url-shortener
log.txt
app
coverage-reports
109 changes: 8 additions & 101 deletions echo-sql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,19 @@ go mod download

## Installation Keploy

Keploy can be installed on Linux directly and on Windows with the help of WSL. Based on your system archieture, install the keploy latest binary release
Install keploy via one-click:-

**1. AMD Architecture**

```shell
curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_amd64.tar.gz" | tar xz -C /tmp

sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin && keploy
```

<details>
<summary> 2. ARM Architecture </summary>

```shell
curl --silent --location "https://github.com/keploy/keploy/releases/latest/download/keploy_linux_arm64.tar.gz" | tar xz -C /tmp

sudo mkdir -p /usr/local/bin && sudo mv /tmp/keploy /usr/local/bin && keploy
```sh
curl --silent -O -L https://keploy.io/install.sh && source install.sh
```

</details>

### Start Postgres Instance

Using the docker-compose file we will start our postgres instance ( use `podman-compose` if you are using RHEL based distribution ):-

```bash
# Start Postgres
docker-compose up -d
docker-compose up -d postgres
```

If there is an error saying keploy-network could not be found. Use the following command to create the docker network
Expand All @@ -57,7 +42,7 @@ docker network create keploy-network
Now, we will create the binary of our application:-

```zsh
go build
go build -cover
```

Once we have our binary file ready,this command will start the recording of API calls using ebpf:-
Expand Down Expand Up @@ -111,7 +96,7 @@ Now, let's see the magic! ✨💫

## Run the Testcases

Now that we have our testcase captured, we will add `ts` to noise field in `test-*.yaml` files.
Now that we have our testcase captured, we will add `ts` to noise field in `test-*.yaml` files.

**1. On line 32 we will add "`body.ts: []`" under the "`header.Date: []`".**

Expand All @@ -121,7 +106,7 @@ Now that we have our testcase captured, we will add `ts` to noise field in `test
Now let's run the test mode (in the echo-sql directory, not the Keploy directory).

```shell
sudo -E keploy test -c "./echo-psql-url-shortener" --delay 10
sudo -E keploy test -c "./echo-psql-url-shortener" --delay 10 --goCoverage
```

output should look like
Expand All @@ -130,82 +115,4 @@ output should look like

So no need to setup fake database/apis like Postgres or write mocks for them. Keploy automatically mocks them and, **The application thinks it's talking to Postgres 😄**

# Using Docker

Keploy can be used on Linux, Windows and MacOS through Docker.

Note: To run Keploy on MacOS through [Docker](https://docs.docker.com/desktop/release-notes/#4252) the version must be ```4.25.2``` or above.

## Create Keploy Alias
To establish a network for your application using Keploy on Docker, follow these steps.

If you're using a docker-compose network, replace keploy-network with your app's `docker_compose_network_name` below.

```shell
alias keploy='sudo docker run --pull always --name keploy-v2 -p 16789:16789 --privileged --pid=host -it -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'
```
## Let's start the MongoDB Instance
Using the docker-compose file we will start our mongodb instance:-

```zsh
docker-compose up -d
```
> Since we are using docker to run the application, we need to update the `postgres` host on line 28 in `main.go`, update the host to `echo-sql-postgres-1`.
Now, we will create the docker image of our application:-

```zsh
docker build -t echo-app:1.0 .
```

## Capture the Testcases

```zsh
keploy record -c "docker run -p 8082:8082 --name echoSqlApp --network keploy-network echo-app:1.0"
```

![Testcase](./img/testcases.png?raw=true)

### Generate testcases
To genereate testcases we just need to make some API calls. You can use Postman, Hoppscotch, or simply curl

1. Generate shortned url

```zsh
curl --request POST \
--url http://localhost:8082/url \
--header 'content-type: application/json' \
--data '{
"url": "https://google.com"
}'
```
this will return the shortened url.
```json
{
"ts": 1645540022,
"url": "http://localhost:8082/Lhr4BWAi"
}
```
2. Redirect to original url from shòrtened url

```bash
curl --request GET \
--url http://localhost:8082/Lhr4BWAi
or by querying through the browser http://localhost:8082/Lhr4BWAi
```
Now, let's see the magic! 🪄💫

Now both these API calls were captured as a testcase and should be visible on the Keploy CLI. You should be seeing an app named keploy folder with the test cases we just captured and data mocks created.

## Run the captured testcases
Now that we have our testcase captured, run the test file.

```zsh
keploy test -c "sudo docker run -p 8082:8082 --net keploy-network --name echoSqlApp echo-app:1.0 echoSqlApp" --delay 10
```
So no need to setup dependencies like mongoDB, web-go locally or write mocks for your testing.

The application thinks it's talking to mongoDB 😄

We will get output something like this:
![Testrun](./img/testrun.png?raw=true)
![keploy coverage](./img/coverage.png?raw=true)
Binary file added echo-sql/img/coverage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 1f31169

Please sign in to comment.