Skip to content

Commit

Permalink
[webui] Add Kiwi::Description model.
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Rolfe committed Oct 27, 2017
1 parent 10d63da commit 7123efd
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/api/app/models/kiwi/description.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Kiwi::Description < ApplicationRecord
belongs_to :image, inverse_of: :description

enum description_type: %i[system]

validates :description_type, inclusion: { in: description_types.keys }

def to_xml
builder = Nokogiri::XML::Builder.new
builder.description({ type: description_type }) do |description|
description.author(author)
description.contact(contact)
description.specification(specification)
end
builder.to_xml save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION | Nokogiri::XML::Node::SaveOptions::FORMAT
end
end

# == Schema Information
#
# Table name: kiwi_descriptions
#
# id :integer not null, primary key
# image_id :integer indexed
# description_type :integer default("system")
# author :string(255)
# contact :string(255)
# specification :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_kiwi_descriptions_on_image_id (image_id)
#
24 changes: 24 additions & 0 deletions src/api/app/models/kiwi/image/xml_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,35 @@ def build

doc = update_packages(doc)
doc = update_repositories(doc)
doc = update_description(doc)

Nokogiri::XML(doc.to_xml, &:noblanks).to_xml(indent: kiwi_indentation(doc))
end

private

def update_description(document)
return document if @image.description.blank?

if document.xpath('image/description[@type="system"]').first
%w[author contact specification].each do |element_name|
update_description_element(document, element_name)
end
else
document.at_css('image').first_element_child.before(@image.description.to_xml)
end
document
end

def update_description_element(document, element_name)
element_xml = document.xpath("image/description[@type='system']/#{element_name}").first
unless element_xml
element_xml = Nokogiri::XML::Node.new(element_name, document)
document.xpath('image/description[@type="system"]').first.add_child(element_xml)
end
element_xml.content = @image.description.send(element_name)
end

def update_packages(document)
# for now we only write the default package group
xml_packages = @image.default_package_group.to_xml
Expand Down
17 changes: 17 additions & 0 deletions src/api/app/models/kiwi/image/xml_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def parse
new_image.use_project_repositories = use_project_repositories?
new_image.repositories = repositories
new_image.package_groups = package_groups
new_image.description = description

new_image
end
Expand Down Expand Up @@ -86,6 +87,22 @@ def package_groups

package_groups
end

# Return an instance of Kiwi::Description model from the parsed xml
def description
attributes = [xml_hash["description"]].flatten.find do |description|
description['type'] == 'system'
end

return if attributes.blank?

Kiwi::Description.new(
description_type: attributes['type'],
author: attributes['author'],
contact: attributes['contact'],
specification: attributes['specification']
)
end
end
end
end
13 changes: 13 additions & 0 deletions src/api/db/migrate/20171013103921_create_kiwi_descriptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateKiwiDescriptions < ActiveRecord::Migration[5.1]
def change
create_table :kiwi_descriptions, id: :integer do |t|
t.references :image, type: :integer
t.integer :description_type, default: 0
t.string :author
t.string :contact
t.string :specification

t.timestamps
end
end
end
14 changes: 14 additions & 0 deletions src/api/db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,19 @@ CREATE TABLE `issues` (
CONSTRAINT `issues_ibfk_2` FOREIGN KEY (`issue_tracker_id`) REFERENCES `issue_trackers` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE `kiwi_descriptions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image_id` int(11) DEFAULT NULL,
`description_type` int(11) DEFAULT '0',
`author` varchar(255) DEFAULT NULL,
`contact` varchar(255) DEFAULT NULL,
`specification` varchar(255) DEFAULT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `index_kiwi_descriptions_on_image_id` (`image_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `kiwi_images` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
Expand Down Expand Up @@ -1257,6 +1270,7 @@ INSERT INTO `schema_migrations` (version) VALUES
('20170921100521'),
('20170925060940'),
('20171011125520'),
('20171013103921'),
('20171019151800');


10 changes: 10 additions & 0 deletions src/api/spec/factories/kiwi_description.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FactoryGirl.define do
factory :kiwi_description, class: Kiwi::Description do
association :image, factory: :kiwi_image

description_type { Kiwi::Description.description_types.keys.first }
author 'example_author'
contact 'example_contact'
specification 'example_specification'
end
end
39 changes: 39 additions & 0 deletions src/api/spec/models/kiwi/description_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'rails_helper'

RSpec.describe Kiwi::Description, type: :model do
let(:kiwi_description) { create(:kiwi_description) }

describe '#to_xml' do
context 'with full description content' do
let(:expected_xml) do
<<-XML.strip_heredoc
<description type="system">
<author>example_author</author>
<contact>example_contact</contact>
<specification>example_specification</specification>
</description>
XML
end

subject { kiwi_description.to_xml }

it { expect(subject).to eq(expected_xml) }
end

context 'with empty description content' do
let(:expected_xml) do
<<-XML.strip_heredoc
<description type="system">
<author/>
<contact/>
<specification/>
</description>
XML
end

subject { create(:kiwi_description, author: nil, contact: '', specification: nil).to_xml }

it { expect(subject).to eq(expected_xml) }
end
end
end
23 changes: 23 additions & 0 deletions src/api/spec/models/kiwi/image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@
bootinclude: nil,
bootdelete: nil
)
expect(subject.description).to have_attributes(
description_type: 'system',
author: 'Christian Bruckmayer',
contact: 'noemail@example.com',
specification: 'Tiny, minimalistic appliances'
)
end
end

Expand Down Expand Up @@ -158,6 +164,22 @@
it { expect(subject.repositories).to be_empty }
it { expect(subject.kiwi_packages).to be_empty }
end

context 'with multiple descriptions in the xml file' do
subject { Kiwi::Image.build_from_xml(kiwi_xml_with_multiple_descriptions, 'some_md5') }

it { expect(subject.valid?).to be_truthy }
it { expect(subject.repositories).to be_empty }
it { expect(subject.kiwi_packages).to be_empty }
it 'parses only the description with type = "system"' do
expect(subject.description).to have_attributes(
description_type: 'system',
author: 'Christian Bruckmayer',
contact: 'noemail@example.com',
specification: 'Tiny, minimalistic appliances'
)
end
end
end

describe '#to_xml' do
Expand Down Expand Up @@ -271,6 +293,7 @@
before do
allow(package).to receive(:kiwi_image_file).and_return('config.kiwi')
allow(package).to receive(:source_file).and_return(Kiwi::Image::DEFAULT_KIWI_BODY)
kiwi_image.save
kiwi_image.package = package
kiwi_image.package_groups << create(:kiwi_package_group_non_empty, kiwi_type: 'image')
kiwi_image.repositories << create(:kiwi_repository)
Expand Down
19 changes: 19 additions & 0 deletions src/api/spec/support/shared_contexts/a_kiwi_image_xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,23 @@
</image>
XML
end

let(:kiwi_xml_with_multiple_descriptions) do
<<-XML
<?xml version="1.0" encoding="UTF-8"?>
<image name="Christians_openSUSE_13.2_JeOS" displayname="Christians_openSUSE_13.2_JeOS" schemaversion="5.2">
<description type="system">
<author>Christian Bruckmayer</author>
<contact>noemail@example.com</contact>
<specification>Tiny, minimalistic appliances</specification>
</description>
<description type="boot">
<author>The KIWI Team</author>
<contact>kiwi@example.com</contact>
<specification>Kiwi, tiny, minimalistic appliances</specification>
</description>
</image>
XML
end

end

0 comments on commit 7123efd

Please sign in to comment.