Jan Pokorny 89a230
From 2d26f69abc2d793e753c0cddb3086264ca2b4e71 Mon Sep 17 00:00:00 2001
Jan Pokorny 89a230
From: Vojtech Trefny <vtrefny@redhat.com>
Jan Pokorny 89a230
Date: Mon, 6 Mar 2023 10:51:42 +0100
Jan Pokorny 89a230
Subject: [PATCH 1/5] Allow changing iSCSI initiator name after setting it
Jan Pokorny 89a230
Jan Pokorny 89a230
Resolves: rhbz#2221935
Jan Pokorny 89a230
---
Jan Pokorny 89a230
 blivet/iscsi.py | 13 +++++++++++--
Jan Pokorny 89a230
 1 file changed, 11 insertions(+), 2 deletions(-)
Jan Pokorny 89a230
Jan Pokorny 89a230
diff --git a/blivet/iscsi.py b/blivet/iscsi.py
Jan Pokorny 89a230
index 86451db3..0d063f2a 100644
Jan Pokorny 89a230
--- a/blivet/iscsi.py
Jan Pokorny 89a230
+++ b/blivet/iscsi.py
Jan Pokorny 89a230
@@ -212,14 +212,23 @@ class iSCSI(object):
Jan Pokorny 89a230
     @initiator.setter
Jan Pokorny 89a230
     @storaged_iscsi_required(critical=True, eval_mode=util.EvalMode.onetime)
Jan Pokorny 89a230
     def initiator(self, val):
Jan Pokorny 89a230
-        if self.initiator_set and val != self._initiator:
Jan Pokorny 89a230
-            raise ValueError(_("Unable to change iSCSI initiator name once set"))
Jan Pokorny 89a230
         if len(val) == 0:
Jan Pokorny 89a230
             raise ValueError(_("Must provide an iSCSI initiator name"))
Jan Pokorny 89a230
 
Jan Pokorny 89a230
+        active = self._get_active_sessions()
Jan Pokorny 89a230
+        if active:
Jan Pokorny 89a230
+            raise errors.ISCSIError(_("Cannot change initiator name with an active session"))
Jan Pokorny 89a230
+
Jan Pokorny 89a230
         log.info("Setting up iSCSI initiator name %s", self.initiator)
Jan Pokorny 89a230
         args = GLib.Variant("(sa{sv})", (val, None))
Jan Pokorny 89a230
         self._call_initiator_method("SetInitiatorName", args)
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+        if self.initiator_set and val != self._initiator:
Jan Pokorny 89a230
+            log.info("Restarting iscsid after initiator name change")
Jan Pokorny 89a230
+            rc = util.run_program(["systemctl", "restart", "iscsid"])
Jan Pokorny 89a230
+            if rc != 0:
Jan Pokorny 89a230
+                raise errors.ISCSIError(_("Failed to restart iscsid after initiator name change"))
Jan Pokorny 89a230
+
Jan Pokorny 89a230
         self._initiator = val
Jan Pokorny 89a230
 
Jan Pokorny 89a230
     def active_nodes(self, target=None):
Jan Pokorny 89a230
-- 
Jan Pokorny 89a230
2.40.1
Jan Pokorny 89a230
Jan Pokorny 89a230
Jan Pokorny 89a230
From 7c226ed0e14efcdd6e562e357d8f3465ad43ef33 Mon Sep 17 00:00:00 2001
Jan Pokorny 89a230
From: Vojtech Trefny <vtrefny@redhat.com>
Jan Pokorny 89a230
Date: Mon, 6 Mar 2023 15:10:28 +0100
Jan Pokorny 89a230
Subject: [PATCH 2/5] Add a basic test case for the iscsi module
Jan Pokorny 89a230
Jan Pokorny 89a230
Related: rhbz#2221935
Jan Pokorny 89a230
---
Jan Pokorny 89a230
 misc/install-test-dependencies.yml |   3 +
Jan Pokorny 89a230
 tests/storage_tests/__init__.py    |   2 +
Jan Pokorny 89a230
 tests/storage_tests/iscsi_test.py  | 157 +++++++++++++++++++++++++++++
Jan Pokorny 89a230
 3 files changed, 162 insertions(+)
Jan Pokorny 89a230
 create mode 100644 tests/storage_tests/iscsi_test.py
Jan Pokorny 89a230
Jan Pokorny 89a230
diff --git a/tests/storage_tests/__init__.py b/tests/storage_tests/__init__.py
Jan Pokorny 89a230
index 3b2a6cc4..e69fcc34 100644
Jan Pokorny 89a230
--- a/tests/storage_tests/__init__.py
Jan Pokorny 89a230
+++ b/tests/storage_tests/__init__.py
Jan Pokorny 89a230
@@ -3,3 +3,5 @@ from .formats_test import *
Jan Pokorny 89a230
 
Jan Pokorny 89a230
 from .partitioning_test import *
Jan Pokorny 89a230
 from .unsupported_disklabel_test import *
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+from .iscsi_test import *
Jan Pokorny 89a230
diff --git a/tests/storage_tests/iscsi_test.py b/tests/storage_tests/iscsi_test.py
Jan Pokorny 89a230
new file mode 100644
Jan Pokorny 89a230
index 00000000..00cc7c36
Jan Pokorny 89a230
--- /dev/null
Jan Pokorny 89a230
+++ b/tests/storage_tests/iscsi_test.py
Jan Pokorny 89a230
@@ -0,0 +1,157 @@
Jan Pokorny 89a230
+import glob
Jan Pokorny 89a230
+import os
Jan Pokorny 89a230
+import re
Jan Pokorny 89a230
+import shutil
Jan Pokorny 89a230
+import subprocess
Jan Pokorny 89a230
+import unittest
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+from contextlib import contextmanager
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+from .storagetestcase import create_sparse_tempfile
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+def read_file(filename, mode="r"):
Jan Pokorny 89a230
+    with open(filename, mode) as f:
Jan Pokorny 89a230
+        content = f.read()
Jan Pokorny 89a230
+    return content
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+@contextmanager
Jan Pokorny 89a230
+def udev_settle():
Jan Pokorny 89a230
+    try:
Jan Pokorny 89a230
+        yield
Jan Pokorny 89a230
+    finally:
Jan Pokorny 89a230
+        os.system("udevadm settle")
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+def _delete_backstore(name):
Jan Pokorny 89a230
+    status = subprocess.call(["targetcli", "/backstores/fileio/ delete %s" % name],
Jan Pokorny 89a230
+                             stdout=subprocess.DEVNULL)
Jan Pokorny 89a230
+    if status != 0:
Jan Pokorny 89a230
+        raise RuntimeError("Failed to delete the '%s' fileio backstore" % name)
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+def delete_iscsi_target(iqn, backstore=None):
Jan Pokorny 89a230
+    status = subprocess.call(["targetcli", "/iscsi delete %s" % iqn],
Jan Pokorny 89a230
+                             stdout=subprocess.DEVNULL)
Jan Pokorny 89a230
+    if status != 0:
Jan Pokorny 89a230
+        raise RuntimeError("Failed to delete the '%s' iscsi device" % iqn)
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+    if backstore is not None:
Jan Pokorny 89a230
+        _delete_backstore(backstore)
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+def create_iscsi_target(fpath, initiator_name=None):
Jan Pokorny 89a230
+    """
Jan Pokorny 89a230
+    Creates a new iSCSI target (using targetcli) on top of the
Jan Pokorny 89a230
+    :param:`fpath` backing file.
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+    :param str fpath: path of the backing file
Jan Pokorny 89a230
+    :returns: iSCSI IQN, backstore name
Jan Pokorny 89a230
+    :rtype: tuple of str
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+    """
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+    # "register" the backing file as a fileio backstore
Jan Pokorny 89a230
+    store_name = os.path.basename(fpath)
Jan Pokorny 89a230
+    status = subprocess.call(["targetcli", "/backstores/fileio/ create %s %s" % (store_name, fpath)], stdout=subprocess.DEVNULL)
Jan Pokorny 89a230
+    if status != 0:
Jan Pokorny 89a230
+        raise RuntimeError("Failed to register '%s' as a fileio backstore" % fpath)
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+    out = subprocess.check_output(["targetcli", "/backstores/fileio/%s info" % store_name])
Jan Pokorny 89a230
+    out = out.decode("utf-8")
Jan Pokorny 89a230
+    store_wwn = None
Jan Pokorny 89a230
+    for line in out.splitlines():
Jan Pokorny 89a230
+        if line.startswith("wwn: "):
Jan Pokorny 89a230
+            store_wwn = line[5:]
Jan Pokorny 89a230
+    if store_wwn is None:
Jan Pokorny 89a230
+        raise RuntimeError("Failed to determine '%s' backstore's wwn" % store_name)
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+    # create a new iscsi device
Jan Pokorny 89a230
+    out = subprocess.check_output(["targetcli", "/iscsi create"])
Jan Pokorny 89a230
+    out = out.decode("utf-8")
Jan Pokorny 89a230
+    match = re.match(r'Created target (.*).', out)
Jan Pokorny 89a230
+    if match:
Jan Pokorny 89a230
+        iqn = match.groups()[0]
Jan Pokorny 89a230
+    else:
Jan Pokorny 89a230
+        _delete_backstore(store_name)
Jan Pokorny 89a230
+        raise RuntimeError("Failed to create a new iscsi target")
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+    if initiator_name:
Jan Pokorny 89a230
+        status = subprocess.call(["targetcli", "/iscsi/%s/tpg1/acls create %s" % (iqn, initiator_name)], stdout=subprocess.DEVNULL)
Jan Pokorny 89a230
+        if status != 0:
Jan Pokorny 89a230
+            delete_iscsi_target(iqn, store_name)
Jan Pokorny 89a230
+            raise RuntimeError("Failed to set ACLs for '%s'" % iqn)
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+    with udev_settle():
Jan Pokorny 89a230
+        status = subprocess.call(["targetcli", "/iscsi/%s/tpg1/luns create /backstores/fileio/%s" % (iqn, store_name)], stdout=subprocess.DEVNULL)
Jan Pokorny 89a230
+    if status != 0:
Jan Pokorny 89a230
+        delete_iscsi_target(iqn, store_name)
Jan Pokorny 89a230
+        raise RuntimeError("Failed to create a new LUN for '%s' using '%s'" % (iqn, store_name))
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+    status = subprocess.call(["targetcli", "/iscsi/%s/tpg1 set attribute generate_node_acls=1" % iqn], stdout=subprocess.DEVNULL)
Jan Pokorny 89a230
+    if status != 0:
Jan Pokorny 89a230
+        raise RuntimeError("Failed to set ACLs for '%s'" % iqn)
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+    return iqn, store_name
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+@unittest.skipUnless(os.geteuid() == 0, "requires root privileges")
Jan Pokorny 89a230
+@unittest.skipUnless(os.environ.get("JENKINS_HOME"), "jenkins only test")
Jan Pokorny 89a230
+@unittest.skipUnless(shutil.which("iscsiadm"), "iscsiadm not available")
Jan Pokorny 89a230
+class ISCSITestCase(unittest.TestCase):
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+    _disk_size = 512 * 1024**2
Jan Pokorny 89a230
+    initiator = 'iqn.1994-05.com.redhat:iscsi-test'
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+    def setUp(self):
Jan Pokorny 89a230
+        self.addCleanup(self._clean_up)
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+        self._dev_file = None
Jan Pokorny 89a230
+        self.dev = None
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+        self._dev_file = create_sparse_tempfile("blivet_test", self._disk_size)
Jan Pokorny 89a230
+        try:
Jan Pokorny 89a230
+            self.dev, self.backstore = create_iscsi_target(self._dev_file, self.initiator)
Jan Pokorny 89a230
+        except RuntimeError as e:
Jan Pokorny 89a230
+            raise RuntimeError("Failed to setup targetcli device for testing: %s" % e)
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+    def _force_logout(self):
Jan Pokorny 89a230
+        subprocess.call(["iscsiadm", "--mode", "node", "--logout", "--name", self.dev], stdout=subprocess.DEVNULL)
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+    def _clean_up(self):
Jan Pokorny 89a230
+        self._force_logout()
Jan Pokorny 89a230
+        delete_iscsi_target(self.dev, self.backstore)
Jan Pokorny 89a230
+        os.unlink(self._dev_file)
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+    def test_discover_login(self):
Jan Pokorny 89a230
+        from blivet.iscsi import iscsi, has_iscsi
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+        if not has_iscsi():
Jan Pokorny 89a230
+            self.skipTest("iSCSI not available, skipping")
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+        iscsi.initiator = self.initiator
Jan Pokorny 89a230
+        nodes = iscsi.discover("127.0.0.1")
Jan Pokorny 89a230
+        self.assertTrue(nodes)
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+        if len(nodes) > 1:
Jan Pokorny 89a230
+            self.skipTest("Discovered more than one iSCSI target on localhost, skipping")
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+        self.assertEqual(nodes[0].address, "127.0.0.1")
Jan Pokorny 89a230
+        self.assertEqual(nodes[0].port, 3260)
Jan Pokorny 89a230
+        self.assertEqual(nodes[0].name, self.dev)
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+        # change the initiator name
Jan Pokorny 89a230
+        iscsi.initiator = self.initiator + "_1"
Jan Pokorny 89a230
+        self.assertEqual(iscsi.initiator, self.initiator + "_1")
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+        # try to login
Jan Pokorny 89a230
+        ret, err = iscsi.log_into_node(nodes[0])
Jan Pokorny 89a230
+        self.assertTrue(ret, "Login failed: %s" % err)
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+        # check the session for initiator name
Jan Pokorny 89a230
+        sessions = glob.glob("/sys/class/iscsi_session/*/")
Jan Pokorny 89a230
+        self.assertTrue(sessions)
Jan Pokorny 89a230
+        self.assertEqual(len(sessions), 1)
Jan Pokorny 89a230
+        initiator = read_file(sessions[0] + "initiatorname").strip()
Jan Pokorny 89a230
+        self.assertEqual(initiator, iscsi.initiator)
Jan Pokorny 89a230
-- 
Jan Pokorny 89a230
2.40.1
Jan Pokorny 89a230
Jan Pokorny 89a230
Jan Pokorny 89a230
From dfd0c59a901f54ecfd8f538a2bb004a2e5ab6eec Mon Sep 17 00:00:00 2001
Jan Pokorny 89a230
From: Vojtech Trefny <vtrefny@redhat.com>
Jan Pokorny 89a230
Date: Mon, 6 Mar 2023 15:14:40 +0100
Jan Pokorny 89a230
Subject: [PATCH 3/5] tests: Use blivet-specific prefix for targetcli backing
Jan Pokorny 89a230
 files
Jan Pokorny 89a230
Jan Pokorny 89a230
The code is originally from libblockdev hence the "bd" prefix, we
Jan Pokorny 89a230
should use a different prefix for blivet to be able to identify
Jan Pokorny 89a230
which test suite failed to clean the files.
Jan Pokorny 89a230
Jan Pokorny 89a230
Related: rhbz#2221935
Jan Pokorny 89a230
---
Jan Pokorny 89a230
 tests/storage_tests/storagetestcase.py | 2 +-
Jan Pokorny 89a230
 1 file changed, 1 insertion(+), 1 deletion(-)
Jan Pokorny 89a230
Jan Pokorny 89a230
diff --git a/tests/storage_tests/storagetestcase.py b/tests/storage_tests/storagetestcase.py
Jan Pokorny 89a230
index 35d57ce9..9f859977 100644
Jan Pokorny 89a230
--- a/tests/storage_tests/storagetestcase.py
Jan Pokorny 89a230
+++ b/tests/storage_tests/storagetestcase.py
Jan Pokorny 89a230
@@ -39,7 +39,7 @@ def create_sparse_tempfile(name, size):
Jan Pokorny 89a230
         :param size: the file size (in bytes)
Jan Pokorny 89a230
         :returns: the path to the newly created file
Jan Pokorny 89a230
     """
Jan Pokorny 89a230
-    (fd, path) = tempfile.mkstemp(prefix="bd.", suffix="-%s" % name)
Jan Pokorny 89a230
+    (fd, path) = tempfile.mkstemp(prefix="blivet.", suffix="-%s" % name)
Jan Pokorny 89a230
     os.close(fd)
Jan Pokorny 89a230
     create_sparse_file(path, size)
Jan Pokorny 89a230
     return path
Jan Pokorny 89a230
-- 
Jan Pokorny 89a230
2.40.1
Jan Pokorny 89a230
Jan Pokorny 89a230
Jan Pokorny 89a230
From 492fc9b8dfc2d4aa7cb44baa4408570babcb5e96 Mon Sep 17 00:00:00 2001
Jan Pokorny 89a230
From: Vojtech Trefny <vtrefny@redhat.com>
Jan Pokorny 89a230
Date: Wed, 19 Jul 2023 13:57:39 +0200
Jan Pokorny 89a230
Subject: [PATCH 4/5] iscsi: Save firmware initiator name to
Jan Pokorny 89a230
 /etc/iscsi/initiatorname.iscsi
Jan Pokorny 89a230
Jan Pokorny 89a230
Resolves: rhbz#2221932
Jan Pokorny 89a230
---
Jan Pokorny 89a230
 blivet/iscsi.py | 5 +++++
Jan Pokorny 89a230
 1 file changed, 5 insertions(+)
Jan Pokorny 89a230
Jan Pokorny 89a230
diff --git a/blivet/iscsi.py b/blivet/iscsi.py
Jan Pokorny 89a230
index 0d063f2a..8080a671 100644
Jan Pokorny 89a230
--- a/blivet/iscsi.py
Jan Pokorny 89a230
+++ b/blivet/iscsi.py
Jan Pokorny 89a230
@@ -160,6 +160,11 @@ class iSCSI(object):
Jan Pokorny 89a230
                 self._initiator = initiatorname
Jan Pokorny 89a230
             except Exception as e:  # pylint: disable=broad-except
Jan Pokorny 89a230
                 log.info("failed to get initiator name from iscsi firmware: %s", str(e))
Jan Pokorny 89a230
+            else:
Jan Pokorny 89a230
+                # write the firmware initiator to /etc/iscsi/initiatorname.iscsi
Jan Pokorny 89a230
+                log.info("Setting up firmware iSCSI initiator name %s", self.initiator)
Jan Pokorny 89a230
+                args = GLib.Variant("(sa{sv})", (initiatorname, None))
Jan Pokorny 89a230
+                self._call_initiator_method("SetInitiatorName", args)
Jan Pokorny 89a230
 
Jan Pokorny 89a230
     # So that users can write iscsi() to get the singleton instance
Jan Pokorny 89a230
     def __call__(self):
Jan Pokorny 89a230
-- 
Jan Pokorny 89a230
2.40.1
Jan Pokorny 89a230
Jan Pokorny 89a230
Jan Pokorny 89a230
From 768d90815b7f95d0d6d278397fd6fd12a0490b5d Mon Sep 17 00:00:00 2001
Jan Pokorny 89a230
From: Vojtech Trefny <vtrefny@redhat.com>
Jan Pokorny 89a230
Date: Wed, 19 Jul 2023 10:38:45 +0200
Jan Pokorny 89a230
Subject: [PATCH 5/5] tests: Improve iscsi_test.ISCSITestCase
Jan Pokorny 89a230
Jan Pokorny 89a230
Changed how we create the initiator name ACLs based on RTT test
Jan Pokorny 89a230
case for rhbz#2084043 and also improved the test case itself.
Jan Pokorny 89a230
Jan Pokorny 89a230
Related: rhbz#2221935
Jan Pokorny 89a230
---
Jan Pokorny 89a230
 tests/storage_tests/iscsi_test.py | 36 +++++++++++++++++++++----------
Jan Pokorny 89a230
 1 file changed, 25 insertions(+), 11 deletions(-)
Jan Pokorny 89a230
Jan Pokorny 89a230
diff --git a/tests/storage_tests/iscsi_test.py b/tests/storage_tests/iscsi_test.py
Jan Pokorny 89a230
index 00cc7c36..6cc83a59 100644
Jan Pokorny 89a230
--- a/tests/storage_tests/iscsi_test.py
Jan Pokorny 89a230
+++ b/tests/storage_tests/iscsi_test.py
Jan Pokorny 89a230
@@ -77,21 +77,17 @@ def create_iscsi_target(fpath, initiator_name=None):
Jan Pokorny 89a230
         _delete_backstore(store_name)
Jan Pokorny 89a230
         raise RuntimeError("Failed to create a new iscsi target")
Jan Pokorny 89a230
 
Jan Pokorny 89a230
-    if initiator_name:
Jan Pokorny 89a230
-        status = subprocess.call(["targetcli", "/iscsi/%s/tpg1/acls create %s" % (iqn, initiator_name)], stdout=subprocess.DEVNULL)
Jan Pokorny 89a230
-        if status != 0:
Jan Pokorny 89a230
-            delete_iscsi_target(iqn, store_name)
Jan Pokorny 89a230
-            raise RuntimeError("Failed to set ACLs for '%s'" % iqn)
Jan Pokorny 89a230
-
Jan Pokorny 89a230
     with udev_settle():
Jan Pokorny 89a230
         status = subprocess.call(["targetcli", "/iscsi/%s/tpg1/luns create /backstores/fileio/%s" % (iqn, store_name)], stdout=subprocess.DEVNULL)
Jan Pokorny 89a230
     if status != 0:
Jan Pokorny 89a230
         delete_iscsi_target(iqn, store_name)
Jan Pokorny 89a230
         raise RuntimeError("Failed to create a new LUN for '%s' using '%s'" % (iqn, store_name))
Jan Pokorny 89a230
 
Jan Pokorny 89a230
-    status = subprocess.call(["targetcli", "/iscsi/%s/tpg1 set attribute generate_node_acls=1" % iqn], stdout=subprocess.DEVNULL)
Jan Pokorny 89a230
-    if status != 0:
Jan Pokorny 89a230
-        raise RuntimeError("Failed to set ACLs for '%s'" % iqn)
Jan Pokorny 89a230
+    if initiator_name:
Jan Pokorny 89a230
+        status = subprocess.call(["targetcli", "/iscsi/%s/tpg1/acls create %s" % (iqn, initiator_name)], stdout=subprocess.DEVNULL)
Jan Pokorny 89a230
+        if status != 0:
Jan Pokorny 89a230
+            delete_iscsi_target(iqn, store_name)
Jan Pokorny 89a230
+            raise RuntimeError("Failed to set ACLs for '%s'" % iqn)
Jan Pokorny 89a230
 
Jan Pokorny 89a230
     return iqn, store_name
Jan Pokorny 89a230
 
Jan Pokorny 89a230
@@ -130,6 +126,7 @@ class ISCSITestCase(unittest.TestCase):
Jan Pokorny 89a230
         if not has_iscsi():
Jan Pokorny 89a230
             self.skipTest("iSCSI not available, skipping")
Jan Pokorny 89a230
 
Jan Pokorny 89a230
+        # initially set the initiator to the correct/allowed one
Jan Pokorny 89a230
         iscsi.initiator = self.initiator
Jan Pokorny 89a230
         nodes = iscsi.discover("127.0.0.1")
Jan Pokorny 89a230
         self.assertTrue(nodes)
Jan Pokorny 89a230
@@ -141,11 +138,28 @@ class ISCSITestCase(unittest.TestCase):
Jan Pokorny 89a230
         self.assertEqual(nodes[0].port, 3260)
Jan Pokorny 89a230
         self.assertEqual(nodes[0].name, self.dev)
Jan Pokorny 89a230
 
Jan Pokorny 89a230
-        # change the initiator name
Jan Pokorny 89a230
+        # change the initiator name to a wrong one
Jan Pokorny 89a230
         iscsi.initiator = self.initiator + "_1"
Jan Pokorny 89a230
         self.assertEqual(iscsi.initiator, self.initiator + "_1")
Jan Pokorny 89a230
 
Jan Pokorny 89a230
-        # try to login
Jan Pokorny 89a230
+        # check the change made it to /etc/iscsi/initiatorname.iscsi
Jan Pokorny 89a230
+        initiator_file = read_file("/etc/iscsi/initiatorname.iscsi").strip()
Jan Pokorny 89a230
+        self.assertEqual(initiator_file, "InitiatorName=%s" % self.initiator + "_1")
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+        # try to login (should fail)
Jan Pokorny 89a230
+        ret, err = iscsi.log_into_node(nodes[0])
Jan Pokorny 89a230
+        self.assertFalse(ret)
Jan Pokorny 89a230
+        self.assertIn("authorization failure", err)
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+        # change the initiator name back to the correct one
Jan Pokorny 89a230
+        iscsi.initiator = self.initiator
Jan Pokorny 89a230
+        self.assertEqual(iscsi.initiator, self.initiator)
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+        # check the change made it to /etc/iscsi/initiatorname.iscsi
Jan Pokorny 89a230
+        initiator_file = read_file("/etc/iscsi/initiatorname.iscsi").strip()
Jan Pokorny 89a230
+        self.assertEqual(initiator_file, "InitiatorName=%s" % self.initiator)
Jan Pokorny 89a230
+
Jan Pokorny 89a230
+        # try to login (should work now)
Jan Pokorny 89a230
         ret, err = iscsi.log_into_node(nodes[0])
Jan Pokorny 89a230
         self.assertTrue(ret, "Login failed: %s" % err)
Jan Pokorny 89a230
 
Jan Pokorny 89a230
-- 
Jan Pokorny 89a230
2.40.1
Jan Pokorny 89a230