-
Notifications
You must be signed in to change notification settings - Fork 36
Description of resource_download.rs file #11
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
Tushar2023
wants to merge
1
commit into
codenet:main
Choose a base branch
from
Tushar2023: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,123 @@ | ||
# [Resource Download](/vmm-reference/src/utils/src/resource_download.rs) | ||
|
||
`resource_download.rs` calls [s3_download.py](/vmm-reference/tests/tools/s3_download.py) to download a resource from resource_manifest.json, which can be found in /vmm-reference/tests/tools/s3 folder. | ||
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 |
||
|
||
```rs | ||
codenet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub enum Error { | ||
DownloadError(String), | ||
} | ||
``` | ||
codenet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Defining an enum for DownloadError that would be used to return error value in case resource download fails. | ||
|
||
codenet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<br> | ||
|
||
``` rs | ||
pub fn s3_download(r_type: &str, r_tags: Option<&str>) -> Result<PathBuf, Error> | ||
``` | ||
|
||
1. `r_type`: argument for the type of the resource {"kernel, "core"} | ||
|
||
2. `r_tags`: Optional tags to filter the resources for example: "halt-after-boot: true", "image format: elf" etc. | ||
|
||
3. `Result<PathBuf, Error>`: Result<T,E> is a type that returns type T value on success, type E error otherwise. | ||
|
||
<br> | ||
|
||
```rs | ||
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)) | ||
} | ||
``` | ||
|
||
1. `dld_script`: variable to store the relative path to s3_download.py | ||
|
||
2. `output`: store the output of the command run to download resources of the given type and according to the given tags, `Command` is an rust api for a process builder. | ||
|
||
3. if the download fails for some reason the function returns the error received | ||
|
||
4. if the download is a success the function returns the path of the resultant image, the path can be used to run the image. `PathBuf` is a rust struct to store mutable paths which can be dereferenced. | ||
|
||
<br> | ||
|
||
```rs | ||
#[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") | ||
)); | ||
} | ||
} | ||
``` | ||
|
||
Unit test for checking cases when s3_download returns errors | ||
|
||
The above function checks for error in case of missing resource type parameter(which is a required parameter) and it also checks for error in case of no resource is found to download. | ||
|
||
<br> | ||
|
||
## Usage | ||
|
||
1. Multiple usages in integration tests: vmm-reference/src/vmm/tests/integration_tests.rs | ||
|
||
```rs | ||
fn test_dummy_vmm_elf() { | ||
let tags = r#" | ||
{ | ||
"halt_after_boot": true, | ||
"image_format": "elf" | ||
} | ||
"#; | ||
|
||
let elf_halt = s3_download("kernel", Some(tags)).unwrap(); | ||
run_vmm(elf_halt); | ||
} | ||
``` | ||
2. Multiple Usages in vmm-reference/src/vmm/src/lib.rs | ||
|
||
```rs | ||
fn default_bzimage_path() -> PathBuf { | ||
let tags = r#" | ||
{ | ||
"halt_after_boot": true, | ||
"image_format": "bzimage" | ||
} | ||
"#; | ||
s3_download("kernel", Some(tags)).unwrap() | ||
} | ||
``` |
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.
Uh oh!
There was an error while loading. Please reload this page.