Skip to content

Commit

Permalink
Merge aac86bf into 8ce7d61
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Apr 17, 2018
2 parents 8ce7d61 + aac86bf commit 16c21fe
Show file tree
Hide file tree
Showing 7 changed files with 71 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/
6 changes: 6 additions & 0 deletions package/yast2-ntp-client.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Tue Apr 17 07:03:46 UTC 2018 - jreidinger@suse.com

- Add support for writting Tinker record via autoyast (bsc#1086526)
- 3.2.16

-------------------------------------------------------------------
Wed Oct 11 08:40:26 UTC 2017 - mvidner@suse.com

Expand Down
2 changes: 1 addition & 1 deletion package/yast2-ntp-client.spec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


Name: yast2-ntp-client
Version: 3.2.15
Version: 3.2.16
Release: 0
Summary: YaST2 - NTP Client Configuration
License: GPL-2.0+
Expand Down
49 changes: 46 additions & 3 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 @@ -39,6 +40,7 @@ class NtpConf < ::CFA::BaseModel
manycastclient
fudge
restrict
tinker
driftfile
logfile
keys
Expand Down Expand Up @@ -228,6 +230,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 +242,18 @@ 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
raise "Unsupported entry #{key}."
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 +333,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 @@ -451,6 +464,36 @@ def options_matcher
end
end

# class to represent tinker misc option - https://www.eecis.udel.edu/~mills/ntp/html/miscopt.html#tinker
class TinkerRecord < CommandRecord
AUGEAS_KEY = "tinker[]".freeze

def value=(val)
values = val.to_s.split("\s")
map = Hash[*values]
ensure_tree_value
tree_value.tree.delete("key")
tree_value.tree.delete("key[]")
# fix writting order as key have to be before trailing comment
any_matcher = CFA::Matcher.new { true }
placer = CFA::BeforePlacer.new(any_matcher)
map.each_pair.reverse_each { |key, value| tree_value.tree.add(key, value, placer) }
end

def value
return [] unless tree_value?
key_matcher = CFA::Matcher.new { |k, _v| k != "#comment" && k != "#comment[]" }
keys = tree_value.tree.select(key_matcher)
keys.map { |option| "#{option[:key]} #{option[:value]}" }.join(" ")
end

# overwrite options matcher as Tinker is stored in autoyast as address and not options
def options_matcher
# no options, everything is in value
Matcher.new { false }
end
end

# class to represent a requestkey entry.
# For example:
# requestkey 1
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
10 changes: 9 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,13 @@
it "reads sync intervall" do
expect(subject.sync_interval).to eq 15
end

it "following write succeed" do
allow(subject).to receive(:write_and_update_policy).and_return(true)
allow(subject).to receive(:check_service).and_return(true)
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 16c21fe

Please sign in to comment.