diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b0555c40d1..07a8c328cd 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -8,7 +8,7 @@ on: pull_request: env: - DISABLE_KNAPSACK: true + DISABLE_KNAPSACK_PRO: false TIMEZONE: UTC COVERAGE: true RAILS_ENV: test @@ -17,7 +17,7 @@ permissions: contents: read jobs: - rspec: + knapsack_rspec_controllers: runs-on: ubuntu-20.04 services: postgres: @@ -33,19 +33,15 @@ jobs: POSTGRES_USER: ofn POSTGRES_PASSWORD: f00d strategy: - matrix: - specs: - - "spec/controllers" - - "spec/models" - - "spec/lib" - - "spec/migrations" - - "spec/serializers" - - "spec/system/admin/[a-o0-9]*" - - "spec/system/admin/[p-z]*" - - "spec/system/consumer/[a-o0-9]*" - - "spec/system/consumer/[p-z]*" - - "engines/*/spec" fail-fast: false + matrix: + # [n] - where the n is a number of parallel jobs you want to run your tests on. + # Use a higher number if you have slow tests to split them between more parallel jobs. + # Remember to update the value of the `ci_node_index` below to (0..n-1). + ci_node_total: [8] + # Indexes for parallel jobs (starting from zero). + # E.g. use [0, 1] for 2 parallel jobs, [0, 1, 2] for 3 parallel jobs, etc. + ci_node_index: [0, 1, 2, 3, 4, 5, 6, 7] steps: - uses: actions/checkout@v3 @@ -72,7 +68,161 @@ jobs: bundle exec rake db:schema:load - name: Run tests - run: bundle exec rspec --profile -- ${{ matrix.specs }} + + env: + KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC: 864ef557d85ea8e603e086c0387d5154 + KNAPSACK_PRO_CI_NODE_TOTAL: ${{ matrix.ci_node_total }} + KNAPSACK_PRO_CI_NODE_INDEX: ${{ matrix.ci_node_index }} + KNAPSACK_PRO_LOG_LEVEL: info + # if you use Knapsack Pro Queue Mode you must set below env variable + # to be able to retry CI build and run previously recorded tests + # https://github.com/KnapsackPro/knapsack_pro-ruby#knapsack_pro_fixed_queue_split-remember-queue-split-on-retry-ci-node + # KNAPSACK_PRO_FIXED_QUEUE_SPLIT: false + # RSpec split test files by test examples feature - it's optional + # https://knapsackpro.com/faq/question/how-to-split-slow-rspec-test-files-by-test-examples-by-individual-it + #KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES: true + KNAPSACK_PRO_TEST_FILE_PATTERN: "spec/controllers/**/*_spec.rb" + + run: | + bundle exec rake knapsack_pro:rspec + + knapsack_rspec_models: + runs-on: ubuntu-20.04 + services: + postgres: + image: postgres:10 + ports: ["5432:5432"] + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + env: + POSTGRES_DB: open_food_network_test + POSTGRES_USER: ofn + POSTGRES_PASSWORD: f00d + strategy: + fail-fast: false + matrix: + # [n] - where the n is a number of parallel jobs you want to run your tests on. + # Use a higher number if you have slow tests to split them between more parallel jobs. + # Remember to update the value of the `ci_node_index` below to (0..n-1). + ci_node_total: [7] + # Indexes for parallel jobs (starting from zero). + # E.g. use [0, 1] for 2 parallel jobs, [0, 1, 2] for 3 parallel jobs, etc. + ci_node_index: [0, 1, 2, 3, 4, 5, 6] + steps: + - uses: actions/checkout@v3 + + - name: Setup redis + uses: supercharge/redis-github-action@1.4.0 + with: + redis-version: 6 + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + bundler-cache: true # runs 'bundle install' and caches installed gems automatically + + - uses: actions/setup-node@v3 + with: + node-version: 16 + + - name: Install JS dependencies + run: yarn install --frozen-lockfile + + - name: Set up database + run: | + bundle exec rake db:create + bundle exec rake db:schema:load + + - name: Run tests + + env: + KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC: 09476e2ce491c12083df62768667c674 + KNAPSACK_PRO_CI_NODE_TOTAL: ${{ matrix.ci_node_total }} + KNAPSACK_PRO_CI_NODE_INDEX: ${{ matrix.ci_node_index }} + KNAPSACK_PRO_LOG_LEVEL: info + # if you use Knapsack Pro Queue Mode you must set below env variable + # to be able to retry CI build and run previously recorded tests + # https://github.com/KnapsackPro/knapsack_pro-ruby#knapsack_pro_fixed_queue_split-remember-queue-split-on-retry-ci-node + # KNAPSACK_PRO_FIXED_QUEUE_SPLIT: false + # RSpec split test files by test examples feature - it's optional + # https://knapsackpro.com/faq/question/how-to-split-slow-rspec-test-files-by-test-examples-by-individual-it + #KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES: true + KNAPSACK_PRO_TEST_FILE_PATTERN: "spec/models/**/*_spec.rb" + + run: | + bundle exec rake knapsack_pro:rspec + + knapsack_rspec_system: + runs-on: ubuntu-20.04 + services: + postgres: + image: postgres:10 + ports: ["5432:5432"] + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + env: + POSTGRES_DB: open_food_network_test + POSTGRES_USER: ofn + POSTGRES_PASSWORD: f00d + strategy: + fail-fast: false + matrix: + # [n] - where the n is a number of parallel jobs you want to run your tests on. + # Use a higher number if you have slow tests to split them between more parallel jobs. + # Remember to update the value of the `ci_node_index` below to (0..n-1). + ci_node_total: [30] + # Indexes for parallel jobs (starting from zero). + # E.g. use [0, 1] for 2 parallel jobs, [0, 1, 2] for 3 parallel jobs, etc. + ci_node_index: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25, 26, 27, 28, 29] + steps: + - uses: actions/checkout@v3 + + - name: Setup redis + uses: supercharge/redis-github-action@1.4.0 + with: + redis-version: 6 + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + bundler-cache: true # runs 'bundle install' and caches installed gems automatically + + - uses: actions/setup-node@v3 + with: + node-version: 16 + + - name: Install JS dependencies + run: yarn install --frozen-lockfile + + - name: Set up database + run: | + bundle exec rake db:create + bundle exec rake db:schema:load + + - name: Run tests + + env: + KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC: ff2456e64c9f2aa5157eb0daf711d3c3 + KNAPSACK_PRO_CI_NODE_TOTAL: ${{ matrix.ci_node_total }} + KNAPSACK_PRO_CI_NODE_INDEX: ${{ matrix.ci_node_index }} + KNAPSACK_PRO_LOG_LEVEL: info + # if you use Knapsack Pro Queue Mode you must set below env variable + # to be able to retry CI build and run previously recorded tests + # https://github.com/KnapsackPro/knapsack_pro-ruby#knapsack_pro_fixed_queue_split-remember-queue-split-on-retry-ci-node + KNAPSACK_PRO_FIXED_QUEUE_SPLIT: true + # RSpec split test files by test examples feature - it's optional + # https://knapsackpro.com/faq/question/how-to-split-slow-rspec-test-files-by-test-examples-by-individual-it + #KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES: true + KNAPSACK_PRO_TEST_FILE_PATTERN: "spec/system/**/*_spec.rb" + + run: | + bundle exec rake knapsack_pro:queue:rspec - name: Archive failed tests screenshots if: failure() @@ -83,7 +233,155 @@ jobs: retention-days: 7 if-no-files-found: ignore - test-the-rest: + knapsack_rspec_engines: + runs-on: ubuntu-20.04 + services: + postgres: + image: postgres:10 + ports: ["5432:5432"] + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + env: + POSTGRES_DB: open_food_network_test + POSTGRES_USER: ofn + POSTGRES_PASSWORD: f00d + strategy: + fail-fast: false + matrix: + # [n] - where the n is a number of parallel jobs you want to run your tests on. + # Use a higher number if you have slow tests to split them between more parallel jobs. + # Remember to update the value of the `ci_node_index` below to (0..n-1). + ci_node_total: [5] + # Indexes for parallel jobs (starting from zero). + # E.g. use [0, 1] for 2 parallel jobs, [0, 1, 2] for 3 parallel jobs, etc. + ci_node_index: [0, 1, 2, 3, 4] + steps: + - uses: actions/checkout@v3 + + - name: Setup redis + uses: supercharge/redis-github-action@1.4.0 + with: + redis-version: 6 + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + bundler-cache: true # runs 'bundle install' and caches installed gems automatically + + - uses: actions/setup-node@v3 + with: + node-version: 16 + + - name: Install JS dependencies + run: yarn install --frozen-lockfile + + - name: Set up database + run: | + bundle exec rake db:create + bundle exec rake db:schema:load + + - name: Run tests + + env: + KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC: 09476e2ce491c12083df62768667c674 + KNAPSACK_PRO_CI_NODE_TOTAL: ${{ matrix.ci_node_total }} + KNAPSACK_PRO_CI_NODE_INDEX: ${{ matrix.ci_node_index }} + KNAPSACK_PRO_LOG_LEVEL: info + # if you use Knapsack Pro Queue Mode you must set below env variable + # to be able to retry CI build and run previously recorded tests + # https://github.com/KnapsackPro/knapsack_pro-ruby#knapsack_pro_fixed_queue_split-remember-queue-split-on-retry-ci-node + # KNAPSACK_PRO_FIXED_QUEUE_SPLIT: false + # RSpec split test files by test examples feature - it's optional + # https://knapsackpro.com/faq/question/how-to-split-slow-rspec-test-files-by-test-examples-by-individual-it + #KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES: true + KNAPSACK_PRO_TEST_FILE_PATTERN: "{spec/lib/**/*_spec.rb, spec/migrations/**/*_spec.rb, spec/serializers/**/*_spec.rb, engines/**/*_spec.rb}" + + run: | + bundle exec rake knapsack_pro:rspec + + - name: Archive failed tests screenshots + if: failure() + uses: actions/upload-artifact@v3 + with: + name: failed-tests-screenshots + path: tmp/capybara/screenshots/*.png + retention-days: 7 + if-no-files-found: ignore + + knapsack_rspec_test_the_rest: + runs-on: ubuntu-20.04 + services: + postgres: + image: postgres:10 + ports: ["5432:5432"] + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + env: + POSTGRES_DB: open_food_network_test + POSTGRES_USER: ofn + POSTGRES_PASSWORD: f00d + strategy: + fail-fast: false + matrix: + # [n] - where the n is a number of parallel jobs you want to run your tests on. + # Use a higher number if you have slow tests to split them between more parallel jobs. + # Remember to update the value of the `ci_node_index` below to (0..n-1). + ci_node_total: [5] + # Indexes for parallel jobs (starting from zero). + # E.g. use [0, 1] for 2 parallel jobs, [0, 1, 2] for 3 parallel jobs, etc. + ci_node_index: [0, 1, 2, 3, 4] + steps: + - uses: actions/checkout@v3 + + - name: Setup redis + uses: supercharge/redis-github-action@1.4.0 + with: + redis-version: 6 + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + bundler-cache: true # runs 'bundle install' and caches installed gems automatically + + - uses: actions/setup-node@v3 + with: + node-version: 16 + + - name: Install JS dependencies + run: yarn install --frozen-lockfile + + - name: Set up database + run: | + bundle exec rake db:create + bundle exec rake db:schema:load + + - name: Run tests + + env: + KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC: e3b8800198d2d89b70c7edbdd85f8fd8 + KNAPSACK_PRO_CI_NODE_TOTAL: ${{ matrix.ci_node_total }} + KNAPSACK_PRO_CI_NODE_INDEX: ${{ matrix.ci_node_index }} + KNAPSACK_PRO_LOG_LEVEL: info + # if you use Knapsack Pro Queue Mode you must set below env variable + # to be able to retry CI build and run previously recorded tests + # https://github.com/KnapsackPro/knapsack_pro-ruby#knapsack_pro_fixed_queue_split-remember-queue-split-on-retry-ci-node + # KNAPSACK_PRO_FIXED_QUEUE_SPLIT: false + # RSpec split test files by test examples feature - it's optional + # https://knapsackpro.com/faq/question/how-to-split-slow-rspec-test-files-by-test-examples-by-individual-it + #KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES: true + KNAPSACK_PRO_TEST_FILE_EXCLUDE_PATTERN: "{engines/**{,/*/**}/*_spec.rb,spec/models/**{,/*/**}/*_spec.rb,spec/controllers/**{,/*/**}/*_spec.rb,spec/serializers/**{,/*/**}/*_spec.rb,spec/lib/**{,/*/**}/*_spec.rb,spec/migrations/**{,/*/**}/*_spec.rb,spec/system/**{,/*/**}/*_spec.rb}" + + + run: | + bundle exec rake knapsack_pro:rspec + + non_knapsack_jest_karma: runs-on: ubuntu-20.04 services: postgres: @@ -122,12 +420,8 @@ jobs: run: | bundle exec rake db:create bundle exec rake db:schema:load - - name: Run JS tests run: bundle exec rake karma:run - name: Run jest tests run: yarn jest - - - name: Run all other tests - run: bundle exec rspec --exclude-pattern "./models/**/*_spec.rb, ./controllers/**/*_spec.rb, ./serializers/**/*_spec.rb, ./lib/**/*_spec.rb, ./migrations/**/*_spec.rb, ./system/**/*_spec.rb" diff --git a/Gemfile b/Gemfile index 7ca4980c28..bbf06ea0b7 100644 --- a/Gemfile +++ b/Gemfile @@ -152,7 +152,7 @@ group :test, :development do gem "factory_bot_rails", '6.2.0', require: false gem 'fuubar', '~> 2.5.1' gem 'json_spec', '~> 1.1.4' - gem 'knapsack', require: false + gem 'knapsack_pro' gem 'letter_opener', '>= 1.4.1' gem 'rspec-rails', ">= 3.5.2" gem 'rspec-retry', require: false diff --git a/Gemfile.lock b/Gemfile.lock index e7363b6c46..596d772b0a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -375,7 +375,7 @@ GEM jsonapi-serializer (2.2.0) activesupport (>= 4.2) jwt (2.5.0) - knapsack (4.0.0) + knapsack_pro (3.4.1) rake launchy (2.5.0) addressable (~> 2.7) @@ -818,7 +818,7 @@ DEPENDENCIES json_spec (~> 1.1.4) jsonapi-serializer jwt (~> 2.3) - knapsack + knapsack_pro letter_opener (>= 1.4.1) listen mime-types diff --git a/Rakefile b/Rakefile index e37edbf5b4..3a8f4a342c 100755 --- a/Rakefile +++ b/Rakefile @@ -8,6 +8,3 @@ require_relative 'config/application' Openfoodnetwork::Application.load_tasks -if !ENV['DISABLE_KNAPSACK'] && defined?(Knapsack) - Knapsack.load_tasks -end diff --git a/bin/knapsack_pro_rspec b/bin/knapsack_pro_rspec new file mode 100755 index 0000000000..65e8608d91 --- /dev/null +++ b/bin/knapsack_pro_rspec @@ -0,0 +1,13 @@ +#!/bin/bash +if [ "$KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC" = "" ]; then + KNAPSACK_PRO_ENDPOINT=https://api-disabled-for-fork.knapsackpro.com \ + KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC=disabled-for-fork \ + KNAPSACK_PRO_MAX_REQUEST_RETRIES=0 \ + bundle exec rake knapsack_pro:rspec # use Regular Mode here always +else + # Regular Mode + bundle exec rake knapsack_pro:rspec + + # or you can use Queue Mode instead of Regular Mode if you like + # bundle exec rake knapsack_pro:queue:rspec +fi \ No newline at end of file diff --git a/spec/base_spec_helper.rb b/spec/base_spec_helper.rb index 008445dd72..eec44cf8a7 100644 --- a/spec/base_spec_helper.rb +++ b/spec/base_spec_helper.rb @@ -24,12 +24,15 @@ Shoulda::Matchers.configure do |config| end end +require 'knapsack_pro' +KnapsackPro::Adapters::RSpecAdapter.bind + # Allow connections to selenium whilst raising errors when connecting to external sites require 'webmock/rspec' WebMock.enable! WebMock.disable_net_connect!( allow_localhost: true, - allow: 'chromedriver.storage.googleapis.com' + allow: ['chromedriver.storage.googleapis.com', 'api.knapsackpro.com'] ) # Requires supporting ruby files with custom matchers and macros, etc, diff --git a/spec/performance/shop_controller_spec.rb b/spec/performance/shop_controller_spec.rb new file mode 100644 index 0000000000..793a84c049 --- /dev/null +++ b/spec/performance/shop_controller_spec.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require 'spec_helper' + +xdescribe ShopController, type: :controller, performance: true do + include FileHelper + + let(:d) { create(:distributor_enterprise) } + let(:enterprise_fee) { create(:enterprise_fee) } + let(:order_cycle) { + create(:simple_order_cycle, distributors: [d], coordinator_fees: [enterprise_fee]) + } + + before do + allow(controller).to receive(:current_distributor) { d } + allow(controller).to receive(:current_order_cycle) { order_cycle } + Spree::Config.currency = 'AUD' + end + + describe "fetching products" do + let(:exchange) { order_cycle.exchanges.to_enterprises(d).outgoing.first } + let(:image) { white_logo_file } + let(:cache_key_patterns) do + [ + 'api\/taxon_serializer\/spree\/taxons', + 'enterprise' + ] + end + + before do + 11.times do + p = create(:simple_product) + p.set_property 'Organic Certified', 'NASAA 12345' + v1 = create(:variant, product: p) + v2 = create(:variant, product: p) + Spree::Image.create! viewable_id: p.master.id, viewable_type: 'Spree::Variant', + attachment: image + + exchange.variants << [v1, v2] + end + end + + it "returns products via json" do + pending("fixing failing performance specs") + results = multi_benchmark(3, cache_key_patterns: cache_key_patterns) do + get :products, xhr: true + expect(response.status).to eq 200 + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c9bee1d80e..4b76dbb9be 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,14 +5,6 @@ require 'base_spec_helper' require 'database_cleaner' require 'view_component/test_helpers' -# This spec_helper.rb is being used by the custom engines in engines/. The engines are not set up to -# use Knapsack, and this provides the option to disable it when running the tests in CI services. -unless ENV['DISABLE_KNAPSACK'] - require 'knapsack' - Knapsack.tracker.config(enable_time_offset_warning: false) unless ENV['CI'] - Knapsack::Adapters::RSpecAdapter.bind -end - Capybara.javascript_driver = :chrome Capybara.default_max_wait_time = 30 Capybara.disable_animation = true diff --git a/spec/support/vcr_setup.rb b/spec/support/vcr_setup.rb index c4af4c19b5..261563fff7 100644 --- a/spec/support/vcr_setup.rb +++ b/spec/support/vcr_setup.rb @@ -9,4 +9,5 @@ VCR.configure do |config| config.configure_rspec_metadata! config.filter_sensitive_data('') { ENV['STRIPE_SECRET_TEST_API_KEY'] } config.filter_sensitive_data('') { ENV['STRIPE_CUSTOMER'] } + config.ignore_hosts('localhost', '127.0.0.1', '0.0.0.0', 'api.knapsackpro.com') end