Skip to content

Commit a17535f

Browse files
dmoucaiosba
authored andcommitted
Consolidate dockerfiles for testing (#519)
* temp remove user cmd * run tests for this branch * move workdir * add a chown * add debug statements to setup-parallel * move useradd * test moving bins to opt/ * more debug msg * absolute paths * don't add volumes * fix docker-entrypoint.sh with DEPLOY_ENV set to test * print env * explicitly pass deploy env in ci * rm env * back to detached mode * return to local deploy env * use tmp dir elsewhere * delete migration creating default directories * set default log path to use /var/log * copy cookies.txt in ci-test-pr workflow * use new log paths config * different logpath syntax * change more paths * update paths * attempt to copy schema.rb to /opt/db * forcibly create /opt/db? * fix local filepath * fix mkdir * fix mkdir * actually do this in the script * check schema.rb in * explicit mkdir /opt/db * maybe just use /opt? * mkdir as root?? * add schema.rb * try new docker test * comment out path mods * add chmod on schema.rb * add debug msg * chmod a+w * ok the other file * only use docker-test.yml * go back to lograge logger * use deploy_env override * log debug mode * try with rake routes * fix workflow syntax * fix workflow syntax * run docker logs * run docker logs again * run docker logs again * run test no parallel * add docker inspect * inspect pender-pender-1 * update syntax * reset puma dirpath? * change storage path?? * modify chmod of all files * try again with parallel tests * mkdir coverage first * chmod coverage folder * add empty coverage dir * rm otel-collector from docker-test.yml * sort .gitignore * remove debug messages from ci test * run workflow with continue-on-error for tests * write test ssm to separate file * use pg_isready and update readme * create a .env.test in the CI environment * add build:. as backup for now * do not use an internal docker network * add chown flags to copy commands in dockerfile * try to fix codeclimate ci step * retry container cp * docker cp with absolute paths * change relative path of cc-test-reporter * run go mod init * actually cd to go module dir * run cc-test-reporter without go modules * check go env * set proper path of resultset * put flags at end * Fixing issues with code coverage report generation and upload to Code Climate (#489) Fixes the steps that generate and upload the code coverage report to Code Climate. The main issue was that previously, we ran the tests, prepared the coverage report, and uploaded it all within the Docker container. Now, however, the tests run inside the Docker container, while the coverage report is prepared outside of it, leading to a mismatch in file paths. The solution I implemented was to use sed to replace the paths, changing /app/pender to /home/runner/work/pender. I also needed to copy codeclimate.json to the coverage directory and set the $CC_TEST_REPORTER_ID environment variable. Reference: CV2-5020. * remove .env before recreating * remove references to .env.test * (1) Using latest Code Climate binary instead of pulling random unexistent branch from GitHub & (2) Fixing error `Error: strconv.Atoi: parsing "2025-02-13T09:46:37-05:00": invalid syntax`. * address PR comments --------- Co-authored-by: Caio Almeida <[email protected]>
1 parent 3e2402b commit a17535f

File tree

14 files changed

+194
-83
lines changed

14 files changed

+194
-83
lines changed

.github/workflows/ci-test-pr.yml

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ jobs:
4848
restore-keys: |
4949
${{ runner.os }}-buildx-
5050
51+
- name: Set up configuration
52+
run: |
53+
touch .env
54+
cp config/database.yml.example config/database.yml
55+
cp config/sidekiq.yml.example config/sidekiq.yml
56+
cp config/config.yml.example config/config.yml
57+
cp config/cookies.txt.example config/cookies.txt
58+
bin/get_env_vars.sh
59+
chmod -R a+w .
60+
5161
- name: Docker Buildx (build)
5262
id: build-image
5363
env:
@@ -63,43 +73,49 @@ jobs:
6373
--file ./Dockerfile ./
6474
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
6575
66-
- name: Set up configuration
67-
run: |
68-
cp config/database.yml.example config/database.yml
69-
cp config/sidekiq.yml.example config/sidekiq.yml
70-
cp config/config.yml.example config/config.yml
71-
bin/get_env_vars.sh
72-
7376
- name: Run container
7477
id: run-container
7578
run: |
76-
docker compose -f docker-compose.yml -f docker-test.yml up -d pender
79+
docker compose -f docker-test.yml up -d pender
7780
7881
- name: Set up PR Tests
7982
id: setup-tests
8083
run: |
81-
docker compose exec -T pender test/setup-parallel
84+
docker compose -f docker-test.yml exec -T -e DEPLOY_ENV=ci pender test/setup-parallel
8285
8386
- name: Run PR Tests
8487
id: run-tests
8588
env:
8689
TEST_RETRY_COUNT: 5
90+
continue-on-error: true
8791
run: |
88-
docker compose exec -e TEST_RETRY_COUNT=$TEST_RETRY_COUNT -T pender bundle exec rake "parallel:test[3]"
89-
docker compose exec -e TEST_RETRY_COUNT=$TEST_RETRY_COUNT -T pender bundle exec rake parallel:spec
92+
docker compose -f docker-test.yml exec -e TEST_RETRY_COUNT=$TEST_RETRY_COUNT -e DEPLOY_ENV=ci -T pender bundle exec rake "parallel:test[3]"
93+
docker compose -f docker-test.yml exec -e TEST_RETRY_COUNT=$TEST_RETRY_COUNT -e DEPLOY_ENV=ci -T pender bundle exec rake parallel:spec
9094
91-
- name: After PR Tests
92-
id: after-tests
95+
- name: Upload coverage to CodeClimate
96+
id: upload-coverage
9397
env:
9498
GIT_SHA: ${{ github.sha }}
9599
GIT_COMMITED_AT: ${{ github.event.head_commit.timestamp }}
100+
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
96101
run: |
97-
docker compose exec -T pender cat tmp/performance.csv
98-
docker compose exec -e GIT_COMMIT_SHA=$GIT_SHA -e GIT_COMMITTED_AT=$GIT_COMMITTED_AT -T pender test/test-coverage
102+
docker compose -f docker-test.yml exec -T pender cat tmp/performance.csv
103+
docker compose -f docker-test.yml exec -T pender ls -l coverage/
104+
docker cp pender-pender-1:/app/pender/tmp/performance.csv performance.csv
105+
docker cp pender-pender-1:/app/pender/coverage/.resultset.json test/.resultset.json
106+
107+
# pulled from test/test-coverage
108+
cd test
109+
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 -o cc-test-reporter
110+
chmod +x cc-test-reporter
111+
sed -i 's/\/app\/pender/\/home\/runner\/work\/pender\/pender/g' .resultset.json # convert container-paths to local-paths
112+
GIT_COMMIT_SHA=$(git rev-parse HEAD) GIT_COMMITTED_AT=$(git log -1 --format=%ct) ./cc-test-reporter format-coverage .resultset.json -t simplecov -o codeclimate.json
113+
sed -i 's/\/home\/runner\/work\/pender\///g' codeclimate.json
114+
cp codeclimate.json ../coverage/
115+
./cc-test-reporter upload-coverage -r $CC_TEST_REPORTER_ID -i codeclimate.json
99116
100117
- name: Reset cache
101118
id: reset-cache
102-
if: ${{ failure() || success() }}
103119
run: |
104120
rm -rf /tmp/.buildx-cache
105121
mv /tmp/.buildx-cache-new /tmp/.buildx-cache

.gitignore

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
1+
*.env
2+
*.sw*
3+
*~
4+
.byebug_history
5+
.idea
16
/.bundle
7+
/.project
28
/db/*.sqlite3
39
/db/*.sqlite3-journal
4-
db/schema.rb
510
/log/*.log
611
/tmp/cache*
712
/tmp/pids
813
/tmp/sessions
914
/tmp/sockets
10-
config/database.yml
1115
config/config.yml
12-
*.sw*
13-
*~
16+
config/cookies.txt
17+
config/database.yml
18+
config/sidekiq.yml
19+
coverage/*
20+
dump.rdb
21+
otel-collector-config.yaml
1422
public/assets
15-
tmp/restart.txt
16-
/.project
1723
public/cache
1824
public/screenshots
19-
tmp/restart.txt
20-
tmp/puma-*
21-
tmp/profile
2225
tmp/performance*
23-
config/sidekiq.yml
24-
dump.rdb
25-
config/cookies.txt
26-
coverage
27-
.idea
28-
.byebug_history
29-
*.env
30-
otel-collector-config.yaml
26+
tmp/profile
27+
tmp/puma-*
28+
tmp/restart.txt
29+
tmp/restart.txt

Dockerfile

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,38 @@
11
FROM ruby:3.3.3-slim
22
33

4-
# PROD
5-
# ENV RAILS_ENV=production \
6-
# BUNDLE_DEPLOYMENT=true \
7-
# BUNDLE_WITHOUT=development:test
8-
9-
# DEV
10-
ENV RAILS_ENV=development \
11-
SERVER_PORT=3200
12-
4+
ENV APP=pender
135
# Set a UTF-8 capabable locale
14-
ENV LC_ALL=C.UTF-8 \
15-
LANG=C.UTF-8 \
16-
LANGUAGE=C.UTF-8
6+
ENV LANG=C.UTF-8
177

18-
ENV APP=pender
8+
ENV RAILS_ENV=development \
9+
SERVER_PORT=3200 \
10+
BUNDLE_DEPLOYMENT="" \
11+
BUNDLE_WITHOUT=""
1912

13+
# Build-time variables
2014
ARG DIRPATH=/app/pender
21-
ARG BUNDLER_VERSION="2.3.5"
15+
ARG BUNDLER_VERSION="2.3.5"
2216

2317
RUN apt-get update && apt-get install -y curl \
2418
build-essential \
2519
git \
2620
libpq-dev --no-install-recommends
2721

28-
# pender user
29-
RUN mkdir -p ${DIRPATH}
30-
RUN useradd ${APP} -s /bin/bash -m
31-
WORKDIR ${DIRPATH}
22+
RUN useradd "${APP}" --shell /bin/bash --create-home
23+
WORKDIR "${DIRPATH}"
3224

33-
# install our app
34-
COPY Gemfile Gemfile.lock ./
35-
RUN gem install bundler -v ${BUNDLER_VERSION} --no-document \
25+
COPY Gemfile Gemfile.lock .
26+
RUN gem install bundler -v "${BUNDLER_VERSION}" --no-document \
3627
&& bundle install --jobs 20 --retry 5
37-
COPY . ./
28+
# FIXME: chown flags required for local macos (and likely windows) builds
29+
COPY --chown=${APP} ./ .
30+
COPY --chown=${APP} bin/ /opt/bin/
31+
COPY --chown=${APP} db/schema.rb /opt/db/
32+
RUN chmod a+w /opt/db/schema.rb
3833

3934
USER ${APP}
4035

41-
# DEV
42-
# RUN chmod +x ./bin/docker-entrypoint.sh
4336
EXPOSE 3200
44-
ENTRYPOINT ["./bin/docker-entrypoint.sh"]
45-
46-
# PROD
4737
# EXPOSE 8000
38+
ENTRYPOINT ["/opt/bin/docker-entrypoint.sh"]

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,33 @@ Besides the specific parsers Pender can parse any link with an oEmbed endpoint o
3434

3535
## Setup
3636

37-
To run Pender, follow these steps:
37+
To set Pender up locally:
38+
39+
```
40+
git clone https://github.com/meedan/pender.git
41+
cd pender
42+
find -name '*.example' | while read f; do cp "$f" "${f%%.example}"; done
43+
```
44+
45+
To run Pender in development mode, follow these steps:
3846

3947
```
40-
$ git clone https://github.com/meedan/pender.git
41-
$ cd pender
42-
$ find -name '*.example' | while read f; do cp "$f" "${f%%.example}"; done
4348
$ docker-compose build
4449
$ docker-compose up --abort-on-container-exit
4550
```
4651
Open http://localhost:3200/api-docs/index.html to access Pender API directly.
4752

53+
To run the full test suite of Pender tests locally the way CI runs them:
54+
55+
```
56+
bin/get_env_vars.sh
57+
docker build . -t pender
58+
docker compose -f docker-test.yml up pender
59+
docker compose -f docker-test.yml exec pender test/setup-parallel
60+
docker compose -f docker-test.yml exec pender bundle exec rake "parallel:test[3]"
61+
docker compose -f docker-test.yml exec pender bundle exec rake "parallel:spec"
62+
```
63+
4864
### Setting Cookies for Requests
4965

5066
We send cookies with certain requests that require logged-in users (e.g. Instagram, TikTok).

bin/docker-entrypoint.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ if [[ -z ${DEPLOY_ENV+x} || -z ${APP+x} ]]; then
33
echo "DEPLOY_ENV and APP must be in the environment. Exiting."
44
exit 1
55
fi
6+
echo "running ${APP} in ${DEPLOY_ENV} environment..."
67

78
# pender
89
if [ "${APP}" = 'pender' ] ; then
@@ -17,7 +18,7 @@ if [ "${APP}" = 'pender' ] ; then
1718
echo "--- STARTUP COMPLETE ---"
1819
fi
1920

20-
DIRPATH=${PWD}/tmp
21+
DIRPATH=/tmp
2122
PUMA="${DIRPATH}/puma-${DEPLOY_ENV}.rb"
2223
mkdir -p "${DIRPATH}/pids"
2324
cp config/puma.rb "${PUMA}"
@@ -27,7 +28,7 @@ if [ "${APP}" = 'pender' ] ; then
2728
port ${SERVER_PORT}
2829
EOF
2930

30-
if [ "${DEPLOY_ENV}" = 'local' ] ; then
31+
if [ "${DEPLOY_ENV}" = 'local' ] || [ "${RAILS_ENV}" = 'test' ] ; then
3132
rm -f "${DIRPATH}/pids/server-${DEPLOY_ENV}.pid"
3233
bundle exec puma -C "${PUMA}"
3334
else # qa, live etc

bin/get_env_vars.sh

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
#!/bin/bash
22
export REGION=eu-west-1
33
aws sts get-caller-identity >/dev/null 2>&1
4-
if (( $? != 0 )); then
4+
5+
if [[ $? != 0 ]]; then
56
echo "Error calling AWS get-caller-identity. Do you have valid credentials?"
6-
else
7+
else
78
SSM_NAMES=$(aws ssm get-parameters-by-path --region $REGION --path /test/pender/ --recursive --with-decryption --output text --query "Parameters[].[Name]")
8-
echo "Getting variables"
9+
10+
if [ -f ".env" ]; then
11+
echo "File .env exists, removing and overwriting."
12+
rm .env
13+
fi
14+
echo "Getting SSM variables"
915
for NAME in $SSM_NAMES; do
1016
echo "."
1117
VALUE=$(aws ssm get-parameters --region $REGION --with-decryption --name "$NAME" | jq .Parameters[].Value)
1218
VARNAME=$(basename "$NAME")
13-
1419
echo "$VARNAME=$VALUE" >> .env
1520
done
1621
fi

bin/setup

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ Dir.chdir APP_ROOT do
2020
puts "\n== Preparing database =="
2121
system "bin/rake db:setup"
2222

23-
puts "\n== Removing old logs and tempfiles =="
23+
puts "\n== Removing old logs =="
2424
system "rm -f log/*"
25-
system "rm -rf tmp/cache"
2625

2726
puts "\n== Restarting application server =="
2827
system "touch tmp/restart.txt"

config/environments/test.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@
4242

4343
config.allow_concurrency = true
4444

45-
config.cache_store = :file_store, "#{Rails.root}/tmp/cache#{ENV['TEST_ENV_NUMBER']}"
45+
config.cache_store = :file_store, "/tmp/cache#{ENV['TEST_ENV_NUMBER']}"
4646

4747
config.lograge.enabled = true
4848

49+
config.logger = ActiveSupport::Logger.new(STDOUT)
4950
config.lograge.logger = ActiveSupport::Logger.new(STDOUT)
5051
config.lograge.custom_options = lambda do |event|
5152
options = event.payload.slice(:request_id, :user_id)
@@ -55,4 +56,7 @@
5556
end
5657
config.lograge.formatter = Lograge::Formatters::Json.new
5758
config.log_level = :warn
59+
config.paths['log'] = "/var/log/#{ENV['DEPLOY_ENV']}.log"
60+
config.paths['tmp'] = "/tmp"
61+
config.paths['db'] = "/opt/db"
5862
end

config/storage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
test:
22
service: Disk
3-
root: <%= Rails.root.join("tmp/storage") %>
3+
root: /tmp
44

55
local:
66
service: Disk

coverage/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)