Esempio n. 1
0
    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': []
            },
        ]
Esempio n. 2
0
 def test_start_backup(self, mocked_task):
     backup = factories.BackupFactory()
     backend = backup.get_backend()
     backend.start_backup()
     self.assert_task_called(mocked_task,
                             'nodeconductor.openstack.backup_start_create',
                             backup.uuid.hex)
Esempio n. 3
0
 def setUp(self):
     super(BackupPermissionsTest, 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.backup = factories.BackupFactory(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 test_backup_schedule_can_not_be_created_with_wrong_source(self):
     # wrong backup source:
     self.backup_schedule_data['schedule'] = '*/5 * * * *'
     backup = factories.BackupFactory()
     unbackupable_url = factories.BackupFactory.get_url(backup)
     self.backup_schedule_data['instance'] = unbackupable_url
     response = self.client.post(self.url, self.backup_schedule_data)
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
     self.assertIn('instance', response.content)
Esempio n. 5
0
    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)
Esempio n. 6
0
    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'])
Esempio n. 7
0
    def test_execute(self):
        # we have schedule
        schedule = factories.BackupScheduleFactory(maximal_number_of_backups=1,
                                                   instance=self.instance)
        # with 2 ready backups
        old_backup1 = factories.BackupFactory(backup_schedule=schedule)
        old_backup2 = factories.BackupFactory(backup_schedule=schedule)
        # and 1 deleted
        deleted_backup = factories.BackupFactory(
            backup_schedule=schedule, state=models.Backup.States.DELETED)

        with patch('celery.app.base.Celery.send_task') as mocked_task:
            backend = schedule.get_backend()
            backend.execute()

            new_backup = models.Backup.objects.filter(
                backup_schedule=schedule,
                state=models.Backup.States.READY).order_by(
                    'created_at').last()

            # after execution old backups have to be deleted
            # new backup have to be created
            mocked_task.assert_has_calls([
                call('nodeconductor.openstack.backup_start_create',
                     (new_backup.uuid.hex, ), {},
                     countdown=2),
                call('nodeconductor.openstack.backup_start_delete',
                     (old_backup1.uuid.hex, ), {},
                     countdown=2),
                call('nodeconductor.openstack.backup_start_delete',
                     (old_backup2.uuid.hex, ), {},
                     countdown=2),
            ],
                                         any_order=True)

        # deleted backup have to stay deleted
        self.assertEqual(deleted_backup.state, models.Backup.States.DELETED)
        # and schedule time have to be changed
        self.assertGreater(schedule.next_trigger_at, timezone.now())
Esempio n. 8
0
 def test_backup_restore(self):
     backup = factories.BackupFactory()
     url = factories.BackupFactory.get_url(backup, action='restore')
     settings = backup.instance.service_project_link.service.settings
     user_input = {
         'flavor':
         factories.FlavorFactory.get_url(
             factories.FlavorFactory(settings=settings)),
         'image':
         factories.ImageFactory.get_url(
             factories.ImageFactory(settings=settings)),
     }
     response = self.client.post(url, data=user_input)
     self.assertEqual(response.status_code, status.HTTP_200_OK,
                      response.data)
Esempio n. 9
0
 def setUp(self):
     self.expired_backup1 = factories.BackupFactory(
         kept_until=timezone.now() - timedelta(minutes=1))
     self.expired_backup2 = factories.BackupFactory(
         kept_until=timezone.now() - timedelta(minutes=10))
Esempio n. 10
0
 def test_backup_delete(self):
     backup = factories.BackupFactory()
     url = factories.BackupFactory.get_url(backup, action='delete')
     response = self.client.post(url)
     self.assertEqual(response.status_code, status.HTTP_200_OK)