Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that a failed deployment is not created #172

Merged
merged 2 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions nautobot_design_builder/design_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ def _run_in_transaction(self, **kwargs): # pylint: disable=too-many-branches, t
This version of `run` is wrapped in a transaction and will roll back database changes
on error. In general, this method should only be called by the `run` method.
"""
sid = transaction.savepoint()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abates , I think that is a better approach to rollback all the changes from a Design Job if it fails.
This means that neither a Deployment or a ChangeSet will be created if it fails or it's run in dry run mode.


self.log_info(message=f"Building {getattr(self.Meta, 'name')}")
extensions = getattr(self.Meta, "extensions", [])

Expand Down Expand Up @@ -307,8 +309,6 @@ def _run_in_transaction(self, **kwargs): # pylint: disable=too-many-branches, t
self.failed = True
return

sid = transaction.savepoint()

try:
for design_file in design_files:
self.implement_design(context, design_file, commit)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
manufacturers:
name: "Test Manufacturer"
wrong: "attribute"
13 changes: 12 additions & 1 deletion nautobot_design_builder/tests/designs/test_designs.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,20 @@ class Meta: # pylint: disable=too-few-public-methods
]


class MultiDesignJobWithError(DesignJob):
class DesignJobModeDeploymentWithError(DesignJob):
"""Design job that includes an error (for unit testing)."""

class Meta: # pylint: disable=too-few-public-methods
name = "File Design with Error"
design_files = [
"templates/simple_design_with_error.yaml.j2",
]
design_mode = DesignModeChoices.DEPLOYMENT


class MultiDesignJobWithError(DesignJob):
"""Multi Design job that includes an error (for unit testing)."""

class Meta: # pylint: disable=too-few-public-methods
name = "Multi File Design with Error"
design_files = [
Expand Down
9 changes: 9 additions & 0 deletions nautobot_design_builder/tests/test_design_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from nautobot.ipam.models import VRF, Prefix, IPAddress

from nautobot.extras.models import Status
from nautobot_design_builder.models import Deployment
from nautobot_design_builder.errors import DesignImplementationError, DesignValidationError
from nautobot_design_builder.tests import DesignTestCase
from nautobot_design_builder.tests.designs import test_designs
Expand All @@ -27,6 +28,14 @@ def test_simple_design_rollback(self):
job.run(data=self.data, commit=True)
self.assertEqual(0, Manufacturer.objects.all().count())

def test_simple_design_rollback_deployment_mode(self):
"""Confirm that database changes are rolled back when an exception is raised and no Design Deployment is created."""
self.assertEqual(0, Manufacturer.objects.all().count())
job = self.get_mocked_job(test_designs.DesignJobModeDeploymentWithError)
job.run(data={**self.data, **{"deployment_name": "whatever"}}, commit=True)
self.assertEqual(0, Manufacturer.objects.all().count())
self.assertEqual(0, Deployment.objects.all().count())

def test_simple_design_report(self):
"""Confirm that a report is generated."""
job = self.get_mocked_job(test_designs.SimpleDesignReport)
Expand Down
Loading