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 e2dedb9
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
40 changes: 40 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,44 @@ 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 = "ES"
servers = public_ntp_servers.select { |s| s.country == 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

# 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

# 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 +257,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
24 changes: 24 additions & 0 deletions test/data/ntp_servers_sample.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---

- 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

- access_policy: Public (glad to receive a note)
address: hora.oxixares.com
country: ES
exact_location: Ogijares (Granada/SPAIN)
location: Spain
stratum: '2'
synchronization: ntp v4.1.1 secondary (stratum 2)/athlon XP 2500+/SuSE Linux 8.2
28 changes: 28 additions & 0 deletions test/ntp_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,34 @@
end
end

describe "#public_ntp_servers" do
before do
allow(Yast::Directory).to receive(:find_data_file)
.and_return(DATA_PATH.join("ntp_servers_sample.yml").to_s)
end

it "returns the list of public NTP servers" do
servers = subject.public_ntp_servers
expect(servers.map(&:hostname)).to eq(
["ntp.cgi.cz", "tick.fh-augsburg.de", "hora.oxixares.com"]
)
end
end

describe "#country_ntp_servers" do
before do
allow(Yast::Directory).to receive(:find_data_file)
.and_return(DATA_PATH.join("ntp_servers_sample.yml").to_s)
end

it "returns the list of public NTP servers" do
servers = subject.country_ntp_servers("es")
expect(servers.map(&:hostname)).to eq(
["hora.oxixares.com", "es.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 e2dedb9

Please sign in to comment.