Skip to content

Commit

Permalink
improve documentation and coverage of chrony
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Nov 27, 2017
1 parent 2456ca5 commit bc90f3f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/lib/cfa/chrony_conf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def initialize(file_handler: nil)
super(CFA::AugeasParser.new("chrony.lns"), PATH, file_handler: file_handler)
end

# loads cfa model and ensure all collection keys have [] suffix
def load
super
fix_collection_names(data)
Expand Down Expand Up @@ -47,6 +48,11 @@ def add_pool(address, options = :default)
data.add(key, value, placer)
end

# modifies pool entry with original address to new adress and specified options
# @param original_address [String] address to modify
# @param new_address [String] new adress of pool entry. Can be same as original one
# @param options [Hash<String, nil | String>] options format is that key is option name and
# value is either nil for keyword options or String with value for key value options
def modify_pool(original_address, new_address, options)
matcher = pool_matcher(original_address)
value = AugeasTreeValue.new(AugeasTree.new, new_address)
Expand All @@ -61,6 +67,8 @@ def modify_pool(original_address, new_address, options)
data.add(key, value, placer)
end

# deletes pool entry
# @param address [String] pool to delete
def delete_pool(address)
matcher = pool_matcher(address)

Expand Down
78 changes: 77 additions & 1 deletion test/cfa/chrony_conf_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def ntp_conf(file)

let(:file) { ntp_file(content) }

let(:content) { "" }

before do
subject.load
end
Expand Down Expand Up @@ -99,7 +101,7 @@ def ntp_conf(file)
end
end

describe "hardware_clock?" do
describe "#hardware_clock?" do
context "hardware clock defined" do
let(:content) do
"refclock PPS /dev/pps0 lock NMEA refid GPS\n"
Expand All @@ -121,4 +123,78 @@ def ntp_conf(file)
end
end
end

describe "#modify_pool" do
let(:content) do
"pool test.ntp.org iburst\n"
end

it "sets new address for original address" do
subject.modify_pool("test.ntp.org", "lest.ntp.org", {})
subject.save

expect(file.content.lines).to include "pool lest.ntp.org\n"
end

it "modifies options according to passed ones" do
subject.modify_pool("test.ntp.org", "lest.ntp.org", "offline" => nil, "maxsources" => "5")
subject.save

expect(file.content.lines).to include "pool lest.ntp.org offline maxsources 5\n"
end

it "works when address does not change" do
subject.modify_pool("test.ntp.org", "test.ntp.org", {})
subject.save

expect(file.content.lines).to include "pool test.ntp.org\n"
end

it "appends new pool entry if original address does not exist" do
subject.modify_pool("lest.ntp.org", "lest.ntp.org", {})
subject.save

expect(file.content.lines).to eq ["pool test.ntp.org iburst\n", "pool lest.ntp.org\n"]
end
end

describe "#delete_pool" do
let(:content) do
"pool test.ntp.org iburst\n" \
"pool lest.ntp.org offline\n"
end

it "deletes pool entry with given address" do
subject.delete_pool("lest.ntp.org")
subject.save

expect(file.content.lines).to eq ["pool test.ntp.org iburst\n"]
end

it "does nothing if pool entry with given address does not exist" do
subject.delete_pool("not.exist.ntp.org")
subject.save

expect(file.content.lines).to eq ["pool test.ntp.org iburst\n", "pool lest.ntp.org offline\n"]
end
end

describe "#default_pool_options" do
it "returns Hash of default options for pool" do
expect(subject.default_pool_options).to be_a(Hash)
end
end

describe "#pools" do
let(:content) do
"pool test.ntp.org iburst\n" \
"pool lest.ntp.org offline\n" \
"pool lest2.ntp.org\n" \
"# fancy comment\n"
end

it "returns Hash with address as key and options as value" do
expect(subject.pools.size).to eq 3
end
end
end

0 comments on commit bc90f3f

Please sign in to comment.