Merge lp:~racb/maas/arch-constraint-wildcard into lp:maas/trunk
| Status: | Merged | ||||
|---|---|---|---|---|---|
| Approved by: | Robie Basak on 2012-10-08 | ||||
| Approved revision: | 1219 | ||||
| Merged at revision: | 1226 | ||||
| Proposed branch: | lp:~racb/maas/arch-constraint-wildcard | ||||
| Merge into: | lp:maas/trunk | ||||
| Diff against target: |
188 lines (+96/-6) 4 files modified
src/maasserver/enum.py (+4/-0) src/maasserver/models/node_constraint_filter.py (+53/-1) src/maasserver/tests/test_api.py (+3/-3) src/maasserver/tests/test_node_constraint_filter.py (+36/-2) |
||||
| To merge this branch: | bzr merge lp:~racb/maas/arch-constraint-wildcard | ||||
| Related bugs: |
|
| Reviewer | Review Type | Date Requested | Status |
|---|---|---|---|
| Jeroen T. Vermeulen (community) | 2012-10-08 | Approve on 2012-10-08 | |
| Martin Packman (community) | Approve on 2012-10-08 | ||
|
Review via email:
|
|||
Commit Message
Add automatic architecture constraint wildcarding and arm alias
- 1218. By Robie Basak on 2012-10-08
-
Post-review fixes
| Jeroen T. Vermeulen (jtv) wrote : | # |
Looks good, but I have some notes that I'd like you to address. Don't worry -- I usually do!
Notes follow.
.
In node_constraint
52 +def constrain_
53 + assert(key == 'architecture')
In python, "assert" is a statement:
assert key == 'architecture'
This can matter because it takes a second argument, namely an error string that will be shown if the assertion fails:
assert key == 'architecture', "This filter is for architecture only."
If you parenthesize as one might in C, things go mysteriously wrong:
assert(key == 'architecture', "This filter is for architecture only.")
This creates a tuple of a boolean and a string, and then asserts that the tuple, not the boolean, evaluates to True. Not what you want! If you need a line break, the idiomatic way of doing it is:
assert key == 'architecture', (
"This filter is for architecture only.")
.
55 + # Replace an alias with its value if it is an alias
56 + try:
57 + aliased_value = primary_
58 + except KeyError:
59 + aliased_value = value
It seems misleading to write this such that the normal, successful, expected-to-be-fast code path is the one taking an exception. An exception conveys both to the reader and to the language implementation that something is out of the ordinary, that the intended code flow is different, and that it's probably worth sacrificing performance in favour of the non-exceptional code path even in extreme trade-offs.
So I think it would be clearer to write it thus:
aliased_value = primary_
.
68 + else:
69 + raise InvalidConstraint(
70 + 'architecture', value, 'Architecture not recognised')
I would recommend "double quotes" for free-form text. This is not shell where you have to keep a constant eye on weird characters in double-quoted strings, and indeed the character most worth worrying about is the apostrophe. It'd be silly to change your quoting just because your sentence sprouted a contraction!
.
In test_api.py:
91 - def test_POST_
92 + def test_POST_
93 # Asking for an unknown arch returns an HTTP conflict
The comment still needs updating!
.
In test_node_
124 + def test_generate_
125 + single_subarch = factory.
126 + double_subarch_1 = factory.
127 + double_subarch_2 = double_
Might I recommend factory.
.
In the same test module, test_architecture is getting out of hand. There is little enough shared test setup that it doesn't seem worth the flexibility cost to bundle all these different behaviours and outcomes together into o...
- 1219. By Robie Basak on 2012-10-08
-
More post-review fixes from jtv's review
| Robie Basak (racb) wrote : | # |
Thanks for the reviews, Martin and Jeroen! Loads of improvements there. I've fixed all of these except for refactoring of the architecture constraint tests for which I'll submit a separate proposal, so I'll land this now.
| Robie Basak (racb) wrote : | # |
Separate proposal is https:/


Looks good.
The generate_ architecture_ wildcards is very cute, I'm not totally convinced it's needed versus just making someone type that out next to the arch enum, but as you'll be the one who has to add arm flavours it's fair enough to save yourself the bother. :)
Some small notes:
+ # Replace an alias with its value if it is an alias architecture_ aliases[ value]
+ try:
+ aliased_value = primary_
+ except KeyError:
+ aliased_value = value
This is not an exceptional circumstance, so I'd use the following (still non-recursive) spelling instead:
aliased_value = primary_ architecture_ aliases. get(value, value)
+ if aliased_value in (choice[0] for choice in ARCHITECTURE_ CHOICES) :
Rather than making a generator on the fly here to test membership, I'd do either: architecture_ wildcards and use the block below only CHOICES_ DICT in maasserver.enum as per other objects there and use `if aliased_value in ARCHITECTURE_ CHOICES_ DICT` instead.
* Add the full form as a mapping to a 1-tuple of itself in generate_
* Create an ARCHITECTURE_
I find test_generate_ architecture_ wildcards slightly hard to understand, a docstring, comment or more assertions might help.