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

refactor(tests): move return code check for ssh into ssh.run #4943

Closed
wants to merge 1 commit into from

Conversation

ShadowCurse
Copy link
Contributor

Changes

Move return code check for ssh into ssh.run.

Reason

Ssh connection should be considered an error by default. In order to unify handling of this case, move check for non zero return code into the function itself.

License Acceptance

By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache 2.0 license. For more information on following Developer
Certificate of Origin and signing off your commits, please check
CONTRIBUTING.md.

PR Checklist

  • I have read and understand CONTRIBUTING.md.
  • I have run tools/devtool checkstyle to verify that the PR passes the
    automated style checks.
  • I have described what is done in these changes, why they are needed, and
    how they are solving the problem in a clear and encompassing way.
  • I have updated any relevant documentation (both in code and in the docs)
    in the PR.
  • I have mentioned all user-facing changes in CHANGELOG.md.
  • If a specific issue led to this PR, this PR closes the issue.
  • When making API changes, I have followed the
    Runbook for Firecracker API changes.
  • I have tested all new and changed functionalities in unit tests and/or
    integration tests.
  • I have linked an issue to every new TODO.

  • This functionality cannot be added in rust-vmm.

Copy link

codecov bot commented Dec 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.07%. Comparing base (81bae9f) to head (ced8c02).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4943   +/-   ##
=======================================
  Coverage   84.07%   84.07%           
=======================================
  Files         251      251           
  Lines       28059    28059           
=======================================
  Hits        23592    23592           
  Misses       4467     4467           
Flag Coverage Δ
5.10-c5n.metal ?
5.10-m5n.metal 84.63% <ø> (+<0.01%) ⬆️
5.10-m6a.metal 83.94% <ø> (ø)
5.10-m6g.metal 80.74% <ø> (ø)
5.10-m6i.metal 84.62% <ø> (ø)
5.10-m7g.metal 80.74% <ø> (ø)
6.1-c5n.metal 84.65% <ø> (ø)
6.1-m5n.metal 84.63% <ø> (-0.01%) ⬇️
6.1-m6a.metal 83.93% <ø> (ø)
6.1-m6g.metal 80.74% <ø> (ø)
6.1-m6i.metal 84.62% <ø> (-0.01%) ⬇️
6.1-m7g.metal 80.74% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@roypat roypat left a comment

Choose a reason for hiding this comment

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

I looked at the same thing back when I introduced check_output, and there are quite a few locations where we do not want to fail on a non-zero return code (e.g. we run some command inside the guest that we expect to return with non-zero code). So I don't think we can blanket do assert rc != 0.

However, I think we can do a blanket assert rc != 255 inside ssh_connection.run, because ssh specifies that a 255 return code indicates something going wrong with the connection.

As for converting more .runs to .check_output, and dropping the rc return value from check_output: yes please

@ShadowCurse
Copy link
Contributor Author

My idea here is that if anything goes wrong with ssh connection "by default" we fail the test and it does not matter if it is an ssh error or a command error. In cases where we want to check the output manually (like in the vsock tests), we will need to explicitly pass check=False to the function.
Because of this I actually removed the check_output as the default run now does the check.

@ShadowCurse ShadowCurse force-pushed the ssh_assert branch 3 times, most recently from ed45e17 to 17e287f Compare December 4, 2024 16:31
@roypat
Copy link
Contributor

roypat commented Dec 4, 2024

Ah, we've kept the APIs as run/check_output to be consistent with the subprocess APIs. If we change this, then we should also change utils.run_cmd/utils.check_output the same way, otherwise things will be confusing.

@ShadowCurse ShadowCurse force-pushed the ssh_assert branch 4 times, most recently from 0cc5e8c to ae951cc Compare December 4, 2024 18:23
@roypat roypat requested a review from pb8o December 5, 2024 07:22
Ssh connection failure should be considered an error by default. In order
to unify handling of this case, move check for non zero return code
into the function itself. Because of this `check_output` method is
not needed anymore and was removed.

Signed-off-by: Egor Lazarchuk <[email protected]>
Comment on lines -127 to -129
def check_output(self, cmd_string, timeout=None, *, debug=False):
"""Same as `run`, but raises an exception on non-zero return code of remote command"""
return self.run(cmd_string, timeout, check=True, debug=debug)
Copy link
Contributor

Choose a reason for hiding this comment

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

We modeled check_output/run on subprocess.check_output/run. I think we should keep them as they are, as they are both useful. Most cases can be handled by check_output, but run is useful if the caller wants to handle the error.

Why not move more run calls to explicitly use check_output instead?

Copy link
Contributor Author

@ShadowCurse ShadowCurse Dec 5, 2024

Choose a reason for hiding this comment

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

Wouldn't it be easier if there was only 1 method to call? By default run will check output and if we need some exception, we will explicitly set check=False.
I did not touch supbprocess.run yet here, but was going to give it the same treatment.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with Pablo. Ideally out run/check_output would behave exactly like subprocess.run/check_output.

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, to me it's two different use cases, and how I see the subprocess API used:

  1. If I want to run a command, I use check_output because it's convenient and I don't have to mess with parameters.
  2. If I want fine control over what the command does, I use run.

subprocess.run has check=False. If we change that here to check=True, it'd be the opposite and surprising for anybody who is used to subprocess

@ShadowCurse ShadowCurse closed this Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants