From 92aed621a26660edfc12c00fef2fb4a76ee1639c Mon Sep 17 00:00:00 2001 From: James Armes Date: Wed, 3 Jul 2024 18:27:57 -0400 Subject: [PATCH] Updated API tests to verify they are instrumented. --- spec/spec_helper.rb | 10 +++++++++- spec/support/examples.rb | 3 +++ spec/support/examples/instrumented.rb | 18 ++++++++++++++++++ spec/unit/document_transfer/api/health_spec.rb | 11 +++-------- .../document_transfer/api/transfer_spec.rb | 7 +++---- 5 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 spec/support/examples.rb create mode 100644 spec/support/examples/instrumented.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8af985a..ab2eb15 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true require 'factory_bot' +require 'rack' +require 'rack/test' # Configure code coverage reporting. if ENV.fetch('COVERAGE', false) @@ -14,9 +16,15 @@ end end +# We need to build a Rack app for testing. This ensures that we're including the +# appropriate middleware and that the app is configured correctly. +ENV['RACK_ENV'] = 'test' +RSPEC_APP = Rack::Builder.parse_file('config.ru') + RSpec.configure do |config| config.include FactoryBot::Syntax::Methods end -# Include factories. +# Include shared examples and factories. +require_relative 'support/examples' require_relative 'support/factories' diff --git a/spec/support/examples.rb b/spec/support/examples.rb new file mode 100644 index 0000000..a216fcd --- /dev/null +++ b/spec/support/examples.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +require_relative 'examples/instrumented' diff --git a/spec/support/examples/instrumented.rb b/spec/support/examples/instrumented.rb new file mode 100644 index 0000000..839472e --- /dev/null +++ b/spec/support/examples/instrumented.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'statsd-instrument' + +# Verify endpoint instrumentation. +RSpec.shared_examples 'instrumented' do |method, path, stat| + include StatsD::Instrument::Matchers + + it 'increments the endpoint counter' do + expect { send(method, path, defined?(params) ? params : {}) }.to \ + trigger_statsd_increment("endpoint.#{stat}.requests") + end + + it 'measures the endpoint duration' do + expect { send(method, path) }.to \ + trigger_statsd_measure("endpoint.#{stat}.duration") + end +end diff --git a/spec/unit/document_transfer/api/health_spec.rb b/spec/unit/document_transfer/api/health_spec.rb index 5f83342..472c10d 100644 --- a/spec/unit/document_transfer/api/health_spec.rb +++ b/spec/unit/document_transfer/api/health_spec.rb @@ -1,19 +1,18 @@ # frozen_string_literal: true -require 'rack/test' - require_relative '../../../../lib/api/api' require_relative '../../../../lib/api/health' describe DocumentTransfer::API::Health do include Rack::Test::Methods - include StatsD::Instrument::Matchers def app - DocumentTransfer::API::API + RSPEC_APP end describe 'GET /health' do + include_examples 'instrumented', :get, '/health', :health + it 'returns 200' do get '/health' expect(last_response).to be_ok @@ -23,9 +22,5 @@ def app get '/health' expect(last_response.body).to eq({ status: 'ok' }.to_json) end - - it 'increments the health check counter' do - expect { get '/health' }.to trigger_statsd_increment('health_check') - end end end diff --git a/spec/unit/document_transfer/api/transfer_spec.rb b/spec/unit/document_transfer/api/transfer_spec.rb index 3b11683..62faf3f 100644 --- a/spec/unit/document_transfer/api/transfer_spec.rb +++ b/spec/unit/document_transfer/api/transfer_spec.rb @@ -1,16 +1,13 @@ # frozen_string_literal: true -require 'rack/test' - require_relative '../../../../lib/api/api' require_relative '../../../../lib/api/transfer' describe DocumentTransfer::API::Transfer do include Rack::Test::Methods - include StatsD::Instrument::Matchers def app - DocumentTransfer::API::API + RSPEC_APP end describe 'POST /transfer' do @@ -35,6 +32,8 @@ def app allow(destination).to receive(:transfer).and_return({ path: 'rspec-folder/rspec.pdf' }) end + include_examples 'instrumented', :post, '/transfer', :transfer + it 'succeeds' do post '/transfer', params