From 837dffddd0c57d9be966e2215c1a57fc26c55f25 Mon Sep 17 00:00:00 2001 From: Douwe Osinga Date: Fri, 20 Oct 2023 10:02:14 +0200 Subject: [PATCH 01/11] chore: added docker compose to make testing more consistent --- Dockerfile | 17 ++++++++++++ docker-compose.yml | 54 ++++++++++++++++++++++++++++++++++++++ spec/apps/dummy/bin/bundle | 0 spec/apps/dummy/bin/rails | 0 spec/apps/dummy/bin/rake | 0 spec/apps/dummy/bin/setup | 0 6 files changed, 71 insertions(+) create mode 100644 Dockerfile create mode 100644 docker-compose.yml mode change 100755 => 100644 spec/apps/dummy/bin/bundle mode change 100755 => 100644 spec/apps/dummy/bin/rails mode change 100755 => 100644 spec/apps/dummy/bin/rake mode change 100755 => 100644 spec/apps/dummy/bin/setup diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8ccf8a0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM ruby:3.2.0 + +WORKDIR /app + +ADD . /app + +# Install nodejs +RUN apt-get update -qq && apt-get install -y nodejs postgresql-client + +# Update bundler to correct version +RUN gem install bundler:2.4.19 + +# Install any needed packages +RUN bundle install + +# Use port 4000 (change this if oZone ever uses it) +EXPOSE 4000 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..0dc3f99 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,54 @@ +version: '2.3' +services: + herokuPostgresql: + image: postgres:13 # downgraded to 13 because 'pg_dump' is of version 13.11_something in the scimit container. + ports: + - "5433:5432" + environment: + - POSTGRES_PASSWORD=Sup3rSecr1tPasw8rd + - POSTGRES_USER=postgres + - PGDATA=/var/lib/postgresql/data/pgdata + volumes: + - postgresql:/var/lib/postgresql/data + + scimitar_image: + build: ./ + image: scimitar_backend + + scimitar_base: + image: scimitar_backend + volumes: + - .:/app + environment: + - REDIS_URL=redis://herokuRedis:6379/0 # was /1, doesn't matter? + tty: true + + scimitar: + extends: + service: scimitar_base + depends_on: + - herokuPostgresql + links: + - herokuPostgresql + command: 'bundle exec rails s -p 4000 -b 0.0.0.0' + environment: + - PORT=4000 + ports: + - "4000:4000" + extra_hosts: + - "host.docker.internal:host-gateway" + + test: + extends: + service: scimitar_base + depends_on: + - herokuPostgresql + command: './spec/apps/dummy/bin/test' + environment: + - RAILS_ENV=test + - DATABASE_URL=postgresql://postgres:Sup3rSecr1tPasw8rd@herokuPostgresql:5432/scimit_test + - RAILS_MASTER_KEY + +volumes: + postgresql: + redis: diff --git a/spec/apps/dummy/bin/bundle b/spec/apps/dummy/bin/bundle old mode 100755 new mode 100644 diff --git a/spec/apps/dummy/bin/rails b/spec/apps/dummy/bin/rails old mode 100755 new mode 100644 diff --git a/spec/apps/dummy/bin/rake b/spec/apps/dummy/bin/rake old mode 100755 new mode 100644 diff --git a/spec/apps/dummy/bin/setup b/spec/apps/dummy/bin/setup old mode 100755 new mode 100644 From 36e10927ad7725acc7f5e9aa9b19f1a79a47dcd7 Mon Sep 17 00:00:00 2001 From: Douwe Osinga Date: Fri, 20 Oct 2023 10:02:28 +0200 Subject: [PATCH 02/11] chore: added test script to ease development --- spec/apps/dummy/bin/test | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 spec/apps/dummy/bin/test diff --git a/spec/apps/dummy/bin/test b/spec/apps/dummy/bin/test new file mode 100644 index 0000000..c70b137 --- /dev/null +++ b/spec/apps/dummy/bin/test @@ -0,0 +1,7 @@ +#!/usr/bin/env sh +set -e + +cd ./spec/app/dummy +RAILS_ENV=test bundle exec rake db:drop db:create db:migrate +cd ../../.. +bundle exec rspec \ No newline at end of file From 5ecec36b4f553f8ae86735d74e13a0aa5f0b63a6 Mon Sep 17 00:00:00 2001 From: Douwe Osinga Date: Fri, 20 Oct 2023 10:13:06 +0200 Subject: [PATCH 03/11] chore: updated test file to work --- spec/apps/dummy/bin/test | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/spec/apps/dummy/bin/test b/spec/apps/dummy/bin/test index c70b137..c926ed3 100644 --- a/spec/apps/dummy/bin/test +++ b/spec/apps/dummy/bin/test @@ -1,7 +1,11 @@ #!/usr/bin/env sh set -e -cd ./spec/app/dummy +# go to dummy app (where the bundle and rake files are) +cd ./spec/apps/dummy +echo "== Creating fresh database... ==" RAILS_ENV=test bundle exec rake db:drop db:create db:migrate -cd ../../.. +cd ../../.. # nagivate back to root of project + +echo "== Running tests... ==" bundle exec rspec \ No newline at end of file From 9bc7b125e2e6fe45b653fa1a8a57ce46e03c40c0 Mon Sep 17 00:00:00 2001 From: Douwe Osinga Date: Tue, 24 Oct 2023 09:20:31 +0200 Subject: [PATCH 04/11] made postgresql container non-heroku specific, removed unnecessary base, image, dev environment --- docker-compose.yml | 40 ++++++---------------------------------- 1 file changed, 6 insertions(+), 34 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 0dc3f99..f3e5984 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,9 +1,9 @@ version: '2.3' services: - herokuPostgresql: - image: postgres:13 # downgraded to 13 because 'pg_dump' is of version 13.11_something in the scimit container. + postgresql: + image: postgres:15 ports: - - "5433:5432" + - 5432 environment: - POSTGRES_PASSWORD=Sup3rSecr1tPasw8rd - POSTGRES_USER=postgres @@ -11,44 +11,16 @@ services: volumes: - postgresql:/var/lib/postgresql/data - scimitar_image: + test: build: ./ - image: scimitar_backend - - scimitar_base: - image: scimitar_backend volumes: - .:/app - environment: - - REDIS_URL=redis://herokuRedis:6379/0 # was /1, doesn't matter? - tty: true - - scimitar: - extends: - service: scimitar_base - depends_on: - - herokuPostgresql - links: - - herokuPostgresql - command: 'bundle exec rails s -p 4000 -b 0.0.0.0' - environment: - - PORT=4000 - ports: - - "4000:4000" - extra_hosts: - - "host.docker.internal:host-gateway" - - test: - extends: - service: scimitar_base depends_on: - - herokuPostgresql + - postgresql command: './spec/apps/dummy/bin/test' environment: - RAILS_ENV=test - - DATABASE_URL=postgresql://postgres:Sup3rSecr1tPasw8rd@herokuPostgresql:5432/scimit_test - - RAILS_MASTER_KEY + - DATABASE_URL=postgresql://postgres:Sup3rSecr1tPasw8rd@postgresql:5432/scimitar-test volumes: postgresql: - redis: From 24e6663a052de4ed0e7b63b9d1d2cc9ba5d7b70b Mon Sep 17 00:00:00 2001 From: Douwe Osinga Date: Tue, 24 Oct 2023 09:20:47 +0200 Subject: [PATCH 05/11] updated docker ruby version, removed unused open port --- Dockerfile | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8ccf8a0..b8e791a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM ruby:3.2.0 +FROM ruby:3.2.2 WORKDIR /app @@ -12,6 +12,3 @@ RUN gem install bundler:2.4.19 # Install any needed packages RUN bundle install - -# Use port 4000 (change this if oZone ever uses it) -EXPOSE 4000 From 4e30922fae5bf5cf65e45059e1eea29b37774f6b Mon Sep 17 00:00:00 2001 From: Douwe Osinga Date: Tue, 24 Oct 2023 09:20:55 +0200 Subject: [PATCH 06/11] simplified test script --- spec/apps/dummy/bin/test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/apps/dummy/bin/test b/spec/apps/dummy/bin/test index c926ed3..ebe8b44 100644 --- a/spec/apps/dummy/bin/test +++ b/spec/apps/dummy/bin/test @@ -4,7 +4,7 @@ set -e # go to dummy app (where the bundle and rake files are) cd ./spec/apps/dummy echo "== Creating fresh database... ==" -RAILS_ENV=test bundle exec rake db:drop db:create db:migrate +bundle exec rake db:reset cd ../../.. # nagivate back to root of project echo "== Running tests... ==" From afb9480241e347fad761f5516609d9431193a15e Mon Sep 17 00:00:00 2001 From: Douwe Osinga Date: Thu, 26 Oct 2023 09:46:45 +0200 Subject: [PATCH 07/11] added `pry` gem for testing ..to be able to do `binding.pry`, see https://github.com/pry/pry --- Gemfile | 5 +++++ Gemfile.lock | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/Gemfile b/Gemfile index bbc2b18..a050bd3 100644 --- a/Gemfile +++ b/Gemfile @@ -5,4 +5,9 @@ source "https://rubygems.org" # gem 'sdoc', :git => 'https://github.com/pond/sdoc.git', :branch => 'master' +group :test do + gem 'pry' + gem 'pry-nav' +end + gemspec diff --git a/Gemfile.lock b/Gemfile.lock index f50ac0a..49073f2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -82,6 +82,7 @@ GEM tzinfo (~> 2.0) builder (3.2.4) byebug (11.1.3) + coderay (1.1.3) concurrent-ruby (1.2.2) crass (1.0.6) date (3.3.3) @@ -121,6 +122,11 @@ GEM mini_portile2 (~> 2.8.0) racc (~> 1.4) pg (1.4.6) + pry (0.14.2) + coderay (~> 1.1) + method_source (~> 1.0) + pry-nav (1.0.0) + pry (>= 0.9.10, < 0.15) psych (5.1.0) stringio racc (1.6.2) @@ -198,6 +204,8 @@ DEPENDENCIES byebug (~> 11.1) doggo (~> 1.3) pg (~> 1.4) + pry + pry-nav rake (~> 13.0) rdoc (~> 6.5) rspec-rails (~> 6.0) From 43cce708667896bd50c93d9f2238e220d0956b67 Mon Sep 17 00:00:00 2001 From: Douwe Osinga Date: Tue, 31 Oct 2023 09:22:29 +0100 Subject: [PATCH 08/11] moved test file to `bin/test` --- {spec/apps/dummy/bin => bin}/test | 0 docker-compose.yml | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename {spec/apps/dummy/bin => bin}/test (100%) diff --git a/spec/apps/dummy/bin/test b/bin/test similarity index 100% rename from spec/apps/dummy/bin/test rename to bin/test diff --git a/docker-compose.yml b/docker-compose.yml index f3e5984..8b75b03 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,7 +17,7 @@ services: - .:/app depends_on: - postgresql - command: './spec/apps/dummy/bin/test' + command: 'bin/test' environment: - RAILS_ENV=test - DATABASE_URL=postgresql://postgres:Sup3rSecr1tPasw8rd@postgresql:5432/scimitar-test From af846c4b7364375faf2cbf60101236defef878f2 Mon Sep 17 00:00:00 2001 From: Douwe Osinga Date: Tue, 31 Oct 2023 09:22:56 +0100 Subject: [PATCH 09/11] run `apt-get fclean` after `apt-get install` to save resources in container --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index b8e791a..934558f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,8 @@ WORKDIR /app ADD . /app # Install nodejs -RUN apt-get update -qq && apt-get install -y nodejs postgresql-client +RUN apt-get update -qq && apt-get install -y nodejs postgresql-client \ + && apt-get clean # Update bundler to correct version RUN gem install bundler:2.4.19 From 488331b5aa2cb6713ab623e25ee75dbf422a5615 Mon Sep 17 00:00:00 2001 From: Douwe Osinga Date: Tue, 31 Oct 2023 09:51:43 +0100 Subject: [PATCH 10/11] updated bundler version for docker the `docker compose build` gave a warning about the wrong bundle version being used to bundle the project, so changed that to make the setup a little smoother --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 934558f..95124d0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,7 @@ RUN apt-get update -qq && apt-get install -y nodejs postgresql-client \ && apt-get clean # Update bundler to correct version -RUN gem install bundler:2.4.19 +RUN gem install bundler:2.4.3 # Install any needed packages RUN bundle install From 5c48fcf7ef498dd50189ee9f2d8f985d888c243d Mon Sep 17 00:00:00 2001 From: Douwe Osinga Date: Tue, 31 Oct 2023 09:57:30 +0100 Subject: [PATCH 11/11] Updated README to feature Docker Compose testing option --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index ed63931..d1eaba1 100644 --- a/README.md +++ b/README.md @@ -621,6 +621,11 @@ bundle install ### Tests +For testing, two main options are available. +The first option is running the project locally. This is also the recommended way, as running the tests on a variety of setups and platforms increases the chance of finding platform- or setup-specific bugs. +The second option is utilising the existing Docker Compose setup provided in the project. You can use this if getting the project to work locally is hard or not feasible. + +#### Testing on your machine You will need to have PostgreSQL running. This database is chosen for tests to prove case-insensitive behaviour via detection of ILIKE in generated queries. Using SQLite would have resulted in a more conceptually self-contained test suite, but SQLite is case-insensitive by default and uses "LIKE" either way, making it hard to "see" if the query system is doing the right thing. After `bundle install` and with PostgreSQL up, set up the test database with: @@ -639,6 +644,16 @@ bundle exec rspec You can get an idea of arising test coverage by opening `coverage/index.html` in your preferred web browser. +#### Testing with Docker (Compose) + +In order to be able to utilise the Docker Compose, you will need to have Docker installed, with the Compose plugin. For an easy install of Docker, with a GUI and the Compose plugin preinstalled, please refer to [Docker Desktop](https://www.docker.com/products/docker-desktop/). + +In order to set up the image, simply run `docker compose build` in a terminal of your choice, in the root of this project. This will download the required image and install the required libraries. After this is complete, running the tests is as easy as running the command `docker compose up test`. + +As said in the previous section, test coverage may be analysed using `coverage/index.html` after running the project. + +You can also open a raw terminal in this test container by running `docker run --rm test sh`. For more Compose commands, please refer to [the docker compose reference manual](https://docs.docker.com/compose/reference/). + ### Internal documentation Locally generated RDoc HTML seems to contain a more comprehensive and inter-linked set of pages than those available from `rubydoc.info`. You can (re)generate the internal [`rdoc` documentation](https://ruby-doc.org/stdlib-2.4.1/libdoc/rdoc/rdoc/RDoc/Markup.html#label-Supported+Formats) with: