Example #1
0
    def test_host(self):
        root = nvme.Root()
        root.clear_existing()
        for p in root.hosts:
            self.assertTrue(False, 'Found Host after clear')

        # create mode
        h1 = nvme.Host(nqn='foo', mode='create')
        self.assertIsNotNone(h1)
        self.assertEqual(len(list(root.hosts)), 1)

        # any mode, should create
        h2 = nvme.Host(nqn='bar', mode='any')
        self.assertIsNotNone(h2)
        self.assertEqual(len(list(root.hosts)), 2)

        # duplicate
        self.assertRaises(nvme.CFSError, nvme.Host, 'foo', mode='create')
        self.assertEqual(len(list(root.hosts)), 2)

        # lookup using any, should not create
        h = nvme.Host('foo', mode='any')
        self.assertEqual(h1, h)
        self.assertEqual(len(list(root.hosts)), 2)

        # lookup only
        h = nvme.Host('bar', mode='lookup')
        self.assertEqual(h2, h)
        self.assertEqual(len(list(root.hosts)), 2)

        # and delete them all
        for h in root.hosts:
            h.delete()
        self.assertEqual(len(list(root.hosts)), 0)
Example #2
0
    def test_allowed_hosts(self):
        root = nvme.Root()

        h = nvme.Host(nqn='hostnqn', mode='create')

        s = nvme.Subsystem(nqn='testnqn', mode='create')

        # add allowed_host
        s.add_allowed_host(nqn='hostnqn')

        # duplicate
        self.assertRaises(nvme.CFSError, s.add_allowed_host, 'hostnqn')

        # invalid
        self.assertRaises(nvme.CFSError, s.add_allowed_host, 'invalid')

        # remove again
        s.remove_allowed_host('hostnqn')

        # duplicate removal
        self.assertRaises(nvme.CFSError, s.remove_allowed_host, 'hostnqn')

        # invalid removal
        self.assertRaises(nvme.CFSError, s.remove_allowed_host, 'foobar')
Example #3
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)