|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | +"""Unit tests for the libnvme Python bindings. |
| 4 | +
|
| 5 | +These tests cover object creation, property access, and error handling. |
| 6 | +They do not require real NVMe hardware to run. |
| 7 | +""" |
| 8 | +import gc |
| 9 | +import unittest |
| 10 | +from libnvme import nvme |
| 11 | + |
| 12 | + |
| 13 | +class TestConstants(unittest.TestCase): |
| 14 | + """Verify that well-known constants are accessible and have correct values.""" |
| 15 | + |
| 16 | + def test_disc_subsys_name_is_string(self): |
| 17 | + self.assertIsInstance(nvme.NVME_DISC_SUBSYS_NAME, str) |
| 18 | + |
| 19 | + def test_disc_subsys_name_value(self): |
| 20 | + self.assertEqual(nvme.NVME_DISC_SUBSYS_NAME, 'nqn.2014-08.org.nvmexpress.discovery') |
| 21 | + |
| 22 | + def test_log_lid_discovery_is_int(self): |
| 23 | + self.assertIsInstance(nvme.NVME_LOG_LID_DISCOVERY, int) |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +class TestGlobalCtx(unittest.TestCase): |
| 28 | + |
| 29 | + def test_creation_no_args(self): |
| 30 | + ctx = nvme.global_ctx() |
| 31 | + self.assertIsNotNone(ctx) |
| 32 | + |
| 33 | + def test_context_manager(self): |
| 34 | + with nvme.global_ctx() as ctx: |
| 35 | + self.assertIsNotNone(ctx) |
| 36 | + |
| 37 | + def test_hosts_iterator_returns_list(self): |
| 38 | + ctx = nvme.global_ctx() |
| 39 | + hosts = list(ctx.hosts()) |
| 40 | + self.assertIsInstance(hosts, list) |
| 41 | + |
| 42 | + def test_refresh_topology_does_not_raise(self): |
| 43 | + ctx = nvme.global_ctx() |
| 44 | + ctx.refresh_topology() |
| 45 | + |
| 46 | + def test_log_level_all_valid_levels(self): |
| 47 | + ctx = nvme.global_ctx() |
| 48 | + for level in ('debug', 'info', 'notice', 'warning', 'err', 'crit', 'alert', 'emerg'): |
| 49 | + with self.subTest(level=level): |
| 50 | + ctx.log_level(level) |
| 51 | + |
| 52 | + |
| 53 | +class TestHost(unittest.TestCase): |
| 54 | + |
| 55 | + def setUp(self): |
| 56 | + self.ctx = nvme.global_ctx() |
| 57 | + |
| 58 | + def tearDown(self): |
| 59 | + self.ctx = None |
| 60 | + gc.collect() |
| 61 | + |
| 62 | + def test_creation_default(self): |
| 63 | + host = nvme.host(self.ctx) |
| 64 | + self.assertIsNotNone(host) |
| 65 | + |
| 66 | + def test_creation_with_explicit_hostnqn(self): |
| 67 | + hostnqn = 'nqn.2014-08.com.example:test-host-creation' |
| 68 | + host = nvme.host(self.ctx, hostnqn=hostnqn) |
| 69 | + self.assertIsNotNone(host) |
| 70 | + self.assertEqual(host.hostnqn, hostnqn) |
| 71 | + |
| 72 | + def test_creation_with_hostnqn_and_hostid(self): |
| 73 | + hostnqn = 'nqn.2014-08.com.example:test-host-props' |
| 74 | + hostid = '11111111-2222-3333-4444-555555555555' |
| 75 | + host = nvme.host(self.ctx, hostnqn=hostnqn, hostid=hostid) |
| 76 | + self.assertEqual(host.hostnqn, hostnqn) |
| 77 | + self.assertEqual(host.hostid, hostid) |
| 78 | + |
| 79 | + def test_creation_with_hostsymname(self): |
| 80 | + hostnqn = 'nqn.2014-08.com.example:test-host-symname' |
| 81 | + symname = 'my-storage-host' |
| 82 | + host = nvme.host(self.ctx, hostnqn=hostnqn, hostsymname=symname) |
| 83 | + self.assertEqual(host.hostsymname, symname) |
| 84 | + |
| 85 | + def test_set_symname(self): |
| 86 | + hostnqn = 'nqn.2014-08.com.example:test-host-set-symname' |
| 87 | + host = nvme.host(self.ctx, hostnqn=hostnqn) |
| 88 | + host.set_symname('updated-symname') |
| 89 | + self.assertEqual(host.hostsymname, 'updated-symname') |
| 90 | + |
| 91 | + def test_dhchap_host_key_is_none_by_default(self): |
| 92 | + hostnqn = 'nqn.2014-08.com.example:test-host-dhchap' |
| 93 | + host = nvme.host(self.ctx, hostnqn=hostnqn) |
| 94 | + self.assertIsNone(host.dhchap_host_key) |
| 95 | + |
| 96 | + def test_subsystems_iterator_returns_list(self): |
| 97 | + host = nvme.host(self.ctx) |
| 98 | + subsystems = list(host.subsystems()) |
| 99 | + self.assertIsInstance(subsystems, list) |
| 100 | + |
| 101 | + def test_str_contains_class_name(self): |
| 102 | + host = nvme.host(self.ctx) |
| 103 | + self.assertIn('nvme.host', str(host)) |
| 104 | + |
| 105 | + def test_context_manager(self): |
| 106 | + with nvme.host(self.ctx) as h: |
| 107 | + self.assertIsNotNone(h) |
| 108 | + |
| 109 | + |
| 110 | +class TestCtrl(unittest.TestCase): |
| 111 | + |
| 112 | + def setUp(self): |
| 113 | + self.ctx = nvme.global_ctx() |
| 114 | + self.subsysnqn = nvme.NVME_DISC_SUBSYS_NAME |
| 115 | + |
| 116 | + def tearDown(self): |
| 117 | + self.ctx = None |
| 118 | + gc.collect() |
| 119 | + |
| 120 | + def _make_loop_ctrl(self): |
| 121 | + return nvme.ctrl(self.ctx, subsysnqn=self.subsysnqn, transport='loop') |
| 122 | + |
| 123 | + def test_creation_loop_transport(self): |
| 124 | + ctrl = self._make_loop_ctrl() |
| 125 | + self.assertIsNotNone(ctrl) |
| 126 | + |
| 127 | + def test_creation_tcp_transport_with_traddr(self): |
| 128 | + ctrl = nvme.ctrl( |
| 129 | + self.ctx, |
| 130 | + subsysnqn=self.subsysnqn, |
| 131 | + transport='tcp', |
| 132 | + traddr='192.168.1.1', |
| 133 | + trsvcid='4420', |
| 134 | + ) |
| 135 | + self.assertIsNotNone(ctrl) |
| 136 | + |
| 137 | + def test_transport_property(self): |
| 138 | + ctrl = self._make_loop_ctrl() |
| 139 | + self.assertEqual(ctrl.transport, 'loop') |
| 140 | + |
| 141 | + def test_subsysnqn_property(self): |
| 142 | + ctrl = self._make_loop_ctrl() |
| 143 | + self.assertEqual(ctrl.subsysnqn, self.subsysnqn) |
| 144 | + |
| 145 | + def test_traddr_property(self): |
| 146 | + ctrl = nvme.ctrl( |
| 147 | + self.ctx, |
| 148 | + subsysnqn=self.subsysnqn, |
| 149 | + transport='tcp', |
| 150 | + traddr='10.0.0.1', |
| 151 | + ) |
| 152 | + self.assertEqual(ctrl.traddr, '10.0.0.1') |
| 153 | + |
| 154 | + def test_trsvcid_property(self): |
| 155 | + ctrl = nvme.ctrl( |
| 156 | + self.ctx, |
| 157 | + subsysnqn=self.subsysnqn, |
| 158 | + transport='tcp', |
| 159 | + traddr='10.0.0.1', |
| 160 | + trsvcid='8009', |
| 161 | + ) |
| 162 | + self.assertEqual(ctrl.trsvcid, '8009') |
| 163 | + |
| 164 | + def test_connected_returns_false_before_connect(self): |
| 165 | + ctrl = self._make_loop_ctrl() |
| 166 | + self.assertFalse(ctrl.connected()) |
| 167 | + |
| 168 | + def test_name_is_none_before_connect(self): |
| 169 | + ctrl = self._make_loop_ctrl() |
| 170 | + self.assertIsNone(ctrl.name) |
| 171 | + |
| 172 | + def test_str_contains_transport(self): |
| 173 | + ctrl = self._make_loop_ctrl() |
| 174 | + s = str(ctrl) |
| 175 | + self.assertIn('loop', s) |
| 176 | + |
| 177 | + def test_context_manager(self): |
| 178 | + with nvme.ctrl(self.ctx, subsysnqn=self.subsysnqn, transport='loop') as c: |
| 179 | + self.assertIsNotNone(c) |
| 180 | + |
| 181 | + def test_namespaces_iterator_returns_list(self): |
| 182 | + ctrl = self._make_loop_ctrl() |
| 183 | + nss = list(ctrl.namespaces()) |
| 184 | + self.assertIsInstance(nss, list) |
| 185 | + |
| 186 | + def test_discovery_ctrl_flag_default_false(self): |
| 187 | + ctrl = self._make_loop_ctrl() |
| 188 | + self.assertFalse(ctrl.discovery_ctrl) |
| 189 | + |
| 190 | + def test_discovery_ctrl_flag_set_and_clear(self): |
| 191 | + ctrl = self._make_loop_ctrl() |
| 192 | + ctrl.discovery_ctrl = True |
| 193 | + self.assertTrue(ctrl.discovery_ctrl) |
| 194 | + ctrl.discovery_ctrl = False |
| 195 | + self.assertFalse(ctrl.discovery_ctrl) |
| 196 | + |
| 197 | + def test_persistent_flag_default_false(self): |
| 198 | + ctrl = self._make_loop_ctrl() |
| 199 | + self.assertFalse(ctrl.persistent) |
| 200 | + |
| 201 | + def test_persistent_flag_set(self): |
| 202 | + ctrl = self._make_loop_ctrl() |
| 203 | + ctrl.persistent = True |
| 204 | + self.assertTrue(ctrl.persistent) |
| 205 | + |
| 206 | + def test_unique_discovery_ctrl_flag(self): |
| 207 | + ctrl = self._make_loop_ctrl() |
| 208 | + ctrl.unique_discovery_ctrl = True |
| 209 | + self.assertTrue(ctrl.unique_discovery_ctrl) |
| 210 | + |
| 211 | + def test_multiple_ctrls_same_ctx(self): |
| 212 | + """Multiple controllers can be created under the same context.""" |
| 213 | + ctrls = [self._make_loop_ctrl() for _ in range(5)] |
| 214 | + self.assertEqual(len(ctrls), 5) |
| 215 | + for c in ctrls: |
| 216 | + self.assertFalse(c.connected()) |
| 217 | + |
| 218 | + |
| 219 | +class TestCtrlErrorHandling(unittest.TestCase): |
| 220 | + """Error paths that can be exercised without real hardware.""" |
| 221 | + |
| 222 | + def setUp(self): |
| 223 | + self.ctx = nvme.global_ctx() |
| 224 | + self.ctrl = nvme.ctrl( |
| 225 | + self.ctx, |
| 226 | + subsysnqn=nvme.NVME_DISC_SUBSYS_NAME, |
| 227 | + transport='loop', |
| 228 | + ) |
| 229 | + |
| 230 | + def tearDown(self): |
| 231 | + self.ctrl = None |
| 232 | + self.ctx = None |
| 233 | + gc.collect() |
| 234 | + |
| 235 | + def test_disconnect_unconnected_raises_attribute_error(self): |
| 236 | + with self.assertRaises(AttributeError): |
| 237 | + self.ctrl.disconnect() |
| 238 | + |
| 239 | + def test_discover_unconnected_raises_attribute_error(self): |
| 240 | + with self.assertRaises(AttributeError): |
| 241 | + self.ctrl.discover() |
| 242 | + |
| 243 | + |
| 244 | +class TestHelperFunctions(unittest.TestCase): |
| 245 | + """Module-level helper functions exposed by the bindings.""" |
| 246 | + |
| 247 | + def test_read_hostnqn_returns_non_empty_string(self): |
| 248 | + hostnqn = nvme.read_hostnqn() |
| 249 | + self.assertIsInstance(hostnqn, str) |
| 250 | + self.assertGreater(len(hostnqn), 0) |
| 251 | + |
| 252 | + def test_read_hostid_returns_non_empty_string(self): |
| 253 | + hostid = nvme.read_hostid() |
| 254 | + self.assertIsInstance(hostid, str) |
| 255 | + self.assertGreater(len(hostid), 0) |
| 256 | + |
| 257 | + |
| 258 | +if __name__ == '__main__': |
| 259 | + unittest.main() |
0 commit comments