Skip to content

Commit

Permalink
Add a NtpServer class
Browse files Browse the repository at this point in the history
* Its responsabilities are to determine the default NTP servers
  and to read a list from a YAML file.
  • Loading branch information
imobachgs committed Feb 17, 2020
1 parent 8117879 commit 1e57624
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/lib/y2network/ntp_server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Copyright (c) [2020] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "yast"
require "yaml"

Yast.import "Product"

module Y2Network
class NtpServer
attr_reader :hostname
attr_reader :country

class << self
# Determines the default servers
#
# The content of this list depends on the base product.
#
# @return [Array<NtpServer>] Default NTP servers
def default_servers(product = nil)
base_products = Yast::Product.FindBaseProducts

host =
if base_products.any? { |p| p["name"] =~ /openSUSE/i }
"opensuse"
else
"suse"
end

(0..3).map { |i| new("#{i}.#{host}.pool.ntp.org") }
end

# Loads the list of servers from the given file
#
# @param path [String] File path
# @return [Array<NtpServer>] Array containing all the servers
def load_from_file(path)
YAML.load_file(path).map do |server|
new(server["address"], country: server["country"])
end
end
end

# Constructor
#
# @param hostname [String] NTP server's hostname
# @param country [String] Country code (e.g., "DE")
def initialize(hostname, country: nil)
@hostname = hostname
@country = country
end
end
end
9 changes: 9 additions & 0 deletions test/data/ntp_servers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---

- 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
64 changes: 64 additions & 0 deletions test/y2network/ntp_server_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright (c) [2020] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "../test_helper"

require "y2network/ntp_server"

describe Y2Network::NtpServer do
describe ".default_servers" do
before do
allow(Yast::Product).to receive(:FindBaseProducts)
.and_return(products)
end

context "when running in an openSUSE system" do
let(:products) do
[{ "name" => "openSUSE" }]
end

it "returns a set of opensuse.pool.ntp.org servers" do
domain = "opensuse.pool.ntp.org"
expect(described_class.default_servers.map(&:hostname)).to eq([
"0.#{domain}", "1.#{domain}", "2.#{domain}", "3.#{domain}"
])
end
end

context "when not running in an openSUSE system" do
let(:products) do
[{ "name" => "SLES" }]
end

it "returns a set of suse.pool.ntp.org servers" do
domain = "suse.pool.ntp.org"
expect(described_class.default_servers.map(&:hostname)).to eq([
"0.#{domain}", "1.#{domain}", "2.#{domain}", "3.#{domain}"
])
end
end
end

describe "'.load_from_file" do
it "returns the list of servers from the file" do
servers = described_class.load_from_file(File.join(DATA_PATH, "ntp_servers.yml"))
expect(servers.map(&:hostname)).to eq(["ntp.cgi.cz"])
end
end
end

0 comments on commit 1e57624

Please sign in to comment.