Skip to content

Commit

Permalink
[Service] Add missing feaures in iSCSI D-Bus API
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Feb 20, 2023
1 parent ee368a0 commit d453966
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 97 deletions.
12 changes: 7 additions & 5 deletions doc/dbus_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,13 @@ Logout(out u result)
##### Properties

~~~
Target readable s
Address readable s
Port readable u
Interface readable s
Startup readable s
Target readable s
Address readable s
Port readable u
Interface readable s
IBFT readable b
Connected readable b
Startup readable,writable s
~~~

##### Details
Expand Down
59 changes: 44 additions & 15 deletions service/lib/dinstaller/dbus/storage/iscsi_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,6 @@ def initialize(iscsi_manager, iscsi_node, path, logger: nil)
@iscsi_node = iscsi_node
end

ISCSI_NODE_INTERFACE = "org.opensuse.DInstaller.Storage1.ISCSI.Node"
private_constant :ISCSI_NODE_INTERFACE

dbus_interface ISCSI_NODE_INTERFACE do
dbus_reader(:target, "s")
dbus_reader(:address, "s")
dbus_reader(:port, "u")
dbus_reader(:interface, "s")
dbus_reader(:connected, "b")
dbus_reader(:startup, "s")
dbus_method(:Login, "in options:a{sv}, out result:u") { |o| login(o) }
dbus_method(:Logout, "out result:u") { logout }
end

# Name of the iSCSI target
#
# @return [String]
Expand Down Expand Up @@ -91,6 +77,13 @@ def interface
iscsi_node.interface || ""
end

# Whether the iSCSI node was initiated by iBTF
#
# @return [Boolean]
def ibft
iscsi_node.ibft?
end

# Whether the node is connected
#
# @return [Boolean]
Expand All @@ -105,6 +98,17 @@ def startup
iscsi_node.startup || ""
end

# Sets a new value for the startup status
#
# @throw [::DBus::Error] Exception if the given value is not valid.
#
# @param value [String]
def startup=(value)
raise ::DBus::Error, "Invalid startup value: #{value}" unless valid_startup?(value)

iscsi_manager.update(iscsi_node, startup: value)
end

# Sets the associated iSCSI node
#
# @note A properties changed signal is always emitted.
Expand Down Expand Up @@ -132,7 +136,7 @@ def login(options = {})
auth = iscsi_auth(options)
startup = options["Startup"]

if startup && !DInstaller::Storage::ISCSI::Manager::STARTUP_OPTIONS.include?(startup)
if startup && !valid_startup?(startup)
logger.info("iSCSI login error: startup value #{startup} is not valid")
return 1
end
Expand All @@ -151,6 +155,31 @@ def logout
success = iscsi_manager.logout(iscsi_node)
success ? 0 : 1
end

ISCSI_NODE_INTERFACE = "org.opensuse.DInstaller.Storage1.ISCSI.Node"
private_constant :ISCSI_NODE_INTERFACE

dbus_interface ISCSI_NODE_INTERFACE do
dbus_reader(:target, "s")
dbus_reader(:address, "s")
dbus_reader(:port, "u")
dbus_reader(:interface, "s")
dbus_reader(:ibft, "b", dbus_name: "IBTF")
dbus_reader(:connected, "b")
dbus_accessor(:startup, "s")
dbus_method(:Login, "in options:a{sv}, out result:u") { |o| login(o) }
dbus_method(:Logout, "out result:u") { logout }
end

private

# Whether the given value is a valid startup status
#
# @param value [String]
# @return [Boolean]
def valid_startup?(value)
DInstaller::Storage::ISCSI::Manager::STARTUP_OPTIONS.include?(value)
end
end
end
end
Expand Down
24 changes: 16 additions & 8 deletions service/lib/dinstaller/storage/iscsi/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def discover_send_targets(host, port, authentication)

# Creates a new iSCSI session
#
# @note iSCSI nodes are probed again if needed, see {#probe_after}.
# @note iSCSI nodes are probed again, see {#probe_after}.
#
# @param node [Node]
# @param authentication [Y2IscsiClient::Authentication]
Expand All @@ -118,13 +118,6 @@ def discover_send_targets(host, port, authentication)
def login(node, authentication, startup: nil)
startup ||= Yast::IscsiClientLib.default_startup_status

if !STARTUP_OPTIONS.include?(startup)
logger.info(
"Cannot create iSCSI session because startup status is not valid: #{startup}"
)
return false
end

ensure_activated

probe_after do
Expand Down Expand Up @@ -163,6 +156,21 @@ def delete(node)
end
end

# Updates an iSCSI node
#
# @note iSCSI nodes are probed again, see {#probe_after}.
#
# @param node [Node]
# @param startup [String] New startup mode value
#
# @return [Boolean] Whether the action successes
def update(node, startup:)
probe_after do
Yast::IscsiClientLib.currentRecord = record_from(node)
Yast::IscsiClientLib.setStartupStatus(startup)
end
end

# Registers a callback to be called when the nodes are probed
#
# @param block [Proc]
Expand Down
22 changes: 22 additions & 0 deletions service/test/dinstaller/dbus/storage/iscsi_node_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@
allow(subject).to receive(:dbus_properties_changed)
end

describe "#startup=" do
context "when the given startup status is not valid" do
let(:startup) { "invalid" }

it "raises a D-Bus error" do
expect(iscsi_manager).to_not receive(:update)

expect { subject.startup = startup }.to raise_error(::DBus::Error, /Invalid startup/)
end
end

context "when the given startup status is valid" do
let(:startup) { "automatic" }

it "updates the iSCSI node" do
expect(iscsi_manager).to receive(:update).with(iscsi_node, startup: startup)

subject.startup = startup
end
end
end

describe "#iscsi_node=" do
it "sets the iSCSI node value" do
node = DInstaller::Storage::ISCSI::Node.new
Expand Down
128 changes: 59 additions & 69 deletions service/test/dinstaller/storage/iscsi/manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,105 +136,75 @@
allow(Yast::IscsiClientLib).to receive(:setStartupStatus)
end

context "if the given startup status is not valid" do
let(:startup) { "invalid" }
let(:startup) { "automatic" }

it "does not try to login" do
expect(Yast::IscsiClientLib).to_not receive(:login_into_current)
before do
allow(Yast::IscsiClientLib).to receive(:login_into_current).and_return(login_success)
allow(Yast::IscsiClientLib).to receive(:setStartupStatus).and_return(startup_success)
end

subject.login(node, auth, startup: startup)
end
let(:login_success) { nil }

it "does not activate iSCSI" do
expect(subject).to_not receive(:activate)
let(:startup_success) { nil }

subject.login(node, auth, startup: startup)
end
it "tries to login" do
expect(Yast::IscsiClientLib).to receive(:login_into_current)

it "does not probe iSCSI" do
expect(subject).to_not receive(:probe)

subject.login(node, auth, startup: startup)
end
subject.login(node, auth, startup: startup)
end

it "returns false" do
result = subject.login(node, auth, startup: startup)
context "if iSCSI activation is not performed yet" do
it "activates iSCSI" do
expect(subject).to receive(:activate)

expect(result).to eq(false)
subject.login(node, auth, startup: startup)
end
end

context "if the given startup status is valid" do
let(:startup) { "automatic" }

context "if iSCSI activation was already performed" do
before do
allow(Yast::IscsiClientLib).to receive(:login_into_current).and_return(login_success)
allow(Yast::IscsiClientLib).to receive(:setStartupStatus).and_return(startup_success)
subject.activate
end

let(:login_success) { nil }

let(:startup_success) { nil }

it "tries to login" do
expect(Yast::IscsiClientLib).to receive(:login_into_current)
it "does not activate iSCSI again" do
expect(subject).to_not receive(:activate)

subject.login(node, auth, startup: startup)
end
end

context "if iSCSI activation is not performed yet" do
it "activates iSCSI" do
expect(subject).to receive(:activate)
context "and the session is created" do
let(:login_success) { true }

subject.login(node, auth, startup: startup)
end
end
context "and the startup status is correctly set" do
let(:startup_success) { true }

context "if iSCSI activation was already performed" do
before do
subject.activate
end

it "does not activate iSCSI again" do
expect(subject).to_not receive(:activate)
it "probes iSCSI" do
expect(subject).to receive(:probe)

subject.login(node, auth, startup: startup)
end
end

context "and the session is created" do
let(:login_success) { true }

context "and the startup status is correctly set" do
let(:startup_success) { true }

it "probes iSCSI" do
expect(subject).to receive(:probe)

subject.login(node, auth, startup: startup)
end

it "returns true" do
result = subject.login(node, auth, startup: startup)
it "returns true" do
result = subject.login(node, auth, startup: startup)

expect(result).to eq(true)
end
expect(result).to eq(true)
end
end

context "and the startup status cannot be set" do
let(:startup_success) { false }
context "and the startup status cannot be set" do
let(:startup_success) { false }

it "probes iSCSI" do
expect(subject).to receive(:probe)
it "probes iSCSI" do
expect(subject).to receive(:probe)

subject.login(node, auth, startup: startup)
end
subject.login(node, auth, startup: startup)
end

it "returns false" do
result = subject.login(node, auth, startup: startup)
it "returns false" do
result = subject.login(node, auth, startup: startup)

expect(result).to eq(false)
end
expect(result).to eq(false)
end
end
end
Expand Down Expand Up @@ -299,4 +269,24 @@
subject.delete(node)
end
end

describe "#update" do
before do
allow(Yast::IscsiClientLib).to receive(:setStartupStatus)
end

let(:node) { DInstaller::Storage::ISCSI::Node.new }

it "updates the iSCSI node" do
expect(Yast::IscsiClientLib).to receive(:setStartupStatus).with("manual")

subject.update(node, startup: "manual")
end

it "probes iSCSI" do
expect(subject).to receive(:probe)

subject.update(node, startup: "manual")
end
end
end

0 comments on commit d453966

Please sign in to comment.