forked from keploy/keploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolang-docker.sh
executable file
·160 lines (131 loc) · 4.33 KB
/
golang-docker.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/bash
source ./../../.github/workflows/test_workflow_scripts/test-iid.sh
# Start mongo before starting keploy.
docker network create keploy-network
docker run --name mongoDb --rm --net keploy-network -p 27017:27017 -d mongo
# Generate the keploy-config file.
sudo -E env PATH=$PATH ./../../keployv2 config --generate
# Update the global noise to ts.
config_file="./keploy.yml"
sed -i 's/global: {}/global: {"body": {"ts":[]}}/' "$config_file"
# Remove any preexisting keploy tests and mocks.
sudo rm -rf keploy/
docker logs mongoDb &
# Start keploy in record mode.
docker build -t gin-mongo .
docker rm -f ginApp 2>/dev/null || true
container_kill() {
echo "Inside container_kill"
pid=$(pgrep -n keploy)
if [ -z "$pid" ]; then
echo "Keploy process not found. It might have already stopped."
return 0 # Process not found isn't a critical failure, so exit with success
fi
echo "$pid Keploy PID"
echo "Killing keploy"
sudo kill $pid
if [ $? -ne 0 ]; then
echo "Failed to kill keploy process, but continuing..."
return 0 # Avoid exiting with 1 in case kill fails
fi
echo "Keploy process killed"
sleep 2
sudo docker rm -f keploy-init
sleep 2
sudo docker rm -f keploy-v2
return 0
}
send_request(){
sleep 10
app_started=false
while [ "$app_started" = false ]; do
if curl -X GET http://localhost:8080/CJBKJd92; then
app_started=true
fi
sleep 3
done
echo "App started"
# Start making curl calls to record the testcases and mocks.
curl --request POST \
--url http://localhost:8080/url \
--header 'content-type: application/json' \
--data '{
"url": "https://google.com"
}'
curl --request POST \
--url http://localhost:8080/url \
--header 'content-type: application/json' \
--data '{
"url": "https://facebook.com"
}'
curl -X GET http://localhost:8080/CJBKJd92
# Wait for 5 seconds for keploy to record the tcs and mocks.
sleep 5
# sudo docker rm -f keploy-v2
# sleep 5
# sudo docker rm -f keploy-init
container_kill
# sleep 5
wait
}
for i in {1..2}; do
container_name="ginApp_${i}"
send_request &
sudo -E env PATH=$PATH ./../../keployv2 record -c "docker run -p8080:8080 --net keploy-network --rm --name $container_name gin-mongo" --container-name "$container_name" &> "${container_name}.txt"
if grep "WARNING: DATA RACE" "${container_name}.txt"; then
echo "Race condition detected in recording, stopping pipeline..."
cat "${container_name}.txt"
# exit 1
fi
if grep "ERROR" "${container_name}.txt"; then
echo "Error found in pipeline..."
cat "${container_name}.txt"
# exit 1
fi
sleep 5
echo "Recorded test case and mocks for iteration ${i}"
done
sleep 4
# container_kill
sudo docker rm -f keploy-v2
sudo docker rm -f keploy-init
echo "Starting the test phase..."
# Start the keploy in test mode.
test_container="ginApp_test"
sudo -E env PATH=$PATH ./../../keployv2 test -c 'docker run -p8080:8080 --net keploy-network --name ginApp_test gin-mongo' --containerName "$test_container" --apiTimeout 60 --delay 20 --generate-github-actions=false &> "${test_container}.txt"
# container_kill
# sudo docker rm -f keploy-v2
if grep "ERROR" "${test_container}.txt"; then
echo "Error found in pipeline..."
cat "${test_container}.txt"
# exit 1
fi
if grep "WARNING: DATA RACE" "${test_container}.txt"; then
echo "Race condition detected in test, stopping pipeline..."
cat "${test_container}.txt"
# exit 1
fi
all_passed=true
for i in {0..1}
do
# Define the report file for each test set
report_file="./keploy/reports/test-run-0/test-set-$i-report.yaml"
# Extract the test status
test_status=$(grep 'status:' "$report_file" | head -n 1 | awk '{print $2}')
# Print the status for debugging
echo "Test status for test-set-$i: $test_status"
# Check if any test set did not pass
if [ "$test_status" != "PASSED" ]; then
all_passed=false
echo "Test-set-$i did not pass."
break # Exit the loop early as all tests need to pass
fi
done
# Check the overall test status and exit accordingly
if [ "$all_passed" = true ]; then
echo "All tests passed"
exit 0
else
cat "${test_container}.txt"
exit 1
fi