def setUp(self):
        self.anonymous_user = AnonymousUserFactory()
        self.user = UserFactory.create(username='******')
        self.provider = ProviderFactory.create(type__name='mock')
        self.user_identity = IdentityFactory.create_identity(
            created_by=self.user, provider=self.provider)
        self.machine = ProviderMachineFactory.create_provider_machine(
            self.user, self.user_identity)
        start_date = timezone.now()
        self.active_instance = InstanceFactory.create(
            name="Instance in active",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.user,
            created_by_identity=self.user_identity,
            start_date=start_date)
        self.status_active = InstanceStatusFactory.create(name='active')
        delta_time = timezone.timedelta(minutes=2)
        InstanceHistoryFactory.create(status=self.status_active,
                                      activity="",
                                      instance=self.active_instance,
                                      start_date=start_date + delta_time * 3)

        self.view = InstanceViewSet.as_view({'post': 'actions'})
        self.url = reverse('api:v2:instance-list')
        self.url += "/" + str(self.active_instance.provider_alias) + "/actions"
        self.mock_driver = get_esh_driver(self.user_identity)
        self.mock_driver.add_core_instance(self.active_instance)

        start_date_second = timezone.now()
        self.active_instance_second = InstanceFactory.create(
            name="Instance in active",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.user,
            created_by_identity=self.user_identity,
            start_date=start_date_second)
        self.status_active_second = InstanceStatusFactory.create(name='active')
        delta_time = timezone.timedelta(minutes=2)
        self.size_small = SizeFactory.create(provider=self.provider,
                                             cpu=2,
                                             disk=20,
                                             root=0,
                                             mem=128)
        self.size_large = SizeFactory.create(provider=self.provider,
                                             cpu=4,
                                             disk=40,
                                             root=0,
                                             mem=256)
        InstanceHistoryFactory.create(status=self.status_active_second,
                                      size=self.size_small,
                                      activity="",
                                      instance=self.active_instance_second,
                                      start_date=start_date_second +
                                      delta_time * 3)
        self.mock_driver_second = get_esh_driver(self.user_identity)
        self.mock_driver.add_core_instance(self.active_instance_second)
Пример #2
0
 def setUp(self):
     self.anonymous_user = AnonymousUserFactory()
     self.user = UserFactory.create(username='******')
     self.provider = ProviderFactory.create()
     self.user_identity = IdentityFactory.create_identity(
         created_by=self.user,
         provider=self.provider)
     self.group = Group.objects.get(name=self.user)
     self.project = ProjectFactory.create(owner=self.group)
     self.leadership = LeadershipFactory.create(
         user=self.user,
         group=self.group
     )
     self.machine = ProviderMachineFactory.create_provider_machine(self.user, self.user_identity)
     self.active_instance = InstanceFactory.create(name="Instance in active",
                                provider_alias=uuid.uuid4(),
                                source=self.machine.instance_source,
                                created_by=self.user,
                                created_by_identity=self.user_identity,
                                start_date=timezone.now())
     self.project_instances = ProjectInstanceFactory.create(project=self.project, instance=self.active_instance)
     self.view = ProjectInstanceViewSet.as_view({'get': 'list'})
     factory = APIRequestFactory()
     url = reverse('api:v2:projectinstance-list')
     url_proj = "?project__id=" + str(self.project.id)
     url += url_proj
     self.request = factory.get(url)
Пример #3
0
def create_an_instance(user=None, ip_address=None):
    # This method should be replaced when the InstanceFactory can create an
    # instance w/o requiring any arguments
    import uuid
    from api.tests.factories import (
        UserFactory, InstanceFactory, ProviderMachineFactory, IdentityFactory,
        ProviderFactory)
    from django.utils import timezone

    if not user:
        user = UserFactory.create()
    staff_user = UserFactory.create(is_staff=True, is_superuser=True)
    provider = ProviderFactory.create()
    user_identity = IdentityFactory.create_identity(
        created_by=user,
        provider=provider)
    staff_user_identity = IdentityFactory.create_identity(
        created_by=staff_user,
        provider=provider)
    machine = ProviderMachineFactory.create_provider_machine(staff_user, staff_user_identity)
    start_date = timezone.now()
    return InstanceFactory.create(
        name="",
        provider_alias=uuid.uuid4(),
        source=machine.instance_source,
        ip_address=ip_address,
        created_by=user,
        created_by_identity=user_identity,
        start_date=start_date)
Пример #4
0
    def test_image_and_instance_scripts_are_included(self):
        user = UserFactory.create()
        instance = InstanceFactory.create(created_by=user)

        # Create/add instance script
        instance_script = BootScriptRawTextFactory.create(created_by=user,
                                                          wait_for_deploy=True)
        instance.scripts.add(instance_script)

        # Create/add image script
        image_script = BootScriptRawTextFactory.create(created_by=user,
                                                       wait_for_deploy=True)
        application_version = instance.source.providermachine.application_version
        application_version.boot_scripts.add(image_script)

        # Mock out ansible_deployment to verify its called with both image and
        # instance scripts
        with mock.patch(
                'service.deploy.ansible_deployment') as ansible_deployment:
            from service.deploy import user_deploy
            user_deploy(instance.ip_address, user.username,
                        instance.provider_alias)
            kwargs = ansible_deployment.call_args[1]
            script_titles = {
                s['name']
                for s in kwargs['extra_vars']['DEPLOY_SCRIPTS']
            }
            self.assertIn(instance_script.get_title_slug(), script_titles)
            self.assertIn(image_script.get_title_slug(), script_titles)
    def test_image_and_instance_scripts_are_included(self):
        user = UserFactory.create()
        instance = InstanceFactory.create(created_by=user)

        # Create/add instance script
        instance_script = BootScriptRawTextFactory.create(
            created_by=user, wait_for_deploy=True
        )
        instance.scripts.add(instance_script)

        # Create/add image script
        image_script = BootScriptRawTextFactory.create(
            created_by=user, wait_for_deploy=True
        )
        application_version = instance.source.providermachine.application_version
        application_version.boot_scripts.add(image_script)

        # Mock out ansible_deployment to verify its called with both image and
        # instance scripts
        with mock.patch(
            'service.deploy.ansible_deployment'
        ) as ansible_deployment:
            from service.deploy import user_deploy
            user_deploy(
                instance.ip_address, user.username, instance.provider_alias
            )
            kwargs = ansible_deployment.call_args[1]
            script_titles = {
                s['name']
                for s in kwargs['extra_vars']['DEPLOY_SCRIPTS']
            }
            self.assertIn(instance_script.get_title_slug(), script_titles)
            self.assertIn(image_script.get_title_slug(), script_titles)
Пример #6
0
def launch_instance(user, time_created, cpu, before=False):
    # context.user is admin and regular user
    provider = ProviderFactory.create()
    from core.models import IdentityMembership, Identity
    user_group = IdentityMembership.objects.filter(member__name=user.username)
    if not user_group:
        user_identity = IdentityFactory.create_identity(created_by=user,
                                                        provider=provider)
    else:
        user_identity = Identity.objects.all().last()
    admin_identity = user_identity

    provider_machine = ProviderMachine.objects.all()
    if not provider_machine:
        machine = ProviderMachineFactory.create_provider_machine(
            user, user_identity)
    else:
        machine = ProviderMachine.objects.all().last()

    status = InstanceStatusFactory.create(name='active')

    instance_state = InstanceFactory.create(provider_alias=uuid.uuid4(),
                                            source=machine.instance_source,
                                            created_by=user,
                                            created_by_identity=user_identity,
                                            start_date=time_created)

    if not before:
        return launch_instance_history(instance_state, cpu, provider, status,
                                       time_created)

    return instance_state, status
def pre_initializations(context):
    # context.user is admin and regular user
    provider = ProviderFactory.create()
    from core.models import IdentityMembership, Identity, ProviderMachine
    user_group = IdentityMembership.objects.all()
    if not user_group:
        user_identity = IdentityFactory.create_identity(
            created_by=context.user, provider=provider
        )
    else:
        user_identity = Identity.objects.all().last()

    provider_machine = ProviderMachine.objects.all()
    if not provider_machine:
        machine = ProviderMachineFactory.create_provider_machine(
            context.user, user_identity
        )
    else:
        machine = ProviderMachine.objects.all().last()
    context.active_instance = InstanceFactory.create(
        name="Instance in active",
        provider_alias=uuid.uuid4(),
        source=machine.instance_source,
        created_by=context.user,
        created_by_identity=user_identity,
        start_date=timezone.now()
    )

    active = InstanceStatusFactory.create(name='active')
    InstanceHistoryFactory.create(
        status=active, activity="", instance=context.active_instance
    )
Пример #8
0
def pre_initializations(context):
    # context.user is admin and regular user
    provider = ProviderFactory.create()
    from core.models import IdentityMembership, Identity, ProviderMachine
    user_group = IdentityMembership.objects.all()
    if not user_group:
        user_identity = IdentityFactory.create_identity(
            created_by=context.user, provider=provider)
    else:
        user_identity = Identity.objects.all().last()

    provider_machine = ProviderMachine.objects.all()
    if not provider_machine:
        machine = ProviderMachineFactory.create_provider_machine(
            context.user, user_identity)
    else:
        machine = ProviderMachine.objects.all().last()
    context.active_instance = InstanceFactory.create(
        name="Instance in active",
        provider_alias=uuid.uuid4(),
        source=machine.instance_source,
        created_by=context.user,
        created_by_identity=user_identity,
        start_date=timezone.now())

    active = InstanceStatusFactory.create(name='active')
    InstanceHistoryFactory.create(status=active,
                                  activity="",
                                  instance=context.active_instance)
def launch_instance(user, time_created, cpu, before=False):
    # context.user is admin and regular user
    provider = ProviderFactory.create()
    from core.models import IdentityMembership, Identity
    user_group = IdentityMembership.objects.filter(member__name=user.username)
    if not user_group:
        user_identity = IdentityFactory.create_identity(
            created_by=user, provider=provider
        )
    else:
        user_identity = Identity.objects.all().last()
    provider_machine = ProviderMachine.objects.all()
    if not provider_machine:
        machine = ProviderMachineFactory.create_provider_machine(
            user, user_identity
        )
    else:
        machine = ProviderMachine.objects.all().last()

    status = InstanceStatusFactory.create(name='active')

    instance_state = InstanceFactory.create(
        provider_alias=uuid.uuid4(),
        source=machine.instance_source,
        created_by=user,
        created_by_identity=user_identity,
        start_date=time_created
    )

    if not before:
        return launch_instance_history(
            instance_state, cpu, provider, status, time_created
        )

    return instance_state, status
Пример #10
0
def create_an_instance(user=None, ip_address=None):
    # This method should be replaced when the InstanceFactory can create an
    # instance w/o requiring any arguments
    import uuid
    from api.tests.factories import (UserFactory, InstanceFactory,
                                     ProviderMachineFactory, IdentityFactory,
                                     ProviderFactory)
    from django.utils import timezone

    if not user:
        user = UserFactory.create()
    staff_user = UserFactory.create(is_staff=True, is_superuser=True)
    provider = ProviderFactory.create()
    user_identity = IdentityFactory.create_identity(created_by=user,
                                                    provider=provider)
    staff_user_identity = IdentityFactory.create_identity(
        created_by=staff_user, provider=provider)
    machine = ProviderMachineFactory.create_provider_machine(
        staff_user, staff_user_identity)
    start_date = timezone.now()
    return InstanceFactory.create(name="",
                                  provider_alias=uuid.uuid4(),
                                  source=machine.instance_source,
                                  ip_address=ip_address,
                                  created_by=user,
                                  created_by_identity=user_identity,
                                  start_date=start_date)
Пример #11
0
 def test_complex_instance_affect_on_metrics(self):
     """
     If an instance *was* active, then *suspended*, then *never makes it back to active*,
     how should we measure the statistics?
     """
     start_date = timezone.now()
     # Adding two minutes to simulate the passage of time.
     delta_time = timezone.timedelta(minutes=2)
     self.complex_instance = InstanceFactory.create(
         name="Instance went active",
         provider_alias=uuid.uuid4(),
         source=self.machine.instance_source,
         created_by=self.staff_user,
         created_by_identity=self.user_identity,
         start_date=start_date
     )
     # Simulate going to active
     InstanceHistoryFactory.create(
         status=self.status_build,
         activity="",
         instance=self.complex_instance,
         start_date=start_date,
     )
     InstanceHistoryFactory.create(
         status=self.status_networking,
         activity="",
         instance=self.complex_instance,
         start_date=start_date + delta_time,
     )
     InstanceHistoryFactory.create(
         status=self.status_deploying,
         activity="",
         instance=self.complex_instance,
         start_date=start_date + delta_time * 2,
     )
     InstanceHistoryFactory.create(
         status=self.status_active,
         activity="",
         instance=self.complex_instance,
         start_date=start_date + delta_time * 3,
     )
     InstanceHistoryFactory.create(
         status=self.status_suspended,
         activity="",
         instance=self.complex_instance,
         start_date=start_date + delta_time * 4,
     )
     InstanceHistoryFactory.create(
         status=self.status_networking,
         activity="",
         instance=self.complex_instance,
         start_date=start_date + delta_time * 5,
     )
     InstanceHistoryFactory.create(
         status=self.status_deploy_error,
         activity="",
         instance=self.complex_instance,
         start_date=start_date + delta_time * 6,
     )
Пример #12
0
 def setUp(self):
     instance = InstanceFactory.create()
     self.request = APIRequestFactory().post('api:v2:email_instance_report',
                                             {
                                                 'message': 'test message',
                                                 'instance': instance.id
                                             })
     self.request.POST._mutable = True
     self.view = InstanceSupportEmailViewSet.as_view({'post': 'create'})
 def setUp(self):
     instance = InstanceFactory.create()
     self.request = APIRequestFactory().post(
         'api:v2:email_instance_report', {
             'message': 'test message',
             'instance': instance.id
         }
     )
     self.request.POST._mutable = True
     self.view = InstanceSupportEmailViewSet.as_view({'post': 'create'})
def launch_instance(user, time_created, cpu):
    # context.user is admin and regular user
    provider = ProviderFactory.create()
    from core.models import IdentityMembership, Identity
    user_group = IdentityMembership.objects.filter(member__name=user.username)
    if not user_group:
        user_identity = IdentityFactory.create_identity(
            created_by=user, provider=provider
        )
    else:
        user_identity = Identity.objects.all().last()
    provider_machine = ProviderMachine.objects.all()
    if not provider_machine:
        machine = ProviderMachineFactory.create_provider_machine(
            user, user_identity
        )
    else:
        machine = ProviderMachine.objects.all().last()

    status = InstanceStatusFactory.create(name='active')

    instance_state = InstanceFactory.create(
        provider_alias=uuid.uuid4(),
        source=machine.instance_source,
        created_by=user,
        created_by_identity=user_identity,
        start_date=time_created
    )

    size = Size(
        alias=uuid.uuid4(),
        name='small',
        provider=provider,
        cpu=cpu,
        disk=1,
        root=1,
        mem=1
    )
    size.save()
    InstanceHistoryFactory.create(
        status=status,
        activity="",
        instance=instance_state,
        start_date=time_created,
        end_date=time_created + timedelta(minutes=30),
        size=size
    )

    return instance_state.provider_alias
def launch_instance(user, time_created, cpu):
    # context.user is admin and regular user
    provider = ProviderFactory.create()
    from core.models import IdentityMembership, Identity
    user_group = IdentityMembership.objects.filter(member__name=user.username)
    if not user_group:
        user_identity = IdentityFactory.create_identity(created_by=user,
                                                        provider=provider)
    else:
        user_identity = Identity.objects.all().last()
    admin_identity = user_identity

    provider_machine = ProviderMachine.objects.all()
    if not provider_machine:
        machine = ProviderMachineFactory.create_provider_machine(
            user, user_identity)
    else:
        machine = ProviderMachine.objects.all().last()

    status = InstanceStatusFactory.create(name='active')

    instance_state = InstanceFactory.create(provider_alias=uuid.uuid4(),
                                            source=machine.instance_source,
                                            created_by=user,
                                            created_by_identity=user_identity,
                                            start_date=time_created)

    size = Size(alias=uuid.uuid4(),
                name='small',
                provider=provider,
                cpu=cpu,
                disk=1,
                root=1,
                mem=1)
    size.save()
    InstanceHistoryFactory.create(status=status,
                                  activity="",
                                  instance=instance_state,
                                  start_date=time_created,
                                  end_date=time_created +
                                  timedelta(minutes=30),
                                  size=size)

    return instance_state.provider_alias
Пример #16
0
 def setUp(self):
     self.anonymous_user = AnonymousUserFactory()
     self.user = UserFactory.create(username='******')
     self.provider = ProviderFactory.create()
     self.user_identity = IdentityFactory.create_identity(
         created_by=self.user,
         provider=self.provider)
     self.group = Group.objects.get(name=self.user)
     self.project = ProjectFactory.create(owner=self.group)
     self.machine = ProviderMachineFactory.create_provider_machine(self.user, self.user_identity)
     self.active_instance = InstanceFactory.create(name="Instance in active",
                                provider_alias=uuid.uuid4(),
                                source=self.machine.instance_source,
                                created_by=self.user,
                                created_by_identity=self.user_identity,
                                start_date=timezone.now())
     self.view = ProjectInstanceViewSet.as_view({'get': 'list'})
     factory = APIRequestFactory()
     url = reverse('api:v2:projectinstance-list')
     url_proj = "?project__id=" + str(self.project.id)
     url += url_proj
     self.request = factory.get(url)
Пример #17
0
    def setUp(self):
        self.anonymous_user = AnonymousUserFactory()
        self.user = UserFactory.create(username='******')
        self.provider = ProviderFactory.create(type__name='mock')
        self.user_identity = IdentityFactory.create_identity(
            created_by=self.user, provider=self.provider
        )
        self.machine = ProviderMachineFactory.create_provider_machine(
            self.user, self.user_identity
        )
        start_date = timezone.now()
        self.active_instance = InstanceFactory.create(
            name="Instance in active",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.user,
            created_by_identity=self.user_identity,
            start_date=start_date
        )
        self.size_small = SizeFactory.create(
            provider=self.provider, cpu=2, disk=20, root=0, mem=128
        )
        self.status_active = InstanceStatusFactory.create(name='active')
        delta_time = timezone.timedelta(minutes=2)
        InstanceHistoryFactory.create(
            status=self.status_active,
            size=self.size_small,
            activity="",
            instance=self.active_instance,
            start_date=start_date + delta_time * 3
        )

        self.view = InstanceViewSet.as_view({'post': 'actions'})
        self.url = reverse('api:v2:instance-list')
        self.url += "/" + str(self.active_instance.provider_alias) + "/actions"
        self.mock_driver = get_esh_driver(self.user_identity)
        self.mock_driver.add_core_instance(self.active_instance)

        start_date_second = timezone.now()
        self.active_instance_second = InstanceFactory.create(
            name="Instance in active",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.user,
            created_by_identity=self.user_identity,
            start_date=start_date_second
        )
        delta_time = timezone.timedelta(minutes=2)
        self.size_small = SizeFactory.create(
            provider=self.provider, cpu=2, disk=20, root=0, mem=128
        )
        self.size_large = SizeFactory.create(
            provider=self.provider, cpu=4, disk=40, root=0, mem=256
        )
        InstanceHistoryFactory.create(
            status=self.status_active,
            size=self.size_small,
            activity="",
            instance=self.active_instance_second,
            start_date=start_date_second + delta_time * 3
        )
        self.mock_driver.add_core_instance(self.active_instance_second)
        self.allocation_source_1 = AllocationSourceFactory.create(
            name='TEST_INSTANCE_ALLOCATION_SOURCE_01', compute_allowed=1000
        )
        UserAllocationSource.objects.create(
            allocation_source=self.allocation_source_1, user=self.user
        )
        UserAllocationSnapshot.objects.create(
            allocation_source=self.allocation_source_1,
            user=self.user,
            burn_rate=1,
            compute_used=0
        )
        AllocationSourceSnapshot.objects.create(
            allocation_source=self.allocation_source_1,
            compute_used=0,
            compute_allowed=168,
            global_burn_rate=1
        )
        InstanceAllocationSourceSnapshot.objects.update_or_create(
            instance=self.active_instance,
            allocation_source=self.allocation_source_1
        )
Пример #18
0
    def setUp(self):
        self.anonymous_user = AnonymousUserFactory()

        self.user = UserFactory.create()
        self.group = GroupFactory.create(name=self.user.username)
        self.membership = GroupMembershipFactory.create(user=self.user,
                                                        group=self.group,
                                                        is_leader=True)
        self.group.user_set.add(self.user)
        self.project = ProjectFactory.create(owner=self.group,
                                             created_by=self.user)
        self.enddated_instance = InstanceFactory.create(
            created_by=self.user,
            end_date=timezone.now(),
            project=self.project)

        self.user2 = UserFactory.create()
        self.group2 = GroupFactory.create(name=self.user2.username)
        self.membership2 = GroupMembershipFactory.create(user=self.user2,
                                                         group=self.group2,
                                                         is_leader=True)
        self.group2.user_set.add(self.user2)
        self.project2 = ProjectFactory.create(owner=self.group2,
                                              created_by=self.user2)

        self.not_user = UserFactory.create()
        self.not_group = GroupFactory.create(name=self.not_user.username)
        self.not_membership = GroupMembershipFactory.create(
            user=self.not_user, group=self.not_group, is_leader=True)
        self.not_group.user_set.add(self.not_user)
        self.not_project = ProjectFactory.create(owner=self.not_group,
                                                 created_by=self.not_user)

        self.unsaved_project = ProjectFactory.build(owner=self.group,
                                                    created_by=self.user)

        list_url = reverse('api:v2:project-list')
        detail_url = reverse('api:v2:project-detail', args=(self.project.id, ))

        self.create_view = ProjectViewSet.as_view({'post': 'create'})
        self.delete_view = ProjectViewSet.as_view({'delete': 'destroy'})
        self.detail_view = ProjectViewSet.as_view({'get': 'retrieve'})
        self.list_view = ProjectViewSet.as_view({'get': 'list'})
        self.update_view = ProjectViewSet.as_view({'patch': 'partial_update'})

        self.factory = APIRequestFactory()
        self.bad_create_request = self.factory.post(list_url)
        self.create_request = self.factory.post(
            list_url, {
                'name': self.unsaved_project.name,
                'description': self.unsaved_project.description,
                'owner': self.group.name
            })
        self.delete_request = self.factory.delete(detail_url)
        self.detail_request = self.factory.get(detail_url)
        self.list_request = self.factory.get(list_url)
        self.updated_project_data = {
            'name': 'updated name',
            'description': 'updated description'
        }
        self.update_request = self.factory.patch(detail_url,
                                                 self.updated_project_data)
Пример #19
0
    def setUp(self):
        self.anonymous_user = AnonymousUserFactory()
        self.user = UserFactory.create(username='******')
        self.provider = ProviderFactory.create(type__name='mock')
        self.user_identity = IdentityFactory.create_identity(
            created_by=self.user,
            provider=self.provider)
        self.machine = ProviderMachineFactory.create_provider_machine(self.user, self.user_identity)
        start_date = timezone.now()
        self.active_instance = InstanceFactory.create(
            name="Instance in active",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.user,
            created_by_identity=self.user_identity,
            start_date=start_date)
        self.size_small = SizeFactory.create(provider=self.provider, cpu=2, disk=20, root=0, mem=128)
        self.status_active = InstanceStatusFactory.create(name='active')
        delta_time = timezone.timedelta(minutes=2)
        InstanceHistoryFactory.create(
                status=self.status_active,
                size=self.size_small,
                activity="",
                instance=self.active_instance,
                start_date=start_date + delta_time*3)

        self.view = InstanceViewSet.as_view({'post': 'actions'})
        self.url = reverse('api:v2:instance-list')
        self.url += "/" + str(self.active_instance.provider_alias) + "/actions"
        self.mock_driver = get_esh_driver(self.user_identity)
        self.mock_driver.add_core_instance(self.active_instance)

        start_date_second = timezone.now()
        self.active_instance_second = InstanceFactory.create(
            name="Instance in active",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.user,
            created_by_identity=self.user_identity,
            start_date=start_date_second)
        delta_time = timezone.timedelta(minutes=2)
        self.size_small = SizeFactory.create(provider=self.provider, cpu=2, disk=20, root=0, mem=128)
        self.size_large = SizeFactory.create(provider=self.provider, cpu=4, disk=40, root=0, mem=256)
        InstanceHistoryFactory.create(
                status=self.status_active,
                size=self.size_small,
                activity="",
                instance=self.active_instance_second,
                start_date=start_date_second + delta_time*3)
        self.mock_driver.add_core_instance(self.active_instance_second)
        self.allocation_source_1 = AllocationSourceFactory.create(name='TEST_INSTANCE_ALLOCATION_SOURCE_01',
                                                                  compute_allowed=1000)
        UserAllocationSource.objects.create(
                allocation_source=self.allocation_source_1,
                user=self.user)
        UserAllocationSnapshot.objects.create(
                allocation_source=self.allocation_source_1,
                user=self.user,
                burn_rate=1,
                compute_used=0)
        AllocationSourceSnapshot.objects.create(
            allocation_source=self.allocation_source_1,
            compute_used=0,
            compute_allowed=168,
            global_burn_rate=1
        )
        InstanceAllocationSourceSnapshot.objects.update_or_create(instance=self.active_instance,
                                                                  allocation_source=self.allocation_source_1)
    def setUp(self):
        self.anonymous_user = AnonymousUserFactory()

        self.user = UserFactory.create()
        self.group = GroupFactory.create(name=self.user.username)
        self.membership = GroupMembershipFactory.create(
            user=self.user, group=self.group, is_leader=True
        )
        self.group.user_set.add(self.user)
        self.project = ProjectFactory.create(
            owner=self.group, created_by=self.user
        )
        self.enddated_instance = InstanceFactory.create(
            created_by=self.user, end_date=timezone.now(), project=self.project
        )

        self.user2 = UserFactory.create()
        self.group2 = GroupFactory.create(name=self.user2.username)
        self.membership2 = GroupMembershipFactory.create(
            user=self.user2, group=self.group2, is_leader=True
        )
        self.group2.user_set.add(self.user2)
        self.project2 = ProjectFactory.create(
            owner=self.group2, created_by=self.user2
        )

        self.not_user = UserFactory.create()
        self.not_group = GroupFactory.create(name=self.not_user.username)
        self.not_membership = GroupMembershipFactory.create(
            user=self.not_user, group=self.not_group, is_leader=True
        )
        self.not_group.user_set.add(self.not_user)
        self.not_project = ProjectFactory.create(
            owner=self.not_group, created_by=self.not_user
        )

        self.unsaved_project = ProjectFactory.build(
            owner=self.group, created_by=self.user
        )

        list_url = reverse('api:v2:project-list')
        detail_url = reverse('api:v2:project-detail', args=(self.project.id, ))

        self.create_view = ProjectViewSet.as_view({'post': 'create'})
        self.delete_view = ProjectViewSet.as_view({'delete': 'destroy'})
        self.detail_view = ProjectViewSet.as_view({'get': 'retrieve'})
        self.list_view = ProjectViewSet.as_view({'get': 'list'})
        self.update_view = ProjectViewSet.as_view({'patch': 'partial_update'})

        self.factory = APIRequestFactory()
        self.bad_create_request = self.factory.post(list_url)
        self.create_request = self.factory.post(
            list_url, {
                'name': self.unsaved_project.name,
                'description': self.unsaved_project.description,
                'owner': self.group.name
            }
        )
        self.delete_request = self.factory.delete(detail_url)
        self.detail_request = self.factory.get(detail_url)
        self.list_request = self.factory.get(list_url)
        self.updated_project_data = {
            'name': 'updated name',
            'description': 'updated description'
        }
        self.update_request = self.factory.patch(
            detail_url, self.updated_project_data
        )
Пример #21
0
    def setUp(self):
        self.list_view = ViewSet.as_view({'get': 'list'})
        self.detailed_view = ViewSet.as_view({'get': 'retrieve'})

        self.anonymous_user = AnonymousUserFactory()
        self.user = UserFactory.create(username='******')
        self.staff_user = UserFactory.create(username='******', is_staff=True, is_superuser=True)
        self.provider = ProviderFactory.create()
        self.user_identity = IdentityFactory.create_identity(
            created_by=self.user,
            provider=self.provider)
        self.staff_user_identity = IdentityFactory.create_identity(
            created_by=self.staff_user,
            provider=self.provider)

        self.machine = ProviderMachineFactory.create_provider_machine(self.staff_user, self.staff_user_identity)
        self.application = self.machine.application_version.application
        start_date = timezone.now()
        self.active_instance = InstanceFactory.create(
            name="Instance went active",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.staff_user,
            created_by_identity=self.staff_user_identity,
            start_date=start_date)
        self.networking_instance = InstanceFactory.create(
            name="Instance stuck in networking",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.staff_user,
            created_by_identity=self.staff_user_identity,
            start_date=start_date)
        self.deploying_instance = InstanceFactory.create(
            name="Instance stuck in deploying",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.staff_user,
            created_by_identity=self.staff_user_identity,
            start_date=start_date)
        self.deploy_error_instance = InstanceFactory.create(
            name="Instance stuck in deploy_error",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.staff_user,
            created_by_identity=self.staff_user_identity,
            start_date=start_date)

        self.status_build = InstanceStatusFactory.create(name='build')
        self.status_suspended = InstanceStatusFactory.create(name='suspended')
        self.status_active = InstanceStatusFactory.create(name='active')
        self.status_networking = InstanceStatusFactory.create(name='networking')
        self.status_deploying = InstanceStatusFactory.create(name='deploying')
        self.status_deploy_error = InstanceStatusFactory.create(name='deploy_error')
        # Adding two minutes to simulate the passage of time.
        delta_time = timezone.timedelta(minutes=2)
        # Simulate 'Deploy error'
        InstanceHistoryFactory.create(
                status=self.status_build,
                activity="",
                instance=self.deploy_error_instance,
                start_date=start_date,
            )
        InstanceHistoryFactory.create(
                status=self.status_networking,
                activity="",
                instance=self.deploy_error_instance,
                start_date=start_date + delta_time,
            )
        InstanceHistoryFactory.create(
                status=self.status_deploying,
                activity="",
                instance=self.deploy_error_instance,
                start_date=start_date + delta_time*2,
            )
        InstanceHistoryFactory.create(
                status=self.status_deploy_error,
                activity="",
                instance=self.deploy_error_instance,
                start_date=start_date + delta_time*3,
            )
        # Simulate 'stuck in networking'
        InstanceHistoryFactory.create(
                status=self.status_build,
                activity="",
                instance=self.networking_instance,
                start_date=start_date,
            )
        InstanceHistoryFactory.create(
                status=self.status_networking,
                activity="",
                instance=self.networking_instance,
                start_date=start_date + delta_time,
            )
        # Simulate 'stuck in deploying'
        InstanceHistoryFactory.create(
                status=self.status_build,
                activity="",
                instance=self.deploying_instance,
                start_date=start_date + delta_time,
            )
        InstanceHistoryFactory.create(
                status=self.status_networking,
                activity="",
                instance=self.deploying_instance,
                start_date=start_date + delta_time,
            )
        InstanceHistoryFactory.create(
                status=self.status_deploying,
                activity="",
                instance=self.deploying_instance,
                start_date=start_date + delta_time,
            )
        # Simulate going to active
        InstanceHistoryFactory.create(
                status=self.status_build,
                activity="",
                instance=self.active_instance,
                start_date=start_date,
            )
        InstanceHistoryFactory.create(
                status=self.status_networking,
                activity="",
                instance=self.active_instance,
                start_date=start_date + delta_time,
            )
        InstanceHistoryFactory.create(
                status=self.status_deploying,
                activity="",
                instance=self.active_instance,
                start_date=start_date + delta_time*2,
            )
        InstanceHistoryFactory.create(
                status=self.status_active,
                activity="",
                instance=self.active_instance,
                start_date=start_date + delta_time*3,
            )
        get_application_metrics(self.application)
Пример #22
0
    def setUp(self):
        self.list_view = InstanceViewSet.as_view({'get': 'list'})
        self.detailed_view = InstanceViewSet.as_view({'get': 'retrieve'})
        self.anonymous_user = AnonymousUserFactory()
        self.user = UserFactory.create(username='******')
        self.admin_user = UserFactory.create(username='******', is_superuser=True, is_staff=True)
        self.provider = ProviderFactory.create()
        self.user_identity = IdentityFactory.create_identity(
            created_by=self.user,
            provider=self.provider)
        self.admin_identity = IdentityFactory.create_identity(
            provider=self.provider,
            created_by=self.admin_user)

        self.machine = ProviderMachineFactory.create_provider_machine(self.user, self.user_identity)
        self.active_instance = InstanceFactory.create(
            name="Instance in active",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.user,
            created_by_identity=self.user_identity,
            start_date=timezone.now())
        self.networking_instance = InstanceFactory.create(
            name="Instance in networking",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.user,
            created_by_identity=self.user_identity,
            start_date=timezone.now())
        self.deploying_instance = InstanceFactory.create(
            name="Instance in deploying",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.user,
            created_by_identity=self.user_identity,
            start_date=timezone.now())
        self.deploy_error_instance = InstanceFactory.create(
            name="Instance in deploy_error",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.user,
            created_by_identity=self.user_identity,
            start_date=timezone.now())

        active = InstanceStatusFactory.create(name='active')
        networking = InstanceStatusFactory.create(name='networking')
        deploying = InstanceStatusFactory.create(name='deploying')
        deploy_error = InstanceStatusFactory.create(name='deploy_error')

        InstanceHistoryFactory.create(
                status=deploy_error,
                activity="",
                instance=self.deploy_error_instance
            )
        InstanceHistoryFactory.create(
                status=deploying,
                activity="",
                instance=self.deploying_instance
            )
        InstanceHistoryFactory.create(
                status=active,
                activity="",
                instance=self.active_instance
            )
        InstanceHistoryFactory.create(
                status=networking,
                activity="",
                instance=self.networking_instance
            )
    def setUp(self):
        self.list_view = ViewSet.as_view({'get': 'list'})
        self.detailed_view = ViewSet.as_view({'get': 'retrieve'})

        self.anonymous_user = AnonymousUserFactory()
        self.user = UserFactory.create(username='******')
        self.staff_user = UserFactory.create(
            username='******', is_staff=True, is_superuser=True
        )
        self.provider = ProviderFactory.create()
        self.user_identity = IdentityFactory.create_identity(
            created_by=self.user, provider=self.provider
        )
        self.staff_user_identity = IdentityFactory.create_identity(
            created_by=self.staff_user, provider=self.provider
        )

        self.machine = ProviderMachineFactory.create_provider_machine(
            self.staff_user, self.staff_user_identity
        )
        self.application = self.machine.application_version.application
        start_date = timezone.now()
        self.active_instance = InstanceFactory.create(
            name="Instance went active",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.staff_user,
            created_by_identity=self.staff_user_identity,
            start_date=start_date
        )
        self.networking_instance = InstanceFactory.create(
            name="Instance stuck in networking",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.staff_user,
            created_by_identity=self.staff_user_identity,
            start_date=start_date
        )
        self.deploying_instance = InstanceFactory.create(
            name="Instance stuck in deploying",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.staff_user,
            created_by_identity=self.staff_user_identity,
            start_date=start_date
        )
        self.deploy_error_instance = InstanceFactory.create(
            name="Instance stuck in deploy_error",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.staff_user,
            created_by_identity=self.staff_user_identity,
            start_date=start_date
        )

        self.status_build = InstanceStatusFactory.create(name='build')
        self.status_suspended = InstanceStatusFactory.create(name='suspended')
        self.status_active = InstanceStatusFactory.create(name='active')
        self.status_networking = InstanceStatusFactory.create(name='networking')
        self.status_deploying = InstanceStatusFactory.create(name='deploying')
        self.status_deploy_error = InstanceStatusFactory.create(
            name='deploy_error'
        )
        # Adding two minutes to simulate the passage of time.
        delta_time = timezone.timedelta(minutes=2)
        # Simulate 'Deploy error'
        InstanceHistoryFactory.create(
            status=self.status_build,
            activity="",
            instance=self.deploy_error_instance,
            start_date=start_date,
        )
        InstanceHistoryFactory.create(
            status=self.status_networking,
            activity="",
            instance=self.deploy_error_instance,
            start_date=start_date + delta_time,
        )
        InstanceHistoryFactory.create(
            status=self.status_deploying,
            activity="",
            instance=self.deploy_error_instance,
            start_date=start_date + delta_time * 2,
        )
        InstanceHistoryFactory.create(
            status=self.status_deploy_error,
            activity="",
            instance=self.deploy_error_instance,
            start_date=start_date + delta_time * 3,
        )
        # Simulate 'stuck in networking'
        InstanceHistoryFactory.create(
            status=self.status_build,
            activity="",
            instance=self.networking_instance,
            start_date=start_date,
        )
        InstanceHistoryFactory.create(
            status=self.status_networking,
            activity="",
            instance=self.networking_instance,
            start_date=start_date + delta_time,
        )
        # Simulate 'stuck in deploying'
        InstanceHistoryFactory.create(
            status=self.status_build,
            activity="",
            instance=self.deploying_instance,
            start_date=start_date + delta_time,
        )
        InstanceHistoryFactory.create(
            status=self.status_networking,
            activity="",
            instance=self.deploying_instance,
            start_date=start_date + delta_time,
        )
        InstanceHistoryFactory.create(
            status=self.status_deploying,
            activity="",
            instance=self.deploying_instance,
            start_date=start_date + delta_time,
        )
        # Simulate going to active
        InstanceHistoryFactory.create(
            status=self.status_build,
            activity="",
            instance=self.active_instance,
            start_date=start_date,
        )
        InstanceHistoryFactory.create(
            status=self.status_networking,
            activity="",
            instance=self.active_instance,
            start_date=start_date + delta_time,
        )
        InstanceHistoryFactory.create(
            status=self.status_deploying,
            activity="",
            instance=self.active_instance,
            start_date=start_date + delta_time * 2,
        )
        InstanceHistoryFactory.create(
            status=self.status_active,
            activity="",
            instance=self.active_instance,
            start_date=start_date + delta_time * 3,
        )