Skip to content

Commit

Permalink
Manage the service status and start mode directly from Ruby code
Browse files Browse the repository at this point in the history
Using the `keep_state` option of Yast2::SystemService#save method. See
commit 7acbe06.
  • Loading branch information
dgdavid committed Aug 13, 2018
1 parent 9007e59 commit 9c4ca86
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 31 deletions.
3 changes: 2 additions & 1 deletion src/include/dhcp-server/dialogs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def initialize_dhcp_server_dialogs(include_target)
Yast.import "Popup"
Yast.import "Label"
Yast.import "Confirm"
Yast.import "Mode"

@functions = { :abort => fun_ref(method(:confirmAbort), "boolean ()") }
end
Expand Down Expand Up @@ -88,7 +89,7 @@ def SaveAndRestart(event)
#
# @return [Boolean] true if settings are saved successfully; false otherwise
def write_settings
DhcpServer.Write && dhcp_service.save
DhcpServer.Write && dhcp_service.save(keep_state: Mode.auto)
end

# Shows a popup asking to user if wants to change settings
Expand Down
30 changes: 0 additions & 30 deletions src/modules/DhcpServer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1746,36 +1746,6 @@ sub Write {
SCR->Write (".sysconfig.dhcpd.DHCPD_OTHER_ARGS", $other_options);
SCR->Write (".sysconfig.dhcpd", undef);

# TODO: Manage the service directly through Yast2::SystemService instead
if (Mode->auto() || Mode->config())
{
if ($start_service)
{
y2milestone ("Enabling the DHCP service");
my $ret = 1;
if (! $write_only)
{
$ret = Service->Restart ($SERVICE);
}
Service->Enable ($SERVICE);
if (!$ret)
{
# error report
Report->Error (__("Error occurred while restarting the DHCP daemon."));
$ok = 0;
}
}
else
{
y2milestone ("Disabling the DHCP service");
if (! $write_only)
{
Service->Stop ($SERVICE);
}
Service->Disable ($SERVICE);
}
}

Progress->NextStage ();

if ($dns_server_available) {
Expand Down
125 changes: 125 additions & 0 deletions test/dialog_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/usr/bin/env rspec
# encoding: utf-8
# Copyright (c) [2018] 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 "yast2/system_service"
require "cwm/service_widget"

Yast.import "DhcpServer"

describe "DhcpServerDialogsInclude" do
class TestDialog
include Yast::I18n
include Yast::UIShortcuts

def initialize
@HELPS = {}
Yast.include self, "dhcp-server/widgets.rb"
Yast.include self, "dhcp-server/dialogs.rb"
end
end

let(:widget) { instance_double(::CWM::ServiceWidget, cwm_definition: {}) }
let(:service) { instance_double(Yast2::SystemService, save: true, currently_active?: true) }

before do
allow_any_instance_of(TestDialog).to receive(:fun_ref)
allow(::CWM::ServiceWidget).to receive(:new).and_return(widget)
allow(Yast2::SystemService).to receive(:find).with("dhcpd").and_return(service)
end

describe "#WriteDialog" do
subject(:dialog) { TestDialog.new }

let(:dhcp_configuration_written) { true }

before do
allow(Yast::DhcpServer).to receive(:Write).and_return(dhcp_configuration_written)
end

it "writes needed configuration" do
expect(Yast::DhcpServer).to receive(:Write)

dialog.WriteDialog
end

context "when configuration is written" do
it "saves the system service" do
expect(service).to receive(:save)

dialog.WriteDialog
end

it "returns :next" do
expect(dialog.WriteDialog).to eq(:next)
end

context "and is in `auto` Mode" do
before do
allow(Yast::Mode).to receive(:auto).and_return(true)
end

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

dialog.WriteDialog
end
end
end

context "when the configuration is not written" do
before do
allow(Yast2::Popup).to receive(:show).and_return(change_settings)
end

let(:change_settings) { :yes }
let(:dhcp_configuration_written) { false }

it "does not save the system service" do
expect(service).to_not receive(:save)

dialog.WriteDialog
end

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

dialog.WriteDialog
end

context "and user decides to change the current setting" do
it "returns :back" do
expect(dialog.WriteDialog).to eq(:back)
end
end

context "and user decides to cancel" do
let(:change_settings) { :no }

it "returns :abort" do
expect(subject.WriteDialog).to eq(:abort)
end
end
end
end
end

0 comments on commit 9c4ca86

Please sign in to comment.