Skip to content

Commit

Permalink
Rely on the new Y2Network::NtpServer class
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Feb 19, 2020
1 parent d8a4a38 commit 0802627
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
21 changes: 5 additions & 16 deletions src/lib/installation/dialogs/ntp_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
require "yast"
require "cwm/dialog"
require "installation/widgets/ntp_server"
require "y2network/ntp_server"

module Installation
module Dialogs
Expand Down Expand Up @@ -68,7 +69,7 @@ def ntp_servers
# TODO: use Yast::NtpClient.ntp_conf if configured
# to better handle going back
servers = dhcp_ntp_servers
servers = [ntp_fallback] if servers.empty? && default_ntp_setup_enabled?
servers = [ntp_fallback.hostname] if servers.empty? && default_ntp_setup_enabled?

servers
end
Expand Down Expand Up @@ -98,23 +99,11 @@ def default_ntp_setup_enabled?

# The fallback servers for NTP configuration
#
# It propose a random pool server in range 0..3
# It propose a random server from the default pool
#
# @return [String] the fallback servers
# @return [Y2Network::NtpServer] the fallback server
def ntp_fallback
"#{rand(4)}.#{ntp_host}.pool.ntp.org"
end

def ntp_host
# copied from timezone/dialogs.rb:
base_products = Yast::Product.FindBaseProducts

if base_products.any? { |p| p["name"] =~ /openSUSE/i }
"opensuse"
else
# TODO: use a SUSE server when available in the future
"novell"
end
Y2Network::NtpServer.default_servers.sample
end
end
end
Expand Down
14 changes: 10 additions & 4 deletions test/dialogs/ntp_setup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,22 @@
end

context "no NTP server set in DHCP and default NTP is enabled in control.xml" do
let(:default_servers) do
[
Y2Network::NtpServer.new("0.opensuse.pool.ntp.org"),
Y2Network::NtpServer.new("1.opensuse.pool.ntp.org")
]
end

before do
allow(Yast::ProductFeatures).to receive(:GetBooleanFeature)
.with("globals", "default_ntp_setup").and_return(true)
allow(Yast::Product).to receive(:FindBaseProducts)
.and_return(["name" => "openSUSE-Tumbleweed-Kubic"])
allow(Y2Network::NtpServer).to receive(:default_servers).and_return(default_servers)
end

it "proposes to use a random openSUSE pool server" do
it "proposes to use a random server from the default pool" do
expect(::Installation::Widgets::NtpServer).to receive(:new).and_wrap_original do |original, arg|
expect(arg.first).to match(/\A[0-3]\.opensuse\.pool\.ntp\.org\z/)
expect(default_servers.map(&:hostname)).to include(arg.first)
original.call(arg)
end
subject.run
Expand Down

0 comments on commit 0802627

Please sign in to comment.