Skip to content

Commit

Permalink
Merge 5c9355e into b6c8b24
Browse files Browse the repository at this point in the history
  • Loading branch information
dgdavid committed Aug 16, 2018
2 parents b6c8b24 + 5c9355e commit 82e4ea8
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 51 deletions.
6 changes: 6 additions & 0 deletions package/yast2-dns-server.changes
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Tue Aug 14 07:46:55 UTC 2018 - dgonzalez@suse.com

- Manage the start mode and service status directly from Ruby.
- 4.1.1

-------------------------------------------------------------------
Tue Aug 7 12:14:35 UTC 2018 - dgonzalez@suse.com

Expand Down
2 changes: 1 addition & 1 deletion package/yast2-dns-server.spec
Expand Up @@ -17,7 +17,7 @@


Name: yast2-dns-server
Version: 4.1.0
Version: 4.1.1
Release: 0
Url: https://github.com/yast/yast-dns-server

Expand Down
26 changes: 23 additions & 3 deletions src/include/dns-server/dialog-main.rb
Expand Up @@ -1898,12 +1898,32 @@ def SaveAndRestart(event)

# Writes DNS server settings and saves the service
#
# @note currently, the DnsServer is a Perl module, reason why the write of
# settings is being performed in two steps.
# As side effect, it also sets/resets the local forwarder
#
# @return [Boolean] true if settings are saved successfully; false otherwise
def write_settings
DnsServer.Write && service.save
return false unless DnsServer.Write

result = service.save(keep_state: Mode.auto)
reset_local_forwarder
result
end

# Resets the local forwarder to "resolver"
#
# Local forwarder will be changed to "resolver" if
#
# * its current value is "bind", and
# * service is stopped (was stopped or could not be (re)started or reloaded)
def reset_local_forwarder
return unless DnsServer.GetLocalForwarder == "bind"

service.refresh

unless service.currently_active?
DnsServer.SetLocalForwarder("resolver")
log.warn("Local forwarder set to: #{DnsServer.GetLocalForwarder}")
end
end

# Shows a popup asking to the user if wants to change settings
Expand Down
43 changes: 0 additions & 43 deletions src/modules/DnsServer.pm
Expand Up @@ -1526,49 +1526,6 @@ sub Write {

$ret = {};

if (Mode->auto() || Mode->config())
{
# named has to be started
if ($start_service)
{
my $success = 1;
if (! $write_only)
{
# named is running
if (Service->Status("named") == 0) {
y2milestone("Reloading service 'named'");
$success = Service->Reload("named")
} else {
y2milestone("Restarting service 'named'");
$success = Service->Restart("named")
}
}
Service->Enable ("named");
if (! $success)
{
# Cannot start service 'named', because of error that follows Error:. Do not translate named.
Report->Error (__("Error occurred while starting service named.\n\n"));
$ok = 0;
# There's no 'named' running -> prevent from blocking DNS queries
$self->SetLocalForwarder("resolver") if GetLocalForwarder() eq "bind";
y2warning("Local forwarder set to: ".GetLocalForwarder());
}
}
# named has to be stopped
else
{
if (! $write_only)
{
y2milestone("Stopping service 'named'");
Service->Stop("named");
# There's no 'named' running. Reset dns forwarder again
$self->SetLocalForwarder("resolver") if GetLocalForwarder() eq "bind";
y2warning("Local forwarder set to: ".GetLocalForwarder());
}
Service->Disable ("named");
}
}

# First run finished
if ($ok and first_run())
{
Expand Down
67 changes: 63 additions & 4 deletions test/main_test.rb
Expand Up @@ -8,6 +8,8 @@
Yast.import "DnsServerUI"

describe "DnsServerDialogMainInclude" do
subject(:main_dialog) { CurrentDialogMain.new }

class CurrentDialogMain
include Yast::I18n
include Yast::UIShortcuts
Expand All @@ -16,22 +18,29 @@ class CurrentDialogMain
def initialize
Yast.include self, "dns-server/dialog-main.rb"
end

def fun_ref(*args)
end
end

let(:auto) { false }

before do
allow(Yast::Mode).to receive(:auto).and_return(auto)
allow_any_instance_of(CurrentDialogMain).to receive(:fun_ref)
end

describe "#WriteDialog" do
subject(:main_dialog) { CurrentDialogMain.new }

before do
allow(Yast::DnsServer).to receive(:Write).and_return(dns_configuration_written)
allow(Yast2::SystemService).to receive(:find).and_return(service)

allow(service).to receive(:currently_active?).and_return(active)
end

let(:service) { instance_double(Yast2::SystemService, save: true) }
let(:service) { instance_double(Yast2::SystemService, save: true, refresh: true) }
let(:dns_configuration_written) { true }
let(:active) { true }

context "when DNS configuration is written" do
it "saves the system service" do
Expand All @@ -43,6 +52,56 @@ def initialize
it "returns :next" do
expect(main_dialog.WriteDialog).to eq(:next)
end


context "and local forwarder is \"bind\"" do
before do
allow(Yast2::Popup).to receive(:show)
allow(Yast::DnsServer).to receive(:GetLocalForwarder).and_return("bind")
end

context "but service is stopped" do
let(:active) { false }

it "resets DNS local forwarder" do
expect(Yast::DnsServer).to receive(:SetLocalForwarder).with("resolver")

main_dialog.WriteDialog
end
end

context "but service is running" do
let(:active) { true }

it "does not reset DNS local forwarder" do
expect(Yast::DnsServer).to_not receive(:SetLocalForwarder)

main_dialog.WriteDialog
end
end
end

context "and local forwarder is not \"bind\"" do
before do
allow(Yast::DnsServer).to receive(:GetLocalForwarder).and_return("whatever")
end

it "does not reset DNS local forwarder" do
expect(Yast::DnsServer).to_not receive(:SetLocalForwarder)

main_dialog.WriteDialog
end
end

context "in auto mode" do
let(:auto) { true }

it "keeps the server status" do
expect(service).to receive(:save).with(hash_including(keep_state: true))

main_dialog.WriteDialog
end
end
end

context "when the configuration is not written" do
Expand All @@ -53,7 +112,7 @@ def initialize
let(:change_settings) { :yes }
let(:dns_configuration_written) { false }

it "aks for changing the current settings" do
it "asks for changing the current settings" do
expect(Yast2::Popup).to receive(:show)
.with(instance_of(String), hash_including(buttons: :yes_no))

Expand Down

0 comments on commit 82e4ea8

Please sign in to comment.