From: Martyn Taylor <mtaylor(a)redhat.com>
---
.../image_factory/aeolus-image/examples/tdl.rng | 206 ++++++++++++++++++++
.../image_factory/aeolus-image/lib/base_command.rb | 6 +
.../aeolus-image/lib/build_command.rb | 10 +
.../aeolus-image/spec/base_command_spec.rb | 14 ++
.../aeolus-image/spec/build_command_spec.rb | 11 +
.../spec/fixtures/invalid_template.tdl | 18 ++
.../aeolus-image/spec/fixtures/valid_template.tdl | 18 ++
7 files changed, 283 insertions(+), 0 deletions(-)
create mode 100644 services/image_factory/aeolus-image/examples/tdl.rng
create mode 100644
services/image_factory/aeolus-image/spec/fixtures/invalid_template.tdl
create mode 100644 services/image_factory/aeolus-image/spec/fixtures/valid_template.tdl
diff --git a/services/image_factory/aeolus-image/examples/tdl.rng
b/services/image_factory/aeolus-image/examples/tdl.rng
new file mode 100644
index 0000000..65fe19a
--- /dev/null
+++ b/services/image_factory/aeolus-image/examples/tdl.rng
@@ -0,0 +1,206 @@
+<?xml version="1.0"?>
+<!-- A Relax NG schema for the TDL (template description language) format -->
+<grammar
xmlns="http://relaxng.org/ns/structure/1.0"
datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
+ <start>
+ <ref name='template'/>
+ </start>
+
+ <define name='template'>
+ <element name='template'>
+ <interleave>
+ <element name='name'>
+ <text/>
+ </element>
+ <element name='os'>
+ <interleave>
+ <element name='name'>
+ <text/>
+ </element>
+ <element name='version'>
+ <text/>
+ </element>
+ <element name='arch'>
+ <choice>
+ <value>i386</value>
+ <value>x86_64</value>
+ </choice>
+ </element>
+ <element name='install'>
+ <choice>
+ <ref name='url'/>
+ <ref name='iso'/>
+ </choice>
+ </element>
+ <optional>
+ <element name='rootpw'>
+ <text/>
+ </element>
+ </optional>
+ <optional>
+ <element name='key'>
+ <text/>
+ </element>
+ </optional>
+ </interleave>
+ </element>
+ <optional>
+ <element name='description'>
+ <text/>
+ </element>
+ </optional>
+ <optional>
+ <element name='packages'>
+ <zeroOrMore>
+ <element name='package'>
+ <attribute name='name'>
+ <text/>
+ </attribute>
+ <interleave>
+ <optional>
+ <element name='repository'>
+ <text/>
+ </element>
+ </optional>
+ <optional>
+ <element name='file'>
+ <text/>
+ </element>
+ </optional>
+ <optional>
+ <element name='arguments'>
+ <text/>
+ </element>
+ </optional>
+ </interleave>
+ </element>
+ </zeroOrMore>
+ </element>
+ </optional>
+ <optional>
+ <element name='files'>
+ <zeroOrMore>
+ <element name='file'>
+ <attribute name='name'>
+ <text/>
+ </attribute>
+ <choice>
+ <ref name='rawtype'/>
+ <ref name='base64type'/>
+ </choice>
+ </element>
+ </zeroOrMore>
+ </element>
+ </optional>
+ <optional>
+ <element name='commands'>
+ <zeroOrMore>
+ <element name='command'>
+ <attribute name='name'>
+ <text/>
+ </attribute>
+ <choice>
+ <ref name='rawtype'/>
+ <ref name='base64type'/>
+ </choice>
+ </element>
+ </zeroOrMore>
+ </element>
+ </optional>
+ <optional>
+ <element name='repositories'>
+ <zeroOrMore>
+ <element name='repository'>
+ <attribute name='name'>
+ <text/>
+ </attribute>
+ <interleave>
+ <element name='url'>
+ <text/>
+ </element>
+ <optional>
+ <element name='signed'>
+ <ref name='bool'/>
+ </element>
+ </optional>
+ </interleave>
+ </element>
+ </zeroOrMore>
+ </element>
+ </optional>
+ </interleave>
+ </element>
+ </define>
+
+ <define name='url'>
+ <attribute name='type'>
+ <value>url</value>
+ </attribute>
+ <element name='url'>
+ <text/>
+ </element>
+ </define>
+
+ <define name='iso'>
+ <attribute name='type'>
+ <value>iso</value>
+ </attribute>
+ <interleave>
+ <element name='iso'>
+ <text/>
+ </element>
+ <optional>
+ <choice>
+ <element name='md5sum'>
+ <text/>
+ </element>
+ <element name='sha1sum'>
+ <text/>
+ </element>
+ <element name='sha256sum'>
+ <text/>
+ </element>
+ </choice>
+ </optional>
+ </interleave>
+ </define>
+
+ <define name='bool'>
+ <choice>
+ <data type="string">
+ <param name="pattern">[Tt][Rr][Uu][Ee]</param>
+ </data>
+ <data type="string">
+ <param name="pattern">[Ff][Aa][Ll][Ss][Ee]</param>
+ </data>
+ <data type="string">
+ <param name="pattern">[Yy][Ee][Ss]</param>
+ </data>
+ <data type="string">
+ <param name="pattern">[Nn][Oo]</param>
+ </data>
+ </choice>
+ </define>
+
+ <define name='rawtype'>
+ <optional>
+ <attribute name='type'>
+ <value>raw</value>
+ </attribute>
+ </optional>
+ <text/>
+ </define>
+
+ <define name='base64'>
+ <data type="string">
+ <param name="pattern">[a-zA-Z0-9+/]+={0,2}</param>
+ </data>
+ </define>
+
+ <define name='base64type'>
+ <attribute name='type'>
+ <value>base64</value>
+ </attribute>
+ <ref name='base64'/>
+ </define>
+
+</grammar>
diff --git a/services/image_factory/aeolus-image/lib/base_command.rb
b/services/image_factory/aeolus-image/lib/base_command.rb
index 7e5f72e..f1adcad 100644
--- a/services/image_factory/aeolus-image/lib/base_command.rb
+++ b/services/image_factory/aeolus-image/lib/base_command.rb
@@ -65,6 +65,12 @@ module Aeolus
exit(code)
end
+ def validate_xml_document(schema_path, xml_string)
+ schema = Nokogiri::XML::RelaxNG(File.read(schema_path))
+ doc = Nokogiri::XML xml_string
+ schema.validate(doc)
+ end
+
private
def load_config
begin
diff --git a/services/image_factory/aeolus-image/lib/build_command.rb
b/services/image_factory/aeolus-image/lib/build_command.rb
index fe7be5a..50a84f3 100644
--- a/services/image_factory/aeolus-image/lib/build_command.rb
+++ b/services/image_factory/aeolus-image/lib/build_command.rb
@@ -26,6 +26,16 @@ module Aeolus
quit(1)
end
+ # Validate XML against TDL Schema
+ errors = validate_xml_document("examples/tdl.rng",
@options[:template_str])
+ if errors.length > 0
+ puts "ERROR: The given Template does not conform to the TDL Schema, see
below for specific details:"
+ errors.each do |error|
+ puts "- " + error.message
+ end
+ quit(1)
+ end
+
#This is a temporary hack in case the agent doesn't show up on bus
immediately
sleep(5)
@console.build(@options[:template_str], @options[:target], @options[:image],
@options[:build]).each do |adaptor|
diff --git a/services/image_factory/aeolus-image/spec/base_command_spec.rb
b/services/image_factory/aeolus-image/spec/base_command_spec.rb
index d8459db..31a0aa0 100644
--- a/services/image_factory/aeolus-image/spec/base_command_spec.rb
+++ b/services/image_factory/aeolus-image/spec/base_command_spec.rb
@@ -57,6 +57,20 @@ module Aeolus
File.delete(new_file)
end
end
+
+ describe "#validate_xml_document?" do
+ it "should return errors given non compliant xml" do
+ b = BaseCommand.new
+ errors = b.send(:validate_xml_document, "examples/tdl.rng",
File.read("spec/fixtures/invalid_template.tdl"))
+ errors.length.should > 0
+ end
+
+ it "should return no errors" do
+ b = BaseCommand.new
+ errors = b.send(:validate_xml_document, "examples/tdl.rng",
File.read("spec/fixtures/valid_template.tdl"))
+ errors.length.should == 0
+ end
+ end
end
end
end
\ No newline at end of file
diff --git a/services/image_factory/aeolus-image/spec/build_command_spec.rb
b/services/image_factory/aeolus-image/spec/build_command_spec.rb
index 90e6929..adabb19 100644
--- a/services/image_factory/aeolus-image/spec/build_command_spec.rb
+++ b/services/image_factory/aeolus-image/spec/build_command_spec.rb
@@ -32,6 +32,17 @@ module Aeolus
end
$stdout.string.should include("This combination of parameters is not
currently supported")
end
+ it "should exit with appropriate message when a non compliant template is
given" do
+ @options[:template] = "spec/fixtures/invalid_template.tdl"
+ @options[:image] = '825c94d1-1353-48ca-87b9-36f02e069a8d'
+ b = BuildCommand.new(@options, @output)
+ begin
+ b.run
+ rescue SystemExit => e
+ e.status.should == 1
+ end
+ $stdout.string.should include("ERROR: The given Template does not conform
to the TDL Schema")
+ end
end
describe "#combo_implemented?" do
diff --git a/services/image_factory/aeolus-image/spec/fixtures/invalid_template.tdl
b/services/image_factory/aeolus-image/spec/fixtures/invalid_template.tdl
new file mode 100644
index 0000000..c6395d0
--- /dev/null
+++ b/services/image_factory/aeolus-image/spec/fixtures/invalid_template.tdl
@@ -0,0 +1,18 @@
+<bad_template>
+ <name>tmpl1</name>
+ <description>foo</description>
+ <os>
+ <name>Fedora</name>
+ <arch>x86_64</arch>
+ <version>14</version>
+ <install type="url">
+
<
url>http://download.fedoraproject.org/pub/fedora/linux/releases/14/Fed...
+ </install>
+ </os>
+ <bad_repositories>
+ <repository name="custom">
+ <
url>http://repos.fedorapeople.org/repos/aeolus/demo/webapp/</url>
+ <signed>false</signed>
+ </repository>
+ </bad_repositories>
+</bad_template>
\ No newline at end of file
diff --git a/services/image_factory/aeolus-image/spec/fixtures/valid_template.tdl
b/services/image_factory/aeolus-image/spec/fixtures/valid_template.tdl
new file mode 100644
index 0000000..2f96604
--- /dev/null
+++ b/services/image_factory/aeolus-image/spec/fixtures/valid_template.tdl
@@ -0,0 +1,18 @@
+<template>
+ <name>tmpl1</name>
+ <description>foo</description>
+ <os>
+ <name>Fedora</name>
+ <arch>x86_64</arch>
+ <version>14</version>
+ <install type="url">
+
<
url>http://download.fedoraproject.org/pub/fedora/linux/releases/14/Fed...
+ </install>
+ </os>
+ <repositories>
+ <repository name="custom">
+ <
url>http://repos.fedorapeople.org/repos/aeolus/demo/webapp/</url>
+ <signed>false</signed>
+ </repository>
+ </repositories>
+</template>
\ No newline at end of file
--
1.7.4