def test_delete_service_when_service_assigned_to_object(self, mock_logger): service = ServiceFactory(active=True) service_env = ServiceEnvironmentFactory(service=service) DataCenterAssetFactory(service_env=service_env) data = { 'uid': service.uid, 'name': 'Service name', 'status': 'Inactive', 'isActive': False, 'environments': ['dev'], 'businessOwners': [{'username': '******'}], 'technicalOwners': [{'username': '******'}] } response = self._make_request( data, settings.HERMES_SERVICE_TOPICS['DELETE'] ) self.assertEqual(response.status_code, 204) mock_logger.error.assert_called_with( 'Can not delete service - it has assigned some base objects', extra={ 'action_type': ACTION_TYPE, 'service_uid': data['uid'], 'service_name': data['name'] } ) service.refresh_from_db() self.assertTrue(service.active)
def test_if_mail_notification_is_send_when_dca_is_updated_through_gui( self): old_service = ServiceFactory(name='test') new_service = ServiceFactory(name='prod') old_service.business_owners.add(UserFactory(email='*****@*****.**')) new_service.business_owners.add(UserFactory(email='*****@*****.**')) old_service_env = ServiceEnvironmentFactory(service=old_service) new_service_env = ServiceEnvironmentFactory(service=new_service) # update without triggering signals DataCenterAsset.objects.filter(pk=self.dca.pk).update( service_env=old_service_env) data_custom_fields = { 'TOTAL_FORMS': 3, 'INITIAL_FORMS': 0, } self._update_dca(dca_data={'service_env': new_service_env.id}, inline_data=data_custom_fields) self.dca.refresh_from_db() self.assertEqual(len(mail.outbox), 1) self.assertEqual( 'Device has been assigned to Service: {} ({})'.format( new_service, self.dca), mail.outbox[0].subject) self.assertCountEqual(mail.outbox[0].to, ['*****@*****.**', '*****@*****.**'])
def setUp(self): super().setUp() self.envs = EnvironmentFactory.create_batch(2) self.services = ServiceFactory.create_batch(2) ServiceEnvironment.objects.create(service=self.services[0], environment=self.envs[0]) self.team = TeamFactory() self.profit_center = ProfitCenterFactory()
def test_delete_with_valid_event_data(self): service = ServiceFactory(active=True) data = { 'uid': service.uid, 'name': 'Service name', 'status': 'Inactive', 'isActive': False, 'environments': ['dev'], 'businessOwners': [{'username': '******'}], 'technicalOwners': [{'username': '******'}] } response = self._make_request( data, settings.HERMES_SERVICE_TOPICS['DELETE'] ) self.assertEqual(response.status_code, 204) service.refresh_from_db() self.assertFalse(service.active)
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')
def test_if_notification_is_send_when_data_center_asset_is_saved(self): old_service = ServiceFactory(name='test') new_service = ServiceFactory(name='prod') old_service.business_owners.add(UserFactory(email='*****@*****.**')) new_service.business_owners.add(UserFactory(email='*****@*****.**')) self.dca = DataCenterAssetFactory( service_env=ServiceEnvironmentFactory(service=old_service)) # fetch DCA to start with clean state in post_commit signals # (ex. invalidate call to notification handler during creating of DCA) self.dca = DataCenterAsset.objects.get(pk=self.dca.pk) self.dca.service_env = ServiceEnvironmentFactory(service=new_service) with transaction.atomic(): self.dca.save() self.assertEqual(len(mail.outbox), 1) self.assertEqual( 'Device has been assigned to Service: {} ({})'.format( new_service, self.dca), mail.outbox[0].subject) self.assertCountEqual(mail.outbox[0].to, ['*****@*****.**', '*****@*****.**'])
def test_update_service_when_service_exist(self): service = ServiceFactory( business_segment__name='existing area', profit_center__name='existing PC', ) service.business_owners.add(UserFactory(username='******')) service.technical_owners.add(UserFactory(username='******')) ServiceEnvironmentFactory( service=service, environment__name='prod' ) data = { 'uid': service.uid, 'name': 'New name', 'status': 'Active', 'isActive': True, 'environments': ['dev'], 'businessOwners': [{'username': '******'}], 'technicalOwners': [{'username': '******'}], 'area': {'name': 'new area', 'profitCenter': 'new-PC'}, } response = self._make_request( data, settings.HERMES_SERVICE_TOPICS['UPDATE'] ) self.assertEqual(response.status_code, 204) service.refresh_from_db() self.assertEqual(service.name, 'New name') self.assertEqual(service.business_segment.name, 'new area') self.assertEqual(service.profit_center.name, 'new-PC') self.assertCountEqual( ['dev'], [env.name for env in service.environments.all()] ) self.assertCountEqual( ['business_user3'], [user.username for user in service.business_owners.all()] ) self.assertCountEqual( ['technical_user3'], [user.username for user in service.technical_owners.all()] )
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, )
def setUp(self): get_user_model().objects.create_superuser( 'test', '*****@*****.**', 'test' ) self.client = APIClient() self.client.login(username='******', password='******') environment = EnvironmentFactory() service = ServiceFactory(name='Service1') service_env = ServiceEnvironment.objects.create( service=service, environment=environment ) asset_model = DataCenterAssetModelFactory( type=ObjectModelType.data_center ) self.server_room = ServerRoomFactory() self.accesory_1 = AccessoryFactory() self.rack_1 = RackFactory( server_room=self.server_room, max_u_height=3 ) self.asset_1 = DataCenterAssetFactory( service_env=service_env, position=1, slot_no='', force_depreciation=False, model=asset_model, rack=self.rack_1, ) self.asset_1.management_ip = '10.15.25.45' self.pdu_1 = DataCenterAssetFactory( service_env=service_env, rack=self.rack_1, orientation=Orientation.left, force_depreciation=False, model=asset_model, position=0, ) self.rack1_accessory = RackAccessoryFactory( rack=self.rack_1, orientation=Orientation.front, accessory=self.accesory_1, position=1 )
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, )
def setUp(self): super().setUp() self.services = ServiceFactory.create_batch(2, active=True) self.inactive_services = ServiceFactory.create_batch(1, active=False)
def test_if_host_update_is_published_to_hermes_when_dca_is_updated_through_gui( # noqa: E501 self, publish_mock): self.cfv1 = CustomFieldValue.objects.create( object=self.dca, custom_field=self.custom_field_str, value='sample_value', ) new_service = ServiceFactory(name='service1', uid='sc-44444') new_service_env = ServiceEnvironmentFactory(service=new_service, environment__name='dev') data_custom_fields = { 'TOTAL_FORMS': 3, 'INITIAL_FORMS': 1, '0-id': self.cfv1.id, '0-custom_field': self.custom_field_str.id, '0-value': 'sample_value22', '1-id': '', '1-custom_field': self.custom_field_choices.id, '1-value': 'qwerty', } with transaction.atomic(): self._update_dca(dca_data={ 'service_env': new_service_env.id, 'hostname': 'my-host.mydc.net', }, inline_data=data_custom_fields) # DCA is saved twice self.assertGreater(len(connection.run_on_commit), 0) self.dca.refresh_from_db() publish_data = publish_mock.call_args[0][1] publish_data.pop('modified') publish_data.pop('created') self.assertEqual( publish_data, { '__str__': 'data center asset: ' + str(self.dca), 'configuration_path': None, 'configuration_variables': { 'test_choice': 'qwerty', }, 'custom_fields': { 'test_str': 'sample_value22', 'test_choice': 'qwerty' }, 'ethernet': [], 'hostname': 'my-host.mydc.net', 'id': self.dca.id, 'ipaddresses': [], 'object_type': 'datacenterasset', 'parent': None, 'remarks': '', 'service_env': { 'id': new_service_env.id, 'service': 'service1', 'environment': 'dev', 'service_uid': 'sc-44444', 'ui_url': '' }, 'tags': [], 'securityscan': None, '_previous_state': { 'hostname': 'ralph1.allegro.pl' }, }) # Despite `save` is called twice, publish update data is called only # once self.assertEqual(publish_mock.call_count, 1) # check if on_commit callbacks are removed from current db connections self.assertEqual(connection.run_on_commit, [])