Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test that generated json from autoyast xml is valid #1946

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci-service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ jobs:
yast2-proxy
yast2-storage-ng
yast2-users
python3-jsonschema

- name: Install RubyGems dependencies
run: bundle config set --local with 'development' && bundle install
Expand Down
2 changes: 1 addition & 1 deletion service/lib/agama/autoyast/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def to_agama(dir)
FileUtils.mkdir_p(path)
import_yast
profile = read_profile
File.write(path.join("autoinst.json"), export_profile(profile).to_json)
File.write(path.join("autoinst.json"), JSON.pretty_generate(export_profile(profile)))
end

private
Expand Down
4 changes: 2 additions & 2 deletions service/lib/agama/autoyast/l10n_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def initialize(profile)
#
# If there is no l10n information, it returns an empty hash.
#
# @return [Hash] Agama "l10n" section
# @return [Hash] Agama "localization" section
def read
l10n = keyboard
.merge(languages)
.merge(timezone)
l10n.empty? ? {} : { "l10n" => l10n }
l10n.empty? ? {} : { "localization" => l10n }
end

private
Expand Down
5 changes: 3 additions & 2 deletions service/lib/agama/autoyast/scripts_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ScriptsReader
# @param profile [ProfileHash] AutoYaST profile
def initialize(profile)
@profile = profile
@anonymous_counter = 0
end

# Returns a hash that corresponds to Agama "scripts" section.
Expand Down Expand Up @@ -100,10 +101,10 @@ def read_init_scripts
# @param section [Hash] AutoYaST script section
def read_script(section)
script = {
"name" => section["file_name"]
"name" => section["filename"] || "annonymous#{@anonymous_counter += 1}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: it should be anonymous but, to be honest, I do not like that prefix that much. I would prefer script-, or unnamed- or something else.

}

if section["location"]
if section["location"] && !section["location"].empty?
script["url"] = section["location"]
elsif section["source"]
script["body"] = section["source"]
Expand Down
34 changes: 30 additions & 4 deletions service/test/agama/autoyast/converter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@
end
let(:xml_valid?) { true }
let(:xml_errors) { [] }
let(:result_path) { File.join(workdir, "autoinst.json") }
let(:result) do
content = File.read(File.join(workdir, "autoinst.json"))
content = File.read(result_path)
JSON.parse(content)
end
let(:storage_manager) do
Expand Down Expand Up @@ -109,7 +110,7 @@

it "evaluates the ERB code" do
subject.to_agama(workdir)
expect(result["l10n"]).to include(
expect(result["localization"]).to include(
"languages" => ["en_US.UTF-8", "es_ES.UTF-8"]
)
end
Expand Down Expand Up @@ -157,9 +158,9 @@
end
end

it "exports l10n settings" do
it "exports localization settings" do
subject.to_agama(workdir)
expect(result["l10n"]).to include(
expect(result["localization"]).to include(
"languages" => ["en_US.UTF-8"],
"timezone" => "Atlantic/Canary",
"keyboard" => "us"
Expand All @@ -177,4 +178,29 @@
subject.to_agama(workdir)
end
end

context "for cloned profile" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is just an AutoYaST profile, right? No matter whether it is cloned or written from scratch.

let(:profile_name) { "cloned.xml" }

it "generate json according to schema" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it "generate json according to schema" do
it "generate JSON according to schema" do

# sadly rubygem-json-schema cannot be used due to too old supported format
if !system("which jsonschema")
pending "can run only if python3-jsonschema is installed"
break
end

subject.to_agama(workdir)

schema = File.expand_path(
"../../../../rust/agama-lib/share/profile.schema.json",
__dir__
)

# filter out deprecation warning as check-jsonschema is not packaged for TW yet
result = `jsonschema -i '#{result_path}' '#{schema}' 2>&1 | \
grep -v 'DeprecationWarning' | \
grep -v 'from jsonschema.cli import main'`
expect(result).to eq ""
end
end
end
6 changes: 2 additions & 4 deletions service/test/agama/autoyast/l10n_reader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
{}
end

let(:l10n) { subject.read["localization"] }

subject do
described_class.new(Yast::ProfileHash.new(profile))
end
Expand All @@ -49,7 +51,6 @@
end

it "includes a 'keyboard' key with its value" do
l10n = subject.read["l10n"]
expect(l10n["keyboard"]).to eq("us")
end
end
Expand All @@ -65,7 +66,6 @@
end

it "includes a 'languages' key with all the languages" do
l10n = subject.read["l10n"]
expect(l10n["languages"]).to eq(["en_US.UTF-8", "es_ES.UTF-8", "cs_CZ.UTF-8"])
end

Expand All @@ -80,7 +80,6 @@
end

it "uses the UTF-8 encoding" do
l10n = subject.read["l10n"]
expect(l10n["languages"]).to eq(["en_US.UTF-8", "es_ES.UTF-8"])
end
end
Expand All @@ -92,7 +91,6 @@
end

it "includes a 'keyboard' key with its value" do
l10n = subject.read["l10n"]
expect(l10n["timezone"]).to eq("Europe/Berlin")
end
end
Expand Down
18 changes: 9 additions & 9 deletions service/test/agama/autoyast/scripts_reader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@

context "when the script definition includes the sources" do
let(:script) do
{ "file_name" => "script.sh",
"location" => "https://example.com/script.sh" }
{ "filename" => "script.sh",
"location" => "https://example.com/script.sh" }
end

it "sets the \"url\" to the \"location\"" do
Expand All @@ -45,8 +45,8 @@
context "when the script definition specifies a location" do
let(:script) do
{
"file_name" => "script.sh",
"source" => "#!/bin/bash\necho 'Hello World!'"
"filename" => "script.sh",
"source" => "#!/bin/bash\necho 'Hello World!'"
}
end

Expand Down Expand Up @@ -77,9 +77,9 @@
it_behaves_like "a script reader", "chroot-scripts", "post"

let(:chroot_script) do
{ "file_name" => "test.sh",
"chrooted" => true,
"source" => "#!/bin/bash\necho 'Hello World!'" }
{ "filename" => "test.sh",
"chrooted" => true,
"source" => "#!/bin/bash\necho 'Hello World!'" }
end

let(:profile) do
Expand All @@ -92,8 +92,8 @@

context "when the \"chrooted\" option is not set" do
let(:chroot_script) do
{ "file_name" => "test.sh",
"source" => "#!/bin/bash\necho 'Hello World!'" }
{ "filename" => "test.sh",
"source" => "#!/bin/bash\necho 'Hello World!'" }
end

it "sets the \"chroot\" option to false" do
Expand Down
Loading
Loading