예제 #1
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 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
예제 #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 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)
예제 #5
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)
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
    )
예제 #7
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)
예제 #8
0
    def setUp(self):
        factory = APIRequestFactory()
        self.anonymous_user = AnonymousUserFactory()
        self.user = UserFactory.create()
        self.provider = ProviderFactory.create()
        self.user_identity = IdentityFactory.create_identity(
            created_by=self.user, provider=self.provider
        )
        self.private_image = ImageFactory.create(
            created_by=self.user, private=True
        )
        self.private_version = ApplicationVersionFactory.create_version(
            self.user, self.user_identity, application=self.private_image
        )
        self.private_machine = ProviderMachineFactory.create_provider_machine(
            self.user,
            self.user_identity,
            application=self.private_image,
            version=self.private_version
        )

        self.staff_user = UserFactory.create(is_staff=True)
        self.staff_user_identity = IdentityFactory.create_identity(
            created_by=self.staff_user, provider=self.provider
        )
        self.public_image = ImageFactory.create(
            created_by=self.staff_user, private=False
        )
        self.public_version = ApplicationVersionFactory.create_version(
            self.staff_user,
            self.staff_user_identity,
            application=self.public_image
        )
        self.public_machine = ProviderMachineFactory.create_provider_machine(
            self.staff_user,
            self.staff_user_identity,
            application=self.public_image,
            version=self.public_version
        )

        self.list_view = ViewSet.as_view({'get': 'list'})
        list_url = reverse(self.url_route + '-list')
        self.list_request = factory.get(list_url)

        self.detail_view = ViewSet.as_view({'get': 'retrieve'})
        detail_url = reverse(self.url_route + '-detail', args=(self.user.id, ))
        self.detail_request = factory.get(detail_url)
    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)
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
예제 #12
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)
예제 #13
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
            )
예제 #14
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
        )
예제 #15
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)
예제 #16
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.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,
        )