Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
224 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |