Skip to content

Commit

Permalink
Docker compose can be asked to run the test suite
Browse files Browse the repository at this point in the history
Include these instructions as the first option as they are the most convenient way for new people to get the tests running. For people developing and running the test suite often, non Docker is much faster. Included some explanation of trade-offs to help the person make the right decision.
  • Loading branch information
tahb committed Nov 25, 2020
1 parent 38ae51c commit ee53981
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 28 deletions.
20 changes: 4 additions & 16 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,10 @@ on:
jobs:
test:
runs-on: ubuntu-latest
env:
RAILS_ENV: test
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Set up Docker
run: docker network create test
- name: Set up Postgres
run: docker run -d --name pg --network test -e POSTGRES_USER=test -e POSTGRES_HOST_AUTH_METHOD=trust -p 5432:5432 postgres:11
- name: Build a new Docker image
run: docker build . --build-arg RAILS_ENV=test -t app:test
- name: Run the tests
run: |
docker run --name test-container \
--network test \
-e RAILS_ENV=test \
-e DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL=true \
-e DATABASE_URL=postgres://test@pg:5432/app_test \
app:test bundle exec rake
- name: Build
run: docker-compose -f docker-compose.ci.yml build
- name: Test
run: docker-compose -f docker-compose.ci.yml run --rm test bundle exec rake
17 changes: 10 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ RUN gem install bundler

# bundle ruby gems based on the current environment, default to production
RUN echo $RAILS_ENV
RUN \
if [ "$RAILS_ENV" = "production" ]; then \
bundle install --without development test --retry 10; \
else \
bundle install --retry 10; \
fi
RUN if [ "$RAILS_ENV" = "production" ]; then \
bundle install --without development test --retry 10; \
else \
bundle install --retry 10; \
fi

COPY . $INSTALL_PATH

RUN RAILS_ENV=$RAILS_ENV SECRET_KEY_BASE="super secret" bundle exec rake assets:precompile --quiet
# Compiling assets requires a key to exist: https://github.com/rails/rails/issues/32947
RUN if [ "$RAILS_ENV" = "production" ]; then \
RAILS_ENV=production SECRET_KEY_BASE="key" bundle exec rake assets:precompile; \
fi


# db setup
COPY ./docker-entrypoint.sh /
Expand Down
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,38 @@ A service to help school buying professionals create tender documents that compl
1. copy `/.env.example` into `/.env.development.local`.

Our intention is that the example should include enough to get the application started quickly. If this is not the case, please ask another developer for a copy of their `/.env.development.local` file.

1. `script/server`
1. Visit http://localhost:3000

## Running the tests

### The whole test suite

`bundle exec rake`
* Using Docker has high parity, you don't have to install any dependencies but it takes longer to run (~20 seconds):

```bash
docker-compose -f docker-compose.test.yml run --rm web bundle exec rake
```
* Without Docker is faster (~5 seconds) but has lower parity and you will need to install local dependencies on your machine first:

```bash
brew install postgres
brew services start postgres
createuser postgres --super
rbenv install 2.6.6 && rbenv local 2.6.6
gem install bundle && bundle
RAILS_ENV=test rake db:setup
```
```ruby
script/test
```

### RSpec only

`bundle exec rspec`
```
bundle exec rspec spec/*
```
## Running Brakeman
Expand Down
36 changes: 36 additions & 0 deletions docker-compose.ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
version: "3.8"
services:
test:
build:
context: .
args:
RAILS_ENV: "test"
BUNDLE_EXTRA_GEM_GROUPS: "test"
command: bundle exec rake
ports:
- "3000:3000"
depends_on:
- test-db
env_file:
- .env.test
environment:
DATABASE_URL: postgres://postgres:password@test-db:5432/buy-for-your-school-test
DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL: "true"
networks:
- test

test-db:
image: postgres
volumes:
- db-data:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_HOST_AUTH_METHOD: trust
networks:
- test

networks:
test:
volumes:
db-data:
39 changes: 39 additions & 0 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
version: "3.8"
services:
test:
build:
context: .
args:
RAILS_ENV: "test"
command: bundle exec rake
ports:
- "3000:3000"
depends_on:
- test-db
env_file:
- .env.test
environment:
DATABASE_URL: postgres://postgres:password@test-db:5432/buy-for-your-school-test
DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL: "true"
volumes:
- .:/srv/app
tty: true
stdin_open: true
networks:
- test

test-db:
image: postgres
volumes:
- db-data:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_HOST_AUTH_METHOD: trust
networks:
- test

networks:
test:
volumes:
db-data:
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "3.7"
version: "3.8"
services:
web:
build:
Expand All @@ -8,7 +8,7 @@ services:
command: bundle exec rails s -p 3000 -b '0.0.0.0'
ports:
- "3000:3000"
links:
depends_on:
- db
env_file:
- .env.development.local
Expand Down
8 changes: 8 additions & 0 deletions script/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

# script/server: Launch the application and any extra required processes
# locally.

set -e

bundle exec rake

0 comments on commit ee53981

Please sign in to comment.