Skip to content

Commit 06011ad

Browse files
authored
Merge pull request #5 from smortex/ci
Add basic unit tests for CI
2 parents f041c9e + e75b977 commit 06011ad

File tree

4 files changed

+68
-25
lines changed

4 files changed

+68
-25
lines changed

.github/workflows/ci.yml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ jobs:
2121
bundler-cache: true
2222
- name: Run rubocop
2323
run: bundle exec rubocop
24-
#test:
25-
# needs: lint
26-
# runs-on: ubuntu-latest
27-
# strategy:
28-
# matrix:
29-
# ruby-version:
30-
# - 2.6
31-
# - 2.7
32-
# - 3.0
33-
# - 3.1
34-
# steps:
35-
# - uses: actions/checkout@v3
36-
# - name: Setup Ruby
37-
# uses: ruby/setup-ruby@v1
38-
# with:
39-
# ruby-version: ${{ matrix.ruby-version }}
40-
# bundler-cache: true
41-
# - name: Run the test suite
42-
# run: bundle exec rspec
24+
test:
25+
needs: lint
26+
runs-on: ubuntu-latest
27+
strategy:
28+
matrix:
29+
ruby-version:
30+
- 2.6
31+
- 2.7
32+
- 3.0
33+
- 3.1
34+
steps:
35+
- uses: actions/checkout@v3
36+
- name: Setup Ruby
37+
uses: ruby/setup-ruby@v1
38+
with:
39+
ruby-version: ${{ matrix.ruby-version }}
40+
bundler-cache: true
41+
- name: Run the test suite
42+
run: bundle exec rspec

.rubocop.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ Metrics/ParameterLists:
2626
Enabled: false
2727
Metrics/PerceivedComplexity:
2828
Enabled: false
29-
Naming/MethodParameterName:
30-
AllowedNames:
31-
- az # availability zone
32-
- id
33-
- lb # load balancer
29+
RSpec/NamedSubject:
30+
Enabled: false
31+
RSpec/SubjectStub:
32+
Enabled: false
3433
Style/Documentation:
3534
Enabled: false
3635
Style/HashSyntax:

lib/riemann/tools/postgresql.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def tick
5959
@conn.exec("DECLARE connection CURSOR FOR SELECT datname, count(datname) FROM pg_stat_activity \
6060
GROUP BY pg_stat_activity.datname")
6161

62-
result = @conn.exec('FETCH ALL in connection')
62+
result = @conn.exec('FETCH ALL IN connection')
6363
result.values.collect do |row|
6464
vals = row.collect.to_a
6565
report(

spec/riemann/tools/postgresql_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
require 'riemann/tools/postgresql'
4+
5+
RSpec.describe Riemann::Tools::Postgresql do
6+
describe('#tick') do
7+
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] }
8+
let(:general_values) do
9+
[
10+
%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],
11+
%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],
12+
]
13+
end
14+
let(:connection_fields) { %w[datname count] }
15+
let(:connection_values) do
16+
[
17+
%w[template1 4],
18+
%w[postgres 1],
19+
[nil, '0'],
20+
]
21+
end
22+
23+
before do
24+
general_result = double
25+
allow(general_result).to receive_messages(fields: general_fields, values: general_values)
26+
27+
connection_result = double
28+
allow(connection_result).to receive_messages(fields: connection_fields, values: connection_values)
29+
30+
conn = double
31+
allow(conn).to receive(:transaction).and_yield
32+
allow(conn).to receive(:exec).with(/\ADECLARE [a-z]+ CURSOR FOR/)
33+
allow(conn).to receive(:exec).with(/\AFETCH ALL IN general/).and_return(general_result)
34+
allow(conn).to receive(:exec).with(/\AFETCH ALL IN connection/).and_return(connection_result)
35+
36+
allow(PG).to receive(:connect).and_return(conn)
37+
allow(subject).to receive(:report) # RSpec/SubjectStub
38+
subject.tick
39+
end
40+
41+
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')) }
42+
it { is_expected.to have_received(:report).with(hash_including(service: 'DB template1 connections', metric: 4, state: 'ok', description: 'PostgreSQL DB Connections')) }
43+
end
44+
end

0 commit comments

Comments
 (0)