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

Verify if binary exists before executing #4635

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hector-vido
Copy link

@hector-vido hector-vido commented Feb 22, 2025

Description

Verify if the binary exists before running it with sudo.
Actually the exit code is not useful, only show that a result different than 0 was returned.

...
INFO Checking if vsock is correctly configured    
INFO Setting up vsock support                     
INFO Using root access: Setting CAP_NET_BIND_SERVICE capability for /home/hector/Projects/crc/out/linux-amd64/crc executable 
exit status 1

With this tiny modification, is easy for the user to understand what is missing:

...
INFO Checking if vsock is correctly configured    
INFO Setting up vsock support                     
INFO Using root access: Setting CAP_NET_BIND_SERVICE capability for /home/hector/Projects/crc/out/linux-amd64/crc executable 
setcap executable not found

I was testing crc inside an openSUSE Tumbleweed version 20250219.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change
  • Chore (non-breaking change which doesn't affect codebase;
    test, version modification, documentation, etc.)

Proposed changes

Just an extra verification for the existence of binaries used and a small unit test.

Unfortunately with this unit test the sudo command will be invoked in each test call and most of the container images doesn't have it. The solution for this was to use an unexported package-level variable, this variable can be overwritten inside the test, avoiding the use of an interface.

Chapter 11 - The Go Programming Language, page 312

Testing

Added a unit test that should fail trying to find a binary named i-dont-exist.
Thanks to @rohanKanojia who gives me a north on that.

Contribution Checklist

  • I Keep It Small and Simple: The smaller the PR is, the easier it is to review and have it merged
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Which platform have you tested the code changes on?
    • Linux
    • Windows
    • MacOS

Copy link

openshift-ci bot commented Feb 22, 2025

Hi @hector-vido. Thanks for your PR.

I'm waiting for a crc-org member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@hector-vido hector-vido force-pushed the verify-binary-existence branch from b8f9bb4 to abf149a Compare February 22, 2025 03:52
@praveenkumar
Copy link
Member

/ok-to-test

@@ -49,6 +49,10 @@ func RunPrivileged(reason string, cmdAndArgs ...string) (string, string, error)
return "", "", errors.New("sudo executable not found")
}
logging.Infof("Using root access: %s", reason)
_, err = exec.LookPath(cmdAndArgs[0])
if err != nil {
return "", "", errors.New(cmdAndArgs[0] + " executable not found")
Copy link
Member

Choose a reason for hiding this comment

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

👍🏼

@rohanKanojia
Copy link
Contributor

@hector-vido : Thanks for your PR! Is it possible to add a unit test to verify that your changes work as expected?

@hector-vido hector-vido force-pushed the verify-binary-existence branch from abf149a to 28a63b6 Compare February 27, 2025 03:49
@openshift-ci openshift-ci bot removed the lgtm label Feb 27, 2025
@hector-vido hector-vido force-pushed the verify-binary-existence branch from 28a63b6 to 3983c64 Compare February 27, 2025 18:58
@hector-vido
Copy link
Author

hector-vido commented Feb 27, 2025

This is interesting, pull-ci-crc-org-crc-main-images and some other tests are failing because sudo is not present.

=== RUN   TestRunPrivileged
exec_test.go:11: 
  Error Trace:	/go/src/github.com/crc-org/crc/pkg/os/exec_test.go:11
  Error:      	Error "sudo executable not found" does not contain "i-dont-exist executable not found"
  Test:       	TestRunPrivileged

Fixed with an unexported package-level variable

@hector-vido hector-vido force-pushed the verify-binary-existence branch from 3983c64 to 5654a32 Compare February 28, 2025 01:00
@openshift-ci openshift-ci bot removed the lgtm label Feb 28, 2025
@hector-vido
Copy link
Author

/retest

2 similar comments
@hector-vido
Copy link
Author

/retest

@hector-vido
Copy link
Author

/retest

@hector-vido hector-vido force-pushed the verify-binary-existence branch from 5654a32 to 58fa905 Compare February 28, 2025 13:22
@openshift-ci openshift-ci bot added the lgtm label Feb 28, 2025
Copy link

openshift-ci bot commented Feb 28, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: praveenkumar, rohanKanojia

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link

openshift-ci bot commented Feb 28, 2025

@hector-vido: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
ci/prow/e2e-crc 58fa905 link true /test e2e-crc

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants