Skip to content

Commit a9e60a9

Browse files
authored
Merge pull request #146 from shiiyan/adapt-echo-mysql-to-docker
2 parents 831fd3d + f1ad70a commit a9e60a9

File tree

5 files changed

+91
-28
lines changed

5 files changed

+91
-28
lines changed

echo-mysql/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
MYSQL_USER=root
22
MYSQL_PASSWORD=password
3-
MYSQL_HOST=localhost
3+
MYSQL_HOST=mysql-container
44
MYSQL_PORT=3306
55
MYSQL_DBNAME=uss

echo-mysql/.env.local

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
MYSQL_USER=root
2+
MYSQL_PASSWORD=password
3+
MYSQL_HOST=localhost
4+
MYSQL_PORT=3306
5+
MYSQL_DBNAME=uss

echo-mysql/Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM golang:1.22-alpine AS build
2+
WORKDIR /src
3+
COPY . .
4+
RUN go build -o echo-mysql .
5+
6+
FROM alpine:3.17
7+
WORKDIR /app
8+
COPY .env .
9+
COPY --from=build /src/echo-mysql .
10+
EXPOSE 9090
11+
CMD ["./echo-mysql"]

echo-mysql/README.md

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ A simple golang based url shortner
55
# Requirments to run
66
1. Golang [How to install Golang](https://go.dev/doc/install)
77
2. Docker [How to install Docker?](https://docs.docker.com/engine/install/)
8+
3. Keploy [Quick Installation (API test generator)](https://github.com/keploy/keploy?tab=readme-ov-file#-quick-installation-api-test-generator)
89

910

1011
# Setting up the project
@@ -13,27 +14,12 @@ Run the following commands to clone the repository and download the necessary Go
1314

1415
``` bash
1516
git clone https://github.com/keploy/samples-go.git && cd samples-go/echo-mysql
16-
go mod download
17-
```
18-
19-
20-
# Running app
21-
22-
## Let's start the MySql Instance
23-
24-
``` bash
25-
sudo docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=uss -p 3306:3306 --rm mysql:latest
2617
```
27-
## Build the application
28-
29-
``` bash
30-
go build -o echo-mysql .
31-
```
3218

33-
# Capture the Testcases
19+
## Run the app and Capture the Testcases
3420

3521
``` bash
36-
sudo -E env PATH=$PATH oss record -c "./echo-mysql"
22+
keploy record -c "docker compose up" --container-name "echo-mysql-container" --buildDelay 60
3723
```
3824

3925
To generate testcases we just need to make some API calls. You can use Postman, Hoppscotch, or simply curl
@@ -51,36 +37,75 @@ To generate testcases we just need to make some API calls. You can use Postman,
5137
```
5238

5339

54-
3. Short URL:
40+
3. Shorten URL:
5541
```bash
5642
-> curl -X POST http://localhost:9090/shorten -H "Content-Type: application/json" -d '{"url": "https://github.com"}'
5743
```
5844

5945

60-
4. Resolve short code:
46+
4. Resolve shortened code:
6147

6248
```bash
6349
-> curl -X GET http://localhost:9090/resolve/4KepjkTT
6450
```
6551

6652
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.
6753

68-
![alt text](https://github.com/Hermione2408/samples-go/blob/app/echo-mysql/img/keploy_record.png?raw=true)
54+
![alt text](https://github.com/keploy/samples-go/blob/main/echo-mysql/img/keploy_record.png?raw=true)
6955

70-
# Run the captured testcases
56+
## Run the captured testcases
7157

7258
Now that we have our testcase captured, run the test file.
7359

7460
```bash
75-
sudo -E env PATH=$PATH oss test -c "./echo-mysql" --delay 20
61+
keploy test -c "docker compose up app" --delay 20
7662
```
7763

78-
So no need to setup dependencies like MySQL, web-go locally or write mocks for your testing.
64+
So no need to setup dependencies like MySQL, web-go locally or write mocks for your testing. `keploy test ` runs the test cases captured in the previous step. It replays the captured API calls against the application to verify its behavior.
7965

80-
oss test runs the test cases captured in the previous step. It replays the captured API calls against the application to verify its behavior.
81-
82-
The application thinks it's talking to MySQL 😄
66+
The application thinks it's talking to MySQL 😄.
8367

8468
We will get output something like this:
8569

86-
![alt text](https://github.com/Hermione2408/samples-go/blob/app/echo-mysql/img/keploy_test.png?raw=true)
70+
![alt text](https://github.com/keploy/samples-go/blob/main/echo-mysql/img/keploy_test.png?raw=true)
71+
72+
## Run the app without docker
73+
74+
If you’d prefer to build and run the application locally without Docker, follow these steps:
75+
76+
### Start the MySql Instance
77+
78+
``` bash
79+
sudo docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=uss -p 3306:3306 --rm mysql:latest
80+
```
81+
### Build the application
82+
83+
prepare the .env file.
84+
85+
```
86+
cp .env.local .env
87+
```
88+
89+
download go modules.
90+
91+
```
92+
go mod download
93+
```
94+
95+
build the app.
96+
97+
``` bash
98+
go build -o echo-mysql .
99+
```
100+
101+
### Capture the Testcases
102+
103+
``` bash
104+
sudo -E env PATH=$PATH keploy record -c "./echo-mysql"
105+
```
106+
107+
### Run the Testcases
108+
109+
```bash
110+
sudo -E env PATH=$PATH keploy test -c "./echo-mysql" --delay 20
111+
```

echo-mysql/docker-compose.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
services:
2+
db:
3+
image: mysql:latest
4+
container_name: mysql-container
5+
restart: unless-stopped
6+
environment:
7+
MYSQL_ROOT_PASSWORD: password
8+
MYSQL_DATABASE: uss
9+
ports:
10+
- "3306:3306"
11+
12+
app:
13+
build:
14+
context: .
15+
dockerfile: Dockerfile
16+
image: echo-mysql-app
17+
container_name: echo-mysql-container
18+
restart: on-failure
19+
ports:
20+
- "9090:9090"
21+
depends_on:
22+
- db

0 commit comments

Comments
 (0)