Project

General

Profile

« Previous | Next » 

Revision c81a105c

Added by Justin Sherrill about 10 years ago

fixing tests after moving to dynflow for repo creation/deletion

View differences:

app/controllers/katello/repositories_controller.rb
respond_to :html, :js
before_filter :find_provider, :only => [:new, :create, :edit, :destroy, :update_gpg_key]
before_filter :find_product, :only => [:new, :create, :edit, :destroy, :update_gpg_key]
before_filter :authorize
before_filter :find_repository, :only => [:edit, :destroy, :enable_repo, :update_gpg_key]
before_filter :find_repository
def rules
read_any_test = lambda{Provider.any_readable?(current_organization)}
read_test = lambda{@product.readable?}
edit_test = lambda{@product.editable?}
org_edit = lambda{current_organization.redhat_manageable?}
{
:new => edit_test,
:create => edit_test,
:default_label => lambda{true},
:edit => read_test,
:update_gpg_key => edit_test,
:destroy => edit_test,
:enable_repo => org_edit,
:auto_complete_library => read_any_test
:enable_repo => org_edit
}
end
def section_id
'contents'
end
def new
render :partial => "new"
end
def edit
render :partial => "edit",
:locals => {
:editable => (@product.editable? && !@repository.promoted?),
:cloned_in_environments => @repository.product.environments.select {|env| @repository.is_cloned_in?(env)}.map(&:name)
}
end
def create
repo_params = params[:repo]
repo_params[:label], label_assigned = generate_label(repo_params[:name], 'repository') if repo_params[:label].blank?
fail HttpErrors::BadRequest, _("Repository can be only created for custom provider.") unless @product.custom?
gpg = GpgKey.readable(current_organization).find(repo_params[:gpg_key]) if repo_params[:gpg_key] && repo_params[:gpg_key] != ""
# Bundle these into one call, perhaps move to Provider
repo_params[:unprotected] ||= false
@product.add_repo(repo_params[:label], repo_params[:name], repo_params[:feed],
repo_params[:content_type], repo_params[:unprotected], gpg)
@product.save!
notify.success _("Repository '%s' created.") % repo_params[:name] unless params[:ignore_success_notice]
notify.message label_assigned unless label_assigned.blank? unless params[:ignore_success_notice]
render :nothing => true
rescue Errors::ConflictException, ActiveRecord::RecordInvalid, Glue::Pulp::PulpErrors::ServiceUnavailable => e
e.class == Glue::Pulp::PulpErrors::ServiceUnavailable ? notify.exception(e) : notify.error(e.to_s)
execute_after_filters
render :nothing => true, :status => :bad_request
end
def update_gpg_key
if params[:gpg_key] != ""
gpg = GpgKey.readable(current_organization).find(params[:gpg_key])
result = gpg.id.to_s
else
gpg = nil
result = ""
end
@repository.gpg_key = gpg
@repository.save!
notify.success _("Repository '%s' updated.") % @repository.name
render :text => escape_html(result)
end
def enable_repo
@repository.enabled = params[:repo] == "1"
@repository.save!
......
render :json => {:id => @repository.id, :can_disable_repo_set => product_content.can_disable?}
end
def destroy
@repository.destroy
if @repository.destroyed?
notify.success _("Repository '%s' removed.") % @repository.name
render :partial => "katello/common/post_delete_close_subpanel",
:locals => { :path => products_repos_provider_path(@provider.id) }
else
err_msg = N_("Removal of the repository failed. If you continue having trouble with this, please contact an Administrator.")
notify.error err_msg
render :nothing => true
end
end
def auto_complete_library
# retrieve and return a list (array) of repo names in library that contain the 'term' that was passed in
term = Util::Search.filter_input params[:term]
name = 'name:' + term
name_query = name + ' OR ' + name + '*'
ids = Repository.readable(current_organization.library).collect{|r| r.id}
repos = Repository.search do
query {string name_query}
filter "and", [
{:terms => {:id => ids}},
{:terms => {:enabled => [true]}}
]
end
render :json => (repos.map do |repo|
label = _("%{repo} (Product: %{product})") % {:repo => repo.name, :product => repo.product}
{:id => repo.id, :label => label, :value => repo.name}
end)
end
protected
def find_provider
@provider = Provider.find(params[:provider_id])
end
def find_product
@product = Product.find(params[:product_id])
end
def find_repository
@repository = Repository.find(params[:id])
end
app/lib/actions/katello/repository/create.rb
module Repository
class Create < Actions::EntryAction
# rubocop:disable MethodLength
def plan(repository, clone = false)
repository.disable_auto_reindex!
repository.save!
......
plan_self
org = repository.organization
# check the instance variable, as we do not want to go to pulp
checksum_type = repository.checksum_type if self.instance_variable_get('@checksum_type')
if repository.puppet?
path = File.join(::Katello.config.puppet_repo_root,
::Katello::KTEnvironment.construct_name(repository.environment.organization,
......
ssl_client_cert: repository.feed_cert,
ssl_client_key: repository.feed_key,
unprotected: repository.unprotected,
checksum_type: checksum_type,
checksum_type: repository.checksum_type,
path: path,
with_importer: true)
app/lib/actions/pulp/repository/create.rb
def yum_distributor
yum_dist_options = { protected: true,
id: input[:pulp_id],
auto_publish: true,
checksum_type: input[:checksum_type] }
auto_publish: true }
yum_dist_options[:checksum_type] = input[:checksum_type] if input[:checksum_type]
Runcible::Models::YumDistributor.new(input[:path],
input[:unprotected] || false,
true,
app/models/katello/content_view.rb
:environments => [organization.library])
end
protected
def remove_repository(repository)
app/models/katello/glue/pulp/repo.rb
end
end)
lazy_accessor :checksum_type,
:initializer => lambda { |s| self.lookup_checksum_type if pulp_id}
lazy_accessor :importers,
:initializer => lambda { |s| pulp_repo_facts["importers"] if pulp_id }
......
pulp_repo_facts.merge(as_json).merge(:sync_state => sync_state)
end
def lookup_checksum_type
def pulp_checksum_type
find_distributor['config']['checksum_type'] if self.yum? && find_distributor
end
......
yum_dist_id = self.pulp_id
yum_dist_options = {:protected => true, :id => yum_dist_id, :auto_publish => true}
#check the instance variable, as we do not want to go to pulp
yum_dist_options['checksum_type'] = self.checksum_type if self.instance_variable_get('@checksum_type')
yum_dist_options['checksum_type'] = self.checksum_type if self.checksum_type
yum_dist = Runcible::Models::YumDistributor.new(self.relative_path, (self.unprotected || false), true,
yum_dist_options)
clone_dist = Runcible::Models::YumCloneDistributor.new(:id => "#{self.pulp_id}_clone",
app/models/katello/repository.rb
:content_view_version => to_version,
:content_type => self.content_type,
:unprotected => self.unprotected) do |clone|
# We need to check this becuase sometimes, we want to build the clone
# before the orchestration begins: ideally, lazy_accessors should not
# call to external services when the resource is locked on creation
if self.pulp_repo_facts && self.checksum_type
clone.checksum_type = self.checksum_type
end
clone.checksum_type = self.checksum_type
clone.pulp_id = clone.clone_id(to_env, content_view, version.try(:version))
clone.relative_path = Repository.clone_repo_path(:repository => self,
:environment => to_env,
db/migrate/20140310102051_repository_add_checksum_type.rb
class RepositoryAddChecksumType < ActiveRecord::Migration
def up
add_column :katello_repositories, :checksum_type, :string, :null => true
end
def down
remove_column :katello_repositories, :checksum_type
end
end
doc/katello.local.rb
# This is the gemfile for foreman that will load the Katello engine.
# It belongs in foreman/bundler.d/
gem 'dynflow', :path => '../dynflow'
gemspec :path => '../katello', :development_group => :katello_dev
spec/controllers/repositories_controller_spec.rb
#
# 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
describe RepositoriesController do
include LocaleHelperMethods
include OrganizationHelperMethods
include ProductHelperMethods
include RepositoryHelperMethods
include OrchestrationHelper
include AuthorizationHelperMethods
describe "(katello)" do
describe "rules" do
before do
setup_controller_defaults
disable_product_orchestration
disable_user_orchestration
@organization = new_test_org
@provider = Provider.create!(:provider_type=>Provider::CUSTOM, :name=>"foo1", :organization=>@organization)
Provider.stubs(:find).returns(@provider)
@product = Product.new({:name=>"prod", :label=> "prod"})
@product.provider = @provider
@product.stubs(:arch).returns('noarch')
@product.save!
Product.stubs(:find).returns(@product)
@repository = OpenStruct.new(:id =>1222)
end
describe "GET New" do
let(:action) {:new}
let(:req) { get :new, :provider_id => @provider.id, :product_id => @product.id}
let(:authorized_user) do
user_with_permissions { |u| u.can(:update, :providers,@provider.id, @organization) }
end
let(:unauthorized_user) do
user_without_permissions
end
it_should_behave_like "protected action"
end
describe "GET Edit" do
before do
Product.stubs(:find).returns(@product)
Katello.pulp_server.extensions.repository.stubs(:find).returns(@repository)
end
let(:action) {:edit}
let(:req) { get :edit, :provider_id => @provider.id, :product_id => @product.id, :id => @repository.id}
let(:authorized_user) do
user_with_permissions { |u| u.can(:read, :providers,@provider.id, @organization) }
end
let(:unauthorized_user) do
user_without_permissions
end
it_should_behave_like "protected action"
end
end
describe "destroy a repository" do
before(:each) do
setup_controller_defaults
@organization = get_organization
@controller.stubs(:current_organization).returns(@organization)
Provider.stubs(:find).returns(stub(:id => 1))
Product.stubs(:find).returns(@product = stub)
@product.stubs(:editable?).returns(true)
@repository = OpenStruct.new(:name=>"deleted", :id => 123456)
Repository.stubs(:find).returns(@repository)
@repository.stubs(:destroy)
end
describe "on success" do
before(:each) { @repository.stubs(:destroyed?).returns(true) }
it "destroys the requested repository" do
@repository.expects(:destroy)
@repository.expects(:destroyed?)
delete :destroy, :id => "123456", :provider_id => "123", :product_id => "123", :format => :js
end
it "updates the view" do
delete :destroy, :id => "123456", :provider_id => "123", :product_id => "123", :format => :js
must_render_template(:partial => 'katello/common/_post_delete_close_subpanel')
end
end
describe "on failure" do
before(:each) { @repository.stubs(:destroyed?).returns(false) }
it "should produce an error notice on failure" do
must_notify_with(:error)
delete :destroy, :id => "123456", :provider_id => "123", :product_id => "123"
end
it "shouldn't render anything on failure" do
delete :destroy, :id => "123456", :provider_id => "123", :product_id => "123"
response.body.must_be :blank?
end
end
end
describe "other-tests" do
before (:each) do
setup_controller_defaults
set_default_locale
@org = new_test_org
@product = new_test_product(@org, @org.library)
test_gpg_content = File.open("#{Katello::Engine.root}/spec/assets/gpg_test_key").read
@gpg = GpgKey.create!(:name => "foo", :organization => @organization, :content => test_gpg_content)
@controller.stubs(:current_organization).returns(@org)
Resources::Candlepin::Content.stubs(:create => {:id => "123"})
end
let(:invalidrepo) do
{
:product_id => @product.id,
:provider_id => @product.provider.id,
:repo => {
:name => 'test',
:feed => 'www.foo.com'
}
}
end
describe "Create a Repo" do
it "should reject invalid urls" do
must_notify_with(:error)
post :create, invalidrepo
response.must_respond_with(400)
end
end
context "Test gpg create" do
before do
disable_product_orchestration
Repository.any_instance.stubs(:create_pulp_repo).returns({})
Repository.any_instance.stubs(:setup_sync_schedule).returns({})
Repository.any_instance.stubs(:set_sync_schedule).returns({})
content = { :name => "FOO",
:id=>"12345",
:contentUrl => '/some/path',
:gpgUrl => nil,
:type => "yum",
:label => 'label',
:vendor => Provider::CUSTOM}
Resources::Candlepin::Content.stubs(:get).returns(content)
Resources::Candlepin::Content.stubs(:create).returns(content)
Repository.any_instance.stubs(:generate_metadata)
@repo_name = "repo-#{rand 10 ** 8}"
post :create, { :product_id => @product.id,
:provider_id => @product.provider.id,
:repo => {:name => @repo_name,
:label => @repo_name,
:feed => "http://foo.com",
:unprotected => false,
:content_type => "yum",
:gpg_key =>@gpg.id.to_s}}
end
specify do
must_respond_with(:success)
end
subject {Repository.find_by_name(@repo_name)}
it{wont_be_nil}
it {subject.gpg_key.must_equal @gpg}
it {subject.unprotected.must_equal false}
end
context "Test update gpg" do
before do
disable_product_orchestration
content = { :name => "FOO",
:id=>"12345",
:contentUrl => '/some/path',
:gpgUrl => nil,
:type => "yum",
:label => 'label',
:vendor => Provider::CUSTOM}
Resources::Candlepin::Content.stubs(:get).returns(content)
Resources::Candlepin::Content.stubs(:create).returns(content)
@repo = new_test_repo(@organization.library, @product, "newname#{rand 10**6}", "http://fedorahosted org")
product = @repo.product
Repository.stubs(:find).returns(@repo)
@repo.stubs(:content).returns(OpenStruct.new(:gpgUrl=>""))
@repo.expects(:update_content).returns(Candlepin::Content.new)
#@repo.stubs(:product).returns(product)
put :update_gpg_key, { :product_id => @product.id,
:provider_id => @product.provider.id,
:id => @repo.id,
:gpg_key => @gpg.id.to_s}
end
specify do
must_respond_with(:success)
end
subject {Repository.find(@repo.id)}
it{wont_be_nil}
it {subject.gpg_key.must_equal @gpg}
end
end
end
end
end
spec/models/glue/candlepin/repository_orchestration_spec.rb
})
repository.update_content
end
# If some of tests in following describe block is broken, read this carefully!
# Situation where two products share same Candlepin content can occur. This happens during
# manifest import in Candlepin so katello has no control over it. However when deleting content
# we must be sure that there is no other product using this content. That's why this test makes
# sure we decide based on #other_repos_with_same_content method whether we delete content or not.
describe "#del_content" do
before do
repository.stubs(:other_repos_with_same_product_and_content).returns([])
repository.product.stubs(:remove_content_by_id).returns(true)
provider = stub
provider.stubs(:redhat_provider?).returns(false)
repository.product.stubs(:provider).returns(provider)
end
context "there isn't another product using same candlepin content" do
before { Resources::Candlepin::Content.expects(:destroy).once }
it "should delete CP content (katello)" do #TODO headpin
repository.del_content
end
end
context "there is another product using same candlepin content" do
before do
Resources::Candlepin::Content.expects(:destroy).never
repository.stubs(:other_repos_with_same_content).returns(['not', 'empty'])
end
it "should not delete CP content (katello)" do #TODO headpin
repository.del_content
end
end
end
end
end
spec/models/product_spec.rb
@repo_name = "repo"
@repo_label = "repo"
disable_repo_orchestration
@p.add_repo(@repo_label, @repo_name, "http://test/repo","yum")
@p.add_repo(@repo_label, @repo_name, "http://test/repo","yum").save!
end
it "should raise conflict error" do
lambda { @p.add_repo(@repo_label, @repo_name, "http://test/repo","yum") }.must_raise(Errors::ConflictException)
lambda {@p.add_repo(@repo_label, @repo_name, "http://test/repo","yum")}.must_raise(
Errors::ConflictException)
end
end
end
test/actions/katello/product_test.rb
#
# 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
describe ::Actions::Katello::Product do
include Dynflow::Testing
include Support::Actions::Fixtures
include FactoryGirl::Syntax::Methods
before :all do
@org = FactoryGirl.build('katello_organization')
@provider = FactoryGirl.build('katello_fedora_hosted_provider', organization: @org)
@product = FactoryGirl.build('katello_product', provider: @provider, cp_id: 1234)
@repository = FactoryGirl.build('katello_repository', product: @product, content_id: 'foobar')
end
describe 'Destroy' do
let(:action_class) { ::Actions::Katello::Product::ContentDestroy }
let(:candlepin_destroy_class) { ::Actions::Candlepin::Product::ContentDestroy }
let(:candlepin_remove_class) { ::Actions::Candlepin::Product::ContentRemove }
it 'plans' do
@repository.stubs(:other_repos_with_same_product_and_content).returns([])
@repository.stubs(:other_repos_with_same_content).returns([])
action = create_action action_class
action.stubs(:action_subject).with(@repository)
plan_action action, @repository
assert_action_planed_with action, candlepin_remove_class, product_id: @product.cp_id,
content_id: @repository.content_id
assert_action_planed_with action, candlepin_destroy_class, content_id: @repository.content_id
end
it 'does not remove content if other content exists in different product' do
repo2 = FactoryGirl.build('katello_repository', product: @product)
@repository.stubs(:other_repos_with_same_product_and_content).returns([])
@repository.stubs(:other_repos_with_same_content).returns([repo2])
action = create_action action_class
action.stubs(:action_subject).with(@repository)
plan_action action, @repository
assert_action_planed action, candlepin_remove_class
refute_action_planed action, candlepin_destroy_class
end
it 'does not destroy or remove content if other content exists in same product' do
repo2 = FactoryGirl.build('katello_repository', product: @product)
@repository.stubs(:other_repos_with_same_product_and_content).returns([repo2])
@repository.stubs(:other_repos_with_same_content).returns([repo2])
action = create_action action_class
action.stubs(:action_subject).with(@repository)
plan_action action, @repository
refute_action_planed action, candlepin_remove_class
refute_action_planed action, candlepin_destroy_class
end
end
end
end
test/controllers/api/v2/repositories_controller_test.rb
def test_create
product = MiniTest::Mock.new
product.expect(:add_repo, {}, [
product.expect(:add_repo, @repository, [
'Fedora_Repository',
'Fedora Repository',
'http://www.google.com',
......
product.expect(:editable?, @product.editable?)
product.expect(:gpg_key, nil)
@controller.expects(:sync_task).with(::Actions::Katello::Repository::Create, @repository).once
Product.stub(:find, product) do
post :create, :name => 'Fedora Repository',
:product_id => @product.id,
......
product.expect(:gpg_key, key)
product.expect(:editable?, @product.editable?)
product.expect(:add_repo, {}, [
product.expect(:add_repo, @repository, [
'Fedora_Repository',
'Fedora Repository',
'http://www.google.com',
......
key
])
@controller.expects(:sync_task).with(::Actions::Katello::Repository::Create, @repository).once
Product.stub(:find, product) do
post :create, :name => 'Fedora Repository',
:product_id => @product.id,
test/glue/pulp/repository_test.rb
@@fedora_17_x86_64.feed = "file:///var/www/test_repos/zoo"
end
def self.delete_repo(repo)
::ForemanTasks.sync_task(::Actions::Pulp::Repository::Destroy, :pulp_id => repo.pulp_id)
end
def delete_repo(repo)
GluePulpRepoTestBase.delete_repo(repo)
end
end
......
def test_create_pulp_repo
assert @fedora_17_x86_64.create_pulp_repo
@fedora_17_x86_64.destroy_repo
end
def test_destroy_repo
@fedora_17_x86_64.create_pulp_repo
assert @fedora_17_x86_64.destroy_repo
delete_repo(@fedora_17_x86_64)
end
end
......
end
def self.after_suite
@@fedora_17_x86_64.destroy_repo
delete_repo(@@fedora_17_x86_64)
VCR.eject_cassette
end
......
end
def self.after_suite
@@fedora_17_x86_64.destroy_repo
delete_repo(@@fedora_17_x86_64)
VCR.eject_cassette
end
......
end
def self.after_suite
@@fedora_17_x86_64.destroy_repo
delete_repo(@@fedora_17_x86_64)
VCR.eject_cassette
end
......
end
def self.after_suite
@@p_forge.destroy_repo
delete_repo(@@p_forge)
VCR.eject_cassette
end
......
end
def self.after_suite
@@fedora_17_x86_64.destroy_repo
delete_repo(@@fedora_17_x86_64)
VCR.eject_cassette
end
......
end
def self.after_suite
@@fedora_17_x86_64.destroy_repo
delete_repo(@@fedora_17_x86_64)
VCR.eject_cassette
end
......
TaskSupport.wait_on_tasks(task_list)
ensure
@@fedora_17_x86_64_dev.destroy_repo
delete_repo(@@fedora_17_x86_64_dev)
end
end
test/support/pulp/repository_support.rb
end
def self.destroy_repo
@repo.destroy_repo
::ForemanTasks.sync_task(::Actions::Pulp::Repository::Destroy, :pulp_id => @repo.pulp_id)
rescue RestClient::ResourceNotFound => e
end

Also available in: Unified diff