Use `agent version` instead of `agent status` to determine the current Agent version
Replaced status by version and changed mocking spec functions
MODIFY
libraries/recipe_helpers.rb
libraries/recipe_helpers.rb
@@ -155,18 +155,21 @@ class Chef
private
include Chef::Mixin::ShellOut
- def agent_status
+ def agent_get_version
return nil unless File.exist?(WIN_BIN_PATH)
- shell_out("\"#{WIN_BIN_PATH}\" status").stdout.strip
+ shell_out("\"#{WIN_BIN_PATH}\" version").stdout.strip
end
def fetch_current_version
- status = agent_status
- return nil if status.nil?
- match_data = status.match(/^Agent \(v(.*)\)/)
-
- # Nightlies like 6.20.0-devel+git.38.cd7f989 fail to parse as Gem::Version because of the '+' sign
- version = match_data[1].tr('+', '-') if match_data
+ raw_version = agent_get_version
+ return nil if raw_version.nil?
+ match_data = raw_version.match(/^Agent ([^\s]*) (- Meta: ([^\s]*) )?- Commit/)
+
+ version = match_data[1] if match_data
+ nightly_version = match_data[3] if match_data[2]
+ # If the Meta tag is catched, we'll add it to the version to specify the nightly version we're using
+ # Nightlies like 6.20.0-devel+git.38.cd7f989 fail to parse as Gem::Version because of the '+' sign so let's use '-'
+ version = version + '-' + nightly_version if nightly_version
Gem::Version.new(version) if version
end

MODIFY
spec/recipe_helpers_spec.rb
spec/recipe_helpers_spec.rb
@@ -37,15 +37,13 @@ describe Chef::Datadog::WindowsInstallHelpers do
context 'when the agent is installed' do
before do
allow(Chef::Datadog::WindowsInstallHelpers)
- .to receive(:agent_status)
- .and_return(agent_status_string)
+ .to receive(:agent_get_version)
+ .and_return(agent_version_string)
end
- let(:agent_status_string) do
+ let(:agent_version_string) do
%(
-.....
-Agent (v6.20.0)
-....
+Agent 6.20.0 - Commit: 3c29612 - Serialization version: v5.0.22 - Go version: go1.17.11
)
end
@@ -70,11 +68,9 @@ Agent (v6.20.0)
end
context 'when the current agent version is a nightly' do
- let(:agent_status) do
+ let(:agent_get_version) do
%(
-.....
-Agent (v6.20.0-devel+git.38.cd7f989)
-....
+Agent 6.20.0-devel - Meta: git.38.cd7f989 - Commit: cd7f98964 - Serialization version: v5.0.22 - Go version: go1.17.11
)
end

Use `agent version` instead of `agent status` to determine the current Agent version (#852)

Replaced status by version and changed mocking spec functions