Esempio n. 1
0
    def test_get_available_networks(self):
        rack, nets = self._generate_rack_with_networks()

        host = CloudHostFactory(hypervisor=DataCenterAssetFullFactory(
            rack=rack))

        self.assertNetworksTheSame(nets, host._get_available_networks())
Esempio 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)
Esempio n. 3
0
    def setUp(self):
        super().setUp()
        self.envs = EnvironmentFactory.create_batch(2)
        self.services = ServiceFactory.create_batch(2)
        self.service_env = []
        for i in range(0, 2):
            self.service_env.append(ServiceEnvironment.objects.create(
                service=self.services[i], environment=self.envs[i]
            ))
        self.service_env[0].service.business_owners = [self.user1]
        self.service_env[0].service.technical_owners = [self.user2]
        self.service_env[0].save()
        self.cloud_provider = CloudProviderFactory(name='openstack')
        self.cloud_flavor = CloudFlavorFactory()
        self.cloud_project = CloudProjectFactory(
            service_env=self.service_env[0]
        )
        self.cloud_host = CloudHostFactory(
            parent=self.cloud_project,
            cloudflavor=self.cloud_flavor
        )
        self.cloud_host2 = CloudHostFactory()

        self.test_cpu = ComponentModel.objects.create(
            name='vcpu1',
            cores=5,
            family='vCPU',
            type=ComponentType.processor,
        )
        self.test_mem = ComponentModel.objects.create(
            name='2000 MiB vMEM',
            size='2000',
            type=ComponentType.memory,
        )
        self.test_disk = ComponentModel.objects.create(
            name='4 GiB vDISK',
            size='4096',
            type=ComponentType.disk,
        )

        VirtualComponent.objects.create(
            base_object=self.cloud_flavor,
            model=self.test_cpu,
        )
        VirtualComponent.objects.create(
            base_object=self.cloud_flavor,
            model=self.test_mem,
        )
        VirtualComponent.objects.create(
            base_object=self.cloud_flavor,
            model=self.test_disk,
        )
Esempio n. 4
0
    def test_get_available_networks_cloud_host(self):
        rack, nets = self._generate_rack_with_networks()

        vm = VirtualServerFullFactory(parent=CloudHostFactory(
            hypervisor=DataCenterAssetFullFactory(rack=rack)))

        self.assertNetworksTheSame(nets, vm._get_available_networks())
Esempio n. 5
0
    def setUp(self):
        asset_model = DataCenterAssetModelFactory()
        self.cloud_provider = CloudProviderFactory(name='openstack')
        self.cloud_flavor = CloudFlavorFactory.create_batch(3)
        self.test_model = ComponentModel(name='delete_test')
        VirtualComponent(model=self.test_model,
                         base_object=self.cloud_flavor[0])

        self.cloud_project_1 = CloudProjectFactory(project_id='project_id1')
        self.cloud_project_2 = CloudProjectFactory(project_id='project_id2')
        self.cloud_project_3 = CloudProjectFactory(project_id='project_os_id1')

        self.host = CloudHostFactory(host_id='host_id1',
                                     parent=self.cloud_project_1,
                                     cloudflavor=self.cloud_flavor[1])
        IPAddress.objects.create(base_object=self.host, address='2.2.3.4')
        IPAddress.objects.create(base_object=self.host, address='1.2.3.4')

        DataCenterAsset.objects.create(
            hostname='hypervisor_os1.dcn.net',
            model=asset_model,
        )

        self.ironic_serial_number_param = 'serial_number'
        self.ralph_serial_number_param = 'sn'
        self.ralph_client = RalphClient('openstack',
                                        self.ironic_serial_number_param,
                                        self.ralph_serial_number_param)
        self.openstack_client = RalphOpenStackInfrastructureClient(
            self.cloud_provider.name)
Esempio n. 6
0
    def test_match_cloud_hosts_all_matched(self):
        asset_model = DataCenterAssetModelFactory()
        num_assets = 10

        assets = [
            DataCenterAsset.objects.create(hostname='hostname-{}'.format(i),
                                           model=asset_model,
                                           sn='SN{}'.format(i))
            for i in range(num_assets)
        ]
        hosts = [
            CloudHostFactory(host_id='fake-instance-uuid-{}'.format(i))
            for i in range(num_assets)
        ]

        nodes = [
            FakeIronicNode(serial_number=asset.sn, instance_uuid=host.host_id)
            for asset, host in zip(assets, hosts)
        ]

        self.cmd._match_nodes_to_hosts(nodes)

        updated_hosts = CloudHost.objects.filter(
            id__in=[host.id for host in hosts])

        for host in updated_hosts:
            self.assertIsNotNone(host.hypervisor)

        expected_serials = [asset.sn for asset in assets]
        expected_serials.sort()

        real_serials = [host.hypervisor.sn for host in updated_hosts]
        real_serials.sort()

        self.assertEqual(expected_serials, real_serials)
Esempio n. 7
0
    def test_match_cloud_hosts_host_not_found(self):
        host = CloudHostFactory(host_id='foo')
        node = FakeIronicNode(serial_number='SN0', instance_uuid='bar')
        self.cmd._match_nodes_to_hosts([node])

        updated_host = CloudHost.objects.get(pk=host.pk)
        self.assertIsNone(updated_host.hypervisor)
Esempio n. 8
0
    def setUp(self):
        asset_model = DataCenterAssetModelFactory()
        self.cloud_provider = CloudProviderFactory(name='openstack')
        self.cloud_flavor = CloudFlavorFactory.create_batch(3)
        self.test_model = ComponentModel(name='delete_test')
        VirtualComponent(model=self.test_model,
                         base_object=self.cloud_flavor[0])

        self.cloud_project = CloudProjectFactory(project_id='project_id1')
        CloudProjectFactory(project_id='project_id2')
        CloudProjectFactory(project_id='project_os_id1')

        host = CloudHostFactory(host_id='host_id1',
                                parent=self.cloud_project,
                                cloudflavor=self.cloud_flavor[1])
        IPAddress.objects.create(base_object=host, address='2.2.3.4')
        IPAddress.objects.create(base_object=host, address='1.2.3.4')

        DataCenterAsset.objects.create(
            hostname='hypervisor_os1.dcn.net',
            model=asset_model,
        )

        self.cmd = Command()
        self.cmd._get_cloud_provider()
        self.cmd.openstack_projects = OPENSTACK_DATA
        self.cmd.openstack_flavors = OPENSTACK_FLAVOR
        self.cmd._get_ralph_data()
        self.cmd.ironic_serial_number_param = 'serial_number'
        self.cmd.ralph_serial_number_param = 'sn'
Esempio n. 9
0
 def test_filter_cloudhost_by_service_uid(self):
     cloud_host = CloudHostFactory()
     url = (reverse('cloudhost-list') +
            '?service_env__service__uid={}'.format(
                cloud_host.service_env.service.uid))
     response = self.client.get(url, format='json')
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(response.data['count'], 1)
Esempio n. 10
0
 def test_cloud_host_parent(self):
     form_data = self._get_basic_form_data()
     form_data['parent'] = CloudHostFactory().pk
     response = self.client.post(self.url, form_data)
     self.assertEquals(
         response.status_code, 302,
         (repr(response.context['form'].errors)
          if response.context and 'form' in response.context else ''))
Esempio n. 11
0
    def setUp(self):
        self.service = ServiceFactory()
        self.service_env = ServiceEnvironmentFactory(service=self.service)
        self.cloud_project = CloudProjectFactory()
        self.cloud_host = CloudHostFactory(parent=self.cloud_project)

        self.custom_field_str = CustomField.objects.create(
            name='test str', type=CustomFieldTypes.STRING, default_value='xyz')
Esempio n. 12
0
 def setUp(self):
     self.cloud_ip = IPAddressFactory(
         ethernet__base_object=CloudHostFactory())
     # fetch "clean" ip from db to fet base object instead of final instance
     # (cloud host in this case)
     self.cloud_ip = IPAddress.objects.get(address=self.cloud_ip.address)
     self.dc_asset_ip = IPAddressFactory(
         ethernet__base_object=DataCenterAssetFactory())
     self.dc_asset_ip = IPAddress.objects.get(
         address=self.dc_asset_ip.address)
Esempio n. 13
0
    def test_match_cloud_hosts_asset_not_found(self):
        asset_model = DataCenterAssetModelFactory()
        DataCenterAsset.objects.create(hostname='hostname-1',
                                       model=asset_model,
                                       sn='FOO')

        host = CloudHostFactory(host_id='buz')
        node = FakeIronicNode(serial_number='BAR', instance_uuid=host.host_id)
        self.cmd._match_nodes_to_hosts([node])

        updated_host = CloudHost.objects.get(pk=host.pk)
        self.assertIsNone(updated_host.hypervisor)
Esempio n. 14
0
    def test_cleanup_doesnt_remove_cloud_projects_with_children(self):
        project = CloudProjectFactory(project_id='im_not_here')
        host = CloudHostFactory(host_id='host_id123',
                                parent=project,
                                cloudflavor=self.cloud_flavor[1])
        self.cmd._get_ralph_data()

        self.cmd._cleanup()

        try:
            CloudProject.objects.get(project_id='im_not_here')
        except ObjectDoesNotExist:
            self.fail('Project "im_not_here" was deleted.')
Esempio n. 15
0
    def test_should_add_cloud_hosts_to_dictionary(self):
        cloud_host = ContentType.objects.get_for_model(CloudHost)
        self.view.object.cloudhost_set.add(*CloudHostFactory.create_batch(4))
        related_objects = {}
        self.view._add_cloud_hosts(related_objects)
        content_type = {
            c_t.content_type
            for c_t in related_objects['cloud_hosts']
        }

        self.assertEqual(4, len(related_objects['cloud_hosts']))
        self.assertEqual(1, len(content_type))
        self.assertEqual(cloud_host, content_type.pop())
Esempio n. 16
0
    def test_used_cloud_flavor_can_be_deleted_with_force(self):
        # given
        cloud_flavor = CloudFlavorFactory()
        CloudHostFactory(cloudflavor=cloud_flavor)

        # when
        url = reverse('cloudflavor-detail', args=(cloud_flavor.pk, ))
        data = {'force': True}
        resp = self.client.delete(url, data=data)

        # then
        self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT)
        self.assertRaises(CloudFlavor.DoesNotExist,
                          cloud_flavor.refresh_from_db)
Esempio n. 17
0
    def test_delete_cloud_flavor_returns_409_if_is_used_by_cloud_hosts(self):
        # given
        cloud_flavor = CloudFlavorFactory()
        CloudHostFactory(cloudflavor=cloud_flavor)

        # when
        url = reverse('cloudflavor-detail', args=(cloud_flavor.pk, ))
        resp = self.client.delete(url)

        # then
        self.assertEqual(resp.status_code, status.HTTP_409_CONFLICT)
        self.assertIn('Cloud flavor is in use and hence is not deletable.',
                      resp.data['detail'])
        self.assertTrue(
            CloudFlavor.objects.filter(pk=cloud_flavor.pk).exists())
Esempio n. 18
0
    def test_match_cloud_hosts_asset_duplicate_sn(self):
        asset_model = DataCenterAssetModelFactory()
        assets = [
            DataCenterAsset.objects.create(hostname='hostname-{}'.format(i),
                                           model=asset_model,
                                           sn=None) for i in range(2)
        ]

        host = CloudHostFactory(host_id='bar')
        node = FakeIronicNode(serial_number=assets[0].sn,
                              instance_uuid=host.host_id)

        self.cmd._match_nodes_to_hosts([node])

        updated_host = CloudHost.objects.get(pk=host.pk)
        self.assertIsNone(updated_host.hypervisor)
Esempio n. 19
0
 def setUp(self):
     super().setUp()
     self.hypervisor = DataCenterAssetFactory()
     self.cloud_hypervisor = CloudHostFactory()
     self.cluster = ClusterFactory()
     self.type = VirtualServerType.objects.create(name='XEN')
     self.virtual_server = VirtualServerFullFactory(
         service_env__environment__name='some_env', )
     self.virtual_server.parent.service_env.service.uid = 's-12345'
     self.virtual_server.parent.service_env.service.save()
     self.virtual_server.service_env.service.business_owners = [self.user1]
     self.virtual_server.service_env.service.technical_owners = [self.user2]
     self.virtual_server.service_env.save()
     self.virtual_server2 = VirtualServerFullFactory()
     self.ip = IPAddressFactory(ethernet=EthernetFactory(
         base_object=self.virtual_server2))
Esempio n. 20
0
    def test_cleanup_doesnt_remove_cloud_flavours_with_assignments(self):
        flavor = CloudFlavorFactory(flavor_id='im_not_here',
                                    name='im_not_here')
        CloudHostFactory(host_id='host_id123', cloudflavor=flavor)
        openstack_flavors = copy(OPENSTACK_FLAVORS)
        openstack_flavors.update({flavor.flavor_id: {"name": flavor.name}})
        ralph_projects = self.ralph_client.get_ralph_servers_data(
            self.ralph_client.get_ralph_projects())
        ralph_flavors = self.ralph_client.get_ralph_flavors()
        servers_to_delete = self.ralph_client.calculate_servers_to_delete(
            OPENSTACK_DATA, ralph_projects, incremental=True)
        self.ralph_client.perform_delete(OPENSTACK_DATA, OPENSTACK_FLAVORS,
                                         ralph_projects, ralph_flavors,
                                         servers_to_delete)

        try:
            CloudFlavor.objects.get(flavor_id='im_not_here')
        except ObjectDoesNotExist:
            self.fail('Flavor "im_not_here" was deleted.')
Esempio n. 21
0
    def setUp(self):
        self.envs = EnvironmentFactory.create_batch(2)
        self.services = ServiceFactory.create_batch(2)
        self.service_env = []
        for i in range(0, 2):
            self.service_env.append(
                ServiceEnvironment.objects.create(service=self.services[i],
                                                  environment=self.envs[i]))

        self.cloud_provider = CloudProviderFactory(name='openstack')
        self.cloud_flavor = CloudFlavorFactory()
        self.cloud_project = CloudProjectFactory()
        self.cloud_host = CloudHostFactory(parent=self.cloud_project)

        self.test_cpu = ComponentModel.objects.create(
            name='vcpu1',
            cores=4,
            family='vCPU',
            type=ComponentType.processor,
        )
        self.test_mem = ComponentModel.objects.create(
            name='1024 MiB vMEM',
            size='1024',
            type=ComponentType.memory,
        )
        self.test_disk = ComponentModel.objects.create(
            name='4 GiB vDISK',
            size='4096',
            type=ComponentType.disk,
        )

        VirtualComponent.objects.create(
            base_object=self.cloud_flavor,
            model=self.test_cpu,
        )
        VirtualComponent.objects.create(
            base_object=self.cloud_flavor,
            model=self.test_mem,
        )
        VirtualComponent.objects.create(
            base_object=self.cloud_flavor,
            model=self.test_disk,
        )
Esempio n. 22
0
 def test_get_data_center_asset_details_related_hosts(self):
     dc_asset_3 = DataCenterAssetFullFactory()
     cloud_host = CloudHostFactory(
         hypervisor=dc_asset_3
     )
     virtual_server = VirtualServerFactory(
         parent=dc_asset_3
     )
     virtual_server_2 = VirtualServerFactory(
         parent=dc_asset_3,
         hostname='random_test_hostname'
     )
     dc_asset_4 = DataCenterAssetFullFactory(
         parent=dc_asset_3
     )
     url = reverse('datacenterasset-detail', args=(dc_asset_3.id,))
     response = self.client.get(url, format='json')
     self.assertEqual(
         len(response.data['related_hosts']['cloud_hosts']), 1
     )
     self.assertEqual(
         len(response.data['related_hosts']['virtual_servers']), 2
     )
     self.assertIn(
         response.data['related_hosts']['virtual_servers'][0]['hostname'],
         (virtual_server.hostname, virtual_server_2.hostname)
     )
     self.assertIn(
         response.data['related_hosts']['virtual_servers'][1]['hostname'],
         (virtual_server.hostname, virtual_server_2.hostname)
     )
     self.assertEqual(
         response.data['related_hosts']['cloud_hosts'][0]['hostname'],
         cloud_host.hostname
     )
     self.assertEqual(
         len(response.data['related_hosts']['physical_servers']), 1
     )
     self.assertEqual(
         response.data['related_hosts']['physical_servers'][0]['hostname'],
         dc_asset_4.hostname
     )
Esempio n. 23
0
    def test_cleanup_doesnt_remove_cloud_projects_with_children(self):
        project = CloudProjectFactory(project_id='im_not_here')
        CloudHostFactory(host_id='host_id123',
                         parent=project,
                         cloudflavor=self.cloud_flavor[1])
        ralph_projects = self.ralph_client.get_ralph_servers_data(
            self.ralph_client.get_ralph_projects())
        ralph_flavors = self.ralph_client.get_ralph_flavors()
        servers_to_delete = self.ralph_client.calculate_servers_to_delete(
            OPENSTACK_DATA,
            ralph_projects,
        )
        self.ralph_client.perform_delete(OPENSTACK_DATA, OPENSTACK_FLAVORS,
                                         ralph_projects, ralph_flavors,
                                         servers_to_delete)

        try:
            CloudProject.objects.get(project_id='im_not_here')
        except ObjectDoesNotExist:
            self.fail('Project "im_not_here" was deleted.')
Esempio n. 24
0
 def test_check_ralph_delete_incremental(self):
     # Create server to be deleted in Ralph
     host_to_delete = CloudHostFactory(host_id='deleted',
                                       parent=self.cloud_project_3,
                                       cloudflavor=self.cloud_flavor[1])
     ralph_projects = self.ralph_client.get_ralph_servers_data(
         self.ralph_client.get_ralph_projects())
     ralph_flavors = self.ralph_client.get_ralph_flavors()
     servers_to_delete = self.ralph_client.calculate_servers_to_delete(
         OPENSTACK_DATA, ralph_projects, incremental=True)
     self.ralph_client.perform_delete(OPENSTACK_DATA, OPENSTACK_FLAVORS,
                                      ralph_projects, ralph_flavors,
                                      servers_to_delete)
     self.assertRaises(ObjectDoesNotExist,
                       CloudHost.objects.get,
                       host_id=host_to_delete.host_id)
     try:
         CloudHost.objects.get(host_id=self.host.host_id)
     except ObjectDoesNotExist:
         self.fail("Removed host that should have been kept.")
Esempio n. 25
0
 def test_service_env_inheritance_on_host_creation(self):
     self.cloud_project.service_env = self.service_env[1]
     self.cloud_project.save()
     new_host = CloudHostFactory(host_id="new_host_id",
                                 parent=self.cloud_project)
     self.assertEqual(new_host.service_env, self.service_env[1])