def test_recursive_delete(self): root = nvme.Root() root.clear_existing() s = nvme.Subsystem(nqn='testnqn', mode='create') n1 = nvme.Namespace(s, mode='create') n2 = nvme.Namespace(s, mode='create') s.delete() self.assertEqual(len(list(root.subsystems)), 0)
def test_namespace_attrs(self): root = nvme.Root() root.clear_existing() s = nvme.Subsystem(nqn='testnqn', mode='create') n = nvme.Namespace(s, mode='create') self.assertFalse(n.get_enable()) self.assertTrue('device' in n.attr_groups) self.assertTrue('path' in n.list_attrs('device')) # no device set yet, should fail self.assertRaises(nvme.CFSError, n.set_enable, 1) # now set a path and enable n.set_attr('device', 'path', '/dev/ram0') n.set_enable(1) self.assertTrue(n.get_enable()) # test double enable n.set_enable(1) # test that we can't write to attrs while enabled self.assertRaises(nvme.CFSError, n.set_attr, 'device', 'path', '/dev/ram1') self.assertRaises(nvme.CFSError, n.set_attr, 'device', 'nguid', '15f7767b-50e7-4441-949c-75b99153dea7') # disable: once and twice n.set_enable(0) n.set_enable(0) # enable again, and remove while enabled n.set_enable(1) n.delete()
def test_namespace(self): root = nvme.Root() root.clear_existing() s = nvme.Subsystem(nqn='testnqn', mode='create') for n in s.namespaces: self.assertTrue(False, 'Found Namespace in new Subsystem') # create mode n1 = nvme.Namespace(s, nsid=3, mode='create') self.assertIsNotNone(n1) self.assertEqual(len(list(s.namespaces)), 1) # any mode, should create n2 = nvme.Namespace(s, nsid=2, mode='any') self.assertIsNotNone(n2) self.assertEqual(len(list(s.namespaces)), 2) # create without nsid, should pick lowest available n3 = nvme.Namespace(s, mode='create') self.assertIsNotNone(n3) self.assertEqual(n3.nsid, 1) self.assertEqual(len(list(s.namespaces)), 3) n4 = nvme.Namespace(s, mode='create') self.assertIsNotNone(n4) self.assertEqual(n4.nsid, 4) self.assertEqual(len(list(s.namespaces)), 4) # duplicate self.assertRaises(nvme.CFSError, nvme.Namespace, 1, mode='create') self.assertEqual(len(list(s.namespaces)), 4) # lookup using any, should not create n = nvme.Namespace(s, nsid=3, mode='any') self.assertEqual(n1, n) self.assertEqual(len(list(s.namespaces)), 4) # lookup only n = nvme.Namespace(s, nsid=2, mode='lookup') self.assertEqual(n2, n) self.assertEqual(len(list(s.namespaces)), 4) # lookup without nsid self.assertRaises(nvme.CFSError, nvme.Namespace, None, mode='lookup') # and delete them all for n in s.namespaces: n.delete() self.assertEqual(len(list(s.namespaces)), 0)
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)