Skip to content

jailer: increase code coverage #1940

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

Merged
merged 1 commit into from
Jun 3, 2020

Conversation

lauralt
Copy link

@lauralt lauralt commented Jun 2, 2020

Reason for This PR

A step forward for #363.

Description of Changes

With the recently merged #1862, realized the coverage for jailer can be quite easily increased.
Split run() in two in order to be able to unit-test some part of it.
Added a few unit tests and made a small cleanup in jailer env tests.

  • This functionality can be added in rust-vmm.

License Acceptance

By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache 2.0 license.

PR Checklist

[Author TODO: Meet these criteria.]
[Reviewer TODO: Verify that these criteria are met. Request changes if not]

  • All commits in this PR are signed (git commit -s).
  • The reason for this PR is clearly provided (issue no. or explanation).
  • The description of changes is clear and encompassing.
  • Any required documentation changes (code and docs) are included in this PR.
  • Any newly added unsafe code is properly documented.
  • Any API changes are reflected in firecracker/swagger.yaml.
  • Any user-facing changes are mentioned in CHANGELOG.md.

@lauralt lauralt self-assigned this Jun 2, 2020
@lauralt lauralt force-pushed the jailer_tests branch 2 times, most recently from 69bf9bf to 5bd84d8 Compare June 2, 2020 09:42
@lauralt lauralt added the Status: Awaiting review Indicates that a pull request is ready to be reviewed label Jun 2, 2020
@@ -210,14 +210,11 @@ impl Env {
.map_err(|e| Error::ChangeFileOwner(path_buf, e))
}

pub fn run(mut self) -> Result<()> {
fn prepare_for_chroot(&mut self) -> Result<()> {
Copy link
Contributor

Choose a reason for hiding this comment

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

The name is a bit misleading, this method doesn't do all the necessary preparing for chroot (cgroups, open /dev/null). Could the 2 things that this method deals with be split in different methods and tested separately?

Copy link
Author

Choose a reason for hiding this comment

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

I agree it's not the best name, didn't have a better idea at that moment.
I split the function in two, now the testing part should be clearer too.

self.prepare_for_chroot()?;

// Safe to unwrap because we check in the call above that `file_name()` is successful.
// The borrow checker doesn't let us to nicely use an `&OsStr` as function parameter
Copy link
Contributor

Choose a reason for hiding this comment

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

OsStr is a borrowed reference to an OsStringfrom inside self. That is why the self is seen as mutably borrowed by the checker. What you can do is make prepare_for_chroot return an OsString. Something around:

fn prepare_for_chroot(&mut self) -> Result<(OsString)> {
...
Ok(exec_file_name.to_os_string())
}
pub fn run {
let exec_file_name = self.prepare_for_chroot()?;
let chroot_exec_file = PathBuf::from("/").join(&exec_file_name);

let cgroup = Cgroup::new(&self.id, self.numa_node, &exec_file_name)?;
}

WDYT?

Copy link
Author

Choose a reason for hiding this comment

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

Nice! Indeed I tried something similar, but with OsStr instead of OsString🤦‍♀️.
I'll update, thanks!

Copy link
Author

Choose a reason for hiding this comment

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

Done.

let some_arg_vals = ArgVals {
node: "1",
id: "bd65600d-8669-4903-8a14-af88203add38",
exec_file: "file1",
Copy link
Contributor

Choose a reason for hiding this comment

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

Cant we use TempFilefor zzns and file1 and TempDir forchroot_base`?

Copy link
Author

Choose a reason for hiding this comment

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

👍 done.

Split `run()` in three in order to be able to unit-test
some part of it.
Add unit tests for some uncovered sections from jailer's
`main.rs`.
Fix a bit the cleanup logic from `test_mknod_and_own_dev()`.

Signed-off-by: Laura Loghin <lauralg@amazon.com>
@ioanachirca ioanachirca merged commit 0e7702e into firecracker-microvm:master Jun 3, 2020
@lauralt lauralt deleted the jailer_tests branch June 3, 2020 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Awaiting review Indicates that a pull request is ready to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants