def setUp(self):
     self.generator = FakeNodesGenerator()
Ejemplo n.º 2
0
 def setUp(self):
     self.generator = FakeNodesGenerator()
class TestFakeNodeGenerator(base.BaseUnitTest):
    def setUp(self):
        self.generator = FakeNodesGenerator()

    def test_generate_fake_nodes_with_total_count(self):
        total_nodes_count = 8
        generated_nodes = self.generator.generate_fake_nodes(total_nodes_count)
        self.assertEqual(total_nodes_count, len(generated_nodes))

    def test_generate_fake_nodes_with_error_offline_nodes(self):
        total_nodes_count = 7
        error_nodes_count = 2
        offline_nodes_count = 1
        offloading_ifaces_nodes_count = 3

        generated_nodes = self.generator.generate_fake_nodes(
            total_nodes_count, error_nodes_count, offline_nodes_count,
            offloading_ifaces_nodes_count
        )
        self.assertEqual(total_nodes_count, len(generated_nodes))
        generated_error_nodes = [n for n in generated_nodes if
                                 n['fields']['status'] == 'error']
        self.assertEqual(error_nodes_count, len(generated_error_nodes))
        generated_offline_nodes = [n for n in generated_nodes if
                                   not n['fields']['online']]
        self.assertEqual(offline_nodes_count, len(generated_offline_nodes))

        offloading_ifaces_nodes = [
            n for n in generated_nodes if
            n['fields']['meta']['interfaces'][0].get('offloading_modes')
        ]
        self.assertEqual(offloading_ifaces_nodes_count,
                         len(offloading_ifaces_nodes))

    def test_generate_fake_nodes_with_wrong_params(self):
        total_nodes_count = 4
        error_nodes_count = 2
        offline_nodes_count = 5
        generated_nodes = self.generator.generate_fake_nodes(
            total_nodes_count, error_nodes_count, offline_nodes_count)
        self.assertEqual(total_nodes_count, len(generated_nodes))

        actual_error_nodes = len(
            [n for n in generated_nodes if n['fields']['status'] == 'error'])
        actual_offline_nodes = len(
            [n for n in generated_nodes if not n['fields']['online']])
        self.assertTrue(
            total_nodes_count >= actual_error_nodes + actual_offline_nodes)

    def test_generate_fake_node_common_structure(self):
        pk = 123
        sample_node_fields = {
            'status': 'discover',
            'name': 'Supermicro X9DRW',
            'hostname': 'node-1',
            'ip': '172.18.67.168',
            'online': True,
            'labels': {},
            'pending_addition': False,
            'platform_name': 'X9DRW',
            'mac': '00:25:90:6a:b1:10',
            'timestamp': '',
            'progress': 0,
            'pending_deletion': False,
            'os_platform': 'ubuntu',
            'manufacturer': 'Supermicro',
            'meta': {
                'cpu': {},
                'interfaces': [],
                'disks': [],
                'system': {},
                'memory': []
            }
        }
        generated_node = self.generator.generate_fake_node(pk)
        self.assertEqual(generated_node.get('pk'), pk)
        self.assertEqual(generated_node.get('model'), 'nailgun.node')
        generated_node_fields = generated_node.get('fields', {})
        self.assertItemsEqual(sample_node_fields.keys(),
                              generated_node_fields.keys())
        self.assertItemsEqual(sample_node_fields['meta'].keys(),
                              generated_node_fields.get('meta', {}).keys())
        self.assertFalse(generated_node_fields.get('pending_deletion'))
        self.assertFalse(generated_node_fields.get('pending_addition'))
        self.assertEqual(generated_node_fields.get('progress'), 0)

    def test_generate_fake_node_with_params(self):
        pk = 123
        is_online = False

        generated_node = self.generator.generate_fake_node(
            pk, is_online=is_online, use_offload_iface=True)
        self.assertEqual(pk, generated_node.get('pk'))
        generated_node_fields = generated_node.get('fields')
        self.assertEqual(is_online, generated_node_fields.get('online'))
        self.assertEqual('discover', generated_node_fields.get('status'))
        self.assertIn('offloading_modes',
                      generated_node_fields['meta'].get('interfaces')[0])

        pk = 321
        generated_node = self.generator.generate_fake_node(
            pk, is_error=True, use_offload_iface=False)
        self.assertEqual(pk, generated_node.get('pk'))
        generated_node_fields = generated_node.get('fields')
        self.assertTrue(generated_node_fields.get('online'))
        self.assertEqual('error', generated_node_fields.get('status'))
        self.assertNotIn('offloading_modes',
                         generated_node_fields['meta'].get('interfaces')[0])

    def test_generate_fake_node_interface_meta(self):
        generated_node_fields = self.generator.generate_fake_node(1)['fields']
        known_mac = generated_node_fields['mac']
        known_ip = generated_node_fields['ip']
        ifaces = generated_node_fields['meta']['interfaces']
        suitable_ifaces = [
            x['name'] for x in ifaces if
            x.get('ip') == known_ip or x.get('mac') == known_mac
        ]
        self.assertEqual(len(set(suitable_ifaces)), 1)

    def test_generate_fake_node_disk_suffixes(self):
        count = 500
        disk_suffixes = list(self.generator._get_disk_suffixes(count))
        self.assertEqual(len(set(disk_suffixes)), count)
        self.assertEqual(disk_suffixes[27], 'ab')

    def test_generate_disks_meta_with_non_zero_size(self):
        memory_random_side_effect = [
            {
                'model': 'Virtual Floppy0',
                'name': 'sde',
                'disk': 'sde',
                'size': 0
            },
            {
                'model': 'Virtual HDisk0',
                'name': 'sdf',
                'disk': 'sdf',
                'size': 0
            },
            {
                'model': 'TOSHIBA MK1002TS',
                'name': 'sda',
                'disk': 'sda',
                'size': 1000204886016
            }
        ]

        with patch('random.choice',
                   side_effect=memory_random_side_effect) as mock_random:
            disks_meta = self.generator._generate_disks_meta(1)
            self.assertNotEqual(disks_meta[0]['size'], 0)
            self.assertEqual(mock_random.call_count, 3)
Ejemplo n.º 4
0
class TestFakeNodeGenerator(base.BaseUnitTest):
    def setUp(self):
        self.generator = FakeNodesGenerator()

    def test_generate_fake_nodes_with_total_count(self):
        total_nodes_count = 8
        generated_nodes = self.generator.generate_fake_nodes(total_nodes_count)
        self.assertEqual(total_nodes_count, len(generated_nodes))

    def test_generate_fake_nodes_with_error_offline_nodes(self):
        total_nodes_count = 7
        error_nodes_count = 2
        offline_nodes_count = 1
        offloading_ifaces_nodes_count = 3

        generated_nodes = self.generator.generate_fake_nodes(
            total_nodes_count, error_nodes_count, offline_nodes_count,
            offloading_ifaces_nodes_count)
        self.assertEqual(total_nodes_count, len(generated_nodes))
        generated_error_nodes = [
            n for n in generated_nodes if n['fields']['status'] == 'error'
        ]
        self.assertEqual(error_nodes_count, len(generated_error_nodes))
        generated_offline_nodes = [
            n for n in generated_nodes if not n['fields']['online']
        ]
        self.assertEqual(offline_nodes_count, len(generated_offline_nodes))

        offloading_ifaces_nodes = [
            n for n in generated_nodes
            if n['fields']['meta']['interfaces'][0].get('offloading_modes')
        ]
        self.assertEqual(offloading_ifaces_nodes_count,
                         len(offloading_ifaces_nodes))

    def test_generate_fake_nodes_with_wrong_params(self):
        total_nodes_count = 4
        error_nodes_count = 2
        offline_nodes_count = 5
        generated_nodes = self.generator.generate_fake_nodes(
            total_nodes_count, error_nodes_count, offline_nodes_count)
        self.assertEqual(total_nodes_count, len(generated_nodes))

        actual_error_nodes = len(
            [n for n in generated_nodes if n['fields']['status'] == 'error'])
        actual_offline_nodes = len(
            [n for n in generated_nodes if not n['fields']['online']])
        self.assertTrue(
            total_nodes_count >= actual_error_nodes + actual_offline_nodes)

    def test_generate_fake_node_common_structure(self):
        pk = 123
        sample_node_fields = {
            'status': 'discover',
            'name': 'Supermicro X9DRW',
            'hostname': 'node-1',
            'ip': '172.18.67.168',
            'online': True,
            'labels': {},
            'pending_addition': False,
            'platform_name': 'X9DRW',
            'mac': '00:25:90:6a:b1:10',
            'timestamp': '',
            'progress': 0,
            'pending_deletion': False,
            'os_platform': 'ubuntu',
            'manufacturer': 'Supermicro',
            'meta': {
                'cpu': {},
                'interfaces': [],
                'disks': [],
                'system': {},
                'memory': []
            }
        }
        generated_node = self.generator.generate_fake_node(pk)
        self.assertEqual(generated_node.get('pk'), pk)
        self.assertEqual(generated_node.get('model'), 'nailgun.node')
        generated_node_fields = generated_node.get('fields', {})
        self.assertItemsEqual(sample_node_fields.keys(),
                              generated_node_fields.keys())
        self.assertItemsEqual(sample_node_fields['meta'].keys(),
                              generated_node_fields.get('meta', {}).keys())
        self.assertFalse(generated_node_fields.get('pending_deletion'))
        self.assertFalse(generated_node_fields.get('pending_addition'))
        self.assertEqual(generated_node_fields.get('progress'), 0)

    def test_generate_fake_node_with_params(self):
        pk = 123
        is_online = False

        generated_node = self.generator.generate_fake_node(
            pk, is_online=is_online, use_offload_iface=True)
        self.assertEqual(pk, generated_node.get('pk'))
        generated_node_fields = generated_node.get('fields')
        self.assertEqual(is_online, generated_node_fields.get('online'))
        self.assertEqual('discover', generated_node_fields.get('status'))
        self.assertIn('offloading_modes',
                      generated_node_fields['meta'].get('interfaces')[0])

        pk = 321
        generated_node = self.generator.generate_fake_node(
            pk, is_error=True, use_offload_iface=False)
        self.assertEqual(pk, generated_node.get('pk'))
        generated_node_fields = generated_node.get('fields')
        self.assertTrue(generated_node_fields.get('online'))
        self.assertEqual('error', generated_node_fields.get('status'))
        self.assertNotIn('offloading_modes',
                         generated_node_fields['meta'].get('interfaces')[0])

    def test_generate_fake_node_interface_meta(self):
        generated_node_fields = self.generator.generate_fake_node(1)['fields']
        known_mac = generated_node_fields['mac']
        known_ip = generated_node_fields['ip']
        ifaces = generated_node_fields['meta']['interfaces']
        suitable_ifaces = [
            x['name'] for x in ifaces
            if x.get('ip') == known_ip or x.get('mac') == known_mac
        ]
        self.assertEqual(len(set(suitable_ifaces)), 1)

    def test_generate_fake_node_disk_suffixes(self):
        count = 500
        disk_suffixes = list(self.generator._get_disk_suffixes(count))
        self.assertEqual(len(set(disk_suffixes)), count)
        self.assertEqual(disk_suffixes[27], 'ab')

    def test_generate_disks_meta_with_non_zero_size(self):
        memory_random_side_effect = [{
            'model': 'Virtual Floppy0',
            'name': 'sde',
            'disk': 'sde',
            'size': 0
        }, {
            'model': 'Virtual HDisk0',
            'name': 'sdf',
            'disk': 'sdf',
            'size': 0
        }, {
            'model': 'TOSHIBA MK1002TS',
            'name': 'sda',
            'disk': 'sda',
            'size': 1000204886016
        }]

        with patch('random.choice',
                   side_effect=memory_random_side_effect) as mock_random:
            disks_meta = self.generator._generate_disks_meta(1)
            self.assertNotEqual(disks_meta[0]['size'], 0)
            self.assertEqual(mock_random.call_count, 3)