Skip to content

Commit

Permalink
Refs #38012 - Refactor java version parsing so it can be tested
Browse files Browse the repository at this point in the history
This moves the code around to easily test the logic without making
actual changes to the logic. Arguably this also makes the hook itself
easier to read.

(cherry picked from commit 10647cd)
  • Loading branch information
ekohl committed Nov 19, 2024
1 parent 3af9f20 commit 78e4b95
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
8 changes: 8 additions & 0 deletions hooks/boot/01-kafo-hook-extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ def available_space(directory = nil)
end
mountpoint[:available_bytes]
end

def parse_java_version(output)
output&.match(/version "\d+\.(?<version>\d+)\.\d+/) do |java_match|
return unless (version = java_match[:version])

yield version.to_i
end
end
end

Kafo::HookContext.send(:include, HookContextExtension)
6 changes: 3 additions & 3 deletions hooks/pre/31-puppet_puppet_server_invalid_java.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
logger.debug("Found Puppetserver #{puppetserver_match[:version]}")
if puppetserver_match[:version] == '8'
java_stdout_stderr, _status = execute_command("source #{sysconfig_file} ; $JAVA_BIN -version", false, true)
java_stdout_stderr&.match(/version "\d+\.(?<version>\d+)\.\d+/) do |java_match|
if java_match[:version] && java_match[:version].to_i < 11
logger.info "Detected Java #{java_match[:version]} which is too old for Puppetserver #{puppetserver_match[:version]}"
parse_java_version(java_stdout_stderr) do |java_version|
if java_version < 11
logger.info "Detected Java #{java_version} which is too old for Puppetserver #{puppetserver_match[:version]}"
if app_value(:noop)
logger.debug 'Would stop puppetserver.service'
else
Expand Down
16 changes: 16 additions & 0 deletions spec/hook_context_extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@
allow(context).to receive(:logger).and_return(logger)
end

describe '.parse_java_version' do
context 'java-1.8.0-openjdk-headless' do
let(:output) do
<<~OUTPUT
openjdk version "1.8.0_362"
OpenJDK Runtime Environment (build 1.8.0_362-b08)
OpenJDK 64-Bit Server VM (build 25.362-b08, mixed mode)
OUTPUT
end

it do
expect { |block| context.parse_java_version(output, &block) }.to yield_with_args(8)
end
end
end

describe '.ensure_packages' do
subject { context.ensure_packages(packages, state) }

Expand Down

0 comments on commit 78e4b95

Please sign in to comment.