From 5e2ad694f376da4e034f53c37b333c79f77023dc Mon Sep 17 00:00:00 2001 From: Petr Chalupa Date: Mon, 20 Jan 2014 15:54:09 +0100 Subject: [PATCH] Add test for ::Actions::Pulp::Repository::Sync --- app/lib/actions/helpers/pulp_task.rb | 13 +++-- app/lib/actions/pulp/repository/sync.rb | 31 ++++++----- test/actions/pulp/repository/sync_test.rb | 66 +++++++++++++++++++++++ 3 files changed, 91 insertions(+), 19 deletions(-) create mode 100644 test/actions/pulp/repository/sync_test.rb diff --git a/app/lib/actions/helpers/pulp_task.rb b/app/lib/actions/helpers/pulp_task.rb index 8cb8eb78600..621995274a4 100644 --- a/app/lib/actions/helpers/pulp_task.rb +++ b/app/lib/actions/helpers/pulp_task.rb @@ -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 diff --git a/app/lib/actions/pulp/repository/sync.rb b/app/lib/actions/pulp/repository/sync.rb index ba1ef0e10b1..0e22cfb087d 100644 --- a/app/lib/actions/pulp/repository/sync.rb +++ b/app/lib/actions/pulp/repository/sync.rb @@ -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] diff --git a/test/actions/pulp/repository/sync_test.rb b/test/actions/pulp/repository/sync_test.rb new file mode 100644 index 00000000000..1724dc75ea0 --- /dev/null +++ b/test/actions/pulp/repository/sync_test.rb @@ -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