Skip to content
This repository has been archived by the owner on Sep 12, 2022. It is now read-only.

Commit

Permalink
Add test to verify ansible is called with image and instance scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
cdosborn committed Mar 6, 2018
1 parent 87fd061 commit 46666ed
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 10 deletions.
4 changes: 3 additions & 1 deletion api/tests/factories/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
from .quota_factory import QuotaFactory
from .allocation_factory import AllocationFactory
from .provider_type_factory import ProviderTypeFactory
from .provider_machine_factory import ProviderMachineFactory, InstanceSourceFactory
from .provider_machine_factory import ProviderMachineFactory
from .platform_type_factory import PlatformTypeFactory
from .size_factory import SizeFactory
from .allocation_source_factory import AllocationSourceFactory, UserAllocationSourceFactory
from .boot_script_factory import BootScriptRawTextFactory, BootScriptURLFactory
from .instance_source_factory import InstanceSourceFactory
21 changes: 21 additions & 0 deletions api/tests/factories/boot_script_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import factory
from factory import fuzzy
from api.tests.factories import UserFactory, InstanceFactory
from core.models import BootScript, ScriptType


class BootScriptFactory(factory.DjangoModelFactory):

class Meta:
model = BootScript

title = fuzzy.FuzzyText(prefix="bootscript-title-")
created_by = factory.SubFactory(UserFactory)


class BootScriptRawTextFactory(BootScriptFactory):
script_type = factory.LazyAttribute(lambda _: ScriptType.objects.get_or_create(name='Raw Text')[0])


class BootScriptURLFactory(BootScriptFactory):
script_type = factory.LazyAttribute(lambda _: ScriptType.objects.get_or_create(name='URL')[0])
3 changes: 3 additions & 0 deletions api/tests/factories/group_factory.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import factory
from factory import fuzzy
from core.models import Group


class GroupFactory(factory.DjangoModelFactory):

class Meta:
model = Group

name = fuzzy.FuzzyText(prefix="name-")
4 changes: 4 additions & 0 deletions api/tests/factories/identity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ def create_identity(created_by, group=None, provider=None, quota=None):

class Meta:
model = Identity

created_by = factory.SubFactory(UserFactory)
provider = factory.SubFactory(ProviderFactory)
quota = factory.SubFactory(QuotaFactory)
3 changes: 3 additions & 0 deletions api/tests/factories/image_factory.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import factory
from core.models import Application as Image
from .user_factory import UserFactory


class ImageFactory(factory.DjangoModelFactory):

class Meta:
model = Image

created_by = factory.SubFactory(UserFactory)
16 changes: 16 additions & 0 deletions api/tests/factories/instance_factory.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import factory
from core.models import Instance
from django.utils import timezone
from .user_factory import UserFactory
from .project_factory import ProjectFactory
from .instance_source_factory import InstanceSourceFactory
from .provider_machine_factory import ProviderMachineFactory
from .identity_factory import IdentityFactory



class InstanceFactory(factory.DjangoModelFactory):

class Meta:
model = Instance
exclude = ('provider_machine',)

provider_machine = factory.SubFactory(ProviderMachineFactory)
start_date = factory.LazyFunction(timezone.now)
project = factory.SubFactory(ProjectFactory)
source = factory.SelfAttribute('provider_machine.instance_source')
created_by = factory.SubFactory(UserFactory)
created_by_identity = factory.LazyAttribute(
lambda model: IdentityFactory(created_by=model.created_by))
12 changes: 12 additions & 0 deletions api/tests/factories/instance_source_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import factory
import uuid
from core.models import ProviderMachine, InstanceSource
from .provider_factory import ProviderFactory

class InstanceSourceFactory(factory.DjangoModelFactory):

class Meta:
model = InstanceSource

identifier = factory.Sequence(lambda n: uuid.uuid4())
provider = factory.SubFactory(ProviderFactory)
10 changes: 8 additions & 2 deletions api/tests/factories/project_factory.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import factory
from factory import fuzzy
from core.models import Project

from .group_factory import GroupFactory
from .user_factory import UserFactory


class ProjectFactory(factory.DjangoModelFactory):

class Meta:
model = Project

name = 'project name'
description = 'project description'
name = fuzzy.FuzzyText(prefix="name-")
description = fuzzy.FuzzyText(prefix="description-")
created_by = factory.SubFactory(UserFactory)
owner = factory.SubFactory(GroupFactory)
13 changes: 6 additions & 7 deletions api/tests/factories/provider_machine_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from core.models import ProviderMachine, InstanceSource
from .version_factory import ApplicationVersionFactory
from .image_factory import ImageFactory
from .provider_factory import ProviderFactory
from .instance_source_factory import InstanceSourceFactory
from .version_factory import ApplicationVersionFactory


class ProviderMachineFactory(factory.DjangoModelFactory):
Expand Down Expand Up @@ -32,10 +35,6 @@ def create_provider_machine(user, identity, application=None, version=None):
class Meta:
model = ProviderMachine


class InstanceSourceFactory(factory.DjangoModelFactory):

class Meta:
model = InstanceSource

identifier = factory.Sequence(lambda n: uuid.uuid4())
# Field occurs on super class of ProviderMachine
instance_source = factory.SubFactory(InstanceSourceFactory)
application_version = factory.SubFactory(ApplicationVersionFactory)
5 changes: 5 additions & 0 deletions api/tests/factories/version_factory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import factory
from core.models import ApplicationVersion
from .image_factory import ImageFactory
from .user_factory import UserFactory
from .identity_factory import IdentityFactory


class ApplicationVersionFactory(factory.DjangoModelFactory):
Expand All @@ -21,3 +23,6 @@ class Meta:
model = ApplicationVersion

application = factory.SubFactory(ImageFactory)
created_by = factory.SubFactory(UserFactory) #factory.SelfAttribute("created_by_identity.created_by")
created_by_identity = factory.LazyAttribute(
lambda model: IdentityFactory(created_by=model.created_by))
36 changes: 36 additions & 0 deletions service/tests/test_deploy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from django.test import TestCase
import mock

from api.tests.factories import BootScriptRawTextFactory, InstanceFactory, UserFactory


class UserDeployTests(TestCase):
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)

0 comments on commit 46666ed

Please sign in to comment.