Skip to content

Commit

Permalink
Fixed broken rollback
Browse files Browse the repository at this point in the history
* Better handle "-0" version suffix coming from zypper
  • Loading branch information
tmuntaner committed May 15, 2018
1 parent 4b345e3 commit bd13471
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 9 deletions.
6 changes: 4 additions & 2 deletions app/controllers/api/connect/v3/systems/products_controller.rb
Expand Up @@ -80,7 +80,7 @@ def migration_paths_as_json(paths)
def require_product
require_params(%i[identifier version arch])

@product = Product.where(identifier: params[:identifier], version: params[:version], arch: params[:arch]).first
@product = Product.where(identifier: params[:identifier], version: Product.clean_up_version(params[:version]), arch: params[:arch]).first

unless @product
raise ActionController::TranslatedError.new(N_('No product found'))
Expand Down Expand Up @@ -134,7 +134,9 @@ def render_service
end

def product_search_params(product_hash)
product_hash.permit(:identifier, :version, :arch, :release_type).to_h.symbolize_keys
hash = product_hash.permit(:identifier, :version, :arch, :release_type).to_h.symbolize_keys
hash[:version] = Product.clean_up_version(hash[:version]) if hash[:version].present?
hash
end

def product_from_hash(product_hash)
Expand Down
Expand Up @@ -2,8 +2,10 @@ class Api::Connect::V4::Repositories::InstallerController < Api::Connect::BaseCo

def index
require_params(%i[identifier version arch])
params = product_params
params[:version] = Product.clean_up_version(params[:version]) if params[:version].present?

product = Product.find_by(product_params.to_h.symbolize_keys)
product = Product.find_by(params.to_h.symbolize_keys)

if product
respond_with ActiveModel::Serializer::CollectionSerializer.new(
Expand Down
Expand Up @@ -19,7 +19,7 @@ def synchronize
products = params.require(:products).map do |product_params|
@system.products.find_by(
identifier: product_params[:identifier],
version: product_params[:version],
version: Product.clean_up_version(product_params[:version]),
arch: product_params[:arch]
)
end
Expand Down
103 changes: 98 additions & 5 deletions spec/requests/api/connect/v3/systems/products_controller_spec.rb
Expand Up @@ -71,6 +71,21 @@
its(:code) { is_expected.to eq('201') }
its(:body) { is_expected.to eq(serialized_json) }

context 'response with "-" in version' do
let(:product) { FactoryGirl.create(:product, :with_mirrored_repositories, :with_mirrored_extensions, version: '24.0') }

let(:payload) do
{
identifier: product.identifier,
version: '24.0-0',
arch: product.arch
}
end

its(:code) { is_expected.to eq('201') }
its(:body) { is_expected.to eq(serialized_json) }
end

describe 'JSON response' do
subject(:json_data) { JSON.parse(response.body, symbolize_names: true) }

Expand Down Expand Up @@ -117,8 +132,6 @@
end

context 'when product is activated' do
subject { response }

let(:system) { activation.system }
let(:payload) do
{
Expand All @@ -134,9 +147,34 @@
).to_json
end

before { get url, headers: headers, params: payload }
its(:code) { is_expected.to eq('200') }
its(:body) { is_expected.to eq(serialized_json) }
describe 'response' do
subject { response }

before { get url, headers: headers, params: payload }

its(:code) { is_expected.to eq('200') }
its(:body) { is_expected.to eq(serialized_json) }
end

describe 'response with "-" in version' do
subject { response }

let(:payload) do
{
identifier: activation.product.identifier,
version: '24.0-0',
arch: activation.product.arch
}
end

before do
activation.service.product.update_attribute(:version, '24.0')
get url, headers: headers, params: payload
end

its(:code) { is_expected.to eq('200') }
its(:body) { is_expected.to eq(serialized_json) }
end
end

context 'with eula_url' do
Expand Down Expand Up @@ -238,6 +276,23 @@
its(:body) { is_expected.to eq(serialized_json) }
end

describe 'response with "-" in product version' do
before { request }
subject { response }

let(:new_product) { FactoryGirl.create(:product, :with_mirrored_repositories, version: '24.0', predecessors: [old_product]) }
let(:payload) do
{
identifier: new_product.identifier,
version: '24.0-0',
arch: new_product.arch
}
end

its(:code) { is_expected.to eq('201') }
its(:body) { is_expected.to eq(serialized_json) }
end

describe 'activations' do
specify { expect { request }.not_to change { system.activations.count } }
it "updates the system's activation with the new product" do
Expand Down Expand Up @@ -379,6 +434,44 @@
is_expected.to eq(expected_response)
end
end

context 'with "-0" version suffix' do
let(:first_product) { FactoryGirl.create(:product, :with_mirrored_repositories, :activated, system: system, product_type: 'base') }
let(:second_product) do
FactoryGirl.create(
:product,
:with_mirrored_repositories,
product_type: 'base',
predecessors: [first_product],
migration_kind: migration_kind
)
end
let(:payload) do
product = second_product.predecessors.first # For initializing everything in the correct order
{
'installed_products': [ {
'identifier': product.identifier,
'version': product.version + '-0',
'arch': product.arch,
'release_type': product.release_type
} ],
'target_base_product': {
'identifier': second_product.identifier,
'version': second_product.version + '-0',
'arch': second_product.arch,
'release_type': second_product.release_type
}
}
end
let(:expected_response) do
[[::V3::UpgradePathItemSerializer.new(second_product)]].to_json
end

its(:code) { is_expected.to eq('200') }
its(:body) do
is_expected.to eq(expected_response)
end
end
end

describe '#migrations' do
Expand Down
Expand Up @@ -49,6 +49,16 @@
its(:code) { is_expected.to eq('200') }
end

describe 'response with "-" in product version' do
let(:product) { FactoryGirl.create(:product, :with_not_mirrored_repositories, version: '24.0') }
let(:params) { { identifier: product.identifier, version: '24.0-0', arch: product.arch } }

before { get url, params: params }

its(:body) { is_expected.to eq '[]' }
its(:code) { is_expected.to eq('200') }
end

context 'with known product with mirrored installer update repositories' do
let(:product) { FactoryGirl.create(:product, :with_mirrored_repositories) }
let(:params) { { identifier: product.identifier, version: product.version, arch: product.arch } }
Expand Down
18 changes: 18 additions & 0 deletions spec/requests/api/connect/v4/systems/products_controller_spec.rb
Expand Up @@ -110,6 +110,24 @@
end
end

context 'In sync with "-0" version suffix' do
it 'checks the system activations and returns a list of system products' do
params = system.products.map do |product|
{
identifier: product.identifier,
version: product.version + '-0',
arch: product.arch,
release_type: product.release_type
}
end

post path, params: { products: params }, headers: headers

expect(response.status).to eq 200
expect(json_response.map { |p| p[:id] }).to match_array(system.product_ids)
end
end

context 'Out of sync' do
it 'removes obsolete activations' do
product = system.products.first
Expand Down

0 comments on commit bd13471

Please sign in to comment.