Project

General

Profile

Download (3.95 KB) Statistics
| Branch: | Tag: | Revision:
8db5edf8 Eric D Helms
require 'rubygems'

661ca339 Justin Sherrill
require './lib/runcible'
8db5edf8 Eric D Helms
661ca339 Justin Sherrill
class RepositorySupport
926b9789 Justin Sherrill
FIXTURE_PATH = '/var/www/repositories'

3e3ad61c Partha Aji
@@repo_id = 'integration_test_id'
661ca339 Justin Sherrill
@@repo_name = @@repo_id
ccaa42a7 Partha Aji
3e3ad61c Partha Aji
def initialize(type = 'yum')
490ff199 David Davis
@repo_resource = TestRuncible.server.extensions.repository
@schedule_resource = TestRuncible.server.resources.repository_schedule
@repo_extension = TestRuncible.server.extensions.repository
@task_resource = TestRuncible.server.resources.task
@schedule_time = '2012-09-25T20:44:00Z/P7D'
@repo_type = type
@importer_type = "#{@repo_type}_importer"

3e3ad61c Partha Aji
if @repo_type == 'yum'
9fc51910 Mike McCune
@distributors = [Runcible::Models::YumDistributor.new('/path', true, true, :id => 'yum_dist')]
926b9789 Justin Sherrill
@repo_url = "file://#{FIXTURE_PATH}/zoo5"
3e3ad61c Partha Aji
elsif @repo_type == 'puppet'
9fc51910 Mike McCune
@distributors = [Runcible::Models::PuppetDistributor.new('/path', true, true, :id => 'puppet_dist')]
3e3ad61c Partha Aji
@repo_url = 'http://davidd.fedorapeople.org/repos/random_puppet/'
63540c7c Eric D. Helms
elsif @repo_type == 'python'
@distributors = [Runcible::Models::PythonDistributor.new]
@repo_url = "https://pypi.python.org"
490ff199 David Davis
end
661ca339 Justin Sherrill
end
8db5edf8 Eric D Helms
661ca339 Justin Sherrill
def distributor
@repo_extension.retrieve_with_details(RepositorySupport.repo_id)['distributors'].first
a92f59a9 Partha Aji
end

8db5edf8 Eric D Helms
def self.repo_name
661ca339 Justin Sherrill
@@repo_name
8db5edf8 Eric D Helms
end

def self.repo_id
661ca339 Justin Sherrill
@@repo_id
8db5edf8 Eric D Helms
end

def self.repo_url
b4428416 Manjiri Tapaswi
@@repo_url
8db5edf8 Eric D Helms
end

3e3ad61c Partha Aji
attr_reader :schedule_time
8db5edf8 Eric D Helms
3e3ad61c Partha Aji
attr_reader :task_resource
8db5edf8 Eric D Helms
3e3ad61c Partha Aji
attr_reader :repo_resource
8db5edf8 Eric D Helms
3e3ad61c Partha Aji
attr_writer :task
8db5edf8 Eric D Helms
3e3ad61c Partha Aji
attr_reader :task
661ca339 Justin Sherrill
3e3ad61c Partha Aji
def create_and_sync_repo(options = {})
661ca339 Justin Sherrill
destroy_repo
b8da58c8 Eric D. Helms
create_repo(options)
sync_repo(options)
8db5edf8 Eric D Helms
end

3e3ad61c Partha Aji
def create_repo(options = {})
63540c7c Eric D. Helms
importer_config = {:id => @importer_type, :feed => @repo_url}
importer_config.merge!(options[:importer_config]) if options.key?(:importer_config)

54addc4c Justin Sherrill
repo = @repo_resource.retrieve(RepositorySupport.repo_id)
3e3ad61c Partha Aji
unless repo.nil?
8db5edf8 Eric D Helms
destroy_repo
end
578b9bb0 Eric D. Helms
54addc4c Justin Sherrill
if options[:importer]
63540c7c Eric D. Helms
repo = @repo_extension.create_with_importer(RepositorySupport.repo_id, importer_config)
54addc4c Justin Sherrill
elsif options[:importer_and_distributor]
repo = @repo_extension.create_with_importer_and_distributors(RepositorySupport.repo_id,
63540c7c Eric D. Helms
importer_config, @distributors)
54addc4c Justin Sherrill
else
repo = @repo_resource.create(RepositorySupport.repo_id)
578b9bb0 Eric D. Helms
end

5afa786f Eric D. Helms
rescue RestClient::ResourceNotFound
8db5edf8 Eric D Helms
54addc4c Justin Sherrill
if options[:importer]
63540c7c Eric D. Helms
repo = @repo_extension.create_with_importer(RepositorySupport.repo_id, importer_config)
54addc4c Justin Sherrill
elsif options[:importer_and_distributor]
repo = @repo_extension.create_with_importer_and_distributors(RepositorySupport.repo_id,
63540c7c Eric D. Helms
importer_config, @distributors)
54addc4c Justin Sherrill
else
repo = @repo_resource.create(RepositorySupport.repo_id)
8db5edf8 Eric D Helms
end

return repo
end

3e3ad61c Partha Aji
def sync_repo(options = {})
54addc4c Justin Sherrill
task = @repo_resource.sync(RepositorySupport.repo_id)
8db5edf8 Eric D Helms
3e3ad61c Partha Aji
unless options[:wait]
54addc4c Justin Sherrill
self.wait_on_response(task)
8db5edf8 Eric D Helms
end
32fb437f Justin Sherrill
54addc4c Justin Sherrill
task
end

def wait_on_response(response)
3e3ad61c Partha Aji
wait_on_tasks(response['spawned_tasks'].map { |task_ref| {'task_id' => task_ref['task_id']} })
8db5edf8 Eric D Helms
end

661ca339 Justin Sherrill
def wait_on_tasks(tasks)
54addc4c Justin Sherrill
tasks.map do |task|
9b1d1989 Eric D. Helms
self.wait_on_task(task)
end
end

3e3ad61c Partha Aji
def wait_on_task(task)
until (['finished', 'error', 'timed_out', 'canceled', 'reset'].include?(task['state']))
54addc4c Justin Sherrill
self.sleep_if_needed
3e3ad61c Partha Aji
task = @task_resource.poll(task['task_id'])
c77338c2 Justin Sherrill
end
32fb437f Justin Sherrill
task
c77338c2 Justin Sherrill
end

661ca339 Justin Sherrill
def sleep_if_needed
b24028dd Justin Sherrill
if VCR.configuration.default_cassette_options[:record] != :none
1e8d158d Justin Sherrill
sleep 0.5 # do not overload backend engines
b24028dd Justin Sherrill
end
end

661ca339 Justin Sherrill
def create_schedule
54addc4c Justin Sherrill
schedule = @schedule_resource.create(RepositorySupport.repo_id, @importer_type, @schedule_time)
0b5305d9 Justin Sherrill
schedule['_id']
end

3e3ad61c Partha Aji
def destroy_repo(id = RepositorySupport.repo_id)
54addc4c Justin Sherrill
if @task
wait_on_response(@task)
@task = nil
8db5edf8 Eric D Helms
end
54addc4c Justin Sherrill
tasks = @repo_resource.delete(id)
wait_on_response(tasks)
f8bc785b Partha Aji
rescue
634c90e7 Eric D. Helms
end

661ca339 Justin Sherrill
def rpm_ids
54addc4c Justin Sherrill
@repo_extension.rpm_ids(RepositorySupport.repo_id)
8db5edf8 Eric D Helms
end
end