From 84c048a8d66e43fa2c7bb2aefd510698452a983e Mon Sep 17 00:00:00 2001 From: Kevin Date: Mon, 12 Aug 2024 19:22:38 +0100 Subject: [PATCH 1/3] WIP --- .github/workflows/ci.yml | 20 +++++++++++-------- Gemfile | 1 + Gemfile.lock | 3 +++ config/database.yml | 2 +- config/environments/test.rb | 5 +++-- db/schema.rb | 2 +- .../notifications/flag_submission_spec.rb | 2 +- spec/support/capybara.rb | 4 ++-- 8 files changed, 24 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3fa1c81a96..141d158a0e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,11 +44,11 @@ jobs: services: postgres: image: postgres:15.7 + ports: + - 5432:5432 env: POSTGRES_USER: postgres - POSTGRES_DB: theodinproject_test - POSTGRES_PASSWORD: "password" - ports: ["5432:5432"] + POSTGRES_PASSWORD: postgres redis: image: redis @@ -82,14 +82,18 @@ jobs: bin/rails css:build - name: Setup test database + run: | + bundle exec rake parallel:create + bundle exec rake parallel:load_schema + bundle exec rake parallel:migrate env: RAILS_ENV: test - DATABASE_URL: postgres://postgres:password@localhost:5432/theodinproject_test - run: | - bin/rails db:schema:load + POSTGRES_USERNAME: postgres + POSTGRES_PASSWORD: postgres - name: Run tests env: RAILS_ENV: test - DATABASE_URL: postgres://postgres:password@localhost:5432/theodinproject_test - run: bin/rspec + POSTGRES_USERNAME: postgres + POSTGRES_PASSWORD: postgres + run: bundle exec rake parallel:spec diff --git a/Gemfile b/Gemfile index 51382009b6..8b799101eb 100644 --- a/Gemfile +++ b/Gemfile @@ -68,6 +68,7 @@ end group :development, :test do gem 'dotenv-rails', '~> 3.1' + gem 'parallel_tests', '~> 4.7' gem 'rspec-rails', '~> 6.1' end diff --git a/Gemfile.lock b/Gemfile.lock index d665bce4a0..746920c70d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -346,6 +346,8 @@ GEM orm_adapter (0.5.0) pagy (6.5.0) parallel (1.26.1) + parallel_tests (4.7.1) + parallel parser (3.3.4.2) ast (~> 2.4.1) racc @@ -630,6 +632,7 @@ DEPENDENCIES omniauth-google-oauth2 (~> 1.1.1) omniauth-rails_csrf_protection (~> 1.0) pagy (~> 6.2) + parallel_tests (~> 4.7) pg (~> 1.5) propshaft (~> 0.9) public_activity (~> 3.0) diff --git a/config/database.yml b/config/database.yml index ff6383a515..d545c48832 100644 --- a/config/database.yml +++ b/config/database.yml @@ -20,7 +20,7 @@ development: &development # DO NOT PUT A REAL USERNAME AND PASSWORD IN THIS FILE test: &test <<: *default - database: <%= ENV['POSTGRES_TEST_DB'] || 'theodinproject_test' %> + database: theodinproject_test<%= ENV['TEST_ENV_NUMBER'] %> production: adapter: postgresql diff --git a/config/environments/test.rb b/config/environments/test.rb index cf1a5b3814..6ce024f720 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -40,13 +40,14 @@ config.action_mailer.perform_caching = false + test_port = 3001 + ENV['TEST_ENV_NUMBER'].to_i config.action_mailer.default_url_options = { host: ENV.fetch('HOST', 'localhost'), - port: ENV.fetch('HOST_PORT', 3001) + port: ENV.fetch('HOST_PORT', test_port) } routes.default_url_options = { host: ENV.fetch('HOST', 'localhost'), - port: ENV.fetch('HOST_PORT', 3001) + port: ENV.fetch('HOST_PORT', test_port) } # Tell Action Mailer not to deliver emails to the real world. diff --git a/db/schema.rb b/db/schema.rb index c235e9dff3..c5b8a5018d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_08_06_175334) do +ActiveRecord::Schema[7.1].define(version: 2024_08_06_175334) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" diff --git a/spec/services/notifications/flag_submission_spec.rb b/spec/services/notifications/flag_submission_spec.rb index 690c2d0756..06a89a3dd9 100644 --- a/spec/services/notifications/flag_submission_spec.rb +++ b/spec/services/notifications/flag_submission_spec.rb @@ -22,7 +22,7 @@ notification_message = "OdinUser has flagged a submission on #{flagged_submission.lesson.title}\n" \ "Reason: inappropriate\n" \ "Extra: I find it offensive\n" \ - 'Resolve the flag here: http://localhost:3001/admin/flags/120' + "Resolve the flag here: http://localhost:#{Rails.application.routes.default_url_options[:port]}/admin/flags/120" expect(notification.message).to eq notification_message end diff --git a/spec/support/capybara.rb b/spec/support/capybara.rb index 457d77fcc9..027379fb69 100644 --- a/spec/support/capybara.rb +++ b/spec/support/capybara.rb @@ -6,8 +6,8 @@ Capybara.configure do |config| config.test_id = 'data-test' config.automatic_label_click = true - config.server_port = 3001 - config.app_host = 'http://localhost:3001' + config.server_port = 3001 + ENV['TEST_ENV_NUMBER'].to_i + config.app_host = "http://localhost:#{config.server_port}" end Capybara.singleton_class.prepend(Module.new do From 58c3522456f09336d378ed893be3cfcb8aa971aa Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 14 Aug 2024 08:26:06 +0100 Subject: [PATCH 2/3] WIP: Spike using a matrix --- .github/workflows/ci.yml | 12 +++++++++++- bin/ci_spec | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100755 bin/ci_spec diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 141d158a0e..92686b1585 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -41,6 +41,16 @@ jobs: tests: name: Tests runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + # Set N number of parallel jobs you want to run tests on. + # Use higher number if you have slow tests to split them on more parallel jobs. + # Remember to update ci_node_index below to 0..N-1 + ci_node_total: [4] + # set N-1 indexes for parallel jobs + # When you run 2 parallel jobs then first job will have index 0, the second job will have index 1 etc + ci_node_index: [0, 1, 2, 3] services: postgres: image: postgres:15.7 @@ -96,4 +106,4 @@ jobs: RAILS_ENV: test POSTGRES_USERNAME: postgres POSTGRES_PASSWORD: postgres - run: bundle exec rake parallel:spec + run: ./bin/ci_spec diff --git a/bin/ci_spec b/bin/ci_spec new file mode 100755 index 0000000000..ca5648aa98 --- /dev/null +++ b/bin/ci_spec @@ -0,0 +1,15 @@ +#!/usr/bin/env ruby + +# This script is used to run tests in parallel in Github Actions. +# parallel_test test -n 8 --only-group 1,2 + +CI_RUNNER_PROCESS_COUNT = 2 + +parallel_tests_is_one_indexed = 1 +node_index = ENV['CI_NODE_INDEX'].to_i + +groups = "#{node_index * CI_RUNNER_PROCESS_COUNT + parallel_tests_is_one_indexed },#{node_index * CI_RUNNER_PROCESS_COUNT + parallel_tests_is_one_indexed + 1}" + +total_parallelism = CI_RUNNER_PROCESS_COUNT * ENV['CI_NODE_TOTAL'].to_i + +exec "bundle exec parallel_test ./spec -t rspec -n #{total_parallelism} --only-group #{groups}" From ce001dd5043de60ff069068f3d0ef6437b3df138 Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 14 Aug 2024 09:17:22 +0100 Subject: [PATCH 3/3] WIP: Consoldate env vars --- .github/workflows/ci.yml | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 92686b1585..faedbe98b0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,6 +64,12 @@ jobs: image: redis ports: ["6379:6379"] + env: + RAILS_ENV: test + POSTGRES_USERNAME: postgres + POSTGRES_PASSWORD: postgres + SKIP_YARN_INSTALL: true + steps: - name: Checkout code uses: actions/checkout@v4 @@ -84,9 +90,6 @@ jobs: yarn install - name: Build assets - env: - RAILS_ENV: test - SKIP_YARN_INSTALL: true run: | bin/rails javascript:build bin/rails css:build @@ -96,14 +99,6 @@ jobs: bundle exec rake parallel:create bundle exec rake parallel:load_schema bundle exec rake parallel:migrate - env: - RAILS_ENV: test - POSTGRES_USERNAME: postgres - POSTGRES_PASSWORD: postgres - name: Run tests - env: - RAILS_ENV: test - POSTGRES_USERNAME: postgres - POSTGRES_PASSWORD: postgres run: ./bin/ci_spec