From fd8fa694016f7e7a02c78223fb791835763b872c Mon Sep 17 00:00:00 2001 From: Oscar Barrios Date: Thu, 30 Jan 2025 17:03:15 +0100 Subject: [PATCH] Refactor how we check if a channel is synchronized --- .../step_definitions/command_steps.rb | 19 +++++----- testsuite/features/support/commonlib.rb | 36 ++++++++++++++++--- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/testsuite/features/step_definitions/command_steps.rb b/testsuite/features/step_definitions/command_steps.rb index 99f0a8c17b18..f8781e4bd5d3 100644 --- a/testsuite/features/step_definitions/command_steps.rb +++ b/testsuite/features/step_definitions/command_steps.rb @@ -168,7 +168,7 @@ raise ScriptError, "Synchronization error, channel #{channel} or #{channel}-#{architecture} in #{product} product not found" if channels_to_synchronize.nil? || channels_to_synchronize.empty? channels_to_synchronize.each do |os_product_version_channel| - command = "spacewalk-common-channels -u admin -p admin -a #{architecture} #{os_product_version_channel}" + command = "spacewalk-common-channels -u admin -p admin -a #{architecture} #{os_product_version_channel.gsub("-#{architecture}", '')}" get_target('server').run(command, verbose: true) log "Channel #{os_product_version_channel} added" end @@ -350,7 +350,7 @@ # Remove from the list to kill those channels that are already synced channels_to_kill.each do |remaining_channel| - if channel_is_synced(remaining_channel) + if channel_sync_completed?(remaining_channel) log "Channel '#{remaining_channel}' is already synced, so there is no need to kill repo-sync process." channels_to_kill.delete(remaining_channel) end @@ -363,17 +363,20 @@ time_spent = 0 checking_rate = 5 repeat_until_timeout(timeout: 60, message: 'Some reposync processes were not killed properly', dont_raise: true) do - command_output, _code = get_target('server').run("ps axo pid,cmd | grep /usr/bin/spacewalk-repo-sync --channel #{channel} | grep -v grep", check_errors: false) + command_output, _code = get_target('server').run('ps axo pid,cmd | grep spacewalk-repo-sync | grep -v grep', verbose: true, check_errors: false) process = command_output.split("\n")[0] + channel_synchronizing = process.split[5].strip if process.nil? log "#{time_spent / 60.to_i} minutes waiting for '#{channel}' channel to start its repo-sync processes." if ((time_spent += checking_rate) % 60).zero? sleep checking_rate next - else + elsif channel_synchronizing == channel pid = process.split[0] - get_target('server').run("kill #{pid}", check_errors: false) + get_target('server').run("kill #{pid}", verbose: true, check_errors: false) log "Reposync of channel #{channel} killed" break + else + log "Warning: Repo-sync process for channel '#{channel_synchronizing}' running." end end end @@ -419,7 +422,7 @@ end begin repeat_until_timeout(timeout: timeout, message: 'Channel not fully synced') do - break if channel_is_synced(channel) + break if channel_sync_completed?(channel) log "#{time_spent / 60.to_i} minutes out of #{timeout / 60.to_i} waiting for '#{channel}' channel to be synchronized" if ((time_spent += checking_rate) % 60).zero? sleep checking_rate @@ -441,7 +444,7 @@ time_spent = 0 checking_rate = 10 # Let's start with a timeout margin aside from the sum of the timeouts for each channel - timeout = 600 + timeout = 900 channels_to_wait.each do |channel| if TIMEOUT_BY_CHANNEL_NAME[channel].nil? log "Unknown timeout for channel #{channel}, assuming one minute" @@ -453,7 +456,7 @@ begin repeat_until_timeout(timeout: timeout, message: 'Product not fully synced') do channels_to_wait.each do |channel| - if channel_is_synced(channel) + if channel_sync_completed?(channel) channels_to_wait.delete(channel) log "Channel #{channel} finished syncing" end diff --git a/testsuite/features/support/commonlib.rb b/testsuite/features/support/commonlib.rb index c7a2e65def91..c09384c11c36 100644 --- a/testsuite/features/support/commonlib.rb +++ b/testsuite/features/support/commonlib.rb @@ -584,11 +584,35 @@ def update_controller_ca certutil -d sql:/root/.pki/nssdb -A -t TC -n "susemanager" -i /etc/pki/trust/anchors/#{server_name}.cert` end +# This method checks if the synchronization for the given channel is completed +# +# @param channel_name [String] the channel to check +# @return [Boolean] true if the synchronization is completed, false otherwise +def channel_sync_completed?(channel_name) + log_tmp_file = '/tmp/reposync.log' + get_target('server').extract('/var/log/rhn/reposync.log', log_tmp_file) + raise ScriptError, 'The file with repository synchronization logs doesn\'t exist or is empty' if !File.exist?(log_tmp_file) || File.empty?(log_tmp_file) + + log_content = File.readlines(log_tmp_file) + channel_found = false + log_content.each do |line| + if line.include?('Channel: ') && line.include?(channel_name) + channel_found = true + elsif line.include?('Channel: ') && !line.include?(channel_name) + channel_found = false + elsif line.include?('Sync of channel completed.') && channel_found + return true + end + end + + false +end + # Determines whether a channel is synchronized on the server. # # @param channel [String] The name of the channel to check. # @return [Boolean] Returns true if the channel is synchronized, false otherwise. -def channel_is_synced(channel) +def channel_is_synced?(channel) sync_status = false # solv is the last file to be written when the server synchronizes a channel, therefore we wait until it exist result, code = get_target('server').run("dumpsolv /var/cache/rhn/repodata/#{channel}/solv", verbose: false, check_errors: false) @@ -736,9 +760,13 @@ def wait_action_complete(actionid, timeout: DEFAULT_TIMEOUT) # @param channels [Array] The list of channels to filter. # @param filters [Array] The list of filters to apply. def filter_channels(channels, filters = []) - filtered_channels = channels.clone - filters.each do |filter| - filtered_channels.delete_if { |channel| channel.include? filter } + if channels.nil? || channels.empty? + puts 'Warning: No channels to filter' + else + filtered_channels = channels.clone + filters.each do |filter| + filtered_channels.delete_if { |channel| channel.include? filter } + end end filtered_channels end