Skip to content

Commit c99473a

Browse files
authored
Add docker testing setup (#1403)
1 parent 5c24399 commit c99473a

9 files changed

+166
-0
lines changed

.docker/ruby_versions.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2.7.7
2+
3.0.5
3+
3.1.3
4+
3.2.1

.docker/scripts/test_all

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
xargs -L 1 ./test_ruby < ruby_versions.txt

.docker/scripts/test_mysql

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
cd /src
4+
bundle update;
5+
6+
RUBY_VERSION=$(ruby -v);
7+
8+
echo "Testing With MySQL and Ruby $RUBY_VERSION";
9+
export DATABASE_URL="mysql2://test:password@mysql:3306/test"
10+
bundle exec rake test;

.docker/scripts/test_postgresql

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
cd /src
4+
bundle update;
5+
6+
RUBY_VERSION=$(ruby -v);
7+
8+
echo "Testing With PostgreSQL and Ruby $RUBY_VERSION";
9+
export DATABASE_URL="postgresql://postgres:password@postgres:5432/test"
10+
bundle exec rake test;

.docker/scripts/test_ruby

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
rbenv global $1;
4+
5+
../test_postgresql
6+
../test_mysql
7+
../test_sqlite

.docker/scripts/test_sqlite

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
cd /src
4+
bundle update;
5+
6+
RUBY_VERSION=$(ruby -v);
7+
8+
echo "Testing With SQLite and Ruby $RUBY_VERSION";
9+
export DATABASE_URL="sqlite3:test_db"
10+
bundle exec rake test;

Dockerfile

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
FROM buildpack-deps:bullseye
2+
3+
# Install rbenv
4+
RUN git clone https://github.com/sstephenson/rbenv.git /root/.rbenv
5+
RUN git clone https://github.com/sstephenson/ruby-build.git /root/.rbenv/plugins/ruby-build
6+
RUN /root/.rbenv/plugins/ruby-build/install.sh
7+
ENV PATH /root/.rbenv/bin:/root/.rbenv/shims:$PATH
8+
RUN echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh # or /etc/profile
9+
RUN echo 'eval "$(rbenv init -)"' >> .bashrc
10+
11+
# Install supported ruby versions
12+
RUN echo 'gem: --no-document' >> ~/.gemrc
13+
14+
COPY .docker/ruby_versions.txt /
15+
RUN xargs -I % sh -c 'rbenv install %; rbenv global %; gem install bundler' < ruby_versions.txt
16+
RUN rbenv rehash
17+
18+
# COPY just enough to bundle. This allows for most code changes without needing to reinstall all gems
19+
RUN mkdir src
20+
COPY Gemfile jsonapi-resources.gemspec Rakefile ./src/
21+
COPY lib/jsonapi/resources/version.rb ./src/lib/jsonapi/resources/
22+
# This will run bundle install for each ruby version and leave the global version set as the last one.
23+
# So we can control the default ruby version with the order in the ruby_versions.txt file, with last being the default
24+
RUN xargs -I % sh -c 'cd src; rbenv global %; bundle install' < /ruby_versions.txt
25+
26+
# Scripts
27+
COPY .docker/scripts/* /
28+
RUN chmod +x /test_*
29+
30+
# COPY in the rest of the project
31+
COPY lib/ ./src/lib
32+
COPY locales/ ./src/locales
33+
COPY test/ ./src/test
34+
RUN ls -la
35+
36+
CMD ["/test_all"]

README.md

+37
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,43 @@ gem install jsonapi-resources
4646

4747
**For further usage see the [v0.10 alpha Guide](http://jsonapi-resources.com/v0.10/guide/)**
4848

49+
## Development
50+
51+
There is a docker compose setup that can be used for testing your code against the supported versions of ruby and
52+
against PostgreSQL, MYSQL, and SQLite databases.
53+
54+
First build the docker image:
55+
```bash
56+
docker compose build
57+
```
58+
Be sure to rebuild after making code changes. It should be fast after the initial build.
59+
60+
### Running the tests
61+
62+
The default command will run everything (it may take a while):
63+
```bash
64+
docker compose run tests
65+
```
66+
67+
To test just one database against the latest ruby:
68+
```bash
69+
docker compose run tests ./test_mysql
70+
docker compose run tests ./test_postgresql
71+
docker compose run tests ./test_sqlite
72+
```
73+
74+
To test a version of ruby against all databases:
75+
```bash
76+
docker compose run tests ./test_ruby 2.7.7
77+
```
78+
79+
The tests by default run against the latest rails version. To override that you can set the RAILS_VERSION environment
80+
variable:
81+
82+
```bash
83+
docker compose run -e RAILS_VERSION=6.1.1 tests ./test_postgresql
84+
```
85+
4986
## Contributing
5087

5188
1. Submit an issue describing any new features you wish it add or the bug you intend to fix

docker-compose.yml

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
version: "3.9"
2+
3+
networks:
4+
back:
5+
6+
services:
7+
postgres:
8+
image: postgres:latest
9+
ports:
10+
- "5432"
11+
networks:
12+
back:
13+
environment:
14+
- POSTGRES_PASSWORD=password
15+
- POSTGRES_DB=test
16+
healthcheck:
17+
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
18+
interval: 2s
19+
retries: 15
20+
21+
mysql:
22+
image: mysql:latest
23+
command: --default-authentication-plugin=mysql_native_password
24+
ports:
25+
- 3306
26+
networks:
27+
- back
28+
environment:
29+
- MYSQL_DATABASE=test
30+
- MYSQL_USER=test
31+
- MYSQL_PASSWORD=password
32+
- MYSQL_ROOT_PASSWORD=password
33+
healthcheck:
34+
test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ]
35+
interval: 2s
36+
retries: 15
37+
38+
tests:
39+
image: jr_tests:latest
40+
build:
41+
context: .
42+
dockerfile: Dockerfile
43+
networks:
44+
- back
45+
depends_on:
46+
postgres:
47+
condition: service_healthy
48+
mysql:
49+
condition: service_healthy

0 commit comments

Comments
 (0)