-
Notifications
You must be signed in to change notification settings - Fork 36
Description of resource_download.rs #13
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
Open
kshitij86
wants to merge
1
commit into
codenet:main
Choose a base branch
from
kshitij86:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # [VMM Resource Downloader](../src/utils/src/resource_download.rs) | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Testing |
||
| The `VMM Resource Downloader` is a utility present in the VMM repository that is used to download resources required for tests and returns the absolute path of the downloaded resource to `stdout`. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Testing |
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Testing |
||
| This is done using the [s3_downloads.py](../tests/tools/s3_download.py) file and the resource list present in [resource_manifest.json](../tests/tools/s3/resource_manifest.json) which contains the description (resource name and types) for several kernel images and `initrd` files. | ||
|
|
||
| ## Code Description | ||
|
|
||
| The resource_download first imports `PathBuf` and `Command`. | ||
|
|
||
| ``` | ||
| use std::path::PathBuf; | ||
| use std::process::Command; | ||
| ``` | ||
|
|
||
| `PathBuf` is used for mutating paths in place with functions like `push` and `set_extension`. | ||
|
|
||
| `Command` is a process builder, providing fine-grained control over how a new process should be spawned. Command can be reused to spawn multiple processes. The builder methods change the command without needing to immediately spawn the process. | ||
|
|
||
| Next, is defined an enum `Error` that is used for error handling, in case there are errors encountered/some operation fails during fetching a resource from S3. | ||
|
|
||
| ``` | ||
| #[derive(Debug, PartialEq)] | ||
| pub enum Error { | ||
| DownloadError(String), | ||
| } | ||
| ``` | ||
| The function `s3_download` does the heavy lifting, which downloads the requested resource and returns its absolute path. It takes two parameters: | ||
|
|
||
| - `r_type: &str` - this is used to describe the resource type to be downloaded, e.g. `kernel`, `disk` etc. | ||
| - `r_tags: Option<&str>` - this takes optional tags to filter resources | ||
|
|
||
| The return value is of type `Result<PathBuf, Error>`, which is either a path to the resource or an `Error` indicating failure. | ||
|
|
||
| `dld_script` is used to store the path to the download script present in `~/tests/tools/`. | ||
| Then, `Command` is used to execute the Python download script along with `r_type` and `r_tags` provided as options parameters to `s3_download` and its result is stored in `output`. | ||
|
|
||
| If the command fails, i.e. `!output.status.success()` is true, an `Error` is returned. | ||
|
|
||
| Else, the command succeeds and the output of the command is taken from stdout, formatted and returned as a `PathBuf`. | ||
|
|
||
| ``` | ||
| pub fn s3_download(r_type: &str, r_tags: Option<&str>) -> Result<PathBuf, Error> { | ||
| let dld_script = format!( | ||
| "{}/../../tests/tools/s3_download.py", | ||
| env!("CARGO_MANIFEST_DIR") | ||
| ); | ||
|
|
||
| let output = Command::new(dld_script.as_str()) | ||
| .arg("-t") | ||
| .arg(r_type) | ||
| .arg("--tags") | ||
| .arg(r_tags.unwrap_or("{}")) | ||
| .arg("-1") | ||
| .output() | ||
| .expect("failed to execute process"); | ||
|
|
||
| if !output.status.success() { | ||
| return Err(Error::DownloadError( | ||
| String::from_utf8(output.stderr).unwrap(), | ||
| )); | ||
| } | ||
|
|
||
| let res: String = String::from_utf8(output.stdout) | ||
| .unwrap() | ||
| .split('\n') | ||
| .map(String::from) | ||
| .next() | ||
| .ok_or_else(|| Error::DownloadError(String::from("Not found.")))?; | ||
| Ok(PathBuf::from(res)) | ||
| } | ||
| ``` | ||
| This is also accompanied by a unit test that checks whether the `s3_download` fails succesfully on certain inputs, like a blank string or an invalid resource type. | ||
|
|
||
| ``` | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_error_cases() { | ||
| assert!(matches!( | ||
| s3_download("", None).unwrap_err(), | ||
| Error::DownloadError(e) if e.contains("Missing required parameter") | ||
| )); | ||
|
|
||
| assert!(matches!( | ||
| s3_download("random", None).unwrap_err(), | ||
| Error::DownloadError(e) if e.contains("No resources found") | ||
| )); | ||
| } | ||
| } | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Testing