Skip to content

Commit

Permalink
Merge d47673d into 8ce7d61
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Apr 16, 2018
2 parents 8ce7d61 + d47673d commit 0fa4626
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ tmp.*
/.yardoc
/coverage
test/data/scr_root/var/lib/YaST2/file_checksums.ycp
test/data/scr_root/etc/sysconfig/
36 changes: 32 additions & 4 deletions src/lib/cfa/ntp_conf.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "yast/logger"
require "cfa/base_model"
require "cfa/augeas_parser"
require "cfa/matcher"
Expand Down Expand Up @@ -209,7 +210,9 @@ def reset_cache
def record_entries
return @record_entries if @record_entries
matcher = Matcher.new do |k, _v|
RECORD_ENTRIES.include?(k.gsub("[]", ""))
# we want to get all content except comments, as now we can process
# even not yet known options with MiscRecord
k !~ /#comment/
end
@record_entries = @augeas_tree.select(matcher).map do |e|
Record.new_from_augeas(e)
Expand All @@ -228,6 +231,7 @@ def record_entries
# for its options in the AugeasElement. For each one,
# a Record subclass is created.
class Record
include ::Yast::Logger
# Creates the corresponding subclass object according
# to its AugeasElement key.
# @param [String] key
Expand All @@ -239,8 +243,22 @@ def self.new_from_augeas(augeas_entry)
# @param [string] key
def self.record_class(key)
entry_type = key.gsub("[]", "")
record_class = "::CFA::NtpConf::#{entry_type.capitalize}Record"
Kernel.const_get(record_class)
record_class = "#{entry_type.capitalize}Record"
if CFA::NtpConf.constants.include?(record_class.to_sym)
CFA::NtpConf.const_get(record_class.to_sym)
else
# it is not a predefined record type, so use a generic misc record
res = Class.new(MiscRecord)
# and specify there its augeas type
res.send(:define_method, :augeas_type) { entry_type + "[]" }
res
end
end

# default implementation just return constant AUGEAS_KEY, but can be
# redefined
def augeas_type
self.class.const_get("AUGEAS_KEY")
end

def initialize(augeas = nil)
Expand Down Expand Up @@ -320,7 +338,7 @@ def raw_options=(raw_options)
protected

def create_augeas
{ key: self.class.const_get("AUGEAS_KEY"), value: nil }
{ key: augeas_type, value: nil }
end

def tree_value?
Expand Down Expand Up @@ -361,6 +379,16 @@ def options_matcher
end
end

# class to represent generic non-specific key
class MiscRecord < Record
def options
[]
end

def options=(_value)
end
end

# class to represent a ntp command record entry. There is a
# subclass for server, peer, broadcast and broacastclient.
class CommandRecord < Record
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/autoyast/autoinst.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
<options>iburst</options>
<type>server</type>
</peer>
<peer>
<address>panic 0</address>
<comment/>
<options/>
<type>tinker</type>
</peer>
</peers>
<restricts config:type="list">
<restrict>
Expand Down
9 changes: 8 additions & 1 deletion test/ntp_client_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative "test_helper"

require "yast2/target_file"
require "fileutils"
require "cfa/memory_file"
require "cfa/ntp_conf"
Expand Down Expand Up @@ -61,7 +62,7 @@
end

it "reads the list of peers" do
expect(subject.ntp_records.size).to eq 4
expect(subject.ntp_records.size).to eq 5
end

it "reads the list of restricts" do
Expand Down Expand Up @@ -89,6 +90,12 @@
it "reads sync intervall" do
expect(subject.sync_interval).to eq 15
end

it "following write succeed" do
puts subject.ntp_records.inspect
expect(Yast::Report).to_not receive(:Error)
expect(subject.Write).to eq true
end
end

context "with an empty AutoYaST configuration" do
Expand Down
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require "yast/rspec"
require "yaml"

ENV["LC_ALL"] = "en_US.utf-8"

RSpec.configure do |config|
config.mock_with :rspec do |mocks|
# If you misremember a method name both in code and in tests,
Expand Down

0 comments on commit 0fa4626

Please sign in to comment.