diff --git a/.github/actions/test_ruby_gem_uploads/Gemfile b/.github/actions/test_ruby_gem_uploads/Gemfile index b5732cf4..027aa6dc 100644 --- a/.github/actions/test_ruby_gem_uploads/Gemfile +++ b/.github/actions/test_ruby_gem_uploads/Gemfile @@ -3,3 +3,8 @@ source 'https://rubygems.org' gem 'rspec' +gem 'knapsack_pro' +gem 'rake' +gem 'rake-compiler' +gem 'logger' +gem 'rspec_trunk_flaky_tests', :path => '../../../rspec-trunk-flaky-tests' diff --git a/.github/actions/test_ruby_gem_uploads/README.md b/.github/actions/test_ruby_gem_uploads/README.md new file mode 100644 index 00000000..d16525ef --- /dev/null +++ b/.github/actions/test_ruby_gem_uploads/README.md @@ -0,0 +1,40 @@ +# Running Ruby Tests Locally + +If you want to locally run the ruby smoke tests that are run in CI, run: + +From `cd rspec-trunk-flaky-tests`: + +1. `cd rspec-trunk-flaky-tests` +2. `bundle install` +3. `bundle exec rake build` +4. `cd ../.github/actions/test_ruby_gem_uploads` +5. Verify [Gemfile](./Gemfile) points to the built gem, i.e. `gem 'rspec_trunk_flaky_tests', :path => '../../../rspec-trunk-flaky-tests'` +6. Run `bundle exec rspec spec/variant_quarantine_spec.rb --format documentation` + +After `cd .github/actions/test_ruby_gem_uploads` + +See more in the [rspec README.md](../../../rspec-trunk-flaky-tests/README.md). + +## Knapsack Pro + +Repeat steps 1-5 above, and run: + +```bash +export KNAPSACK_PRO_CI_NODE_BUILD_ID=$(openssl rand -base64 32) +export KNAPSACK_PRO_TEST_DIR=spec +export KNAPSACK_PRO_TEST_FILE_PATTERN="**/*.rb" +export KNAPSACK_PRO_PROJECT_DIR=. +export KNAPSACK_PRO_REPOSITORY_ADAPTER=git +export KNAPSACK_PRO_LOG_LEVEL=debug +export KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC="" +export KNAPSACK_PRO_CI_NODE_TOTAL=1 +export KNAPSACK_PRO_CI_NODE_INDEX=0 +export KNAPSACK_PRO_FIXED_QUEUE_SPLIT=false + +bundle exec rake "knapsack_pro:queue:rspec:initialize" +bundle exec rake "knapsack_pro:queue:rspec" +``` + +### Reference + +- [More information about Queue Mode](https://docs.knapsackpro.com/ruby/queue-mode/) diff --git a/.github/actions/test_ruby_gem_uploads/Rakefile b/.github/actions/test_ruby_gem_uploads/Rakefile new file mode 100644 index 00000000..8aa8ab52 --- /dev/null +++ b/.github/actions/test_ruby_gem_uploads/Rakefile @@ -0,0 +1,5 @@ +require 'rake/testtask' +require 'rake/extensiontask' +require 'knapsack_pro' +require 'rspec/core/rake_task' +KnapsackPro.load_tasks if defined?(KnapsackPro) \ No newline at end of file diff --git a/.github/actions/test_ruby_gem_uploads/action.yaml b/.github/actions/test_ruby_gem_uploads/action.yaml index cde857a4..cc2ca0a0 100644 --- a/.github/actions/test_ruby_gem_uploads/action.yaml +++ b/.github/actions/test_ruby_gem_uploads/action.yaml @@ -24,6 +24,9 @@ inputs: platform: description: The platform that the gem was built for required: true + knapsack-pro-test-suite-token-rspec: + description: Optional Knapsack Pro test suite token for RSpec + required: true runs: using: composite @@ -63,8 +66,8 @@ runs: gem unpack "${GEM_FILE}" --target=rspec_trunk_flaky_tests mv rspec_trunk_flaky_tests/rspec_trunk_flaky_tests*/lib rspec_trunk_flaky_tests/lib bundle install + sed -i "s|gem 'rspec_trunk_flaky_tests'.*|gem 'rspec_trunk_flaky_tests', '${GEM_VERSION}', :path => './rspec_trunk_flaky_tests'|" Gemfile bundle lock --remove-platform ruby || true - echo "gem 'rspec_trunk_flaky_tests', '${GEM_VERSION}', :path => './rspec_trunk_flaky_tests'" >> Gemfile bundle install --gemfile Gemfile --local - name: Run regular tests @@ -117,3 +120,56 @@ runs: TRUNK_ORG_URL_SLUG: ${{ inputs.trunk-org-slug }} TRUNK_API_TOKEN: ${{ inputs.trunk-token }} TRUNK_VARIANT: smoke-test-variant + + - name: Run knapsack_pro queue and verify a single local bundle + shell: bash + if: inputs.knapsack-pro-test-suite-token-rspec != '' + working-directory: ${{ github.action_path }} + env: + KNAPSACK_PRO_TEST_DIR: spec + # Required when using queue intialization + KNAPSACK_PRO_TEST_FILE_PATTERN: spec/*.rb + KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC: ${{ inputs.knapsack-pro-test-suite-token-rspec }} + KNAPSACK_PRO_CI_NODE_TOTAL: 1 + KNAPSACK_PRO_CI_NODE_INDEX: 0 + # https://docs.knapsackpro.com/ruby/queue-mode/#dynamic-split-vs-fixed-split + KNAPSACK_PRO_FIXED_QUEUE_SPLIT: false + TRUNK_API_TOKEN: ${{ inputs.trunk-token }} + TRUNK_ORG_URL_SLUG: ${{ inputs.trunk-org-slug }} + TRUNK_PUBLIC_API_ADDRESS: ${{ inputs.trunk-public-api-address }} + TRUNK_LOCAL_UPLOAD_DIR: bundles + run: | + set -euo pipefail + rm -rf "${TRUNK_LOCAL_UPLOAD_DIR}" + mkdir -p "${TRUNK_LOCAL_UPLOAD_DIR}" + set +e + # Initialize is not required, but some custom setups will use it + bundle exec rake "knapsack_pro:queue:rspec:initialize" + + FILE_COUNT=$(find "${TRUNK_LOCAL_UPLOAD_DIR}" -maxdepth 1 -type f 2>/dev/null | wc -l) + if [ "${FILE_COUNT}" -ne 0 ]; then + echo "knapsack_pro manual queue initialization should not trigger an upload" + ls -la "${TRUNK_LOCAL_UPLOAD_DIR}" + exit 1 + fi + + OUTPUT_FILE=$(mktemp) + bundle exec rake "knapsack_pro:queue:rspec" 2>&1 | tee "${OUTPUT_FILE}" + set -e + OUTPUT=$(cat "${OUTPUT_FILE}") + rm -f "${OUTPUT_FILE}" + + FILE_COUNT=$(find "${TRUNK_LOCAL_UPLOAD_DIR}" -maxdepth 1 -type f 2>/dev/null | wc -l) + if [ "${FILE_COUNT}" -ne 1 ]; then + echo "knapsack_pro execution run should trigger an upload" + ls -la "${TRUNK_LOCAL_UPLOAD_DIR}" + exit 1 + fi + + REPORT_COUNT=$(echo "${OUTPUT}" | grep -c "Local Flaky tests report generated" || echo "0") + if [ "${REPORT_COUNT}" -ne 1 ]; then + echo "Expected 'Local Flaky tests report generated' to appear exactly once, found ${REPORT_COUNT} times" + exit 1 + fi + + echo "Knapsack run correctly generated exactly one bundle" diff --git a/.github/actions/test_ruby_gem_uploads/spec/spec_helper.rb b/.github/actions/test_ruby_gem_uploads/spec/spec_helper.rb index c4abdd50..87c9e5d4 100644 --- a/.github/actions/test_ruby_gem_uploads/spec/spec_helper.rb +++ b/.github/actions/test_ruby_gem_uploads/spec/spec_helper.rb @@ -1,6 +1,11 @@ # frozen_string_literal: true require 'trunk_spec_helper' +if ENV['KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC'] + # Enable Knapsack queueing when CI provides a suite token + require 'knapsack_pro' + KnapsackPro::Adapters::RSpecAdapter.bind +end # This file was generated by the `rspec --init` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # The generated `.rspec` file contains `--require spec_helper` which will cause diff --git a/.github/workflows/release_ruby_gem.yml b/.github/workflows/release_ruby_gem.yml index 547b1461..663f8d68 100644 --- a/.github/workflows/release_ruby_gem.yml +++ b/.github/workflows/release_ruby_gem.yml @@ -126,6 +126,7 @@ jobs: trunk-token: ${{ secrets.TRUNK_STAGING_ORG_API_TOKEN }} platform: ${{ matrix.platform.name }} artifact-pattern: cross-gem-${{ matrix.platform.name }} + knapsack-pro-test-suite-token-rspec: ${{ secrets.KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC }} publish_ruby_gem: runs-on: ubuntu-latest diff --git a/.github/workflows/smoke_test.yml b/.github/workflows/smoke_test.yml index e43b2f14..987f1ef1 100644 --- a/.github/workflows/smoke_test.yml +++ b/.github/workflows/smoke_test.yml @@ -127,6 +127,7 @@ jobs: trunk-org-slug: trunk-staging-org platform: x86_64-linux artifact-pattern: "" + knapsack-pro-test-suite-token-rspec: ${{ secrets.KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC }} test_ruby_gem_production: name: Test Ruby gem on production @@ -151,6 +152,7 @@ jobs: trunk-org-slug: trunk platform: x86_64-linux artifact-pattern: "" + knapsack-pro-test-suite-token-rspec: ${{ secrets.KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC }} production-slack-workflow-status: if: always() && github.ref == 'refs/heads/main' && (needs.build_cli.outputs.production-success != 'true' || needs.test_ruby_gem_production.result != 'success') diff --git a/.github/workflows/smoke_test_main.yml b/.github/workflows/smoke_test_main.yml index 01e3ac23..0ff067bf 100644 --- a/.github/workflows/smoke_test_main.yml +++ b/.github/workflows/smoke_test_main.yml @@ -228,6 +228,7 @@ jobs: trunk-org-slug: trunk-staging-org platform: x86_64-linux artifact-pattern: "" + knapsack-pro-test-suite-token-rspec: ${{ secrets.KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC }} test_ruby_gem_production: name: Test Ruby gem on production @@ -252,6 +253,7 @@ jobs: trunk-org-slug: trunk platform: x86_64-linux artifact-pattern: "" + knapsack-pro-test-suite-token-rspec: ${{ secrets.KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC }} production-slack-workflow-status: if: always() && github.ref == 'refs/heads/main' && (needs.test_cli.result != 'success' || needs.test_ruby_gem_production.result != 'success') diff --git a/rspec-trunk-flaky-tests/lib/trunk_spec_helper.rb b/rspec-trunk-flaky-tests/lib/trunk_spec_helper.rb index 5e12c49a..8e859014 100644 --- a/rspec-trunk-flaky-tests/lib/trunk_spec_helper.rb +++ b/rspec-trunk-flaky-tests/lib/trunk_spec_helper.rb @@ -65,7 +65,8 @@ def knapsack_detector_mode? def knapsack_detector_command? command_line = "#{$PROGRAM_NAME} #{ARGV.join(' ')}".strip - command_line.include?('knapsack_pro:rspec_test_example_detector') + command_line.include?('knapsack_pro:rspec_test_example_detector') || + command_line.include?('knapsack_pro:queue:rspec:initialize') end def trunk_disabled