Skip to content

Commit

Permalink
Merge pull request #5 from smortex/ci
Browse files Browse the repository at this point in the history
Add basic unit tests for CI
  • Loading branch information
jamtur01 authored Jul 7, 2024
2 parents f041c9e + e75b977 commit 06011ad
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 25 deletions.
38 changes: 19 additions & 19 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 4 additions & 5 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib/riemann/tools/postgresql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
44 changes: 44 additions & 0 deletions spec/riemann/tools/postgresql_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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_messages(fields: general_fields, values: general_values)

connection_result = double
allow(connection_result).to receive_messages(fields: connection_fields, values: 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

0 comments on commit 06011ad

Please sign in to comment.