diff --git a/definitions/checks/check_subscription_manager_release.rb b/definitions/checks/check_subscription_manager_release.rb new file mode 100644 index 000000000..2ce508588 --- /dev/null +++ b/definitions/checks/check_subscription_manager_release.rb @@ -0,0 +1,41 @@ +class Checks::CheckSubscriptionManagerRelease < ForemanMaintain::Check + metadata do + label :check_subscription_manager_release + description 'Check if subscription-manager release is not set to a minor version' + + confine do + feature(:instance).downstream + end + end + + def run + status, output = execute_with_status('LC_ALL=C subscription-manager release --show') + + # If command fails (subscription-manager not installed or system not registered), pass the check + return if status != 0 + + assert(valid_release?(output), error_message(output)) + end + + private + + def valid_release?(output) + # Valid formats: "Release not set" or "Release: X" where X is a major version without dots + return true if output == 'Release not set' + return true if /^Release:\s+\d+$/.match?(output) + + false + end + + def extract_release(output) + match = output.match(/^Release:\s+(.+)$/) + match ? match[1] : output + end + + def error_message(output) + subman_release = extract_release(output) + "Your system is configured to use RHEL #{subman_release}, but Satellite is only "\ + "supported on the latest RHEL. Please unset the release in subscription-manager by "\ + "calling `subscription-manager release --unset`." + end +end diff --git a/definitions/scenarios/satellite_upgrade.rb b/definitions/scenarios/satellite_upgrade.rb index b1daa7b6b..936911f0d 100644 --- a/definitions/scenarios/satellite_upgrade.rb +++ b/definitions/scenarios/satellite_upgrade.rb @@ -35,6 +35,7 @@ def compose Checks::SystemRegistration, Checks::CheckHotfixInstalled, Checks::CheckTmout, + Checks::CheckSubscriptionManagerRelease, Checks::CheckUpstreamRepository, Checks::Container::PodmanLogin, # if downstream, connected, containers used Checks::Disk::AvailableSpace, diff --git a/definitions/scenarios/update.rb b/definitions/scenarios/update.rb index 7a8c80461..7f34cb569 100644 --- a/definitions/scenarios/update.rb +++ b/definitions/scenarios/update.rb @@ -33,6 +33,7 @@ def compose Checks::SystemRegistration, Checks::CheckHotfixInstalled, Checks::CheckTmout, + Checks::CheckSubscriptionManagerRelease, Checks::CheckIpv6Disable, Checks::CheckUpstreamRepository, Checks::Container::PodmanLogin, # if downstream, connected, containers used diff --git a/test/definitions/checks/check_subscription_manager_release_test.rb b/test/definitions/checks/check_subscription_manager_release_test.rb new file mode 100644 index 000000000..fb3cd7435 --- /dev/null +++ b/test/definitions/checks/check_subscription_manager_release_test.rb @@ -0,0 +1,47 @@ +require 'test_helper' + +describe Checks::CheckSubscriptionManagerRelease do + include DefinitionsTestHelper + + subject { Checks::CheckSubscriptionManagerRelease.new } + + before do + assume_feature_present(:satellite) + end + + it 'succeeds when release is not set' do + subject.expects(:execute_with_status).with('LC_ALL=C subscription-manager release --show'). + returns([ + 0, 'Release not set' + ]) + result = run_step(subject) + + assert result.success? + end + + it 'succeeds when release is set to a major version' do + subject.expects(:execute_with_status).with('LC_ALL=C subscription-manager release --show'). + returns([0, 'Release: 9']) + result = run_step(subject) + + assert result.success? + end + + it 'fails when release is set to a minor version' do + subject.expects(:execute_with_status).with('LC_ALL=C subscription-manager release --show'). + returns([0, 'Release: 9.5']) + result = run_step(subject) + + assert result.fail? + assert_match(/Your system is configured to use RHEL 9\.5/, result.output) + assert_match(/subscription-manager release --unset/, result.output) + end + + it 'succeeds when subscription-manager is not installed or system is not registered' do + subject.expects(:execute_with_status).with('LC_ALL=C subscription-manager release --show'). + returns([1, 'This system is not yet registered.']) + result = run_step(subject) + + assert result.success? + end +end diff --git a/test/definitions/scenarios/capsule_upgrade_test.rb b/test/definitions/scenarios/capsule_upgrade_test.rb index 718fb3137..22e8b0c68 100644 --- a/test/definitions/scenarios/capsule_upgrade_test.rb +++ b/test/definitions/scenarios/capsule_upgrade_test.rb @@ -23,6 +23,7 @@ Checks::SystemRegistration, Checks::CheckHotfixInstalled, Checks::CheckTmout, + Checks::CheckSubscriptionManagerRelease, Checks::CheckUpstreamRepository, Checks::Disk::AvailableSpace, Checks::NonRhPackages, diff --git a/test/definitions/scenarios/satellite_upgrade_test.rb b/test/definitions/scenarios/satellite_upgrade_test.rb index eaea0c972..54615480e 100644 --- a/test/definitions/scenarios/satellite_upgrade_test.rb +++ b/test/definitions/scenarios/satellite_upgrade_test.rb @@ -72,6 +72,7 @@ Checks::SystemRegistration, Checks::CheckHotfixInstalled, Checks::CheckTmout, + Checks::CheckSubscriptionManagerRelease, Checks::CheckUpstreamRepository, Checks::Disk::AvailableSpace, Checks::Disk::AvailableSpaceCandlepin, diff --git a/test/definitions/scenarios/update_test.rb b/test/definitions/scenarios/update_test.rb index 91b917ad0..515883885 100644 --- a/test/definitions/scenarios/update_test.rb +++ b/test/definitions/scenarios/update_test.rb @@ -44,6 +44,7 @@ Checks::SystemRegistration, Checks::CheckHotfixInstalled, Checks::CheckTmout, + Checks::CheckSubscriptionManagerRelease, Checks::CheckUpstreamRepository, Checks::Disk::AvailableSpace, Checks::Disk::AvailableSpaceCandlepin, @@ -165,6 +166,7 @@ Checks::SystemRegistration, Checks::CheckHotfixInstalled, Checks::CheckTmout, + Checks::CheckSubscriptionManagerRelease, Checks::CheckUpstreamRepository, Checks::Disk::AvailableSpace, Checks::Disk::AvailableSpaceCandlepin, @@ -286,6 +288,7 @@ Checks::SystemRegistration, Checks::CheckHotfixInstalled, Checks::CheckTmout, + Checks::CheckSubscriptionManagerRelease, Checks::CheckUpstreamRepository, Checks::Disk::AvailableSpace, Checks::Disk::AvailableSpaceCandlepin,