示例#1
0
 def setUp(self):
     self.aoestat = AoEStat()
     self.aoestat.open_file = mock_open_file
     self.aoestat.mk_map = mock_mk_map
示例#2
0
 def setUp(self):
     self.aoestat = AoEStat()
     self.aoestat.open_file = mock_open_file
     self.aoestat.mk_map = mock_mk_map
示例#3
0
class TestAoestat(unittest.TestCase):
    """
    tests for the etherdrv-stat command
    """
    def setUp(self):
        self.aoestat = AoEStat()
        self.aoestat.open_file = mock_open_file
        self.aoestat.mk_map = mock_mk_map

    def test_get_devices(self):
        """
        we can read /proc/ethdrv/devices
        """
        self.aoestat.get_devices()
        self.assertIsInstance(self.aoestat.devices, list)

        devfile = ETHDRV_DEVICES_FILE.splitlines()
        self.assertEqual(len(devfile), len(self.aoestat.devices))

        for device in self.aoestat.devices:
            self.assertRegexpMatches(device.target, r"[0-9]*\.[0-9]*")
            self.assertRegexpMatches(device.size, r"[0-9]*\.[0-9]*GB")

    def test_get_targets(self):
        """
        we can read /proc/ethdrv/targets
        """
        self.aoestat.update()
        for device in self.aoestat.devices:
            self.assertIsInstance(device.macs, list)
            self.assertNotEqual(len(device.macs), 0)
            self.assertIsInstance(device.ports, set)

    def test_str_output(self):
        """
        can output a string
        """
        self.aoestat.update()
        self.assertIsInstance(self.aoestat.output(), str)

    def test_json_output(self):
        """
        output is a string containing json that transforms cleanly to a dict
        """
        self.aoestat.update()
        joutput = self.aoestat.output(json=True)
        doutput = JSONDecoder().decode(joutput)

        self.assertIsInstance(doutput, dict)

    def test_update_with_no_targets(self):
        """
        if /dev/ethdrv is empty we report the target as 'init'
        """
        global ETHDRV_DEV_DIR
        backup = ETHDRV_DEV_DIR
        ETHDRV_DEV_DIR = ''
        self.aoestat.update()
        for targ in self.aoestat.devices:
            self.assertEqual(targ.file, 'init')
        ETHDRV_DEV_DIR = backup

    def test_compare_outputs(self):
        """
        compare .output to the sample 'OUTPUT'
        captured from previous version of ethdrv-stat
        """
        self.aoestat.update()
        self.assertEqual(self.aoestat.output(), OUTPUT)

    def test_paths_list_length(self):
        """
        test that the -a output is the right length
        it should be more than without -a
        """
        self.aoestat.update()
        output = self.aoestat.output()
        output_a = self.aoestat.output(paths=True)

        targets = 0
        ports = 0
        numlines = len(output_a.splitlines())

        for target in self.aoestat.devices:
            targets += 1
            ports += len(target.ports)

        expected_len = targets + ports
        output_len = len(output.splitlines())

        self.assertGreater(numlines, output_len)
        self.assertEqual(numlines, expected_len)

    def test_int2bitmask(self):
        self.assertEqual(int2bitmask(15903), '11111000011111')
        self.assertEqual(int2bitmask(16383), '11111111111111')
        self.assertEqual(int2bitmask(1), '1')
        self.assertEqual(int2bitmask(2), '10')
        self.assertEqual(int2bitmask(3), '11')

    def test_bitmask2index(self):
        ones = '11111111111111'
        self.assertEqual(bitmask2index(ones), list(range(len(ones))))
        self.assertEqual(bitmask2index('1'), [0])
        self.assertEqual(bitmask2index('10'), [1])
        self.assertEqual(bitmask2index('11'), [0, 1])
        self.assertEqual(bitmask2index('111'), [0, 1, 2])
        self.assertEqual(bitmask2index('11111000011111'),
                         [0, 1, 2, 3, 4, 9, 10, 11, 12, 13])
示例#4
0
class TestAoestat(unittest.TestCase):
    """
    tests for the etherdrv-stat command
    """

    def setUp(self):
        self.aoestat = AoEStat()
        self.aoestat.open_file = mock_open_file
        self.aoestat.mk_map = mock_mk_map

    def test_get_devices(self):
        """
        we can read /proc/ethdrv/devices
        """
        self.aoestat.get_devices()
        self.assertIsInstance(self.aoestat.devices, list)

        devfile = ETHDRV_DEVICES_FILE.splitlines()
        self.assertEqual(len(devfile), len(self.aoestat.devices))

        for device in self.aoestat.devices:
            self.assertRegexpMatches(device.target, r"[0-9]*\.[0-9]*")
            self.assertRegexpMatches(device.size, r"[0-9]*\.[0-9]*GB")

    def test_get_targets(self):
        """
        we can read /proc/ethdrv/targets
        """
        self.aoestat.update()
        for device in self.aoestat.devices:
            self.assertIsInstance(device.macs, list)
            self.assertNotEqual(len(device.macs), 0)
            self.assertIsInstance(device.ports, set)

    def test_str_output(self):
        """
        can output a string
        """
        self.aoestat.update()
        self.assertIsInstance(self.aoestat.output(), str)

    def test_json_output(self):
        """
        output is a string containing json that transforms cleanly to a dict
        """
        self.aoestat.update()
        joutput = self.aoestat.output(json=True)
        doutput = JSONDecoder().decode(joutput)

        self.assertIsInstance(doutput, dict)

    def test_update_with_no_targets(self):
        """
        if /dev/ethdrv is empty we report the target as 'init'
        """
        global ETHDRV_DEV_DIR
        backup = ETHDRV_DEV_DIR
        ETHDRV_DEV_DIR = ""
        self.aoestat.update()
        for targ in self.aoestat.devices:
            self.assertEqual(targ.file, "init")
        ETHDRV_DEV_DIR = backup

    def test_compare_outputs(self):
        """
        compare .output to the sample 'OUTPUT'
        captured from previous version of ethdrv-stat
        """
        self.aoestat.update()
        self.assertEqual(self.aoestat.output(), OUTPUT)

    def test_paths_list_length(self):
        """
        test that the -a output is the right length
        it should be more than without -a
        """
        self.aoestat.update()
        output = self.aoestat.output()
        output_a = self.aoestat.output(paths=True)

        targets = 0
        ports = 0
        numlines = len(output_a.splitlines())

        for target in self.aoestat.devices:
            targets += 1
            ports += len(target.ports)

        expected_len = targets + ports
        output_len = len(output.splitlines())

        self.assertGreater(numlines, output_len)
        self.assertEqual(numlines, expected_len)

    def test_int2bitmask(self):
        self.assertEqual(int2bitmask(15903), "11111000011111")
        self.assertEqual(int2bitmask(16383), "11111111111111")
        self.assertEqual(int2bitmask(1), "1")
        self.assertEqual(int2bitmask(2), "10")
        self.assertEqual(int2bitmask(3), "11")

    def test_bitmask2index(self):
        ones = "11111111111111"
        self.assertEqual(bitmask2index(ones), list(range(len(ones))))
        self.assertEqual(bitmask2index("1"), [0])
        self.assertEqual(bitmask2index("10"), [1])
        self.assertEqual(bitmask2index("11"), [0, 1])
        self.assertEqual(bitmask2index("111"), [0, 1, 2])
        self.assertEqual(bitmask2index("11111000011111"), [0, 1, 2, 3, 4, 9, 10, 11, 12, 13])