def get_users_and_expected_results(self): models.Backup.objects.all().delete() instance = factories.InstanceFactory() backup1 = factories.BackupFactory(instance=instance) backup2 = factories.BackupFactory(instance=instance) # deleted backup should not be visible even for user with permissions factories.BackupFactory(instance=instance, state=models.Backup.States.DELETED) user_with_view_permission = structure_factories.UserFactory.create( is_staff=True, is_superuser=True) user_without_view_permission = structure_factories.UserFactory.create() return [ { 'user': user_with_view_permission, 'expected_results': [{ 'url': factories.BackupFactory.get_url(backup1) }, { 'url': factories.BackupFactory.get_url(backup2) }] }, { 'user': user_without_view_permission, 'expected_results': [] }, ]
def test_backup_schedule_for_unstable_source_should_not_start(self): backupable = factories.InstanceFactory( state=models.Instance.States.ERRED) schedule = factories.BackupScheduleFactory(instance=backupable) backup = schedule.get_backend() self.assertEqual(backup.check_instance_state(), False) self.assertEqual(backup.create_backup(), None)
def _instance_data(user, instance=None): if instance is None: instance = factories.InstanceFactory() factories.FloatingIPFactory( service_project_link=instance.service_project_link, status='DOWN') image = factories.ImageFactory( settings=instance.service_project_link.service.settings) flavor = factories.FlavorFactory( settings=instance.service_project_link.service.settings) ssh_public_key = structure_factories.SshPublicKeyFactory(user=user) return { 'name': 'test_host', 'description': 'test description', 'flavor': factories.FlavorFactory.get_url(flavor), 'image': factories.ImageFactory.get_url(image), 'service_project_link': factories.OpenStackServiceProjectLinkFactory.get_url( instance.service_project_link), 'ssh_public_key': structure_factories.SshPublicKeyFactory.get_url(ssh_public_key), 'system_volume_size': image.min_disk }
def setUp(self): super(BackupSchedulePermissionsTest, self).setUp() # objects self.customer = structure_factories.CustomerFactory() self.project = structure_factories.ProjectFactory( customer=self.customer) self.project_group = structure_factories.ProjectGroupFactory( customer=self.customer) self.project_group.projects.add(self.project) self.service = factories.OpenStackServiceFactory( customer=self.customer) self.spl = factories.OpenStackServiceProjectLinkFactory( service=self.service, project=self.project) self.instance = factories.InstanceFactory( service_project_link=self.spl) self.schedule = factories.BackupScheduleFactory(instance=self.instance) # users self.staff = structure_factories.UserFactory(username='******', is_staff=True) self.regular_user = structure_factories.UserFactory( username='******') self.project_admin = structure_factories.UserFactory(username='******') self.project.add_user(self.project_admin, structure_models.ProjectRole.ADMINISTRATOR) self.customer_owner = structure_factories.UserFactory(username='******') self.customer.add_user(self.customer_owner, structure_models.CustomerRole.OWNER) self.project_group_manager = structure_factories.UserFactory( username='******') self.project_group.add_user(self.project_group_manager, structure_models.ProjectGroupRole.MANAGER)
def setUp(self): self.customer = structure_factories.CustomerFactory() self.project = structure_factories.ProjectFactory(customer=self.customer) self.spl1 = openstack_factories.OpenStackServiceProjectLinkFactory(project=self.project) self.spl2 = openstack_factories.OpenStackServiceProjectLinkFactory(project=self.project) two_months_ago = timezone.now() - relativedelta(months=+2) self.instance1 = openstack_factories.InstanceFactory(service_project_link=self.spl1, state=structure_models.Resource.States.ONLINE, created=two_months_ago) self.instance2 = openstack_factories.InstanceFactory(service_project_link=self.spl2, state=structure_models.Resource.States.ONLINE, created=two_months_ago) # mock estimate calculation task for tests: self.INSTANCE_MONTHLY_COST = 10 OpenStackCostTrackingBackend.get_monthly_cost_estimate = classmethod(lambda c, i: self.INSTANCE_MONTHLY_COST)
def test_user_cannot_backup_unstable_instance(self): backupable = factories.InstanceFactory( state=models.Instance.States.RESIZING) backup_data = { 'instance': factories.InstanceFactory.get_url(backupable), } url = factories.BackupFactory.get_list_url() response = self.client.post(url, data=backup_data) self.assertEqual(response.status_code, status.HTTP_409_CONFLICT) self.assertEqual(response.data['detail'], 'Instance should be in stable state.')
def test_filter_backup_by_scope(self): user = structure_factories.UserFactory.create(is_staff=True) instance1 = factories.InstanceFactory() factories.BackupFactory(instance=instance1) factories.BackupFactory(instance=instance1) instance2 = factories.InstanceFactory() factories.BackupFactory(instance=instance2) self.client.force_authenticate(user=user) response = self.client.get(factories.BackupFactory.get_list_url()) self.assertEqual(3, len(response.data)) response = self.client.get(factories.BackupFactory.get_list_url(), data={'instance': instance1.uuid.hex}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(2, len(response.data)) self.assertEqual(factories.InstanceFactory.get_url(instance1), response.data[0]['instance'])
def setUp(self): self.url = factories.BackupScheduleFactory.get_list_url() self.user = structure_factories.UserFactory.create(is_staff=True) self.client.force_authenticate(user=self.user) backupable = factories.InstanceFactory() self.backup_schedule_data = { 'retention_time': 3, 'instance': factories.InstanceFactory.get_url(backupable), 'schedule': '*/5 * * * *', 'maximal_number_of_backups': 3, }
def test_start_restoration(self, mocked_task): backup = factories.BackupFactory() instance = factories.InstanceFactory() user_input = {} snapshot_ids = [] backend = backup.get_backend() backend.start_restoration(instance.uuid.hex, user_input, snapshot_ids) self.assert_task_called( mocked_task, 'nodeconductor.openstack.backup_start_restore', backup.uuid.hex, instance.uuid.hex, user_input, snapshot_ids)
def test_backup_schedule_creation_with_default_timezone(self): backupable = factories.InstanceFactory() backup_schedule_data = { 'retention_time': 3, 'instance': factories.InstanceFactory.get_url(backupable), 'schedule': '*/5 * * * *', 'maximal_number_of_backups': 3, } response = self.client.post(self.url, backup_schedule_data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data['timezone'], settings.TIME_ZONE)
def test_ancestor_price_estimate_will_be_decreased_after_descendor_deletion(self): instance = openstack_factories.InstanceFactory() total = 77 month = 10 year = 2015 models.PriceEstimate.update_price_for_scope(instance, month, year, total) instance.delete() project = instance.service_project_link.project self.assertEqual(models.PriceEstimate.objects.get(scope=project, month=month, year=year).total, 0) self.assertEqual(models.PriceEstimate.objects.get(scope=project.customer, month=month, year=year).total, 0)
def test_backup_schedule_creation_with_incorrect_timezone(self): backupable = factories.InstanceFactory() backup_schedule_data = { 'retention_time': 3, 'instance': factories.InstanceFactory.get_url(backupable), 'schedule': '*/5 * * * *', 'timezone': 'incorrect', 'maximal_number_of_backups': 3, } response = self.client.post(self.url, backup_schedule_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertIn('timezone', response.data)
def setUp(self): self.not_active_schedule = factories.BackupScheduleFactory( is_active=False) backupable = factories.InstanceFactory(state=Instance.States.OFFLINE) self.schedule_for_execution = factories.BackupScheduleFactory( instance=backupable) self.schedule_for_execution.next_trigger_at = timezone.now( ) - timedelta(minutes=10) self.schedule_for_execution.save() self.future_schedule = factories.BackupScheduleFactory() self.future_schedule.next_trigger_at = timezone.now() + timedelta( minutes=2) self.future_schedule.save()
def setUp(self): self.user = structure_factories.UserFactory.create() self.client.force_authenticate(self.user) self.instance = factories.InstanceFactory( state=models.Instance.States.OFFLINE) membership = self.instance.service_project_link membership.project.add_user(self.user, structure_models.ProjectRole.ADMINISTRATOR) self.instance_security_groups = factories.InstanceSecurityGroupFactory.create_batch( 2, instance=self.instance) self.service_security_groups = [ g.security_group for g in self.instance_security_groups ] for security_group in self.service_security_groups: security_group.service_project_link = membership security_group.save()
def test_backup_manually_create(self): # success: backupable = factories.InstanceFactory( state=models.Instance.States.OFFLINE) backup_data = { 'instance': factories.InstanceFactory.get_url(backupable), } url = factories.BackupFactory.get_list_url() response = self.client.post(url, data=backup_data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) models.Backup.objects.get(instance_id=backupable.id) # fail: backup_data = { 'instance': 'some_random_url', } response = self.client.post(url, data=backup_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertIn('instance', response.content)
def get_users_and_expected_results(self): instance = factories.InstanceFactory() schedule = factories.BackupScheduleFactory(instance=instance) user_with_view_permission = structure_factories.UserFactory.create( is_staff=True, is_superuser=True) user_without_view_permission = structure_factories.UserFactory.create() return [ { 'user': user_with_view_permission, 'expected_results': [{ 'url': backup_schedule_url(schedule) }] }, { 'user': user_without_view_permission, 'expected_results': [] }, ]
def setUp(self): self.instance = factories.InstanceFactory( state=models.Instance.States.OFFLINE)
def setUp(self): self.instance = factories.InstanceFactory() self.error = OpenStackBackendError('Unable to find network')