Repository navigation
Expand file tree
/
Copy pathtest-e2e.sh
More file actions
executable file
·278 lines (228 loc) · 10.3 KB
/
Copy pathtest-e2e.sh
File metadata and controls
executable file
·278 lines (228 loc) · 10.3 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/bin/bash
set -e
set -o pipefail
# Run locally with:
#docker run --privileged --rm \
# -v "$(pwd):/workspace" \
# --name dind \
# -w /workspace \
# docker:28-dind \
# sh -c " \
# dockerd-entrypoint.sh --feature containerd-snapshotter=true > /tmp/dockerd.log 2>&1 & \
# sleep 2 && \
# apk --update add bash && \
# DOCKER_HOST=unix:///var/run/docker.sock ./test-e2e.sh \
# "
echo "=== E2E Test Script ==="
ROOT_CA_FILE="test-e2e-rootCA.pem"
PEERDB_CONTAINER="peerdb-container"
PEERDB_IMAGE="peerdb-image"
PLAYWRIGHT_IMAGE="peerdb-playwright-image"
NETWORK="peerdb-e2e-network"
LOGS_DIR="logs"
mkdir -p "$LOGS_DIR"
cleanup_peerdb_container=0
cleanup_elasticsearch_container=0
cleanup_postgres_container=0
cleanup_peerdb_image=0
cleanup_playwright_image=0
cleanup_network=0
cleanup_certs=0
cleanup() {
set +e
if [ "$cleanup_peerdb_container" -ne 0 ]; then
echo "Storing logs of PeerDB into $LOGS_DIR/peerdb.log"
docker logs "$PEERDB_CONTAINER" > "$LOGS_DIR/peerdb.log" 2>&1
echo "Stopping PeerDB Docker container (if still running)"
docker stop "$PEERDB_CONTAINER"
docker rm -f "$PEERDB_CONTAINER"
fi
if [ "$cleanup_elasticsearch_container" -ne 0 ]; then
echo "Storing logs of elasticsearch into $LOGS_DIR/elasticsearch.log"
docker logs peerdb-elastic > "$LOGS_DIR/elasticsearch.log" 2>&1
echo "Stopping elasticsearch Docker container (if still running)"
docker stop peerdb-elastic
docker rm -f peerdb-elastic
fi
if [ "$cleanup_postgres_container" -ne 0 ]; then
echo "Storing logs of postgres into $LOGS_DIR/postgres.log"
docker logs peerdb-postgres > "$LOGS_DIR/postgres.log" 2>&1
echo "Stopping postgres Docker container (if still running)"
docker stop peerdb-postgres
docker rm -f peerdb-postgres
fi
if [ "$cleanup_peerdb_image" -ne 0 ]; then
echo "Removing PeerDB Docker image"
docker image rm -f "$PEERDB_IMAGE"
fi
if [ "$cleanup_playwright_image" -ne 0 ]; then
echo "Removing playwright Docker image"
docker image rm -f "$PLAYWRIGHT_IMAGE"
fi
if [ "$cleanup_network" -ne 0 ]; then
echo "Removing Docker network"
docker network rm "$NETWORK"
fi
if [ "$cleanup_certs" -ne 0 ]; then
echo "Cleaning up temporary files"
rm "$ROOT_CA_FILE" config-e2e.yml
rm -rf testdata-e2e
fi
}
trap cleanup EXIT
# Create Docker network for E2E tests.
echo "Creating Docker network..."
docker network create "$NETWORK"
cleanup_network=1
echo "1. Installing dependencies and generating certificates..."
# Install required tools for certificate generation.
apk --update add openssl curl
# Install Go tools for certificate generation.
curl -L https://github.com/FiloSottile/mkcert/releases/download/v1.4.4/mkcert-v1.4.4-linux-amd64 -o /usr/local/bin/mkcert && chmod +x /usr/local/bin/mkcert
mkcert -install
# Generate certificates
mkcert peerdb-container 127.0.0.1 ::1
chmod 644 peerdb-container+2.pem peerdb-container+2-key.pem
# Copy mkcert CA certificate for Docker build.
cp "$(mkcert -CAROOT)/rootCA.pem" "$ROOT_CA_FILE"
cleanup_certs=1
# Use config.yml, replacing localhost domain string with $PEERDB_CONTAINER, to expose all features of PeerDB in e2e tests,
# putting the navbar into the document flow, and leaving the loading of further results and facets to the tests. A navbar
# which stays at the top of the viewport is painted wherever the page happens to be scrolled when a full page screenshot is
# taken, so such a screenshot would depend on what the test did before it. A search otherwise fills its columns to about two
# viewports and keeps loading while the page is scrolled, and how much that loads follows how tall the results have rendered
# by the time the page is measured, so a screenshot of it would depend on how quickly the site answered. A test which is
# about either of the two asks for the site's own behaviour on its own (overrideSiteFeatures).
sed -e "s/localhost/$PEERDB_CONTAINER/g" \
-e 's/navbarPosition: ""/navbarPosition: static/' \
-e 's/disableLoadingOnScroll: false/disableLoadingOnScroll: true/' config.yml > config-e2e.yml
echo "2. Building Docker images..."
# Build both PeerDB and Playwright images in parallel.
PEERDB_IMAGE="$PEERDB_IMAGE" PLAYWRIGHT_IMAGE="$PLAYWRIGHT_IMAGE" docker buildx bake -f test-docker-bake.hcl
cleanup_peerdb_image=1
cleanup_playwright_image=1
echo "3. Starting PostgreSQL container..."
docker run -d \
--name peerdb-postgres \
--network "$NETWORK" \
-e LOG_TO_STDOUT=1 \
-e PGSQL_ROLE_1_USERNAME=test \
-e PGSQL_ROLE_1_PASSWORD=test \
-e PGSQL_DB_1_NAME=test \
-e PGSQL_DB_1_OWNER=test \
registry.gitlab.com/tozd/docker/postgresql:18
cleanup_postgres_container=1
echo "4. Starting Elastic container..."
docker run -d \
--name peerdb-elastic \
--network "$NETWORK" \
-e network.bind_host=0.0.0.0 \
-e network.publish_host=localhost \
-e discovery.type=single-node \
-e "xpack.security.enabled=false" \
-e "ingest.geoip.downloader.enabled=false" \
-e "cluster.routing.allocation.disk.watermark.flood_stage=100%" \
"${CI_REGISTRY_IMAGE:-registry.gitlab.com/peerdb/peerdb}/elastic/${ELASTIC_VERSION:-7.17.9}:latest"
cleanup_elasticsearch_container=1
echo "5. Waiting for Elasticsearch service to be ready..."
for i in $(seq 1 120); do docker exec peerdb-elastic curl -sf "http://localhost:9200/_cluster/health?wait_for_status=yellow&timeout=10s" && break || { [ "$i" -eq 120 ] && exit 1; sleep 1; }; done
echo "6. Populating PeerDB with documents..."
# The subject of a user is made out of the domain of the site they signed into, so the users the permission claims of the test data name are
# the users of a site served under the domain the test data was written for. The config above renames that domain to the container the tests
# reach the site at, so the claims are renamed with it, in a copy of the test data which is what is populated below. The test data itself
# keeps naming the domain a local run serves the site under. The copy is made only now, so that it is not part of the build context of the
# images built above.
cp -a testdata testdata-e2e
find testdata-e2e -name '*.json' -exec sed -i "s/@localhost/@$PEERDB_CONTAINER/g" {} +
echo "postgres://test:test@peerdb-postgres:5432/test" > .postgresql.secret
mkdir -p coverage
# We chown to the container user so the process running inside Docker container can write to coverage.
chown 1000:1000 coverage
mkdir -p .storage
# We chown to the container user so the process running inside Docker container can write to.
chown 1000:1000 .storage
docker run --rm \
--network "$NETWORK" \
-v "$(pwd):/data" \
-e GOCOVERDIR=/data/coverage \
-e SSL_CERT_FILE=/data/"$ROOT_CA_FILE" \
-e SSL_CERT_DIR=/etc/ssl/certs \
"$PEERDB_IMAGE" \
-c /data/config-e2e.yml \
-d /data/.postgresql.secret \
--elastic.url=http://peerdb-elastic:9200 \
-S /data/.storage \
populate --test-data=/data/testdata-e2e > "$LOGS_DIR/populate.log" 2>&1 || { tail -n 50 "$LOGS_DIR/populate.log"; exit 1; }
echo "7. Reindexing PeerDB..."
docker run --rm \
--network "$NETWORK" \
-v "$(pwd):/data" \
-e GOCOVERDIR=/data/coverage \
-e SSL_CERT_FILE=/data/"$ROOT_CA_FILE" \
-e SSL_CERT_DIR=/etc/ssl/certs \
"$PEERDB_IMAGE" \
-c /data/config-e2e.yml \
-d /data/.postgresql.secret \
--elastic.url=http://peerdb-elastic:9200 \
-S /data/.storage \
db reindex > "$LOGS_DIR/reindex.log" 2>&1 || { tail -n 50 "$LOGS_DIR/reindex.log"; exit 1; }
echo "8. Merging away deleted documents in Elasticsearch..."
# Populating and reindexing rewrite documents, and every rewrite leaves the copy it replaced behind as a
# deleted document. A deleted document still counts towards the term statistics BM25 computes a score from,
# and how many of them are left over differs from one run to the next, which reorders the results of every
# search carrying a query and makes the screenshots of those searches differ for no reason of their own.
docker exec peerdb-elastic curl -sf -XPOST "http://localhost:9200/_forcemerge?max_num_segments=1" > /dev/null
echo "9. Starting PeerDB container..."
# Start PeerDB container with certificates.
docker run -d \
--name "$PEERDB_CONTAINER" \
--network "$NETWORK" \
-v "$(pwd):/data" \
-e GOCOVERDIR=/data/coverage \
-e SSL_CERT_FILE=/data/"$ROOT_CA_FILE" \
-e SSL_CERT_DIR=/etc/ssl/certs \
"$PEERDB_IMAGE" \
-c /data/config-e2e.yml \
-k /data/peerdb-container+2.pem \
-K /data/peerdb-container+2-key.pem \
-d /data/.postgresql.secret \
--elastic.url=http://peerdb-elastic:9200 \
-S /data/.storage
cleanup_peerdb_container=1
echo "10. Waiting for PeerDB service to be ready..."
for i in $(seq 1 120); do docker exec peerdb-elastic curl -sf -k "https://$PEERDB_CONTAINER:8080/" && break || { [ "$i" -eq 120 ] && exit 1; sleep 1; }; done
echo "11. Running Playwright tests..."
# Set environment variables for Playwright.
export LINK_PUBLISH_JOB_ID="${CI_JOB_ID}"
# The directories Playwright writes its reports and results into are mounted into the container below.
# We create them ourselves, because Docker would otherwise create the missing ones owned by root.
mkdir -p playwright-report test-results playwright-screenshots coverage-frontend a11y-report .nyc_output
# We chown to the container user so the process running inside Docker container can write to them.
chown 1000:1000 playwright-report test-results playwright-screenshots coverage-frontend a11y-report .nyc_output
# Run Playwright tests in separate container.
docker run --rm \
--name peerdb-playwright \
--network "$NETWORK" \
-v "$(pwd)/playwright-report:/src/peerdb/playwright-report" \
-v "$(pwd)/test-results:/src/peerdb/test-results" \
-v "$(pwd)/playwright-screenshots:/src/peerdb/playwright-screenshots" \
-v "$(pwd)/coverage-frontend:/src/peerdb/coverage-frontend" \
-v "$(pwd)/a11y-report:/src/peerdb/a11y-report" \
-v "$(pwd)/.nyc_output:/src/peerdb/.nyc_output" \
-e PEERDB_URL="https://$PEERDB_CONTAINER:8080" \
-e LINK_PUBLISH_JOB_ID \
-e UPDATE_SCREENSHOTS \
-e CI_COMMIT_SHA \
-e CI_COMMIT_REF_NAME \
-e CI_PIPELINE_ID \
-e CI_JOB_ID \
"$PLAYWRIGHT_IMAGE"
# Stop the PeerDB container and check its exit code.
echo "12. Stopping PeerDB container..."
docker stop "$PEERDB_CONTAINER"
PEERDB_EXIT_CODE=$(docker wait "$PEERDB_CONTAINER")
if [ "$PEERDB_EXIT_CODE" -ne 0 ]; then
echo "ERROR: PeerDB container exited with code $PEERDB_EXIT_CODE"
exit 1
fi
echo "=== E2E Tests Completed Successfully ==="