def test_port(self): root = nvme.Root() root.clear_existing() for p in root.ports: self.assertTrue(False, 'Found Port after clear') # create mode p1 = nvme.Port(portid=0, mode='create') self.assertIsNotNone(p1) self.assertEqual(len(list(root.ports)), 1) # any mode, should create p2 = nvme.Port(portid=1, mode='any') self.assertIsNotNone(p2) self.assertEqual(len(list(root.ports)), 2) # duplicate self.assertRaises(nvme.CFSError, nvme.Port, portid=0, mode='create') self.assertEqual(len(list(root.ports)), 2) # lookup using any, should not create p = nvme.Port(portid=0, mode='any') self.assertEqual(p1, p) self.assertEqual(len(list(root.ports)), 2) # lookup only p = nvme.Port(portid=1, mode='lookup') self.assertEqual(p2, p) self.assertEqual(len(list(root.ports)), 2) # and delete them all for p in root.ports: p.delete() self.assertEqual(len(list(root.ports)), 0)
def test_loop_port(self): root = nvme.Root() root.clear_existing() s = nvme.Subsystem(nqn='testnqn', mode='create') p = nvme.Port(portid=0, mode='create') # subsystem doesn't exists, should fail self.assertRaises(nvme.CFSError, p.add_subsystem, 'invalidnqn') self.assertTrue('addr' in p.attr_groups) # no trtype set yet, should fail self.assertRaises(nvme.CFSError, p.add_subsystem, 'testnqn') # now set trtype to loop and other attrs and enable p.set_attr('addr', 'trtype', 'loop') p.set_attr('addr', 'adrfam', 'ipv4') p.set_attr('addr', 'traddr', '192.168.0.1') p.set_attr('addr', 'treq', 'not required') p.set_attr('addr', 'trsvcid', '1023') p.add_subsystem('testnqn') # test double add self.assertRaises(nvme.CFSError, p.add_subsystem, 'testnqn') # test that we can't write to attrs while enabled self.assertRaises(nvme.CFSError, p.set_attr, 'addr', 'trtype', 'rdma') self.assertRaises(nvme.CFSError, p.set_attr, 'addr', 'adrfam', 'ipv6') self.assertRaises(nvme.CFSError, p.set_attr, 'addr', 'traddr', '10.0.0.1') self.assertRaises(nvme.CFSError, p.set_attr, 'addr', 'treq', 'required') self.assertRaises(nvme.CFSError, p.set_attr, 'addr', 'trsvcid', '21') # remove: once and twice p.remove_subsystem('testnqn') self.assertRaises(nvme.CFSError, p.remove_subsystem, 'testnqn') # check that the attrs haven't been tampered with self.assertEqual(p.get_attr('addr', 'trtype'), 'loop') self.assertEqual(p.get_attr('addr', 'adrfam'), 'ipv4') self.assertEqual(p.get_attr('addr', 'traddr'), '192.168.0.1') self.assertEqual(p.get_attr('addr', 'treq'), 'not required') self.assertEqual(p.get_attr('addr', 'trsvcid'), '1023') # add again, and try to remove while enabled p.add_subsystem('testnqn') p.delete()
def test_save_restore(self): root = nvme.Root() root.clear_existing() h = nvme.Host(nqn='hostnqn', mode='create') s = nvme.Subsystem(nqn='testnqn', mode='create') s.add_allowed_host(nqn='hostnqn') s2 = nvme.Subsystem(nqn='testnqn2', mode='create') s2.set_attr('attr', 'allow_any_host', 1) n = nvme.Namespace(s, nsid=42, mode='create') n.set_attr('device', 'path', '/dev/ram0') n.set_enable(1) nguid = n.get_attr('device', 'nguid') p = nvme.Port(portid=66, mode='create') p.set_attr('addr', 'trtype', 'loop') p.set_attr('addr', 'adrfam', 'ipv4') p.set_attr('addr', 'traddr', '192.168.0.1') p.set_attr('addr', 'treq', 'not required') p.set_attr('addr', 'trsvcid', '1023') p.add_subsystem('testnqn') # save, clear, and restore root.save_to_file('test.json') root.clear_existing() root.restore_from_file('test.json') # additional restores should fai self.assertRaises(nvme.CFSError, root.restore_from_file, 'test.json', False) # ... unless forced! root.restore_from_file('test.json', True) # rebuild our view of the world h = nvme.Host(nqn='hostnqn', mode='lookup') s = nvme.Subsystem(nqn='testnqn', mode='lookup') s2 = nvme.Subsystem(nqn='testnqn2', mode='lookup') n = nvme.Namespace(s, nsid=42, mode='lookup') p = nvme.Port(portid=66, mode='lookup') self.assertEqual(s.get_attr('attr', 'allow_any_host'), "0") self.assertEqual(s2.get_attr('attr', 'allow_any_host'), "1") self.assertIn('hostnqn', s.allowed_hosts) # and check everything is still the same self.assertTrue(n.get_enable()) self.assertEqual(n.get_attr('device', 'path'), '/dev/ram0') self.assertEqual(n.get_attr('device', 'nguid'), nguid) self.assertEqual(p.get_attr('addr', 'trtype'), 'loop') self.assertEqual(p.get_attr('addr', 'adrfam'), 'ipv4') self.assertEqual(p.get_attr('addr', 'traddr'), '192.168.0.1') self.assertEqual(p.get_attr('addr', 'treq'), 'not required') self.assertEqual(p.get_attr('addr', 'trsvcid'), '1023') self.assertIn('testnqn', p.subsystems) self.assertNotIn('testtnqn2', p.subsystems)
def test_referral(self): root = nvme.Root() root.clear_existing() # create port p = nvme.Port(portid=1, mode='create') self.assertEqual(len(list(p.referrals)), 0) # create mode r1 = nvme.Referral(p, name="1", mode='create') self.assertIsNotNone(r1) self.assertEqual(len(list(p.referrals)), 1) # any mode, should create r2 = nvme.Referral(p, name="2", mode='any') self.assertIsNotNone(r2) self.assertEqual(len(list(p.referrals)), 2) # duplicate self.assertRaises(nvme.CFSError, nvme.Referral, p, name="2", mode='create') self.assertEqual(len(list(p.referrals)), 2) # lookup using any, should not create r = nvme.Referral(p, name="1", mode='any') self.assertEqual(r1, r) self.assertEqual(len(list(p.referrals)), 2) # lookup only r = nvme.Referral(p, name="2", mode='lookup') self.assertEqual(r2, r) self.assertEqual(len(list(p.referrals)), 2) # non-existant lookup self.assertRaises(nvme.CFSError, nvme.Referral, p, name="foo", mode='lookup') # basic state self.assertTrue('addr' in r.attr_groups) self.assertFalse(r.get_enable()) # now set trtype to loop and other attrs and enable r.set_attr('addr', 'trtype', 'loop') r.set_attr('addr', 'adrfam', 'ipv4') r.set_attr('addr', 'traddr', '192.168.0.1') r.set_attr('addr', 'treq', 'not required') r.set_attr('addr', 'trsvcid', '1023') r.set_enable(1) # test double enable r.set_enable(1) # test that we can't write to attrs while enabled self.assertRaises(nvme.CFSError, r.set_attr, 'addr', 'trtype', 'rdma') self.assertRaises(nvme.CFSError, r.set_attr, 'addr', 'adrfam', 'ipv6') self.assertRaises(nvme.CFSError, r.set_attr, 'addr', 'traddr', '10.0.0.1') self.assertRaises(nvme.CFSError, r.set_attr, 'addr', 'treq', 'required') self.assertRaises(nvme.CFSError, r.set_attr, 'addr', 'trsvcid', '21') # disable: once and twice r.set_enable(0) r.set_enable(0) # check that the attrs haven't been tampered with self.assertEqual(r.get_attr('addr', 'trtype'), 'loop') self.assertEqual(r.get_attr('addr', 'adrfam'), 'ipv4') self.assertEqual(r.get_attr('addr', 'traddr'), '192.168.0.1') self.assertEqual(r.get_attr('addr', 'treq'), 'not required') self.assertEqual(r.get_attr('addr', 'trsvcid'), '1023') # enable again, and try to remove while enabled r.set_enable(1) r.delete() # remove the other one while disabled: r1.delete() self.assertEqual(len(list(p.referrals)), 0)