Skip to content

Commit

Permalink
Merge pull request #186 from envato/docker
Browse files Browse the repository at this point in the history
Docker development environment
  • Loading branch information
orien committed May 25, 2020
2 parents 3914bd6 + c5104be commit 4ee0d7f
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 20 deletions.
18 changes: 18 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
*.gem
*.rbc
/.*
!/.rspec
/coverage/
/Dockerfile
/docker-compose*.yml
/pkg/
/tmp/
/profiles/
/_yardoc/
/doc/
/rdoc/
/Gemfile.lock
/log/
/spec/examples.txt
/spec/reports/
/spec/support/database.yml
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM ruby:2.5-alpine

WORKDIR /double_entry

RUN set -ex; \
apk add --no-cache \
build-base \
git \
mariadb-dev \
postgresql-dev \
sqlite-dev \
tzdata \
; \
gem update --system

COPY Gemfile* double_entry.gemspec ./
COPY lib/double_entry/version.rb ./lib/double_entry/version.rb
RUN bundle install

COPY . ./
COPY spec/support/database.example.yml ./spec/support/database.yml
46 changes: 30 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,42 +292,56 @@ See the Github project [issues](https://github.com/envato/double_entry/issues).

## Development Environment Setup

1. Clone this repo.
We're using Docker to provide a convenient and consistent environment for
executing tests during development. This allows engineers to quickly set up
a productive development environment.

```sh
git clone [email protected]:envato/double_entry.git && cd double_entry
```
Note: Most development files are mounted in the Docker container. This
enables engineers to edit files in their favourite editor (on the host
OS) and have the changes immediately available in the Docker container
to be exercised.

One exception to this is the RSpec configuration. Changes to these files will
require a rebuild of the Docker image (step 2).

2. Run the included setup script to install the gem dependencies.
Prerequisites:

* Docker
* Docker Compose
* Git

1. Clone this repo.

```sh
./script/setup.sh
git clone [email protected]:envato/double_entry.git && cd double_entry
```

3. Install MySQL, PostgreSQL and SQLite. We run tests against all three databases.
4. Create a database in MySQL.
2. Build the Docker image we'll use to run tests
```sh
mysql -u root -e 'create database double_entry_test;'
docker-compose build --pull double_entry
```
5. Create a database in PostgreSQL.
3. Startup a container and attach a terminal. This will also start up a
MySQL and Postgres database.
```sh
psql -c 'create database double_entry_test;' -U postgres
docker-compose run --rm double_entry ash
```
6. Specify how the tests should connect to the database
4. Run the tests
```sh
cp spec/support/{database.example.yml,database.yml}
vim spec/support/database.yml
DB=mysql bundle exec rspec
DB=postgres bundle exec rspec
DB=sqlite bundle exec rspec
```
7. Run the tests
5. When finished, exit the container terminal and shut down the databases.
```sh
bundle exec rake
exit
docker-compose down
```
## Contributors
Expand Down
28 changes: 28 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
version: '3'
services:
double_entry:
build: .
environment:
POSTGRES_HOST: postgres
MYSQL_HOST: mysql
volumes:
- ./lib:/double_entry/lib
- ./spec/double_entry:/double_entry/spec/double_entry
- ./spec/double_entry_spec.rb:/double_entry/spec/double_entry_spec.rb
- ./spec/generators:/double_entry/spec/generators
- ./spec/active_record:/double_entry/spec/active_record
- ./spec/performance:/double_entry/spec/performance
- ./script:/double_entry/script
depends_on:
- mysql
- postgres
mysql:
image: mysql
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
MYSQL_DATABASE: double_entry_test
postgres:
image: postgres:alpine
environment:
POSTGRES_HOST_AUTH_METHOD: trust
POSTGRES_DB: double_entry_test
8 changes: 7 additions & 1 deletion script/jack_hammer
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ require 'optparse'
require 'bundler/setup'
require 'active_record'
require 'database_cleaner'
require 'erb'
require 'yaml'

support = File.expand_path("../../spec/support/", __FILE__)

Expand All @@ -25,7 +27,11 @@ if db_engine == 'sqlite'
exit 0
end

ActiveRecord::Base.establish_connection YAML.load_file(File.join(support, "database.yml"))[db_engine]
database_config_file = File.join(__dir__, '..', 'spec', 'support', 'database.yml')
database_config_raw = File.read(database_config_file)
database_config_yaml = ERB.new(database_config_raw).result
database_config = YAML.load(database_config_yaml)
ActiveRecord::Base.establish_connection(database_config[db_engine])
require "#{support}/schema"

lib = File.expand_path('../../lib', __FILE__)
Expand Down
3 changes: 2 additions & 1 deletion spec/support/database.example.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
postgres:
host: localhost
host: <%= ENV.fetch('POSTGRES_HOST') { 'localhost' } %>
adapter: postgresql
encoding: unicode
database: double_entry_test
Expand All @@ -8,6 +8,7 @@ postgres:
password:
min_messages: warning
mysql:
host: <%= ENV.fetch('MYSQL_HOST') { '127.0.0.1' } %>
adapter: mysql2
encoding: utf8
database: double_entry_test
Expand Down
9 changes: 7 additions & 2 deletions spec/support/database.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
require 'active_record'
require 'database_cleaner'
require 'erb'
require 'yaml'

FileUtils.mkdir_p 'tmp'

db_engine = ENV['DB'] || 'mysql'
database_config_file = File.expand_path('../database.yml', __FILE__)
database_config_file = File.join(__dir__, 'database.yml')

raise <<-MSG.strip_heredoc unless File.exist?(database_config_file)
Please configure your spec/support/database.yml file.
See spec/support/database.example.yml'
MSG

ActiveRecord::Base.belongs_to_required_by_default = true if ActiveRecord.version.version >= '5'
ActiveRecord::Base.establish_connection(YAML.load_file(database_config_file)[db_engine])
database_config_raw = File.read(database_config_file)
database_config_yaml = ERB.new(database_config_raw).result
database_config = YAML.load(database_config_yaml)
ActiveRecord::Base.establish_connection(database_config[db_engine])

RSpec.configure do |config|
config.before(:suite) do
Expand Down

0 comments on commit 4ee0d7f

Please sign in to comment.