forked from canonical/checkbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add uniqueness for Template Units id (BugFix) (canonical#951)
* Make sure Template units are units with id A Template unit always contains an id field. It should therefore inherit UnitWithId instead of Unit, so that the `partial_id()` and `id()` methods are inherited as well. * Prevent Template Units id from being rendered when using Jinja2 By default, templates using Jinja2 engine gets their id rendered as soon as they get accessed, but since they don't have the appropriate environment, the parameters are replaced with an empty string. This is why when you run the command `checkbox-cli list template`, you may see things like this: template 'com.canonical.certification::camera/still_' instead of template 'com.canonical.certification::camera/still_{{ name }}' Note that this does not happen with standard Python formatted strings: template 'com.canonical.certification::camera/led_{name}' In order to make the behavior consistent regardless of the template engine used, this commit: - introduces a unit property that always returns "template" (similar to how the Job unit always returns "job"). - modifies Unit.get_record_value() to prevent rendering using Jinja2 templating if the unit is a Checkbox Template It also contains unit tests for both the TemplateUnit and its Validator. * Have Template Unit's "template-id" field generated from the "id" field, if absent * Expose Template Unit's template-id field instead of id PlainBoxObject is used when exposing Units at a high level; for instance, when running `checkbox-cli list template`. Instead of exposing the Template Unit's id field, expose the template-id field. * Use Template partial_id and id, similar to Job's partial_id and id In order to have more granularity on what is displayed for the template id, use methods similar to the Job Unit. * Add unit test to ensure templates have a unique id * Implement explain() in TemplateUnit This method overrides UnitWithId.explain() which displays the Unit's id. In the case of a Template, this is misleading, and the template_id (or, in this case, template_partial_id to prevent displaying the provider namespace) should be used instead. This is useful when reporting errors using `./manage.py validate`, for instance. * Update Template Unit documentation about template-id field * Cleanup Template Unit reference documentation
- Loading branch information
Showing
7 changed files
with
319 additions
and
39 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,40 @@ | ||
# This file is part of Checkbox. | ||
# | ||
# Copyright 2024 Canonical Ltd. | ||
# Written by: | ||
# Pierre Equoy <[email protected]> | ||
# | ||
# Checkbox is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License version 3, | ||
# as published by the Free Software Foundation. | ||
# | ||
# Checkbox is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
from unittest import TestCase | ||
|
||
from plainbox.impl.highlevel import Explorer | ||
from plainbox.impl.unit.template import TemplateUnit | ||
|
||
|
||
class TestExplorer(TestCase): | ||
def test_template_to_obj__without_template_id(self): | ||
template = TemplateUnit({ | ||
"id": "id", | ||
}) | ||
explorer = Explorer() | ||
obj = explorer._template_to_obj(template) | ||
self.assertEqual(obj.name, "id") | ||
|
||
def test_template_to_obj__with_template_id(self): | ||
template = TemplateUnit({ | ||
"template-id": "template-id", | ||
}) | ||
explorer = Explorer() | ||
obj = explorer._template_to_obj(template) | ||
self.assertEqual(obj.name, "template-id") |
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
Oops, something went wrong.