From 4ebf02091a9e2f4d734a235d2b1f9019145b7136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Fri, 18 Nov 2022 08:16:58 -1000 Subject: [PATCH 1/2] Add basic unit tests for CI --- .github/workflows/ci.yml | 38 +++++++++++----------- .rubocop.yml | 9 +++--- lib/riemann/tools/postgresql.rb | 2 +- spec/riemann/tools/postgresql_spec.rb | 46 +++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 25 deletions(-) create mode 100644 spec/riemann/tools/postgresql_spec.rb diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 298caa7..5e6950b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,22 +21,22 @@ jobs: bundler-cache: true - name: Run rubocop run: bundle exec rubocop - #test: - # needs: lint - # runs-on: ubuntu-latest - # strategy: - # matrix: - # ruby-version: - # - 2.6 - # - 2.7 - # - 3.0 - # - 3.1 - # steps: - # - uses: actions/checkout@v3 - # - name: Setup Ruby - # uses: ruby/setup-ruby@v1 - # with: - # ruby-version: ${{ matrix.ruby-version }} - # bundler-cache: true - # - name: Run the test suite - # run: bundle exec rspec + test: + needs: lint + runs-on: ubuntu-latest + strategy: + matrix: + ruby-version: + - 2.6 + - 2.7 + - 3.0 + - 3.1 + steps: + - uses: actions/checkout@v3 + - name: Setup Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby-version }} + bundler-cache: true + - name: Run the test suite + run: bundle exec rspec diff --git a/.rubocop.yml b/.rubocop.yml index c854505..0f13ea9 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -26,11 +26,10 @@ Metrics/ParameterLists: Enabled: false Metrics/PerceivedComplexity: Enabled: false -Naming/MethodParameterName: - AllowedNames: - - az # availability zone - - id - - lb # load balancer +RSpec/NamedSubject: + Enabled: false +RSpec/SubjectStub: + Enabled: false Style/Documentation: Enabled: false Style/HashSyntax: diff --git a/lib/riemann/tools/postgresql.rb b/lib/riemann/tools/postgresql.rb index 01aa8bf..3b2366a 100644 --- a/lib/riemann/tools/postgresql.rb +++ b/lib/riemann/tools/postgresql.rb @@ -59,7 +59,7 @@ def tick @conn.exec("DECLARE connection CURSOR FOR SELECT datname, count(datname) FROM pg_stat_activity \ GROUP BY pg_stat_activity.datname") - result = @conn.exec('FETCH ALL in connection') + result = @conn.exec('FETCH ALL IN connection') result.values.collect do |row| vals = row.collect.to_a report( diff --git a/spec/riemann/tools/postgresql_spec.rb b/spec/riemann/tools/postgresql_spec.rb new file mode 100644 index 0000000..e4353df --- /dev/null +++ b/spec/riemann/tools/postgresql_spec.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +require 'riemann/tools/postgresql' + +RSpec.describe Riemann::Tools::Postgresql do + describe('#tick') do + let(:general_fields) { %w[datid datname numbackends xact_commit xact_rollback blks_read blks_hit tup_returned tup_fetched tup_inserted tup_updated tup_deleted conflicts temp_files temp_bytes deadlocks checksum_failures checksum_last_failure blk_read_time blk_write_time stats_reset size] } + let(:general_values) do + [ + %w[32768 pakotoa_development 0 5190 1 156 218306 3480939 37885 0 0 0 0 0 0 0 nil nil 0 0 2022-11-12 13:50:09.308897-10 8704559], + %w[32927 pakotoa_test 0 1146 14 871 56655 170965 27329 794 669 4 0 0 0 0 nil nil 0 0 2022-11-18 07:31:43.057234-10 8614447], + ] + end + let(:connection_fields) { %w[datname count] } + let(:connection_values) do + [ + %w[template1 4], + %w[postgres 1], + [nil, '0'], + ] + end + + before do + general_result = double + allow(general_result).to receive(:fields).and_return(general_fields) + allow(general_result).to receive(:values).and_return(general_values) + + connection_result = double + allow(connection_result).to receive(:fields).and_return(connection_fields) + allow(connection_result).to receive(:values).and_return(connection_values) + + conn = double + allow(conn).to receive(:transaction).and_yield + allow(conn).to receive(:exec).with(/\ADECLARE [a-z]+ CURSOR FOR/) + allow(conn).to receive(:exec).with(/\AFETCH ALL IN general/).and_return(general_result) + allow(conn).to receive(:exec).with(/\AFETCH ALL IN connection/).and_return(connection_result) + + allow(PG).to receive(:connect).and_return(conn) + allow(subject).to receive(:report) # RSpec/SubjectStub + subject.tick + end + + it { is_expected.to have_received(:report).with(hash_including(service: 'DB pakotoa_development tup_returned', metric: 3_480_939, state: 'ok', description: 'PostgreSQL DB tup returned')) } + it { is_expected.to have_received(:report).with(hash_including(service: 'DB template1 connections', metric: 4, state: 'ok', description: 'PostgreSQL DB Connections')) } + end +end From e75b9776aa6782acc1ad98263491846ab28cde6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Sun, 7 Jul 2024 19:24:58 +0200 Subject: [PATCH 2/2] Pet rubocop --- spec/riemann/tools/postgresql_spec.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/spec/riemann/tools/postgresql_spec.rb b/spec/riemann/tools/postgresql_spec.rb index e4353df..f45aee0 100644 --- a/spec/riemann/tools/postgresql_spec.rb +++ b/spec/riemann/tools/postgresql_spec.rb @@ -22,12 +22,10 @@ before do general_result = double - allow(general_result).to receive(:fields).and_return(general_fields) - allow(general_result).to receive(:values).and_return(general_values) + allow(general_result).to receive_messages(fields: general_fields, values: general_values) connection_result = double - allow(connection_result).to receive(:fields).and_return(connection_fields) - allow(connection_result).to receive(:values).and_return(connection_values) + allow(connection_result).to receive_messages(fields: connection_fields, values: connection_values) conn = double allow(conn).to receive(:transaction).and_yield