Skip to content

Commit

Permalink
Extend NtpClient to offer an NtpServer based API
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Feb 19, 2020
1 parent a2624a6 commit cf25327
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/modules/NtpClient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
require "ui/text_helpers"
require "erb"
require "yast2/systemctl"
require "y2network/ntp_server"

module Yast
class NtpClientClass < Module
Expand Down Expand Up @@ -193,7 +194,35 @@ def MakePoolRecord(country_code, location)
}
end

# Returns the know ntp servers
#
# @return [Array<Y2Network::NtpServer>] Known NTP servers
def public_ntp_servers
update_ntp_servers! if @ntp_servers.nil?
@ntp_servers.values.map do |srv|
Y2Network::NtpServer.new(
srv["address"], country: srv["country"], location: srv["location"]
)
end
end

# Returns the NTP servers for the given country
#
# @param country [String] Country code
# @return [Array<Y2Network::NtpServer>] NTP servers for the given country
def country_ntp_servers(country)
normalized_country = country.upcase
servers = public_ntp_servers.select { |s| s.country.upcase == normalized_country }
# bnc#458917 add country, in case data/country.ycp does not have it
country_server = make_country_ntp_server(country)
unless servers.map(&:hostname).include?(country_server.hostname)
servers << country_server
end
servers
end

# Get the list of known NTP servers
# @deprecated Use public_ntp_servers instead
# @return a list of known NTP servers
def GetNtpServers
update_ntp_servers! if @ntp_servers.nil?
Expand All @@ -219,9 +248,11 @@ def GetCountryNames
end

# Get list of public NTP servers for a country
#
# @param [String] country two-letter country code
# @param [Boolean] terse_output display additional data (location etc.)
# @return [Array] of servers (usable as combo-box items)
# @deprecated Use public_ntp_servers_by_country instead
def GetNtpServersByCountry(country, terse_output)
country_names = {}
servers = GetNtpServers()
Expand Down Expand Up @@ -919,6 +950,7 @@ def country_server_label(location = "", country = "")
# @return [Array <Hash>] pool records for given countries
def pool_servers_for(known_countries)
known_countries.map do |short_country, country_name|
# bnc#458917 add country, in case data/country.ycp does not have it
MakePoolRecord(short_country, country_name)
end
end
Expand All @@ -945,6 +977,15 @@ def unsupported_error(unsupported)

Yast::Report.Error(wrap_text(msg, width - 4))
end

# Pool server for the given country
#
# @param country [String] Country code
# @return [Y2Network::NtpServer]
def make_country_ntp_server(country)
record = MakePoolRecord(country, "")
Y2Network::NtpServer.new(record["address"], country: record["country"])
end
end

NtpClient = NtpClientClass.new
Expand Down
16 changes: 16 additions & 0 deletions test/data/ntp_servers_sample.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---

- access_policy: open access
address: ntp.cgi.cz
country: CZ
exact_location: Prague, The Czech Republic
location: Czech Republic
stratum: '2'
synchronization: NTP V4 secondary (stratum 2), PC/FreebSD

- address: tick.fh-augsburg.de
country: DE
exact_location: Augsburg University of Applied Sciences (FH), Augsburg, Bavaria,
Germany
stratum: '2'
synchronization: NTP V3 secondary (stratum 2), i486/Linux
32 changes: 32 additions & 0 deletions test/ntp_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,38 @@
end
end

describe "#public_ntp_servers" do
before do
allow(subject).to receive(:GetAllKnownCountries)
.and_return("DE" => "Germany", "CZ" => "Czech Republic")
allow(Yast::Directory).to receive(:find_data_file).with("ntp_servers.yml")
.and_return(DATA_PATH.join("ntp_servers_sample.yml").to_s)
end

it "returns the list of public NTP servers including the default one for each country" do
servers = subject.public_ntp_servers
expect(servers.map(&:hostname)).to eq(
["ntp.cgi.cz", "tick.fh-augsburg.de", "de.pool.ntp.org", "cz.pool.ntp.org"]
)
end
end

describe "#country_ntp_servers" do
before do
allow(subject).to receive(:GetAllKnownCountries)
.and_return("DE" => "Germany", "CZ" => "Czech Republic")
allow(Yast::Directory).to receive(:find_data_file).with("ntp_servers.yml")
.and_return(DATA_PATH.join("ntp_servers_sample.yml").to_s)
end

it "returns the list of public NTP servers for the given country" do
servers = subject.country_ntp_servers("DE")
expect(servers.map(&:hostname)).to eq(
["tick.fh-augsburg.de", "de.pool.ntp.org"]
)
end
end

describe "#MakePoolRecord" do
let(:record) do
{
Expand Down
4 changes: 4 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
require "yast"
require "yast/rspec"
require "yaml"
require "pathname"

TESTS_PATH = Pathname.new(File.dirname(__FILE__))
DATA_PATH = TESTS_PATH.join("data")

RSpec.configure do |config|
config.mock_with :rspec do |mocks|
Expand Down

0 comments on commit cf25327

Please sign in to comment.