Project

General

Profile

Download (5.25 KB) Statistics
| Branch: | Tag: | Revision:
8db5edf8 Eric D Helms
# Copyright 2012 Red Hat, Inc.
#
73caf37e Partha Aji
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8db5edf8 Eric D Helms
require 'rubygems'

5afa786f Eric D. Helms
require './lib/runcible/resources/repository'
0b5305d9 Justin Sherrill
require './lib/runcible/resources/repository_schedule'
578b9bb0 Eric D. Helms
require './lib/runcible/resources/task'
require './lib/runcible/extensions/repository'
ccaa42a7 Partha Aji
require './lib/runcible/extensions/distributor'
require './lib/runcible/extensions/yum_distributor'
require './lib/runcible/extensions/importer'
require './lib/runcible/extensions/yum_importer'
8db5edf8 Eric D Helms
63e1f2a8 Eric D. Helms
module RepositorySupport
8db5edf8 Eric D Helms
63e1f2a8 Eric D. Helms
@repo_url = "file://#{File.expand_path(File.dirname(__FILE__))}".gsub("support", "fixtures/repositories/zoo5")
578b9bb0 Eric D. Helms
@repo_id = "integration_test_id"
@repo_name = @repo_id
3b6e4a21 Eric D. Helms
@repo_resource = Runcible::Resources::Repository
0b5305d9 Justin Sherrill
@schedule_resource = Runcible::Resources::RepositorySchedule
3b6e4a21 Eric D. Helms
@repo_extension = Runcible::Extensions::Repository
@task_resource = Runcible::Resources::Task
0b5305d9 Justin Sherrill
@schedule_time = '2012-09-25T20:44:00Z/P7D'
@importer_type = 'yum_importer'
ccaa42a7 Partha Aji
@distributors = [Runcible::Extensions::YumDistributor.new('/path', true, true)]

8db5edf8 Eric D Helms
a92f59a9 Partha Aji
def self.distributor
50bf07ad Eric D. Helms
Runcible::Extensions::Repository.retrieve_with_details(RepositorySupport.repo_id)['distributors'].first
a92f59a9 Partha Aji
end

8db5edf8 Eric D Helms
def self.repo_name
@repo_name
end

def self.repo_id
@repo_id
end

def self.repo_url
@repo_url
end

0b5305d9 Justin Sherrill
def self.schedule_time
@schedule_time
end

8db5edf8 Eric D Helms
def self.task_resource
@task_resource
end

def self.repo_resource
@repo_resource
end

578b9bb0 Eric D. Helms
def self.task=(task)
8db5edf8 Eric D Helms
@task = task
end

def self.task
@task
end

b8da58c8 Eric D. Helms
def self.create_and_sync_repo(options={})
5354f21d Partha Aji
destroy_repo
b8da58c8 Eric D. Helms
create_repo(options)
sync_repo(options)
8db5edf8 Eric D Helms
end

578b9bb0 Eric D. Helms
def self.create_repo(options={})
8db5edf8 Eric D Helms
repo = nil
b43ba6f2 Eric D. Helms
VCR.use_cassette('support/repository') do
5afa786f Eric D. Helms
repo = @repo_resource.retrieve(@repo_id)
8db5edf8 Eric D Helms
end

if !repo.nil?
destroy_repo
end
578b9bb0 Eric D. Helms
b43ba6f2 Eric D. Helms
VCR.use_cassette('support/repository') do
578b9bb0 Eric D. Helms
if options[:importer]
0b5305d9 Justin Sherrill
repo = @repo_extension.create_with_importer(@repo_id, {:id=>@importer_type, :feed_url => @repo_url})
ccaa42a7 Partha Aji
elsif options[:importer_and_distributor]
repo = @repo_extension.create_with_importer_and_distributors(@repo_id, {:id=>@importer_type, :feed_url => @repo_url}, @distributors)
578b9bb0 Eric D. Helms
else
repo = @repo_resource.create(@repo_id)
end
end

5afa786f Eric D. Helms
rescue RestClient::ResourceNotFound
8db5edf8 Eric D Helms
b43ba6f2 Eric D. Helms
VCR.use_cassette('support/repository') do
578b9bb0 Eric D. Helms
if options[:importer]
0b5305d9 Justin Sherrill
repo = @repo_extension.create_with_importer(@repo_id, {:id=>@importer_type, :feed_url => @repo_url})
ccaa42a7 Partha Aji
elsif options[:importer_and_distributor]
repo = @repo_extension.create_with_importer_and_distributors(@repo_id, {:id=>@importer_type, :feed_url => @repo_url}, @distributors)
578b9bb0 Eric D. Helms
else
repo = @repo_resource.create(@repo_id)
end
8db5edf8 Eric D Helms
end

return repo
end

b8da58c8 Eric D. Helms
def self.sync_repo(options={})
b43ba6f2 Eric D. Helms
VCR.use_cassette('support/repository') do
578b9bb0 Eric D. Helms
@task = @repo_resource.sync(@repo_name).first
8db5edf8 Eric D Helms
b8da58c8 Eric D. Helms
if !options[:wait]
c77338c2 Justin Sherrill
self.wait_on_task(task)
8db5edf8 Eric D Helms
end
end
9b1d1989 Eric D. Helms
return @task
8db5edf8 Eric D Helms
rescue Exception => e
b8da58c8 Eric D. Helms
puts e
8db5edf8 Eric D Helms
end

9b1d1989 Eric D. Helms
def self.wait_on_tasks(tasks)
tasks.each do |task|
self.wait_on_task(task)
end
end

c77338c2 Justin Sherrill
def self.wait_on_task task
b43ba6f2 Eric D. Helms
VCR.use_cassette('support/task') do
while !(['finished', 'error', 'timed_out', 'canceled', 'reset'].include?(task['state'])) do
task = @task_resource.poll(task["task_id"])
b24028dd Justin Sherrill
self.sleep_if_needed
b43ba6f2 Eric D. Helms
end
c77338c2 Justin Sherrill
end
end

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

0b5305d9 Justin Sherrill
def self.create_schedule
schedule = {}
b43ba6f2 Eric D. Helms
VCR.use_cassette('support/repository') do
0b5305d9 Justin Sherrill
schedule = @schedule_resource.create(@repo_id, @importer_type, @schedule_time)
end
schedule['_id']
end

8db5edf8 Eric D Helms
def self.destroy_repo(id=@repo_id)
b43ba6f2 Eric D. Helms
VCR.use_cassette('support/repository') do
8db5edf8 Eric D Helms
if @task
b24028dd Justin Sherrill
wait_on_task(@task)
8db5edf8 Eric D Helms
@task = nil
end

82aa3eff Eric D. Helms
tasks = @repo_resource.delete(id)
wait_on_tasks(tasks)
8db5edf8 Eric D Helms
end
rescue Exception => e
634c90e7 Eric D. Helms
end

def self.rpm_ids
pkg_ids = []
b43ba6f2 Eric D. Helms
VCR.use_cassette('support/repository', :match_requests_on => [:body_json, :path, :method]) do
634c90e7 Eric D. Helms
pkg_ids = @repo_extension.rpm_ids(@repo_id)
end
return pkg_ids
8db5edf8 Eric D Helms
end

end