Repository navigation
snap: report missing snaps instead of raising IndexError - #12570
felixfontein merged 4 commits into
Conversation
snap info reports a missing snap two different ways. When several snaps are
queried it emits "warning: no snap found" on stdout and exits 0; when a single
snap is queried it emits "error: no snap found" on stderr and exits 1.
names_from_snaps only matched the warning form, so the single-snap case fell
through to process_one, where snap info output contains no "name:" line and
res[0] raised IndexError instead of reporting the missing snap.
The same block also scanned stdout for the snap names while the single-snap
message is on stderr, so even a matching prefix would have produced an empty
list. It now scans check_error, which is already the correct stream for both
cases, and strips the surrounding quotes so the failure reads
Snaps not found: ['hello-world'].
process_one also raises a clear error when there is no "name:" line, so a
future change in snap info output fails with a message rather than an
IndexError.
Adds a unit case mocking the exact output from the report: rc 1, empty stdout,
and 'error: no snap found for "hello-world"' on stderr. Against the previous
code it reproduces the reported failure; with this change the full snap suite
passes.
Signed-off-by: S Prajwall Narayana <[email protected]>
|
Session |
The collection's fragments link both the issue and the pull request; this one only had the issue because the PR did not exist when it was written. Signed-off-by: S Prajwall Narayana <[email protected]>
felixfontein
left a comment
There was a problem hiding this comment.
Thanks for your contribution!
| ] | ||
| ) | ||
|
|
||
| TEST_SPEC["test_cases"].append( |
There was a problem hiding this comment.
Why not add this directly to the above list?
There was a problem hiding this comment.
Not only that, but please also add a corresponding test case with multiple snaps.
russoz
left a comment
There was a problem hiding this comment.
Thanks for your contribution! Got a couple of comments.
| @@ -0,0 +1,2 @@ | |||
| bugfixes: | |||
| - "snap - fix ``IndexError`` when a snap does not exist. ``snap info`` reports a missing snap as ``error: no snap found`` on stderr when a single snap is queried, but the module only matched the ``warning: no snap found`` emitted on stdout when several are, so the single-snap case fell through and crashed instead of reporting the missing snap (https://github.com/ansible-collections/community.general/issues/12375, https://github.com/ansible-collections/community.general/pull/12570)." | |||
There was a problem hiding this comment.
I would suggest making it a little bit more terse - the explanation of what happened to trigger the problem is not exactly a log of the change ;-)
| # is, so both prefixes and both streams have to be handled. Matching | ||
| # only the warning let the single-snap case fall through to | ||
| # process_one, which then raised IndexError because there is no | ||
| # "name:" line to read. |
There was a problem hiding this comment.
over-explained - could you please make it more concise?
| # "name:" line to read. | ||
| snaps_not_found = [ | ||
| x.split()[-1].strip('"') | ||
| for x in check_error.split("\n") |
There was a problem hiding this comment.
I see, the error was that the code was line splitting out when it should have been check_err. I believe that was me, a while back.
| snaps_not_found = [ | ||
| x.split()[-1].strip('"') | ||
| for x in check_error.split("\n") | ||
| if x.startswith(("warning: no snap found", "error: no snap found")) |
There was a problem hiding this comment.
I would consider using a regexp with the expression r'^(warning|error): no snap found'. We are already using re` in this module, and there is a nifty block of pre-defined regexps by the beggining of the code.
| if not res: | ||
| self.do_raise(msg=f"Unable to determine snap name from 'snap info' output:\n{out}") |
There was a problem hiding this comment.
This happens when the snap name is not recognized by snapd (regardless of installation status). I tried locally:
$ snap info bogus-hello-world
error: no snap found for "bogus-hello-world"
If we run the module with only one snap name, and an invalid one (like the above), then the res list will come out empty in L374, and the exception will be raised in L376.
However, this function is only called in L411 below, after the validation in L404-409, which will raise an exception in case of error. Therefore, this change here is redundant, please remove it.
| ] | ||
| ) | ||
|
|
||
| TEST_SPEC["test_cases"].append( |
There was a problem hiding this comment.
Not only that, but please also add a corresponding test case with multiple snaps.
| # snapd emits "error: no snap found" on stderr with rc=1 when a single | ||
| # snap is queried, not the "warning:" on stdout it uses for several. | ||
| id="issue_12375_single_snap_not_found", | ||
| input={"name": ["hello-world"]}, |
There was a problem hiding this comment.
To prevent confusion for unwary, please use an obviously wrong name for this test, like bogus-world or this-snap-does-not-exist or something like that. Using the name of a valid, real, existing one to test the non-existing case might cause trouble to people learning their way into this.
… the tests - Remove the process_one guard: names_from_snaps only calls it after the no-snap-found check above, which already raises, so it was unreachable. - Match the missing-snap lines with a precompiled regexp alongside the other module-level patterns, instead of a startswith tuple. - Cut the inline comment and the changelog fragment down to the change itself. - Move both test cases into the TEST_SPEC list rather than appending after it, use an obviously invalid snap name, and add the multiple-snap case. Note: the regexp captures the name, so the multi-snap failure message now reads ['a', 'b'] rather than ['"a"', '"b"'] — the quotes snapd emits are no longer carried into the message. The new multi-snap test pins that.
|
Thanks both — all seven comments addressed in 4ba4e94. @russoz one thing worth flagging explicitly, because it is a user-visible change rather than a pure refactor. Moving to a named capture group means the multi-snap failure message now reads: where the previous The single-snap path is new, so it has no prior behaviour to preserve — but that one does, and I did not want to change it silently. I went with the unquoted form so both paths report the same way, and added Happy to strip only the leading prefix and keep the quotes if you would rather leave the existing message untouched. On the redundant guard — you were right, it was unreachable: |
russoz
left a comment
There was a problem hiding this comment.
Two minor nits and I think we're good.
| __disable_re = re.compile(r"(?:\S+\s+){5}(?P<notes>\S+)") | ||
| __set_param_re = re.compile(r"(?P<snap_prefix>\S+:)?(?P<key>\S+)\s*=\s*(?P<value>.+)") | ||
| __list_re = re.compile(r"^(?P<name>\S+)\s+\S+\s+(?P<rev>\S+)\s+(?P<channel>\S+)\s+\S+\s+(?P<notes>\S+)") | ||
| __no_snap_re = re.compile(r"^(?:warning|error): no snap found for \"(?P<name>[^\"]+)\"") |
There was a problem hiding this comment.
I would recommend avoiding the escaped quotes within the string. To differentiate the string quoting, I am suggesting the triple ''', but feel free to use something else.
| __no_snap_re = re.compile(r"^(?:warning|error): no snap found for \"(?P<name>[^\"]+)\"") | |
| __no_snap_re = re.compile(r'''^(?:warning|error): no snap found for "(?P<name>[^\"]+)"''') |
| ), | ||
| ), | ||
| dict( | ||
| # https://github.com/ansible-collections/community.general/issues/12375 |
There was a problem hiding this comment.
This is redundant. When the PR is merged, all commits are squashed ibto one and its commit hash gets associated with the line in git blame. If memory serves the PR is easily spotted as well.
Moreover, you already got the PR number in the test case name.
|
Both done, thanks. On the first one I also dropped the backslash inside the character class — On the second: agreed, removed both. The PR number is already in the test case ids. |
…ssue links russoz's review on ansible-collections#12570: - __no_snap_re: use a triple-quoted raw string so the embedded double quotes need no backslash escaping. The backslash inside the character class goes too, since it was only there to survive the old quoting. - tests: drop the issue URL comments. The PR number is already in each test case id, and git blame ties the lines to the squashed merge commit.
9986158 to
6b3b27f
Compare
Backport to stable-13: 💚 backport PR created✅ Backport PR branch: Backported as #12582 🤖 @patchback |
|
@Developer1010x thanks for fixing this! |
Backport to stable-12: 💔 cherry-picking failed — conflicts found❌ Failed to cleanly apply 8287b00 on top of patchback/backports/stable-12/8287b00a3140e75fa16d1ab0f7eae55c984c2202/pr-12570 Backporting merged PR #12570 into main
🤖 @patchback |
…nstead of raising IndexError (#12582) snap: report missing snaps instead of raising IndexError (#12570) * snap: report missing snaps instead of raising IndexError snap info reports a missing snap two different ways. When several snaps are queried it emits "warning: no snap found" on stdout and exits 0; when a single snap is queried it emits "error: no snap found" on stderr and exits 1. names_from_snaps only matched the warning form, so the single-snap case fell through to process_one, where snap info output contains no "name:" line and res[0] raised IndexError instead of reporting the missing snap. The same block also scanned stdout for the snap names while the single-snap message is on stderr, so even a matching prefix would have produced an empty list. It now scans check_error, which is already the correct stream for both cases, and strips the surrounding quotes so the failure reads Snaps not found: ['hello-world']. process_one also raises a clear error when there is no "name:" line, so a future change in snap info output fails with a message rather than an IndexError. Adds a unit case mocking the exact output from the report: rc 1, empty stdout, and 'error: no snap found for "hello-world"' on stderr. Against the previous code it reproduces the reported failure; with this change the full snap suite passes. * snap: cite the PR alongside the issue in the changelog fragment The collection's fragments link both the issue and the pull request; this one only had the issue because the PR did not exist when it was written. * snap: address review — use a regexp, drop the redundant guard, extend the tests - Remove the process_one guard: names_from_snaps only calls it after the no-snap-found check above, which already raises, so it was unreachable. - Match the missing-snap lines with a precompiled regexp alongside the other module-level patterns, instead of a startswith tuple. - Cut the inline comment and the changelog fragment down to the change itself. - Move both test cases into the TEST_SPEC list rather than appending after it, use an obviously invalid snap name, and add the multiple-snap case. Note: the regexp captures the name, so the multi-snap failure message now reads ['a', 'b'] rather than ['"a"', '"b"'] — the quotes snapd emits are no longer carried into the message. The new multi-snap test pins that. * snap: address review nits — triple-quote the regexp, drop redundant issue links russoz's review on #12570: - __no_snap_re: use a triple-quoted raw string so the embedded double quotes need no backslash escaping. The backslash inside the character class goes too, since it was only there to survive the old quoting. - tests: drop the issue URL comments. The PR number is already in each test case id, and git blame ties the lines to the squashed merge commit. --------- (cherry picked from commit 8287b00) Signed-off-by: S Prajwall Narayana <[email protected]> Co-authored-by: S Prajwall Narayana <[email protected]>
…lections#12570) * snap: report missing snaps instead of raising IndexError snap info reports a missing snap two different ways. When several snaps are queried it emits "warning: no snap found" on stdout and exits 0; when a single snap is queried it emits "error: no snap found" on stderr and exits 1. names_from_snaps only matched the warning form, so the single-snap case fell through to process_one, where snap info output contains no "name:" line and res[0] raised IndexError instead of reporting the missing snap. The same block also scanned stdout for the snap names while the single-snap message is on stderr, so even a matching prefix would have produced an empty list. It now scans check_error, which is already the correct stream for both cases, and strips the surrounding quotes so the failure reads Snaps not found: ['hello-world']. process_one also raises a clear error when there is no "name:" line, so a future change in snap info output fails with a message rather than an IndexError. Adds a unit case mocking the exact output from the report: rc 1, empty stdout, and 'error: no snap found for "hello-world"' on stderr. Against the previous code it reproduces the reported failure; with this change the full snap suite passes. Signed-off-by: S Prajwall Narayana <[email protected]> * snap: cite the PR alongside the issue in the changelog fragment The collection's fragments link both the issue and the pull request; this one only had the issue because the PR did not exist when it was written. Signed-off-by: S Prajwall Narayana <[email protected]> * snap: address review — use a regexp, drop the redundant guard, extend the tests - Remove the process_one guard: names_from_snaps only calls it after the no-snap-found check above, which already raises, so it was unreachable. - Match the missing-snap lines with a precompiled regexp alongside the other module-level patterns, instead of a startswith tuple. - Cut the inline comment and the changelog fragment down to the change itself. - Move both test cases into the TEST_SPEC list rather than appending after it, use an obviously invalid snap name, and add the multiple-snap case. Note: the regexp captures the name, so the multi-snap failure message now reads ['a', 'b'] rather than ['"a"', '"b"'] — the quotes snapd emits are no longer carried into the message. The new multi-snap test pins that. * snap: address review nits — triple-quote the regexp, drop redundant issue links russoz's review on ansible-collections#12570: - __no_snap_re: use a triple-quoted raw string so the embedded double quotes need no backslash escaping. The backslash inside the character class goes too, since it was only there to survive the old quoting. - tests: drop the issue URL comments. The PR number is already in each test case id, and git blame ties the lines to the squashed merge commit. --------- Signed-off-by: S Prajwall Narayana <[email protected]>
SUMMARY
Fixes #12375.
snap inforeports a missing snap two different ways:warning: no snap found for "..."error: no snap found for "..."names_from_snapsonly matched the warning form, so the single-snap case fell through toprocess_one, where the output contains noname:line andres[0]raisedIndexErrorinstead of reporting the missing snap. That is the traceback in the issue, and the run info there confirms it:The same block also scanned stdout for the snap names while the single-snap message is on stderr, so even a matching prefix would have produced an empty list. It now scans
check_error, which is already the correct stream in both branches.Three changes:
warning: no snap foundanderror: no snap found, and read them fromcheck_error.Snaps not found: ['hello-world'].rather than['"hello-world"'].process_onewhen there is noname:line, so a future change insnap infooutput fails with a message rather than anIndexError.ISSUE TYPE
COMPONENT NAME
snap
ADDITIONAL INFORMATION
Added a unit case mocking exactly the output from the report: rc 1, empty stdout,
error: no snap found for "hello-world"on stderr.Against the current code it reproduces the reported failure:
With this change the full
test_snap.pysuite passes (5 passed).Happy to change the wording of the failure message if you would prefer something else.