Skip to content

Commit da42b65

Browse files
Merge pull request #8 from aboutsource/fix/local_release_gathering
Use regex to extract release string
2 parents d23bc12 + 1f50391 commit da42b65

4 files changed

Lines changed: 116 additions & 62 deletions

File tree

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ source 'https://rubygems.org'
44

55
# Specify your gem's dependencies in sensu-plugin-minio.gemspec
66
gemspec
7+
8+
gem 'parallel', '< 1.20.0'

Gemfile.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ GEM
1717
jaro_winkler (1.5.4)
1818
json (2.6.2)
1919
mixlib-cli (1.7.0)
20-
parallel (1.22.1)
20+
parallel (1.19.2)
2121
parser (3.1.2.0)
2222
ast (~> 2.4.1)
2323
public_suffix (4.0.7)
@@ -60,6 +60,7 @@ PLATFORMS
6060

6161
DEPENDENCIES
6262
bundler (~> 2.1)
63+
parallel (< 1.20.0)
6364
rake (~> 13.0)
6465
rspec (~> 3.10)
6566
rubocop (~> 0.54, <= 0.81)

bin/check-minio-update.rb

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
class CheckMinioUpdate < Sensu::Plugin::Check::CLI
2222
include Sensu::Plugin::Utils
2323

24+
RELEASE_PATTERN = /(?<release>RELEASE.\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}Z)/.freeze # rubocop:disable Layout/LineLength
25+
2426
option :checkurl,
2527
description: 'Base URL to check for updates',
2628
short: '-u URL',
@@ -39,49 +41,55 @@ class CheckMinioUpdate < Sensu::Plugin::Check::CLI
3941
default: 30
4042

4143
def run
42-
checkurl = config[:checkurl]
43-
platform = config[:platform]
44-
timeout = config[:timeout].to_i
45-
46-
begin
47-
Timeout.timeout(timeout) do
48-
latest_version = get_latest_version(checkurl, platform)
49-
50-
if local_version == latest_version
51-
ok 'No new minio version available'
52-
else
53-
critical "New minio version available #{latest_version}"
54-
end
44+
Timeout.timeout(config[:timeout].to_i) do
45+
if local_version == latest_version
46+
ok 'No new minio version available'
47+
else
48+
critical "New minio version available #{latest_version}"
5549
end
56-
rescue IOError => e
57-
unknown e.message.to_s
58-
rescue Timeout::Error
59-
unknown 'Connection timed out'
6050
end
51+
rescue IOError => e
52+
unknown e.message.to_s
53+
rescue Timeout::Error
54+
unknown 'Connection timed out'
6155
end
6256

6357
private
6458

65-
def get_latest_version(checkurl, platform)
66-
uri = URI.parse("#{checkurl}/#{platform}/minio.shasum")
67-
response = Net::HTTP.get_response(uri)
59+
def latest_version
60+
@latest_version ||= begin
61+
uri = URI.parse("#{config[:checkurl]}/#{config[:platform]}/minio.shasum")
62+
response = Net::HTTP.get_response(uri)
6863

69-
unless response.is_a?(Net::HTTPSuccess)
70-
raise IOError, "Unable to gather latest minio version: #{response.body}"
71-
end
64+
unless response.is_a?(Net::HTTPSuccess)
65+
raise IOError, "Unable to gather latest minio version: #{response.body}"
66+
end
7267

73-
response.body.split.last.split('.', 2).last
68+
extract_release(response.body)
69+
end
7470
end
7571

7672
def local_version
77-
stdout, stderr, status = Open3.capture3(
78-
{ 'PATH' => ENV['PATH'] }, 'minio --version', unsetenv_others: true
79-
)
73+
@local_version ||= begin
74+
stdout, stderr, status = Open3.capture3(
75+
{ 'PATH' => ENV['PATH'] }, 'minio --version', unsetenv_others: true
76+
)
77+
78+
unless status.success?
79+
raise IOError, "Unable to gather local minio version: #{stderr}"
80+
end
81+
82+
extract_release(stdout)
83+
end
84+
end
85+
86+
def extract_release(release_source_str)
87+
match_data = RELEASE_PATTERN.match(release_source_str)
8088

81-
unless status.success?
82-
raise IOError, "Unable to gather local minio version: #{stderr}"
89+
if match_data.nil?
90+
raise IOError, "Unable to extract release: #{release_source_str}"
8391
end
8492

85-
stdout.lines.first.split.last
93+
match_data[:release]
8694
end
8795
end

spec/check_minio_update_spec.rb

Lines changed: 74 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323

2424
let(:response) do
2525
{
26-
body:
27-
'285ec90006a6961ebcb7dd9685acc0ebcd08f561 '\
28-
'minio.RELEASE.2021-07-08T19-43-25Z',
26+
body: '3832278ee2bb74d41b617788b4244e410b29e4a8 minio.RELEASE.2022-07-17T15-43-14Z', # rubocop:disable Layout/LineLength
2927
status: 200
3028
}
3129
end
@@ -40,16 +38,14 @@
4038
end
4139

4240
let(:stdout) do
43-
"minio version RELEASE.2021-07-08T19-43-25Z\n"\
44-
"commit: dd53b287f2eeed9cd3872eeae7d64696bfd7829d\n"\
45-
'go version: go1.18.3'
41+
'minio version RELEASE.2022-07-17T15-43-14Z (commit-id=1b339ea062b423f1c6fbeb02116d020d18418917)' # rubocop:disable Layout/LineLength
4642
end
4743

4844
let(:stderr) { nil }
4945
let(:success) { true }
5046

5147
context 'with matching local and remote version' do
52-
it 'should be ok if versions are equal' do
48+
it 'should be ok' do
5349
expect { check.run }.to raise_error do |error|
5450
expect(error).to be_a SystemExit
5551
expect(error.status).to eq 0
@@ -58,6 +54,7 @@
5854
expect(check).to have_received(:output).with(
5955
'No new minio version available'
6056
)
57+
6158
expect(checksum_request).to have_been_requested
6259
end
6360
end
@@ -67,7 +64,7 @@
6764
{
6865
body:
6966
'285ec90006a6961ebcb7dd9685acc0ebcd08f561 '\
70-
'minio.RELEASE.2022-07-08T19-43-25Z',
67+
'minio.RELEASE.3022-07-17T15-43-14Z',
7168
status: 200
7269
}
7370
end
@@ -79,43 +76,89 @@
7976
end
8077

8178
expect(check).to have_received(:output).with(
82-
'New minio version available RELEASE.2022-07-08T19-43-25Z'
79+
'New minio version available RELEASE.3022-07-17T15-43-14Z'
8380
)
81+
8482
expect(checksum_request).to have_been_requested
8583
end
8684
end
8785

8886
context 'with unknown local version' do
89-
let(:stdout) { nil }
90-
let(:stderr) { 'Minio not found' }
91-
let(:success) { false }
92-
93-
it 'should be unknown' do
94-
expect { check.run }.to raise_error do |error|
95-
expect(error).to be_a SystemExit
96-
expect(error.status).to eq 3
87+
context 'when minio executable not found' do
88+
let(:stdout) { nil }
89+
let(:stderr) { 'minio not found' }
90+
let(:success) { false }
91+
92+
it 'should be unknown' do
93+
expect { check.run }.to raise_error do |error|
94+
expect(error).to be_a SystemExit
95+
expect(error.status).to eq 3
96+
end
97+
98+
expect(check).to have_received(:output).with(
99+
'Unable to gather local minio version: minio not found'
100+
)
101+
102+
expect(checksum_request).not_to have_been_requested
97103
end
104+
end
98105

99-
expect(check).to have_received(:output).with(
100-
'Unable to gather local minio version: Minio not found'
101-
)
102-
expect(checksum_request).to have_been_requested
106+
context 'when release could not be extracted' do
107+
let(:stdout) { 'INVALID' }
108+
109+
it 'should be unknown' do
110+
expect { check.run }.to raise_error do |error|
111+
expect(error).to be_a SystemExit
112+
expect(error.status).to eq 3
113+
end
114+
115+
expect(check).to have_received(:output).with(
116+
'Unable to extract release: INVALID'
117+
)
118+
119+
expect(checksum_request).not_to have_been_requested
120+
end
103121
end
104122
end
105123

106-
context 'with release url not found' do
107-
let(:response) { { body: '404 Not Found', status: 404 } }
124+
context 'with unknown remote version' do
125+
context 'with release url not found' do
126+
let(:response) { { body: '404 Not Found', status: 404 } }
108127

109-
it 'should be unknown' do
110-
expect { check.run }.to raise_error do |error|
111-
expect(error).to be_a SystemExit
112-
expect(error.status).to eq 3
128+
it 'should be unknown' do
129+
expect { check.run }.to raise_error do |error|
130+
expect(error).to be_a SystemExit
131+
expect(error.status).to eq 3
132+
end
133+
134+
expect(check).to have_received(:output).with(
135+
'Unable to gather latest minio version: 404 Not Found'
136+
)
137+
138+
expect(checksum_request).to have_been_requested
113139
end
140+
end
114141

115-
expect(check).to have_received(:output).with(
116-
'Unable to gather latest minio version: 404 Not Found'
117-
)
118-
expect(checksum_request).to have_been_requested
142+
context 'when release could not be extracted' do
143+
let(:response) do
144+
{
145+
body: '3832278ee2bb74d41b617788b4244e410b29e4a8 INVALID',
146+
status: 200
147+
}
148+
end
149+
150+
it 'should be unknown' do
151+
expect { check.run }.to raise_error do |error|
152+
expect(error).to be_a SystemExit
153+
expect(error.status).to eq 3
154+
end
155+
156+
expect(check).to have_received(:output).with(
157+
'Unable to extract release: 3832278ee2bb74d41b617788b4244e410b29e4a8 INVALID' # rubocop:disable Layout/LineLength
158+
)
159+
160+
expect(checksum_request).to have_been_requested
161+
end
119162
end
120163
end
121164
end

0 commit comments

Comments
 (0)