Skip to content

Commit

Permalink
Add a new NtpServer widget
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Apr 19, 2017
1 parent 5643e19 commit 3b9e6bb
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Makefile.am
Expand Up @@ -217,7 +217,8 @@ ylibdialog_DATA = \
ylibwidgetdir = "${yast2dir}/lib/installation/widgets"
ylibwidget_DATA = \
lib/installation/widgets/overview.rb \
lib/installation/widgets/system_role.rb
lib/installation/widgets/system_role.rb \
lib/installation/widgets/ntp_server.rb

EXTRA_DIST = $(module_DATA) $(client_DATA) $(ynclude_DATA) $(scrconf_DATA) $(schemafiles_DATA) $(desktop_DATA) $(fillup_DATA) $(ylibdialog_DATA) $(ylib_DATA) $(ylibtransfer_DATA)

Expand Down
96 changes: 96 additions & 0 deletions src/lib/installation/widgets/ntp_server.rb
@@ -0,0 +1,96 @@
require "yast"
require "installation/system_role"

Yast.import "CWM"
Yast.import "Popup"
Yast.import "Label"
Yast.import "IP"
Yast.import "Hostname"

module Installation
module Widgets
# This widget is responsible of validating and storing the NTP server to use.
class NtpServer < CWM::InputField
# intentional no translation for CaaSP
def label
"NTP Servers"
end

# Store the value of the input field if validates
def store
role["ntp_servers"] = servers
end

# Initializes the widget's value
def init
self.value = (role["ntp_servers"] || []).join(" ")
end

# Validate input
#
# * All specified IPs or hostnames should be valid
# * If no server is specified, ask the user whether proceed with installation or not
#
# @return [Boolean] true if value is valid; false otherwise.
def validate
return skip_ntp_server? if servers.empty?
return true if servers.all? { |v| Yast::IP.Check(v) || Yast::Hostname.CheckFQ(v) }
Yast::Popup.Error(
# TRANSLATORS: error message for invalid administration node location
_("Not valid address/hostname for NTP servers")
)
false
end

private

# Parses the widget's value an return the potential list of hostnames/addresses
#
# @return [Array<String>] List of hostnames/addresses
def servers
value.tr(",", " ").split(" ")
end

# Determine if the user wants to intentionally skip the NTP server configuration
#
# @return [Boolean] true if user wants to skip it; false otherwise.
def skip_ntp_server?
Yast::Popup.AnyQuestion(
_("NTP Server"),
# TRANSLATORS: error message for invalid ntp server name/address
_("You have not configured an NTP server. This may lead to\n" \
"your cluster not functioning properly or at all.\n" \
"Proceed with caution and at your own risk.\n\n" \
"Would you like to continue with the installation?"),
Yast::Label.YesButton,
Yast::Label.NoButton,
:yes
)
end

# Return the dashboard role
def role
::Installation::SystemRole.find("dashboard_role")
end
end

# NTP Server widget placeholder
class NtpServerPlace < CWM::ReplacePoint
def initialize
@ntp_server = NtpServer.new
@empty = CWM::Empty.new("no_ntp_server")
super(id: "ntp_server_placeholder", widget: @empty)
end

# Show the NtpServer widget
def show
replace(@ntp_server)
end

# Hide the NtpServer widget
def hide
replace(@empty)
end
end
end
end
3 changes: 2 additions & 1 deletion test/Makefile.am
Expand Up @@ -35,7 +35,8 @@ TESTS = \
updates_manager_test.rb \
widgets_overview_test.rb \
widgets_system_role_test.rb \
lib/system_role_handlers_runner_test.rb
lib/system_role_handlers_runner_test.rb \
lib/widgets/ntp_server_test.rb

TEST_EXTENSIONS = .rb
RB_LOG_COMPILER = rspec
Expand Down
124 changes: 124 additions & 0 deletions test/lib/widgets/ntp_server_test.rb
@@ -0,0 +1,124 @@
#!/usr/bin/env rspec

require_relative "../../test_helper"
require "installation/widgets/ntp_server"

describe ::Installation::Widgets::NtpServer do
subject(:widget) { ::Installation::Widgets::NtpServer.new }
let(:dashboard_role) { ::Installation::SystemRole.new(id: "dashboard_role") }

before do
allow(::Installation::SystemRole).to receive(:find)
.with("dashboard_role").and_return(dashboard_role)
end

describe "#label" do
it "returns 'NTP Servers'" do
expect(widget.label).to eq("NTP Servers")
end
end

describe "#init" do
it "reads initial value from dashboard role" do
allow(dashboard_role).to receive(:[]).with("ntp_servers")
.and_return(["server1"])
expect(widget).to receive(:value=).with("server1")
widget.init
end
end

describe "#store" do
before do
allow(widget).to receive(:value).and_return(value)
end

context "when value is an empty" do
let(:value) { "" }

it "sets the role ntp_servers property to an empty array" do
widget.store
expect(dashboard_role["ntp_servers"]).to eq([])
end
end

context "when value is a hostname/address" do
let(:value) { "server1" }

it "sets the role ntp_servers property to an array containing the hostname/address" do
widget.store
expect(dashboard_role["ntp_servers"]).to eq(["server1"])
end
end

context "when more than one hostname/address separated by spaces" do
let(:value) { "server1 server2" }

it "sets the role ntp_servers property to an array containing all the hostnames/addresses" do
widget.store
expect(dashboard_role["ntp_servers"]).to eq(["server1", "server2"])
end
end

context "when more than one hostname/address separated by commas" do
let(:value) { "server1,server2" }

it "sets the role ntp_servers property to an array containing all the hostnames/addresses" do
widget.store
expect(dashboard_role["ntp_servers"]).to eq(["server1", "server2"])
end
end

context "when more than one hostname/address separated by mixed spaces and commas" do
let(:value) { "server1,server2 server3" }

it "sets the role ntp_servers property to an array containing all the hostnames/addresses" do
widget.store
expect(dashboard_role["ntp_servers"]).to eq(["server1", "server2", "server3"])
end
end
end

describe "#validate" do
before do
allow(widget).to receive(:value).and_return(value)
end

context "when valid IP addresses are provided" do
let(:value) { "192.168.122.1 10.0.0.1" }

it "returns true" do
expect(widget.validate).to eq(true)
end
end

context "when valid hostnames are provided" do
let(:value) { "ntp.suse.de ntp.suse.cz" }

it "returns true" do
expect(widget.validate).to eq(true)
end
end

context "when non valid addresses/hostnames are provided" do
let(:value) { "ntp.suse.de ***" }

it "returns false" do
allow(Yast::Popup).to receive(:Error)
expect(widget.validate).to eq(false)
end

it "reports the problem to the user" do
expect(Yast::Popup).to receive(:Error)
widget.validate
end
end

context "when no value is provided" do
let(:value) { "" }

it "returns false" do
expect(widget.validate).to eq(false)
end
end
end
end

0 comments on commit 3b9e6bb

Please sign in to comment.