Skip to content

Commit

Permalink
Add test for ::Actions::Pulp::Repository::Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
pitr-ch committed Jan 20, 2014
1 parent b41ad9c commit 5e2ad69
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 19 deletions.
13 changes: 8 additions & 5 deletions app/lib/actions/helpers/pulp_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@ module Helpers
module PulpTask
include Dynflow::Action::Polling

private
def done?
!!external_task[:finish_time]
end

def external_task
output[:pulp_task]
end

private

def external_task=(external_task_data)
output[:pulp_task] = external_task_data
end

def poll_external_task
as_pulp_user { ::Katello.pulp_server.resources.task.poll(external_task[:task_id]) }
as_pulp_user { task_resource.poll(external_task[:task_id]) }
end

def done?
!!external_task[:finish_time]
def task_resource
::Katello.pulp_server.resources.task
end

end
end
end
31 changes: 17 additions & 14 deletions app/lib/actions/pulp/repository/sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,32 @@ class Sync < Actions::Base
end

def invoke_external_task
sync_options = {}
sync_options[:max_speed] ||= ::Katello.config.pulp.sync_KBlimit if ::Katello.config.pulp.sync_KBlimit # set bandwidth limit
sync_options[:num_threads] ||= ::Katello.config.pulp.sync_threads if ::Katello.config.pulp.sync_threads # set threads per sync
sync_options = {}

pulp_tasks = pulp_resources.repository.sync(input[:pulp_id], { override_config: sync_options })
output[:pulp_tasks] = pulp_tasks
if ::Katello.config.pulp.sync_KBlimit
# set bandwidth limit
sync_options[:max_speed] ||= ::Katello.config.pulp.sync_KBlimit
end
if ::Katello.config.pulp.sync_threads
# set threads per sync
sync_options[:num_threads] ||= ::Katello.config.pulp.sync_threads
end

output[:pulp_tasks] = pulp_tasks =
pulp_resources.repository.sync(input[:pulp_id], { override_config: sync_options })

# TODO: would be better polling for the whole task group to make sure
# we're really finished at the end.
# Look at it once we have more Pulp actions rewritten so that we can find
# a common pattern.
pulp_task = pulp_tasks.find do |task|
task['tags'].include?("pulp:action:sync")
end
return pulp_task
pulp_tasks.find { |task| task['tags'].include?('pulp:action:sync') }
end

def run_progress
sync_task = output[:pulp_task]
content_progress = sync_task &&
sync_task[:progress] &&
sync_task[:progress][:yum_importer] &&
sync_task[:progress][:yum_importer][:content]
content_progress = external_task &&
external_task[:progress] &&
external_task[:progress][:yum_importer] &&
external_task[:progress][:yum_importer][:content]

if content_progress && content_progress[:size_total].to_i > 0
left = content_progress[:size_left].to_f / content_progress[:size_total]
Expand Down
66 changes: 66 additions & 0 deletions test/actions/pulp/repository/sync_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#
# Copyright 2013 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public
# License as published by the Free Software Foundation; either version
# 2 of the License (GPLv2) or (at your option) any later version.
# There is NO WARRANTY for this software, express or implied,
# including the implied warranties of MERCHANTABILITY,
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of GPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.

require 'katello_test_helper'

module Katello
action_class = ::Actions::Pulp::Repository::Sync

describe action_class do
include Dynflow::Testing

def progress_hash(left, total)
{ 'task_id' => '76fb4115-2ec4-4945-815b-0f9d216b4183',
'progress' => {
'yum_importer' => {
'content' => {
'size_total' => total,
'size_left' => left } } } }

end

it 'runs' do
User.stubs(:current).returns mock('user', remote_id: 'user')
action = create_action action_class
task1 = { 'tags' => ['pulp:action:sync'],
'task_id' => '76fb4115-2ec4-4945-815b-0f9d216b4183' }
task2 = task1.merge progress_hash 6, 8
task3 = task1.merge(progress_hash 0, 8).merge('finish_time' => 'now')
pulp_response = [task1, { 'task_id' => 'other' }]

plan_action action, pulp_id: 'pulp-id'
action = run_action action do |action|
repository = mock 'repository',
sync: pulp_response
pulp_resources = mock 'pulp_resources', repository: repository
action.expects(:pulp_resources).returns(pulp_resources)
action.
stubs(:task_resource).
returns(mock('task_resource').
tap { |m| m.expects(:poll).twice.returns(task2, task3) })
end

action.external_task.must_equal(task1)
action.run_progress.must_equal 0.01

clock_progress action
action.external_task.must_equal task2
action.run_progress.must_equal 0.25
action.wont_be :done?

clock_progress action
action.external_task.must_equal task3
action.run_progress.must_equal 1
action.must_be :done?
end
end
end

0 comments on commit 5e2ad69

Please sign in to comment.