Ejemplo n.º 1
0
 def setUp(self):
     super().setUp()
     self.cf = CustomField.objects.create(
         name='test_cf', use_as_configuration_variable=True)
     # is should be skipped in API
     self.bo_asset = BackOfficeAssetFactory(barcode='12345',
                                            hostname='host1')
     self.conf_module_1 = ConfigurationModuleFactory()
     self.conf_module_2 = ConfigurationModuleFactory(
         parent=self.conf_module_1, name='ralph')
     self.conf_class_1 = ConfigurationClassFactory(
         module=self.conf_module_2, class_name='cls1')
     self.dc_asset = DataCenterAssetFullFactory(
         service_env__service__name='test-service',
         service_env__service__uid='sc-123',
         service_env__environment__name='prod',
         configuration_path=self.conf_class_1,
     )
     self.dc_asset.update_custom_field('test_cf', 'abc')
     self.virtual = VirtualServerFullFactory(
         parent=self.dc_asset,
         configuration_path__module__name='ralph2',
         service_env__service__uid='sc-222',
         service_env__environment__name='some_env',
     )
     self.virtual.update_custom_field('test_cf', 'def')
     se = ServiceEnvironmentFactory(service__uid='sc-333')
     self.cloud_host = CloudHostFullFactory(
         configuration_path__module__name='ralph3',
         service_env=se,
         parent__service_env=se,
         hostname='aaaa',
         hypervisor=self.dc_asset)
     self.cloud_host.ip_addresses = ['10.20.30.40']
     self.cloud_host.update_custom_field('test_cf', 'xyz')
Ejemplo n.º 2
0
    def test_match_cloud_hosts_ignore_already_matched(self):
        unassigned_hypervisor = DataCenterAsset.objects.create(
            hostname='hypervisor',
            model=DataCenterAssetModelFactory(),
            sn='hypervisor-SN')

        with_hypervisor = CloudHostFullFactory(host_id='with hypervisor')
        with_hypervisor_modified = with_hypervisor.modified
        with_hypervisor_node = FakeIronicNode(
            serial_number=with_hypervisor.hypervisor.sn,
            instance_uuid=with_hypervisor.host_id)

        without_hypervisor = CloudHostFactory(host_id='no hypervisor')
        without_hypervisor_modified = without_hypervisor.modified
        without_hypervisor_node = FakeIronicNode(
            serial_number=unassigned_hypervisor.sn,
            instance_uuid=without_hypervisor.host_id)

        nodes = [with_hypervisor_node, without_hypervisor_node]

        self.cmd._match_nodes_to_hosts(nodes)
        without_hypervisor.refresh_from_db()
        with_hypervisor.refresh_from_db()

        # should not be modified by the command
        self.assertTrue(with_hypervisor_modified == with_hypervisor.modified)
        # should be modified by the command
        self.assertTrue(
            without_hypervisor_modified < without_hypervisor.modified)
Ejemplo n.º 3
0
 def test_changelist_view(self):
     DataCenterAssetFullFactory.create_batch(5)
     VirtualServerFullFactory.create_batch(5)
     CloudHostFullFactory.create_batch(4)
     ClusterFactory.create_batch(4)
     with self.assertNumQueries(19):
         result = self.client.get(
             reverse('admin:data_center_dchost_changelist'), )
     # DCAssets - 5
     # VirtualServer + hypervisors - 10
     # Cluster - 4
     # CloudHost + hypervisors - 8
     self.assertEqual(result.context_data['cl'].result_count, 27)
Ejemplo n.º 4
0
 def test_changelist_cloudhost_location(self):
     CloudHostFullFactory(hypervisor=DataCenterAssetFullFactory(
         rack__name='Rack #1',
         rack__server_room__name='SR1',
         rack__server_room__data_center__name='DC1',
         hostname='s12345.mydc.net',
     ))
     result = self.client.get(
         reverse('admin:data_center_dchost_changelist'), )
     self.assertContains(result, 'DC1 / SR1 / Rack #1 / s12345.mydc.net')
Ejemplo n.º 5
0
class DCHostAPITests(RalphAPITestCase):
    def setUp(self):
        super().setUp()
        self.cf = CustomField.objects.create(
            name='test_cf', use_as_configuration_variable=True)
        # is should be skipped in API
        self.bo_asset = BackOfficeAssetFactory(barcode='12345',
                                               hostname='host1')
        self.conf_module_1 = ConfigurationModuleFactory()
        self.conf_module_2 = ConfigurationModuleFactory(
            parent=self.conf_module_1, name='ralph')
        self.conf_class_1 = ConfigurationClassFactory(
            module=self.conf_module_2, class_name='cls1')
        self.dc_asset = DataCenterAssetFullFactory(
            service_env__service__name='test-service',
            service_env__service__uid='sc-123',
            service_env__environment__name='prod',
            configuration_path=self.conf_class_1,
        )
        self.dc_asset.update_custom_field('test_cf', 'abc')
        self.virtual = VirtualServerFullFactory(
            parent=self.dc_asset,
            configuration_path__module__name='ralph2',
            service_env__service__uid='sc-222',
            service_env__environment__name='some_env',
        )
        self.virtual.update_custom_field('test_cf', 'def')
        se = ServiceEnvironmentFactory(service__uid='sc-333')
        self.cloud_host = CloudHostFullFactory(
            configuration_path__module__name='ralph3',
            service_env=se,
            parent__service_env=se,
            hostname='aaaa',
            hypervisor=self.dc_asset)
        self.cloud_host.ip_addresses = ['10.20.30.40']
        self.cloud_host.update_custom_field('test_cf', 'xyz')

    def test_get_dc_hosts_list(self):
        url = reverse('dchost-list')
        with self.assertNumQueries(12):
            response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['count'], 3)

    def test_filter_by_type_dc_asset(self):
        url = '{}?{}'.format(reverse('dchost-list'),
                             urlencode({'object_type': 'datacenterasset'}))
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['count'], 1)
        dca = response.data['results'][0]
        self.assertEqual(dca['hostname'], self.dc_asset.hostname)
        self.assertEqual(len(dca['ethernet']), 3)
        self.assertEqual(len(dca['ipaddresses']), 2)
        self.assertCountEqual(dca['tags'], ['abc, cde', 'xyz'])
        self.assertEqual(dca['configuration_path']['module']['name'], 'ralph')
        self.assertEqual(dca['service_env']['service_uid'], 'sc-123')
        self.assertEqual(dca['object_type'], 'datacenterasset')
        self.assertEqual(dca['custom_fields'], {'test_cf': 'abc'})
        self.assertEqual(dca['configuration_variables'], {'test_cf': 'abc'})

    def test_filter_by_type_virtual(self):
        url = '{}?{}'.format(reverse('dchost-list'),
                             urlencode({'object_type': 'virtualserver'}))
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['count'], 1)
        virt = response.data['results'][0]
        self.assertEqual(virt['hostname'], self.virtual.hostname)
        self.assertEqual(len(virt['ethernet']), 2)
        self.assertEqual(len(virt['ipaddresses']), 1)
        self.assertCountEqual(virt['tags'], ['abc, cde', 'xyz'])
        self.assertEqual(virt['configuration_path']['module']['name'],
                         'ralph2')
        self.assertEqual(virt['service_env']['service_uid'], 'sc-222')
        self.assertEqual(virt['object_type'], 'virtualserver')
        self.assertEqual(virt['custom_fields'], {'test_cf': 'def'})
        self.assertEqual(virt['configuration_variables'], {'test_cf': 'def'})

    def test_filter_by_type_cloudhost(self):
        url = '{}?{}'.format(reverse('dchost-list'),
                             urlencode({'object_type': 'cloudhost'}))
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['count'], 1)
        cloud = response.data['results'][0]
        self.assertEqual(cloud['hostname'], self.cloud_host.hostname)
        self.assertCountEqual(cloud['tags'], ['abc, cde', 'xyz'])
        self.assertEqual(cloud['configuration_path']['module']['name'],
                         'ralph3')
        self.assertEqual(cloud['service_env']['service_uid'], 'sc-333')
        self.assertEqual(cloud['object_type'], 'cloudhost')
        self.assertEqual(len(cloud['ethernet']), 1)
        self.assertEqual(len(cloud['ipaddresses']), 1)
        self.assertEqual(cloud['custom_fields'], {'test_cf': 'xyz'})
        self.assertEqual(cloud['configuration_variables'], {'test_cf': 'xyz'})

    def test_filter_by_hostname(self):
        url = '{}?{}'.format(reverse('dchost-list'),
                             urlencode({'hostname': 'aaaa'}))
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['count'], 1)

    def test_filter_by_name(self):
        url = '{}?{}'.format(reverse('dchost-list'),
                             urlencode({'name': 'aaaa'}))
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['count'], 1)

    def test_filter_by_service_uid(self):
        url = '{}?{}'.format(reverse('dchost-list'),
                             urlencode({'service': 'sc-222'}))
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['count'], 1)

    def test_filter_by_ip(self):
        url = '{}?{}'.format(reverse('dchost-list'),
                             urlencode({'ip': '10.20.30.40'}))
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['count'], 1)

    def test_filter_by_configuration_path(self):
        url = '{}?{}'.format(reverse('dchost-list'),
                             urlencode({'configuration_path': 'ralph/cls1'}))
        response = self.client.get(url, format='json')
        self.assertEqual(len(response.data['results']), 1)
        self.assertEqual(response.data['results'][0]['id'], self.dc_asset.id)

    def test_filter_by_configuration_path_module_name(self):
        url = '{}?{}'.format(
            reverse('dchost-list'),
            urlencode({'configuration_path__module__name': 'ralph'}))
        response = self.client.get(url, format='json')
        self.assertEqual(len(response.data['results']), 1)
        self.assertEqual(response.data['results'][0]['id'], self.dc_asset.id)

    def test_filter_by_env_name(self):
        url = '{}?{}'.format(reverse('dchost-list'),
                             urlencode({'env': 'some_env'}))
        response = self.client.get(url, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['count'], 1)
Ejemplo n.º 6
0
 def setUp(self):
     self.cloud_host = CloudHostFullFactory()
     self.dc_asset = DataCenterAssetFullFactory()
     self.virtual_server = VirtualServerFactory()