Skip to content

Add docker testing setup #1403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .docker/ruby_versions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
2.7.7
3.0.5
3.1.3
3.2.1
3 changes: 3 additions & 0 deletions .docker/scripts/test_all
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

xargs -L 1 ./test_ruby < ruby_versions.txt
10 changes: 10 additions & 0 deletions .docker/scripts/test_mysql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

cd /src
bundle update;

RUBY_VERSION=$(ruby -v);

echo "Testing With MySQL and Ruby $RUBY_VERSION";
export DATABASE_URL="mysql2://test:password@mysql:3306/test"
bundle exec rake test;
10 changes: 10 additions & 0 deletions .docker/scripts/test_postgresql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

cd /src
bundle update;

RUBY_VERSION=$(ruby -v);

echo "Testing With PostgreSQL and Ruby $RUBY_VERSION";
export DATABASE_URL="postgresql://postgres:password@postgres:5432/test"
bundle exec rake test;
7 changes: 7 additions & 0 deletions .docker/scripts/test_ruby
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

rbenv global $1;

../test_postgresql
../test_mysql
../test_sqlite
10 changes: 10 additions & 0 deletions .docker/scripts/test_sqlite
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

cd /src
bundle update;

RUBY_VERSION=$(ruby -v);

echo "Testing With SQLite and Ruby $RUBY_VERSION";
export DATABASE_URL="sqlite3:test_db"
bundle exec rake test;
36 changes: 36 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
FROM buildpack-deps:bullseye

# Install rbenv
RUN git clone https://github.com/sstephenson/rbenv.git /root/.rbenv
RUN git clone https://github.com/sstephenson/ruby-build.git /root/.rbenv/plugins/ruby-build
RUN /root/.rbenv/plugins/ruby-build/install.sh
ENV PATH /root/.rbenv/bin:/root/.rbenv/shims:$PATH
RUN echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh # or /etc/profile
RUN echo 'eval "$(rbenv init -)"' >> .bashrc

# Install supported ruby versions
RUN echo 'gem: --no-document' >> ~/.gemrc

COPY .docker/ruby_versions.txt /
RUN xargs -I % sh -c 'rbenv install %; rbenv global %; gem install bundler' < ruby_versions.txt
RUN rbenv rehash

# COPY just enough to bundle. This allows for most code changes without needing to reinstall all gems
RUN mkdir src
COPY Gemfile jsonapi-resources.gemspec Rakefile ./src/
COPY lib/jsonapi/resources/version.rb ./src/lib/jsonapi/resources/
# This will run bundle install for each ruby version and leave the global version set as the last one.
# So we can control the default ruby version with the order in the ruby_versions.txt file, with last being the default
RUN xargs -I % sh -c 'cd src; rbenv global %; bundle install' < /ruby_versions.txt

# Scripts
COPY .docker/scripts/* /
RUN chmod +x /test_*

# COPY in the rest of the project
COPY lib/ ./src/lib
COPY locales/ ./src/locales
COPY test/ ./src/test
RUN ls -la

CMD ["/test_all"]
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,43 @@ gem install jsonapi-resources

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

## Development

There is a docker compose setup that can be used for testing your code against the supported versions of ruby and
against PostgreSQL, MYSQL, and SQLite databases.

First build the docker image:
```bash
docker compose build
```
Be sure to rebuild after making code changes. It should be fast after the initial build.

### Running the tests

The default command will run everything (it may take a while):
```bash
docker compose run tests
```

To test just one database against the latest ruby:
```bash
docker compose run tests ./test_mysql
docker compose run tests ./test_postgresql
docker compose run tests ./test_sqlite
```

To test a version of ruby against all databases:
```bash
docker compose run tests ./test_ruby 2.7.7
```

The tests by default run against the latest rails version. To override that you can set the RAILS_VERSION environment
variable:

```bash
docker compose run -e RAILS_VERSION=6.1.1 tests ./test_postgresql
```

## Contributing

1. Submit an issue describing any new features you wish it add or the bug you intend to fix
Expand Down
49 changes: 49 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
version: "3.9"

networks:
back:

services:
postgres:
image: postgres:latest
ports:
- "5432"
networks:
back:
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_DB=test
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
interval: 2s
retries: 15

mysql:
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
ports:
- 3306
networks:
- back
environment:
- MYSQL_DATABASE=test
- MYSQL_USER=test
- MYSQL_PASSWORD=password
- MYSQL_ROOT_PASSWORD=password
healthcheck:
test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ]
interval: 2s
retries: 15

tests:
image: jr_tests:latest
build:
context: .
dockerfile: Dockerfile
networks:
- back
depends_on:
postgres:
condition: service_healthy
mysql:
condition: service_healthy