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

Set a non-zero exit code when a resource not found #561

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion dsc/assertion.dsc.resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"3": "JSON Serialization error",
"4": "Invalid input format",
"5": "Resource instance failed schema validation",
"6": "Command cancelled"
"6": "Command cancelled",
"7": "Resource not found"
},
"validate": {
"executable": "dsc",
Expand Down
3 changes: 2 additions & 1 deletion dsc/group.dsc.resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"3": "JSON Serialization error",
"4": "Invalid input format",
"5": "Resource instance failed schema validation",
"6": "Command cancelled"
"6": "Command cancelled",
"7": "Resource not found"
},
"validate": {
"executable": "dsc",
Expand Down
3 changes: 2 additions & 1 deletion dsc/parallel.dsc.resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"3": "JSON Serialization error",
"4": "Invalid input format",
"5": "Resource instance failed schema validation",
"6": "Command cancelled"
"6": "Command cancelled",
"7": "Resource not found"
},
"validate": {
"executable": "dsc",
Expand Down
16 changes: 8 additions & 8 deletions dsc/src/resource_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

use crate::args::OutputFormat;
use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_JSON_ERROR, add_type_name_to_json, write_output};
use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_JSON_ERROR, EXIT_DSC_RESOURCE_NOT_FOUND, add_type_name_to_json, write_output};
use dsc_lib::configure::config_doc::{Configuration, ExecutionKind};
use dsc_lib::configure::add_resource_export_results_to_configuration;
use dsc_lib::dscresources::{resource_manifest::Kind, invoke_result::{GetResult, ResourceGetResponse}};
Expand All @@ -18,7 +18,7 @@ use std::process::exit;
pub fn get(dsc: &DscManager, resource_type: &str, mut input: String, format: &Option<OutputFormat>) {
let Some(mut resource) = get_resource(dsc, resource_type) else {
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
return
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
};

debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
Expand Down Expand Up @@ -60,7 +60,7 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: &Option<OutputForm
let mut input = String::new();
let Some(mut resource) = get_resource(dsc, resource_type) else {
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
return
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
};

debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
Expand Down Expand Up @@ -112,7 +112,7 @@ pub fn set(dsc: &DscManager, resource_type: &str, mut input: String, format: &Op

let Some(mut resource) = get_resource(dsc, resource_type) else {
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
return
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
};

debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
Expand Down Expand Up @@ -158,7 +158,7 @@ pub fn test(dsc: &DscManager, resource_type: &str, mut input: String, format: &O

let Some(mut resource) = get_resource(dsc, resource_type) else {
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
return
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
};

debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
Expand Down Expand Up @@ -199,7 +199,7 @@ pub fn test(dsc: &DscManager, resource_type: &str, mut input: String, format: &O
pub fn delete(dsc: &DscManager, resource_type: &str, mut input: String) {
let Some(mut resource) = get_resource(dsc, resource_type) else {
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
return
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
};

debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as);
Expand Down Expand Up @@ -230,7 +230,7 @@ pub fn delete(dsc: &DscManager, resource_type: &str, mut input: String) {
pub fn schema(dsc: &DscManager, resource_type: &str, format: &Option<OutputFormat>) {
let Some(resource) = get_resource(dsc, resource_type) else {
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
return
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
};
if resource.kind == Kind::Adapter {
error!("Can not perform this operation on the adapter {} itself", resource.type_name);
Expand Down Expand Up @@ -260,7 +260,7 @@ pub fn export(dsc: &mut DscManager, resource_type: &str, format: &Option<OutputF
let mut input = String::new();
let Some(dsc_resource) = get_resource(dsc, resource_type) else {
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
return
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
};

if dsc_resource.kind == Kind::Adapter {
Expand Down
1 change: 1 addition & 0 deletions dsc/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub const EXIT_JSON_ERROR: i32 = 3;
pub const EXIT_INVALID_INPUT: i32 = 4;
pub const EXIT_VALIDATION_FAILED: i32 = 5;
pub const EXIT_CTRL_C: i32 = 6;
pub const EXIT_DSC_RESOURCE_NOT_FOUND: i32 = 7;

pub const DSC_CONFIG_ROOT: &str = "DSC_CONFIG_ROOT";
pub const DSC_TRACE_LEVEL: &str = "DSC_TRACE_LEVEL";
Expand Down
18 changes: 18 additions & 0 deletions dsc/tests/dsc_discovery.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,22 @@ Describe 'tests for resource discovery' {
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly "Lookup table found resource 'testclassresource/testclassresource' in adapter 'Microsoft.DSC/PowerShell'"
$env:PSModulePath = $oldPSModulePath
}

It 'Verify non-zero exit code when resource not found' {

$out = dsc resource get -r abc/def
$LASTEXITCODE | Should -Be 7
$out = dsc resource get --all -r abc/def
$LASTEXITCODE | Should -Be 7
$out = 'abc' | dsc resource set -r abc/def
$LASTEXITCODE | Should -Be 7
$out = 'abc' | dsc resource test -r abc/def
$LASTEXITCODE | Should -Be 7
$out = 'abc' | dsc resource delete -r abc/def
$LASTEXITCODE | Should -Be 7
$out = dsc resource export -r abc/def
$LASTEXITCODE | Should -Be 7
$out = dsc resource schema -r abc/def
$LASTEXITCODE | Should -Be 7
}
}
Loading