Skip to content

Commit 83af511

Browse files
authored
Earthfile for running ci tests locally (#320)
1 parent bfe9777 commit 83af511

File tree

3 files changed

+231
-133
lines changed

3 files changed

+231
-133
lines changed

.github/workflows/ci.yml

Lines changed: 55 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,73 @@
11
name: CI
22
on: [push, pull_request]
33
jobs:
4-
test-elixir:
5-
runs-on: ubuntu-16.04
6-
env:
7-
MIX_ENV: test
4+
test:
5+
name: unittest
6+
runs-on: ubuntu-latest
87
strategy:
98
fail-fast: false
109
matrix:
11-
include:
12-
- pair:
13-
elixir: 1.8.2
14-
otp: 20.3.8.26
15-
- pair:
16-
elixir: 1.11.3
17-
otp: 23.2.5
18-
lint: lint
10+
elixirbase:
11+
- "1.11.0-erlang-23.1.1-alpine-3.13.1"
12+
- "1.11.0-erlang-21.3.8.21-alpine-3.13.1"
1913
steps:
20-
- uses: actions/checkout@v2
21-
22-
- uses: erlef/setup-elixir@v1
14+
- uses: earthly/actions/setup-earthly@v1
2315
with:
24-
otp-version: ${{matrix.pair.otp}}
25-
elixir-version: ${{matrix.pair.elixir}}
26-
27-
- name: Install Dependencies
28-
run: mix deps.get --only test
29-
30-
- run: mix format --check-formatted
31-
if: ${{ matrix.lint }}
32-
33-
- run: mix deps.get && mix deps.unlock --check-unused
34-
if: ${{ matrix.lint }}
35-
36-
- run: mix deps.compile
37-
38-
- run: mix compile --warnings-as-errors
39-
if: ${{ matrix.lint }}
40-
41-
- run: mix test
42-
43-
- run: mix test.as_a_dep
44-
45-
test-mysql:
46-
runs-on: ubuntu-18.04
47-
48-
services:
49-
mysql:
50-
image: mysql:${{ matrix.mysql_version }}
51-
env:
52-
MYSQL_ROOT_PASSWORD: root
53-
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
54-
55-
container: elixir:1.9-slim
56-
16+
version: v0.5.10
17+
- uses: actions/checkout@v2
18+
- name: test ectl_sql
19+
run: earthly -P --ci --build-arg ELIXIR_BASE=${{matrix.elixirbase}} +test
20+
test-postgres:
21+
name: postgres integration test
22+
runs-on: ubuntu-latest
5723
strategy:
24+
fail-fast: false
5825
matrix:
59-
mysql_version: ["5.7"]
60-
26+
elixirbase:
27+
- "1.9.4-erlang-22.3.4.16-alpine-3.13.1"
28+
postgres:
29+
- "11.11"
30+
- "9.6"
31+
- "9.5"
6132
steps:
62-
- name: Install MySQL Client
63-
run: |
64-
apt-get update
65-
apt-get install -y default-mysql-client
66-
mysql --version
67-
- uses: actions/checkout@v1
68-
- name: Install Dependencies
69-
run: |
70-
apt-get install -y git
71-
mix local.rebar --force
72-
mix local.hex --force
73-
mix deps.get
74-
- run: MYSQL_URL=root:root@mysql ECTO_ADAPTER=myxql mix test
75-
76-
test-mssql:
77-
runs-on: ubuntu-16.04
78-
79-
services:
80-
mssql:
81-
image: mcr.microsoft.com/mssql/server:${{ matrix.mssql_version }}
82-
env:
83-
ACCEPT_EULA: Y
84-
SA_PASSWORD: some!Password
85-
ports:
86-
- 1433:1433
87-
33+
- uses: earthly/actions/setup-earthly@v1
34+
with:
35+
version: v0.5.10
36+
- uses: actions/checkout@v2
37+
- name: test ecto_sql
38+
run: earthly -P --ci --build-arg ELIXIR_BASE=${{matrix.elixirbase}} --build-arg POSTGRES=${{matrix.postgres}} +integration-test-postgres
39+
test-mysql:
40+
name: mysql integration test
41+
runs-on: ubuntu-latest
8842
strategy:
43+
fail-fast: false
8944
matrix:
90-
mssql_version: ["2017-latest", "2019-latest"]
91-
otp: [22.1.7]
92-
elixir: [1.9.4]
93-
94-
env:
95-
ACCEPT_EULA: Y
96-
MIX_ENV: test
97-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
98-
45+
elixirbase:
46+
- "1.9.4-erlang-22.3.4.16-alpine-3.13.1"
47+
mysql:
48+
- "5.7"
9949
steps:
100-
- name: Install MsSql Client Tools
101-
run: |
102-
sudo apt-get update
103-
sudo apt-get install -y mssql-tools unixodbc-dev
104-
- uses: actions/checkout@v2
105-
- name: Setup elixir
106-
uses: actions/setup-elixir@v1
50+
- uses: earthly/actions/setup-earthly@v1
10751
with:
108-
otp-version: ${{matrix.otp}}
109-
elixir-version: ${{matrix.elixir}}
110-
- name: Install Dependencies
111-
run: mix deps.get
112-
- run: |
113-
export PATH="/opt/mssql-tools/bin:$PATH"
114-
ECTO_ADAPTER=tds mix test
115-
116-
test-pg:
117-
runs-on: ubuntu-18.04
118-
119-
services:
120-
pg:
121-
image: postgres:${{ matrix.pg_version }}
122-
env:
123-
POSTGRES_USER: postgres
124-
POSTGRES_PASSWORD: postgres
125-
POSTGRES_DB: postgres
126-
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
127-
128-
container: elixir:1.9-slim
129-
52+
version: v0.5.10
53+
- uses: actions/checkout@v2
54+
- name: test ecto_sql
55+
run: earthly -P --ci --build-arg ELIXIR_BASE=${{matrix.elixirbase}} --build-arg POSTGRES=${{matrix.postgres}} +integration-test-mysql
56+
test-mssql:
57+
name: mssql integration test
58+
runs-on: ubuntu-latest
13059
strategy:
60+
fail-fast: false
13161
matrix:
132-
pg_version: ["9.5", "9.6", "11"]
133-
62+
elixirbase:
63+
- "1.9.4-erlang-22.1.7-alpine-3.11.3"
64+
mssql:
65+
- "2017"
66+
- "2019"
13467
steps:
135-
- name: Install PG Client
136-
run: |
137-
apt-get update
138-
apt-get install -y wget ca-certificates gnupg
139-
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
140-
echo "deb http://apt.postgresql.org/pub/repos/apt/ buster-pgdg main ${{ matrix.pg_version }}" >> /etc/apt/sources.list.d/pgdg.list
141-
apt-get update
142-
apt-get install -y postgresql-${{ matrix.pg_version }} postgresql-contrib-${{ matrix.pg_version }}
143-
psql --version
144-
- uses: actions/checkout@v1
145-
- name: Install Dependencies
146-
run: |
147-
apt-get install -y git
148-
mix local.rebar --force
149-
mix local.hex --force
150-
mix deps.get
151-
- run: PG_URL=postgres:postgres@pg ECTO_ADAPTER=pg mix test
68+
- uses: earthly/actions/setup-earthly@v1
69+
with:
70+
version: v0.5.10
71+
- uses: actions/checkout@v2
72+
- name: test ecto_sql
73+
run: earthly -P --ci --build-arg ELIXIR_BASE=${{matrix.elixirbase}} --build-arg MSSQL=${{matrix.mssql}} +integration-test-mssql

Earthfile

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
all:
2+
BUILD +test-all
3+
BUILD +integration-test-all
4+
5+
6+
test-all:
7+
BUILD \
8+
--build-arg ELIXIR_BASE=1.11.0-erlang-23.1.1-alpine-3.13.1 \
9+
--build-arg ELIXIR_BASE=1.11.0-erlang-21.3.8.21-alpine-3.13.1 \
10+
+test
11+
12+
13+
test:
14+
FROM +test-setup
15+
RUN MIX_ENV=test mix deps.compile
16+
COPY --dir bench integration_test lib test ./
17+
18+
RUN mix deps.get && mix deps.unlock --check-unused
19+
RUN mix deps.compile
20+
RUN mix compile #--warnings-as-errors
21+
RUN mix test
22+
23+
24+
integration-test-all:
25+
ARG ELIXIR_BASE=1.11.0-erlang-23.1.1-alpine-3.13.1
26+
BUILD \
27+
--build-arg POSTGRES=11.11 \
28+
--build-arg POSTGRES=9.6 \
29+
--build-arg POSTGRES=9.5 \
30+
+integration-test-postgres
31+
32+
BUILD \
33+
--build-arg MYSQL=5.7 \
34+
+integration-test-mysql
35+
36+
BUILD \
37+
--build-arg MSSQL=2017 \
38+
--build-arg MSSQL=2019 \
39+
+integration-test-mssql
40+
41+
42+
integration-test-base:
43+
FROM +setup-base
44+
RUN apk add --no-progress --update docker docker-compose
45+
46+
RUN mix local.rebar --force
47+
RUN mix local.hex --force
48+
49+
50+
COMMON_INTEGRATION_SETUP_AND_MIX:
51+
COMMAND
52+
COPY mix.exs mix.lock .formatter.exs .
53+
COPY --dir bench integration_test lib test ./
54+
RUN mix deps.get
55+
RUN mix deps.compile
56+
RUN mix compile #--warnings-as-errors
57+
58+
59+
integration-test-postgres:
60+
FROM +integration-test-base
61+
ARG POSTGRES="11.11"
62+
63+
IF [ "$POSTGRES" = "9.5" ]
64+
# for 9.5 we require a downgraded version of pg_dump;
65+
# and in the 3.4 version, it is not included in postgresql-client but rather in postgresql
66+
RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.4/main' >> /etc/apk/repositories
67+
RUN apk add postgresql=9.5.13-r0
68+
ELSE
69+
RUN apk add postgresql-client
70+
END
71+
72+
DO +COMMON_INTEGRATION_SETUP_AND_MIX
73+
74+
# then run the tests
75+
WITH DOCKER \
76+
--pull "postgres:$POSTGRES"
77+
RUN set -e; \
78+
timeout=$(expr $(date +%s) + 30); \
79+
docker run --name pg --network=host -d -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres "postgres:$POSTGRES"; \
80+
# wait for postgres to start
81+
while ! pg_isready --host=127.0.0.1 --port=5432 --quiet; do \
82+
test "$(date +%s)" -le "$timeout" || (echo "timed out waiting for postgres"; exit 1); \
83+
echo "waiting for postgres"; \
84+
sleep 1; \
85+
done; \
86+
# run tests
87+
PG_URL=postgres:postgres@127.0.0.1 ECTO_ADAPTER=pg mix test;
88+
END
89+
90+
91+
integration-test-mysql:
92+
FROM +integration-test-base
93+
RUN apk add mysql-client
94+
95+
DO +COMMON_INTEGRATION_SETUP_AND_MIX
96+
97+
ARG MYSQL="5.7"
98+
WITH DOCKER \
99+
--pull "mysql:$MYSQL"
100+
RUN set -e; \
101+
timeout=$(expr $(date +%s) + 30); \
102+
docker run --name mysql --network=host -d -e MYSQL_ROOT_PASSWORD=root "mysql:$MYSQL"; \
103+
# wait for mysql to start
104+
while ! mysqladmin ping --host=127.0.0.1 --port=3306 --protocol=TCP --silent; do \
105+
test "$(date +%s)" -le "$timeout" || (echo "timed out waiting for mysql"; exit 1); \
106+
echo "waiting for mysql"; \
107+
sleep 1; \
108+
done; \
109+
# run tests
110+
MYSQL_URL=root:root@127.0.0.1 ECTO_ADAPTER=myxql mix test;
111+
END
112+
113+
114+
integration-test-mssql:
115+
FROM +integration-test-base
116+
117+
RUN apk add --no-cache curl gnupg --virtual .build-dependencies -- && \
118+
curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/msodbcsql17_17.5.2.1-1_amd64.apk && \
119+
curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/mssql-tools_17.5.2.1-1_amd64.apk && \
120+
echo y | apk add --allow-untrusted msodbcsql17_17.5.2.1-1_amd64.apk mssql-tools_17.5.2.1-1_amd64.apk && \
121+
apk del .build-dependencies && rm -f msodbcsql*.sig mssql-tools*.apk
122+
ENV PATH="/opt/mssql-tools/bin:${PATH}"
123+
124+
DO +COMMON_INTEGRATION_SETUP_AND_MIX
125+
126+
ARG MSSQL="2017"
127+
WITH DOCKER \
128+
--pull "mcr.microsoft.com/mssql/server:$MSSQL-latest"
129+
RUN set -e; \
130+
timeout=$(expr $(date +%s) + 30); \
131+
docker run -d -p 1433:1433 --name mssql -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=some!Password' "mcr.microsoft.com/mssql/server:$MSSQL-latest"; \
132+
# wait for mssql to start
133+
while ! sqlcmd -S tcp:127.0.0.1,1433 -U sa -P 'some!Password' -Q "SELECT 1" >/dev/null 2>&1; do \
134+
test "$(date +%s)" -le "$timeout" || (echo "timed out waiting for mssql"; exit 1); \
135+
echo "waiting for mssql"; \
136+
sleep 1; \
137+
done; \
138+
# run tests
139+
ECTO_ADAPTER=tds mix test;
140+
END
141+
142+
143+
setup-base:
144+
ARG ELIXIR_BASE=1.11.0-erlang-23.1.1-alpine-3.13.1
145+
FROM hexpm/elixir:$ELIXIR_BASE
146+
RUN apk add --no-progress --update git build-base
147+
ENV ELIXIR_ASSERT_TIMEOUT=10000
148+
WORKDIR /src/ecto_sql
149+
150+
151+
test-setup:
152+
FROM +setup-base
153+
COPY mix.exs .
154+
COPY mix.lock .
155+
COPY .formatter.exs .
156+
RUN mix local.rebar --force
157+
RUN mix local.hex --force
158+
RUN mix deps.get

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ MySQL and PostgreSQL can be installed directly on most systems. For MSSQL, you m
3535

3636
docker run -d -p 1433:1433 --name mssql -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=some!Password' mcr.microsoft.com/mssql/server:2017-latest
3737

38+
### Running containerized tests
39+
40+
It is also possible to run the integration tests under a containerized environment using [earthly](https://earthly.dev/get-earthly):
41+
42+
$ earthly -P +all
43+
44+
You can also use this to interactively debug any failing integration tests using the corresponding commands:
45+
46+
$ earthly -P -i --build-arg ELIXIR_BASE=1.8.2-erlang-20.3.8.26-alpine-3.11.6 --build-arg MYSQL=5.7 +integration-test-mysql
47+
$ earthly -P -i --build-arg ELIXIR_BASE=1.8.2-erlang-20.3.8.26-alpine-3.11.6 --build-arg MSSQL=2019 +integration-test-mssql
48+
$ earthly -P -i --build-arg ELIXIR_BASE=1.8.2-erlang-20.3.8.26-alpine-3.11.6 --build-arg POSTGRES=11.11 +integration-test-postgres
49+
50+
Then once you enter the containerized shell, you can inspect the underlying databases with the respective commands:
51+
52+
PGPASSWORD=postgres psql -h 127.0.0.1 -U postgres -d postgres ecto_test
53+
MYSQL_PASSWORD=root mysql -h 127.0.0.1 -uroot -proot ecto_test
54+
sqlcmd -U sa -P 'some!Password'
55+
3856
## License
3957

4058
Copyright (c) 2012 Plataformatec \

0 commit comments

Comments
 (0)