Skip to content

Commit 2fea018

Browse files
committed
add back some useful test
1 parent 2c8308f commit 2fea018

File tree

9 files changed

+154
-5
lines changed

9 files changed

+154
-5
lines changed

lib/solarwinds_apm.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
begin
1010
require 'solarwinds_apm/logger'
1111
require 'solarwinds_apm/version'
12-
require 'solarwinds_apm/noop'
1312
require 'opentelemetry-api'
1413
if ENV.fetch('SW_APM_ENABLED', 'true') == 'false'
1514
SolarWindsAPM.logger.info '==================================================================='
1615
SolarWindsAPM.logger.info 'SW_APM_ENABLED environment variable detected and was set to false. SolarWindsAPM disabled'
1716
SolarWindsAPM.logger.info '==================================================================='
17+
require 'solarwinds_apm/noop'
1818
return
1919
end
2020

@@ -36,8 +36,8 @@
3636
SolarWindsAPM.logger.warn 'SolarWindsAPM not loaded. SolarWinds APM disabled'
3737
SolarWindsAPM.logger.warn 'Please check previous log messages.'
3838
SolarWindsAPM.logger.warn '=============================================================='
39+
require 'solarwinds_apm/noop'
3940
end
40-
4141
elsif ENV['SW_APM_AUTO_CONFIGURE'] == 'false'
4242
SolarWindsAPM.logger.warn '=============================================================='
4343
SolarWindsAPM.logger.warn 'SW_APM_AUTO_CONFIGURE set to false.'
@@ -48,6 +48,7 @@
4848
SolarWindsAPM.logger.warn 'See: https://github.com/solarwinds/apm-ruby/blob/main/CONFIGURATION.md#in-code-configuration'
4949
SolarWindsAPM.logger.warn "\e[1mPlease discard this message if application have already taken this action.\e[0m"
5050
SolarWindsAPM.logger.warn '=============================================================='
51+
require 'solarwinds_apm/noop'
5152
end
5253
end
5354
rescue StandardError => e

lib/solarwinds_apm/api/tracing.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def solarwinds_ready?(wait_milliseconds = 3000, integer_response: false)
3737

3838
root_sampler = ::OpenTelemetry.tracer_provider.sampler.instance_variable_get(:@root)
3939
is_ready = root_sampler.wait_until_ready(wait_milliseconds / 1000)
40-
puts "is_ready: #{is_ready}"
4140
!!is_ready
4241
end
4342
end

lib/solarwinds_apm/noop/api.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ module NoopAPI
1919
# Tracing
2020
module Tracing
2121
# (wait_milliseconds=3000, integer_response: false)
22-
def solarwinds_ready?(*_args, **options)
23-
options && options[:integer_response] ? 0 : false
22+
def solarwinds_ready?(_wait_milliseconds = 3000, integer_response: false)
23+
_noop = integer_response
24+
false
2425
end
2526
end
2627

test/initest_helper.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright (c) 2024 SolarWinds, LLC.
4+
# All rights reserved.
5+
6+
require 'minitest'
7+
require 'minitest/autorun'
8+
require 'minitest/spec'
9+
require 'minitest/reporters'
10+
require './lib/solarwinds_apm/logger'
11+
12+
ENV['SW_APM_SERVICE_KEY'] = 'this-is-a-dummy-api-token-for-testing-111111111111111111111111111111111:test-service'
13+
14+
# write to a file as well as STDOUT (comes in handy with docker runs)
15+
# This approach preserves the coloring of pass fail, which the cli
16+
# `./run_tests.sh 2>&1 | tee -a test/docker_test.log` does not
17+
if ENV['TEST_RUNS_TO_FILE']
18+
FileUtils.mkdir_p('log') # create if it doesn't exist
19+
$out_file = if ENV['TEST_RUNS_FILE_NAME']
20+
File.new(ENV['TEST_RUNS_FILE_NAME'], 'a')
21+
else
22+
File.new("log/test_direct_runs_#{Time.now.strftime('%Y%m%d_%H_%M')}.log", 'a')
23+
end
24+
$out_file.sync = true
25+
$stdout.sync = true
26+
27+
def $stdout.write(string)
28+
$out_file.write(string)
29+
super
30+
end
31+
end
32+
33+
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
34+
35+
$LOAD_PATH.unshift("#{Dir.pwd}/lib/")
36+
37+
def noop_shared_test
38+
_(defined?(SolarWindsAPM::API)).must_equal 'constant'
39+
_(SolarWindsAPM::API.solarwinds_ready?(300)).must_equal false
40+
assert_nil SolarWindsAPM::API.in_span('params')
41+
_(SolarWindsAPM::API.set_transaction_name).must_equal true
42+
_(SolarWindsAPM::API.current_trace_info.hash_for_log.to_s).must_equal '{}'
43+
_(SolarWindsAPM::API.current_trace_info.for_log).must_equal ''
44+
_(SolarWindsAPM::API.current_trace_info.tracestring).must_equal '00-00000000000000000000000000000000-0000000000000000-00'
45+
_(SolarWindsAPM::API.current_trace_info.trace_flags).must_equal '00'
46+
_(SolarWindsAPM::API.current_trace_info.span_id).must_equal '0000000000000000'
47+
_(SolarWindsAPM::API.current_trace_info.trace_id).must_equal '00000000000000000000000000000000'
48+
_(SolarWindsAPM::API.current_trace_info.do_log).must_equal :never
49+
50+
in_span_result = SolarWindsAPM::API.in_span('params') do |_span|
51+
value = 1 + 1
52+
value
53+
end
54+
55+
_(in_span_result).must_equal 2
56+
end

test/run_tests.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ for file in $RESOURCE_DETECTOR_TEST_FILE; do
9393
check_status
9494
done
9595

96+
NUMBER_FILE=$(find test/solarwinds_apm/init_test/*_test.rb -type f | wc -l)
97+
for ((i = 1; i <= $NUMBER_FILE; i++)); do
98+
check_file_name=init_${i}_test.rb
99+
BUNDLE_GEMFILE=gemfiles/test_gems.gemfile bundle exec ruby -I test test/solarwinds_apm/init_test/init_${i}_test.rb
100+
check_status
101+
done
102+
96103
echo ""
97104
echo "--- SUMMARY ------------------------------"
98105
grep -E '===|failures|FAIL|ERROR' "$TEST_RUNS_FILE_NAME"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright (c) 2024 SolarWinds, LLC.
4+
# All rights reserved.
5+
6+
require 'initest_helper'
7+
8+
describe 'solarwinds_apm_init_1' do
9+
it 'SW_APM_ENABLED_set_to_disabled' do
10+
puts "\n\033[1m=== TEST RUN: #{RUBY_VERSION} #{File.basename(__FILE__)} #{Time.now.strftime('%Y-%m-%d %H:%M')} ===\033[0m\n"
11+
12+
log_output = StringIO.new
13+
SolarWindsAPM.logger = Logger.new(log_output)
14+
15+
ENV['SW_APM_ENABLED'] = 'false'
16+
17+
require './lib/solarwinds_apm'
18+
assert_includes log_output.string,
19+
'SW_APM_ENABLED environment variable detected and was set to false. SolarWindsAPM disabled'
20+
21+
noop_shared_test
22+
end
23+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright (c) 2024 SolarWinds, LLC.
4+
# All rights reserved.
5+
6+
require 'initest_helper'
7+
8+
describe 'solarwinds_apm_init_2' do
9+
it 'SW_APM_SERVICE_KEY_is_invalid' do
10+
puts "\n\033[1m=== TEST RUN: #{RUBY_VERSION} #{File.basename(__FILE__)} #{Time.now.strftime('%Y-%m-%d %H:%M')} ===\033[0m\n"
11+
12+
log_output = StringIO.new
13+
SolarWindsAPM.logger = Logger.new(log_output)
14+
15+
ENV['SW_APM_SERVICE_KEY'] = ':abcd'
16+
17+
require './lib/solarwinds_apm'
18+
assert_includes log_output.string, 'SW_APM_SERVICE_KEY problem. API Token in wrong format. Masked token'
19+
20+
noop_shared_test
21+
end
22+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright (c) 2024 SolarWinds, LLC.
4+
# All rights reserved.
5+
6+
require 'initest_helper'
7+
8+
describe 'solarwinds_apm_init_3' do
9+
it 'SW_APM_AUTO_CONFIGURE_set_to_false' do
10+
puts "\n\033[1m=== TEST RUN: #{RUBY_VERSION} #{File.basename(__FILE__)} #{Time.now.strftime('%Y-%m-%d %H:%M')} ===\033[0m\n"
11+
12+
log_output = StringIO.new
13+
SolarWindsAPM.logger = Logger.new(log_output)
14+
15+
ENV['SW_APM_AUTO_CONFIGURE'] = 'false'
16+
17+
require './lib/solarwinds_apm'
18+
assert_includes log_output.string, 'SW_APM_AUTO_CONFIGURE set to false.'
19+
20+
noop_shared_test
21+
end
22+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright (c) 2024 SolarWinds, LLC.
4+
# All rights reserved.
5+
6+
require 'initest_helper'
7+
8+
describe 'solarwinds_apm_init_4' do
9+
it 'everything_default' do
10+
puts "\n\033[1m=== TEST RUN: #{RUBY_VERSION} #{File.basename(__FILE__)} #{Time.now.strftime('%Y-%m-%d %H:%M')} ===\033[0m\n"
11+
12+
log_output = StringIO.new
13+
SolarWindsAPM.logger = Logger.new(log_output)
14+
15+
require './lib/solarwinds_apm'
16+
assert_includes log_output.string, 'Current solarwinds_apm version:'
17+
end
18+
end

0 commit comments

Comments
 (0)