diff --git a/.github/workflows/rubocop-analysis.yml b/.github/workflows/rubocop-analysis.yml index ad647053..335c9a95 100644 --- a/.github/workflows/rubocop-analysis.yml +++ b/.github/workflows/rubocop-analysis.yml @@ -28,10 +28,6 @@ jobs: with: ruby-version: '3.1.0' - # This step is not necessary if you add the gem to your Gemfile - - name: Install Code Scanning integration - run: bundle add code-scanning-rubocop --skip-install - - name: Install dependencies run: | bundle install diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2dcf20fb..70fa25aa 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -130,17 +130,14 @@ On the host machine, you can use the `docker_tests` Rake task to run the test su ### Run Test Suite -Run the test suite: +Run the test suite (some test cases rely on env `APM_RUBY_TEST_KEY`): ```bash # run tests in a ruby:3.1.0-bullseye container -bundle exec rake docker_tests - -# run tests in a ruby:2.7.5 container -bundle exec rake 'docker_tests[2.7.5]' +bundle exec rake 'docker_tests[,,,APM_RUBY_TEST_KEY=your_service_key]' # run tests in a ruby:3.2-alpine linux/amd64 container -bundle exec rake 'docker_tests[3.2-alpine,,linux/amd64]' +bundle exec rake 'docker_tests[3.2-alpine,,linux/amd64,APM_RUBY_TEST_KEY=your_service_key]' ``` Test logs are written to the project's `log` directory, which is bind mounted and available on the host machine. @@ -159,25 +156,17 @@ In the container, set up the environment: test/test_setup.sh ``` -To run the full suite: +To run the full suite (some test cases rely on env `APM_RUBY_TEST_KEY`): ```bash -test/run_tests.sh +APM_RUBY_TEST_KEY=your_service_key test/run_tests.sh ``` -To run a single test file: +To run a single test file or single test case: ```bash # most tests require just the unit.gemfile dependencies -BUNDLE_GEMFILE=gemfiles/unit.gemfile bundle update - -BUNDLE_GEMFILE=gemfiles/unit.gemfile bundle exec ruby -I test test/opentelemetry/solarwinds_propagator_test.rb -``` - -To run a specific test with 'trace_state_header' inside test case name: - -```bash -BUNDLE_GEMFILE=gemfiles/unit.gemfile bundle update - -BUNDLE_GEMFILE=gemfiles/unit.gemfile bundle exec ruby -I test test/opentelemetry/solarwinds_propagator_test.rb -n /trace_state_header/ +bundle update +bundle exec ruby -I test test/opentelemetry/solarwinds_propagator_test.rb +bundle exec ruby -I test test/opentelemetry/solarwinds_propagator_test.rb -n /trace_state_header/ ``` diff --git a/Gemfile b/Gemfile index c9799f61..a6125ce1 100644 --- a/Gemfile +++ b/Gemfile @@ -2,17 +2,45 @@ source 'https://rubygems.org' -# this Gemfile is very minimal -# use rake commands or gemfiles in the gemfiles directory for testing - -gem 'rake', '>= 0.9.0' +gem 'rake' group :development, :test do + if RUBY_VERSION < '3.0.0' + gem 'ffi', '<= 1.16.3' # set this version due to ffi 1.17.0 need rubygems version > 3 (https://rubygems.org/gems/ffi/versions/1.17.0-arm-linux-musl) + gem 'google-protobuf', '< 3.25.4' + elsif RUBY_VERSION < '3.1.0' + gem 'ffi', '<= 1.17' + else + gem 'ffi' + end + gem 'benchmark-ips', '>= 2.7.2' + gem 'bson' gem 'byebug', '>= 8.0.0' - gem 'irb', '>= 1.0.0' # if RUBY_VERSION >= '2.6.0' + gem 'e2mmap' + gem 'get_process_mem' + gem 'irb', '>= 1.0.0' + gem 'logging' + gem 'lumberjack' gem 'memory_profiler' - gem 'rubocop' + gem 'minitest', '< 5.25.0' + gem 'minitest-debugger', require: false + gem 'minitest-focus', '>= 1.1.2' + gem 'minitest-hooks', '>= 1.5.0' + gem 'minitest-reporters', '< 1.0.18' + gem 'mocha' + gem 'rack-cache' + gem 'rack-test' + gem 'rubocop', require: false gem 'rubocop-performance', require: false gem 'rubocop-rake', require: false + gem 'code-scanning-rubocop', '~> 0.6.1' + gem 'simplecov', require: false, group: :test + gem 'simplecov-console', require: false, group: :test + gem 'webmock' if RUBY_VERSION >= '2.0.0' + gem 'base64' if RUBY_VERSION >= '3.4.0' + + gem 'opentelemetry-propagator-b3' + gem 'opentelemetry-test-helpers' + gemspec end diff --git a/Rakefile b/Rakefile index 8f412c91..27d023c1 100755 --- a/Rakefile +++ b/Rakefile @@ -45,16 +45,27 @@ argument can be used to override the image architecture if multi-platform is sup tag: any available image tag e.g. 3.2-bookworm, 2.7.5-alpine3.15, etc. default: 3.1-bullseye. runtests: true or false. default: true. platform: run image for specified OS/Arch, e.g. linux/amd64. default: none. +env_vars: comma-separated list of environment variables in KEY=VALUE format. default: none. Example: - bundle exec rake docker_tests - bundle exec rake 'docker_tests[3.3-rc]' - bundle exec rake 'docker_tests[,false]' - bundle exec rake 'docker_tests[2.7.6-alpine3.15,,linux/amd64]'" -task :docker_tests, [:tag, :runtests, :platform] do |_, args| + bundle exec rake 'docker_tests[,,,APM_RUBY_TEST_KEY=your_service_key]' + bundle exec rake 'docker_tests[3.3-rc,,,APM_RUBY_TEST_KEY=your_service_key]' + bundle exec rake 'docker_tests[,false,,APM_RUBY_TEST_KEY=your_service_key]' + bundle exec rake 'docker_tests[2.7.6-alpine3.15,,linux/amd64,APM_RUBY_TEST_KEY=your_service_key]' + bundle exec rake 'docker_tests[3.1-bullseye,true,,APM_RUBY_TEST_KEY=your_service_key]'" +task :docker_tests, [:tag, :runtests, :platform, :env_vars] do |_, args| args.with_defaults(tag: '3.1-bullseye', runtests: 'true') opt = +' --rm --tty --volume $PWD:/code/ruby-solarwinds --workdir /code/ruby-solarwinds' opt << " --platform #{args.platform}" unless args.platform.to_s.empty? + + # Add custom environment variables if provided + unless args.env_vars.to_s.empty? + env_vars = args.env_vars.split(',').map(&:strip) + env_vars.each do |env_var| + opt << " -e #{env_var}" if env_var.include?('=') + end + end + if args.runtests == 'true' opt << ' --entrypoint test/test_setup.sh -e RUN_TESTS=1' else diff --git a/gemfiles/test_gems.gemfile b/gemfiles/test_gems.gemfile deleted file mode 100644 index 58a3d5b5..00000000 --- a/gemfiles/test_gems.gemfile +++ /dev/null @@ -1,51 +0,0 @@ -# frozen_string_literal: true - -source 'https://rubygems.org' - -group :development, :test do - if RUBY_VERSION < '3.0.0' - gem 'ffi', '<= 1.16.3' # set this version due to ffi 1.17.0 need rubygems version > 3 (https://rubygems.org/gems/ffi/versions/1.17.0-arm-linux-musl) - gem 'google-protobuf', '< 3.25.4' - elsif RUBY_VERSION < '3.1.0' - gem 'ffi', '<= 1.17' - else - gem 'ffi' - end - gem 'benchmark-ips', '>= 2.7.2' - gem 'bson' - gem 'byebug', '>= 8.0.0' - gem 'debugger', platform: :mri_19 - gem 'e2mmap' - gem 'get_process_mem' - gem 'irb', '>= 1.0.0' - gem 'logging' - gem 'lumberjack' - gem 'memory_profiler' - gem 'minitest', '< 5.25.0' - gem 'minitest-debugger', require: false - gem 'minitest-focus', '>= 1.1.2' - gem 'minitest-hooks', '>= 1.5.0' - gem 'minitest-reporters', '< 1.0.18' - gem 'mocha' - gem 'rack-cache' - gem 'rack-test' - gem 'rake' - gem 'rubocop', require: false - gem 'simplecov', require: false, group: :test - gem 'simplecov-console' - gem 'webmock' if RUBY_VERSION >= '2.0.0' - gem 'grpc', '~> 1.71' - gem 'base64' if RUBY_VERSION >= '3.4.0' - - gem 'opentelemetry-sdk' - gem 'opentelemetry-instrumentation-all' - gem 'opentelemetry-propagator-b3' - gem 'opentelemetry-exporter-otlp' - gem 'opentelemetry-metrics-sdk' - gem 'opentelemetry-exporter-otlp-metrics' - gem 'opentelemetry-test-helpers' - - gem 'opentelemetry-resource-detector-azure' - gem 'opentelemetry-resource-detector-aws' if RUBY_VERSION >= '3.1.0' - gem 'opentelemetry-resource-detector-container' -end diff --git a/gemfiles/unit.gemfile b/gemfiles/unit.gemfile deleted file mode 100644 index 1c6f0c29..00000000 --- a/gemfiles/unit.gemfile +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true - -source 'https://rubygems.org' - -eval(File.read(File.join(File.dirname(__FILE__), 'test_gems.gemfile'))) # rubocop:disable Security/Eval - -gem 'code-scanning-rubocop', '~> 0.6.1' diff --git a/test/Dockerfile b/test/Dockerfile index b1be0ee4..1e93e421 100644 --- a/test/Dockerfile +++ b/test/Dockerfile @@ -9,8 +9,6 @@ ARG BUNDLE_RUBYGEMS__PKG__GITHUB__COM ENV TZ=America/Vancouver RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone -ENV SW_APM_REPORTER=file - # install OS packages RUN apt-get update \ && apt-get install -y --no-install-recommends \ diff --git a/test/api/custom_instrumentation_test.rb b/test/api/custom_instrumentation_test.rb index 8614fb13..5b266afe 100644 --- a/test/api/custom_instrumentation_test.rb +++ b/test/api/custom_instrumentation_test.rb @@ -30,6 +30,7 @@ it 'test_custom_instrumentation_simple_case' do class MyClass include SolarWindsAPM::API::Tracer + def new_method(param1, param2) param1 + param2 end @@ -54,6 +55,7 @@ def new_method(param1, param2) it 'test_custom_instrumentation_simple_case_with_custom_name_and_options' do class MyClass include SolarWindsAPM::API::Tracer + def new_method(param1, param2) param1 + param2 end @@ -83,6 +85,7 @@ def self.new_method(param1, param2) class << self include SolarWindsAPM::API::Tracer + add_tracer :new_method, 'custom_name', { attributes: { 'foo' => 'bar' }, kind: :unknown } end end diff --git a/test/api/tracing_ready_test.rb b/test/api/tracing_ready_test.rb index 655ef402..39bae4ed 100644 --- a/test/api/tracing_ready_test.rb +++ b/test/api/tracing_ready_test.rb @@ -18,10 +18,18 @@ @memory_exporter = OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new OpenTelemetry.tracer_provider.add_span_processor(OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(@memory_exporter)) + if ENV.key?('APM_RUBY_TEST_STAGING_KEY') + collector = 'https://apm.collector.st-ssp.solarwinds.com:443' + headers = ENV['APM_RUBY_TEST_STAGING_KEY'] + else + collector = 'https://apm.collector.cloud.solarwinds.com:443' + headers = ENV.fetch('APM_RUBY_TEST_KEY', nil) + end + @config = { - collector: 'https://apm.collector.st-ssp.solarwinds.com:443', + collector: collector, service: 'test-ruby', - headers: "Bearer #{ENV.fetch('APM_RUBY_TEST_STAGING_KEY', nil)}", + headers: "Bearer #{headers}", tracing_mode: true, trigger_trace_enabled: true } diff --git a/test/initest_helper.rb b/test/initest_helper.rb index 1ef70c5e..bdc26fad 100644 --- a/test/initest_helper.rb +++ b/test/initest_helper.rb @@ -8,28 +8,11 @@ require 'minitest/spec' require 'minitest/reporters' require './lib/solarwinds_apm/logger' +require 'simplecov' +SimpleCov.start ENV['SW_APM_SERVICE_KEY'] = 'this-is-a-dummy-api-token-for-testing-111111111111111111111111111111111:test-service' -# write to a file as well as STDOUT (comes in handy with docker runs) -# This approach preserves the coloring of pass fail, which the cli -# `./run_tests.sh 2>&1 | tee -a test/docker_test.log` does not -if ENV['TEST_RUNS_TO_FILE'] - FileUtils.mkdir_p('log') # create if it doesn't exist - $out_file = if ENV['TEST_RUNS_FILE_NAME'] - File.new(ENV['TEST_RUNS_FILE_NAME'], 'a') - else - File.new("log/test_direct_runs_#{Time.now.strftime('%Y%m%d_%H_%M')}.log", 'a') - end - $out_file.sync = true - $stdout.sync = true - - def $stdout.write(string) - $out_file.write(string) - super - end -end - Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new $LOAD_PATH.unshift("#{Dir.pwd}/lib/") diff --git a/test/minitest_helper.rb b/test/minitest_helper.rb index 370dc227..5648b489 100644 --- a/test/minitest_helper.rb +++ b/test/minitest_helper.rb @@ -18,50 +18,21 @@ require 'lumberjack' require 'logging' require 'bson' -require 'grpc' require 'pp' require './lib/solarwinds_apm/version' require './lib/solarwinds_apm/logger' +require './lib/solarwinds_apm/constants' +require 'opentelemetry-exporter-otlp-metrics' -# simplecov coverage information require 'simplecov' -require 'simplecov-console' -SimpleCov.formatter = SimpleCov::Formatter::Console -SimpleCov::Formatter::Console.max_rows = 15 -SimpleCov::Formatter::Console.max_lines = 5 -SimpleCov::Formatter::Console.missing_len = 20 SimpleCov.start # needed by most tests ENV['SW_APM_SERVICE_KEY'] = 'this-is-a-dummy-api-token-for-testing-111111111111111111111111111111111:test-service' - -# write to a file as well as STDOUT (comes in handy with docker runs) -# This approach preserves the coloring of pass fail, which the cli -# `./run_tests.sh 2>&1 | tee -a test/docker_test.log` does not -if ENV['TEST_RUNS_TO_FILE'] - FileUtils.mkdir_p('log') # create if it doesn't exist - $out_file = if ENV['TEST_RUNS_FILE_NAME'] - File.new(ENV['TEST_RUNS_FILE_NAME'], 'a') - else - File.new("log/test_direct_runs_#{Time.now.strftime('%Y%m%d_%H_%M')}.log", 'a') - end - $out_file.sync = true - $stdout.sync = true - - def $stdout.write(string) - $out_file.write(string) - super - end -end - -# Print out a headline in with the settings used in the test run -puts "\n\033[1m=== TEST RUN: #{RUBY_VERSION} #{File.basename(ENV.fetch('BUNDLE_GEMFILE', nil))} #{ENV.fetch('DBTYPE', nil)} #{ENV.fetch('TEST_PREPARED_STATEMENT', nil)} #{Time.now.strftime('%Y-%m-%d %H:%M')} ===\033[0m\n" unless ENV['DBO_PATCH_TEST'] - ENV['RACK_ENV'] = 'test' Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -# Bundler.require(:default, :test) # this load the solarwinds_apm library SolarWindsAPM.logger.level = 1 # Dummy Propagator for testing @@ -202,53 +173,6 @@ def create_span nil) end -## -# clear_all_traces -# -# Truncates the trace output file to zero -# -def clear_all_traces - return unless SolarWindsAPM.loaded && ENV['SW_APM_REPORTER'] == 'file' - - sleep 0.5 - File.truncate(SolarWindsAPM::OboeInitOptions.instance.host, 0) -end - -## -# obtain_all_traces -# -# Retrieves all traces written to the trace file -# -def obtain_all_traces - return [] unless SolarWindsAPM.loaded && ENV['SW_APM_REPORTER'] == 'file' - - sleep 0.5 - io = File.open(SolarWindsAPM::OboeInitOptions.instance.host, 'r') - contents = io.readlines(nil) - io.close - - return contents if contents.empty? - - traces = [] - - if Gem.loaded_specs['bson'] && Gem.loaded_specs['bson'].version.to_s < '4.0' - s = StringIO.new(contents[0]) - - until s.eof? - traces << if BSON.respond_to? :read_bson_document - BSON.read_bson_document(s) - else - BSON::Document.from_bson(s) - end - end - else - bbb = BSON::ByteBuffer.new(contents[0]) - traces << Hash.from_bson(bbb) until bbb.empty? - end - - traces -end - ## # create_context # diff --git a/test/opentelemetry/otlp_processor_sampled_test.rb b/test/opentelemetry/otlp_processor_sampled_test.rb index 20b10665..2a45fc17 100644 --- a/test/opentelemetry/otlp_processor_sampled_test.rb +++ b/test/opentelemetry/otlp_processor_sampled_test.rb @@ -5,7 +5,6 @@ require 'minitest_helper' require './lib/solarwinds_apm/opentelemetry' -require './lib/solarwinds_apm/constants' require './lib/solarwinds_apm/support/txn_name_manager' require './lib/solarwinds_apm/support/utils' require './lib/solarwinds_apm/sampling/sampling_patch' diff --git a/test/opentelemetry/otlp_processor_test.rb b/test/opentelemetry/otlp_processor_test.rb index 695ddaa9..10a9c4b7 100644 --- a/test/opentelemetry/otlp_processor_test.rb +++ b/test/opentelemetry/otlp_processor_test.rb @@ -6,7 +6,6 @@ require 'minitest_helper' require './lib/solarwinds_apm/config' require './lib/solarwinds_apm/opentelemetry' -require './lib/solarwinds_apm/constants' require './lib/solarwinds_apm/support/txn_name_manager' require './lib/solarwinds_apm/otel_native_config' require './lib/solarwinds_apm/api' diff --git a/test/opentelemetry/otlp_processor_unsampled_test.rb b/test/opentelemetry/otlp_processor_unsampled_test.rb index 562f178c..fd8a487f 100644 --- a/test/opentelemetry/otlp_processor_unsampled_test.rb +++ b/test/opentelemetry/otlp_processor_unsampled_test.rb @@ -5,7 +5,6 @@ require 'minitest_helper' require './lib/solarwinds_apm/opentelemetry' -require './lib/solarwinds_apm/constants' require './lib/solarwinds_apm/support/txn_name_manager' require './lib/solarwinds_apm/support/utils' require './lib/solarwinds_apm/sampling/sampling_patch' diff --git a/test/opentelemetry/solarwinds_propagator_test.rb b/test/opentelemetry/solarwinds_propagator_test.rb index 7cd96ee1..3cb2619c 100644 --- a/test/opentelemetry/solarwinds_propagator_test.rb +++ b/test/opentelemetry/solarwinds_propagator_test.rb @@ -6,7 +6,6 @@ require 'minitest_helper' require 'minitest/mock' require './lib/solarwinds_apm/opentelemetry' -require './lib/solarwinds_apm/constants' require './lib/solarwinds_apm/support/utils' require './lib/solarwinds_apm/support/transaction_settings' require './lib/solarwinds_apm/config' diff --git a/test/patch/sw_mysql2_patch_integrate_test.rb b/test/patch/sw_mysql2_patch_integrate_test.rb index 6d66e7f0..1d1659ff 100644 --- a/test/patch/sw_mysql2_patch_integrate_test.rb +++ b/test/patch/sw_mysql2_patch_integrate_test.rb @@ -10,7 +10,6 @@ require './lib/solarwinds_apm/otel_native_config' require './lib/solarwinds_apm/api' require './lib/solarwinds_apm/support' -require './lib/solarwinds_apm/constants' require './lib/solarwinds_apm/patch/tag_sql/sw_dbo_utils' # rubocop:disable Naming/MethodName diff --git a/test/patch/sw_pg_patch_integrate_test.rb b/test/patch/sw_pg_patch_integrate_test.rb index eea02441..5152fafa 100644 --- a/test/patch/sw_pg_patch_integrate_test.rb +++ b/test/patch/sw_pg_patch_integrate_test.rb @@ -10,7 +10,6 @@ require './lib/solarwinds_apm/otel_native_config' require './lib/solarwinds_apm/api' require './lib/solarwinds_apm/support' -require './lib/solarwinds_apm/constants' require './lib/solarwinds_apm/patch/tag_sql/sw_dbo_utils' # rubocop:disable Naming/MethodName diff --git a/test/run_tests.sh b/test/run_tests.sh index c9859e60..a2de28b9 100755 --- a/test/run_tests.sh +++ b/test/run_tests.sh @@ -2,114 +2,139 @@ # Copyright (c) SolarWinds, LLC. # All rights reserved. # -# This script can be used to run all or select tests in a Linux environment with -# the solarwinds_apm dependencies already installed, which is usually achieved by a combination of: -# - the setup of the docker image and -# - running the test_setup.sh script +# This script runs all *_test.rb files in the test/ directory using bundle exec ruby -I test +# It provides comprehensive logging and summary reporting of test results. # -# This script offers the following options: -# -g gemfile - restrict the tests to the ones associated with this gemfile (path from gem-root) +# Usage: ./run_tests.sh [options] +# Options: +# -p - Run only test files matching the pattern (e.g., -p "api/*") +# -v - Verbose output +# -h - Show help # -check_status() { - status=$? - [[ $status -gt $exit_status ]] && exit_status=$status - [[ $status -ne 0 ]] && echo "!!! Test suite failed for $check_file_name with Ruby $ruby_version !!!" +set -e + +# Initialize variables +exit_status=0 +total_tests=0 +passed_tests=0 +failed_tests=0 +skipped_tests=0 +test_pattern="*_test.rb" + +# Arrays to track test results +declare -a passed_files=() +declare -a failed_files=() +declare -a skipped_files=() + +# Function to log messages +log_message() { + local message="$1" + local timestamp=$(date "+%Y-%m-%d %H:%M:%S") + echo "[$timestamp] $message" } -gemfiles=( - "gemfiles/unit.gemfile" -) - -## -# Read opts -while getopts ":g:" opt; do - case ${opt} in - g ) # process option g - gemfiles=("$OPTARG") - ;; - \? ) echo " -Usage: $0 [-g gemfile] - -g gemfile - restrict the tests to the ones associated with this gemfile (path from gem-root) -" - exit 1 - ;; - esac -done +# Function to check test result and update counters +check_test_result() { + local test_file="$1" + local test_output="$2" + local status="$3" + + total_tests=$((total_tests + 1)) + + if [[ $status -eq 0 ]]; then + passed_tests=$((passed_tests + 1)) + passed_files+=("$test_file") + log_message "✅ PASSED: $test_file" + else + failed_tests=$((failed_tests + 1)) + failed_files+=("$test_file") + exit_status=1 + log_message "❌ FAILED: $test_file (exit code: $status)" + fi + echo "$test_output" +} -## -# setup files and env vars -exit_status=-1 +# Function to run a single test file +run_test_file() { + local test_file="$1" + local relative_path=${test_file#test/} -rm -f gemfiles/*.lock + log_message "Running: $relative_path" -time=$(date "+%Y%m%d_%H%M") -export TEST_RUNS_FILE_NAME="log/testrun_$time.log" -export TEST_RUNS_TO_FILE=true -echo "logfile name: $TEST_RUNS_FILE_NAME" - -ruby_version="$(ruby -e 'print(RUBY_VERSION)')" -echo "*** ruby version $ruby_version ***" - -echo "Remove previous logfile" -rm log/*.log - -## -# loop through gemfiles to set up and run tests -for gemfile in "${gemfiles[@]}" ; do - - echo "*** installing gems from $gemfile ***" - if ! BUNDLE_GEMFILE=$gemfile bundle update; then - echo "Problem during gem install. Skipping tests for $gemfile" - exit_status=1 - continue - fi - # and here we are finally running the tests!!! - check_file_name=$gemfile - BUNDLE_GEMFILE=$gemfile bundle exec rake test - check_status -done + # Capture both stdout and stderr + local output + local status -# explicitly test for solarwinds initialization -if ! BUNDLE_GEMFILE=gemfiles/test_gems.gemfile bundle update; then - echo "Problem during gem install. Skipping tests for $gemfile" - exit_status=1 - continue -fi + if output=$(bundle exec ruby -I test "$test_file"); then + status=0 + else + status=$? + fi -# for dbo patch test -PATCH_TEST_FILE=$(find test/patch/*_test.rb -type f) -for file in $PATCH_TEST_FILE; do - check_file_name=$file - BUNDLE_GEMFILE=gemfiles/test_gems.gemfile DBO_PATCH_TEST=1 bundle exec ruby -I test $file - check_status -done + check_test_result "$relative_path" "$output" "$status" +} -# for resource detection test -RESOURCE_DETECTOR_TEST_FILE=$(find test/support/resource_detector/aws/*_test.rb -type f) -for file in $RESOURCE_DETECTOR_TEST_FILE; do - check_file_name=$file - BUNDLE_GEMFILE=gemfiles/test_gems.gemfile RESOURCE_TEST=1 bundle exec ruby -I test $file - check_status +# Setup logging +time=$(date "+%Y%m%d_%H%M") +export TEST_RUNS_FILE_NAME="./log/testrun_$time.log" + +# Remove previous log files +rm -f ./log/*.log + +log_message "=== SolarWinds APM Ruby Test Runner ===" +log_message "Test pattern: $test_pattern" +log_message "Ruby version: $(ruby -e 'print(RUBY_VERSION)'); $(bundle -v)" +log_message "Searching for test files matching: test/**/$test_pattern" +test_files=$(find test -name "$test_pattern" -type f | sort) + +# Count total files to run +total_files=$(echo "$test_files" | wc -l | tr -d ' ') +log_message "Found $total_files test files to run" + +# Run each test file +# test_files=(test/solarwinds_apm/init_test/init_1_test.rb) +current_file=0 +for test_file in $test_files; do + current_file=$((current_file + 1)) + log_message "[$current_file/$total_files] Processing: $test_file" + run_test_file "$test_file" done -# for otlp processor test -OTLP_PROCESSOR_TEST_FILE=$(find test/opentelemetry/otlp_processor_*_test.rb -type f) -for file in $OTLP_PROCESSOR_TEST_FILE; do - check_file_name=$file - BUNDLE_GEMFILE=gemfiles/test_gems.gemfile bundle exec ruby -I test $file - check_status -done +# Generate summary +log_message "" +log_message "=== TEST EXECUTION SUMMARY ===" +log_message "Total files processed: $total_tests" +log_message "Passed: $passed_tests" +log_message "Failed: $failed_tests" +log_message "Success rate: $((passed_tests * 100 / total_tests))%" + +if [[ ${#passed_files[@]} -gt 0 ]]; then + log_message "" + log_message "✅ PASSED FILES:" + for file in "${passed_files[@]}"; do + log_message " - $file" + done +fi -NUMBER_FILE=$(find test/solarwinds_apm/init_test/*_test.rb -type f | wc -l) -for ((i = 1; i <= $NUMBER_FILE; i++)); do - check_file_name=init_${i}_test.rb - BUNDLE_GEMFILE=gemfiles/test_gems.gemfile bundle exec ruby -I test test/solarwinds_apm/init_test/init_${i}_test.rb - check_status -done +if [[ ${#failed_files[@]} -gt 0 ]]; then + log_message "" + log_message "❌ FAILED FILES:" + for file in "${failed_files[@]}"; do + log_message " - $file" + done +fi +# Print summary to console echo "" -echo "--- SUMMARY ------------------------------" -grep -E '===|failures|FAIL|ERROR' "$TEST_RUNS_FILE_NAME" +echo "==================== FINAL SUMMARY ====================" +echo "Total Files: $total_tests | Passed: $passed_tests | Failed: $failed_tests" +echo "Success rate: $((passed_tests * 100 / total_tests))%" + +if [[ $exit_status -eq 0 ]]; then + echo "🎉 ALL TESTS PASSED!" +else + echo "💥 SOME TESTS FAILED!" +fi exit $exit_status diff --git a/test/sampling/http_sampler_test.rb b/test/sampling/http_sampler_test.rb index ed230687..601d55bd 100644 --- a/test/sampling/http_sampler_test.rb +++ b/test/sampling/http_sampler_test.rb @@ -16,10 +16,18 @@ @memory_exporter = OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new OpenTelemetry.tracer_provider.add_span_processor(OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(@memory_exporter)) + if ENV.key?('APM_RUBY_TEST_STAGING_KEY') + collector = 'https://apm.collector.st-ssp.solarwinds.com:443' + headers = ENV['APM_RUBY_TEST_STAGING_KEY'] + else + collector = 'https://apm.collector.cloud.solarwinds.com:443' + headers = ENV.fetch('APM_RUBY_TEST_KEY', nil) + end + @config = { - collector: 'https://apm.collector.st-ssp.solarwinds.com:443', + collector: collector, service: 'test-ruby', - headers: "Bearer #{ENV.fetch('APM_RUBY_TEST_STAGING_KEY', nil)}", + headers: "Bearer #{headers}", tracing_mode: true, trigger_trace_enabled: true } diff --git a/test/sampling_test_helper.rb b/test/sampling_test_helper.rb index 6f14e5ff..8310d5cc 100644 --- a/test/sampling_test_helper.rb +++ b/test/sampling_test_helper.rb @@ -6,6 +6,9 @@ require 'opentelemetry-metrics-sdk' require 'opentelemetry-exporter-otlp-metrics' require 'opentelemetry-test-helpers' +require './lib/solarwinds_apm/sampling' +require 'simplecov' +SimpleCov.start ENV['OTEL_METRICS_EXPORTER'] = 'none' diff --git a/test/solarwinds_apm/init_test/init_1_test.rb b/test/solarwinds_apm/init_test/init_1_test.rb index d2588372..07497d9d 100644 --- a/test/solarwinds_apm/init_test/init_1_test.rb +++ b/test/solarwinds_apm/init_test/init_1_test.rb @@ -7,8 +7,6 @@ describe 'solarwinds_apm_init_1' do it 'SW_APM_ENABLED_set_to_disabled' do - puts "\n\033[1m=== TEST RUN: #{RUBY_VERSION} #{File.basename(__FILE__)} #{Time.now.strftime('%Y-%m-%d %H:%M')} ===\033[0m\n" - log_output = StringIO.new SolarWindsAPM.logger = Logger.new(log_output) diff --git a/test/solarwinds_apm/init_test/init_2_test.rb b/test/solarwinds_apm/init_test/init_2_test.rb index e10cd0b0..2875fcf4 100644 --- a/test/solarwinds_apm/init_test/init_2_test.rb +++ b/test/solarwinds_apm/init_test/init_2_test.rb @@ -7,8 +7,6 @@ describe 'solarwinds_apm_init_2' do it 'SW_APM_SERVICE_KEY_is_invalid' do - puts "\n\033[1m=== TEST RUN: #{RUBY_VERSION} #{File.basename(__FILE__)} #{Time.now.strftime('%Y-%m-%d %H:%M')} ===\033[0m\n" - log_output = StringIO.new SolarWindsAPM.logger = Logger.new(log_output) diff --git a/test/solarwinds_apm/init_test/init_3_test.rb b/test/solarwinds_apm/init_test/init_3_test.rb index becf5d4b..9134f4e1 100644 --- a/test/solarwinds_apm/init_test/init_3_test.rb +++ b/test/solarwinds_apm/init_test/init_3_test.rb @@ -7,8 +7,6 @@ describe 'solarwinds_apm_init_3' do it 'SW_APM_AUTO_CONFIGURE_set_to_false' do - puts "\n\033[1m=== TEST RUN: #{RUBY_VERSION} #{File.basename(__FILE__)} #{Time.now.strftime('%Y-%m-%d %H:%M')} ===\033[0m\n" - log_output = StringIO.new SolarWindsAPM.logger = Logger.new(log_output) diff --git a/test/solarwinds_apm/init_test/init_4_test.rb b/test/solarwinds_apm/init_test/init_4_test.rb index 9e91d672..27ae3bd6 100644 --- a/test/solarwinds_apm/init_test/init_4_test.rb +++ b/test/solarwinds_apm/init_test/init_4_test.rb @@ -7,8 +7,6 @@ describe 'solarwinds_apm_init_4' do it 'everything_default' do - puts "\n\033[1m=== TEST RUN: #{RUBY_VERSION} #{File.basename(__FILE__)} #{Time.now.strftime('%Y-%m-%d %H:%M')} ===\033[0m\n" - log_output = StringIO.new SolarWindsAPM.logger = Logger.new(log_output) diff --git a/test/support/service_key_checker_test.rb b/test/support/service_key_checker_test.rb index 0a0996b3..604971a4 100644 --- a/test/support/service_key_checker_test.rb +++ b/test/support/service_key_checker_test.rb @@ -4,7 +4,6 @@ # All rights reserved. require 'minitest_helper' -# require './lib/solarwinds_apm/logger' require './lib/solarwinds_apm/config' require './lib/solarwinds_apm/support/service_key_checker' diff --git a/test/support/trace_context_log_test.rb b/test/support/trace_context_log_test.rb index f8871ece..c18182a6 100644 --- a/test/support/trace_context_log_test.rb +++ b/test/support/trace_context_log_test.rb @@ -9,6 +9,8 @@ require './lib/solarwinds_apm/support/logger_formatter' require './lib/solarwinds_apm/support/logging_log_event' require './lib/solarwinds_apm/support/lumberjack_formatter' +# to suppress unexpected configuration error due to undefined method `meter_provider=' +require 'opentelemetry-metrics-sdk' describe 'Trace Context in Log Test' do before do diff --git a/test/test_setup.sh b/test/test_setup.sh index 41fb1db0..cd69e708 100755 --- a/test/test_setup.sh +++ b/test/test_setup.sh @@ -19,8 +19,8 @@ echo "Finished Setup" if [ -n "$RUN_TESTS" ]; then { echo "Start Run Unit Test" - export SW_APM_REPORTER=file mkdir -p log + bundle install test/run_tests.sh status=$? echo "Finished Unit Test"