This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test to verify ansible is called with image and instance scripts
- Loading branch information
Showing
11 changed files
with
117 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |