Skip to content

Commit f462fed

Browse files
author
Steve Lee (POWERSHELL HE/HIM) (from Dev Box)
committed
Fix discovering incompatible resource manifests to be non-fatal
1 parent 82e71c8 commit f462fed

5 files changed

Lines changed: 74 additions & 11 deletions

File tree

dsc/tests/dsc_discovery.tests.ps1

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,16 @@ Describe 'tests for resource discovery' {
317317
}
318318
'@
319319
Set-Content -Path "$testdrive/test.dsc.resource.json" -Value $invalidManifest
320-
$out = dsc -l info resource list 'Test/InvalidManifest' 2> "$testdrive/error.txt" | ConvertFrom-Json
321-
$LASTEXITCODE | Should -Be 0
322-
$out | Should -BeNullOrEmpty -Because (Get-Content -Raw -Path "$testdrive/error.txt")
323-
Get-Content -Raw -Path "$testdrive/error.txt" | Should -Match "INFO Failed to load manifest for resource '.*test.dsc.resource.json':" -Because (Get-Content -Raw -Path "$testdrive/error.txt")
320+
try {
321+
$env:DSC_RESOURCE_PATH = $testdrive + [System.IO.Path]::PathSeparator + $env:PATH
322+
323+
$out = dsc -l info resource list 'Test/InvalidManifest' 2> "$testdrive/error.txt" | ConvertFrom-Json
324+
$LASTEXITCODE | Should -Be 0
325+
$out | Should -BeNullOrEmpty -Because (Get-Content -Raw -Path "$testdrive/error.txt")
326+
$errorLog = Get-Content -Raw -Path "$testdrive/error.txt"
327+
$errorLog | Should -Match "INFO Failed to load manifest: Manifest: Invalid manifest for resource '.*test.dsc.resource.json'" -Because $errorLog
328+
} finally {
329+
$env:DSC_RESOURCE_PATH = $null
330+
}
324331
}
325332
}

dsc/tests/dsc_extension_discover.tests.ps1

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,61 @@ Describe 'Discover extension tests' {
101101
}
102102
$foundWideLine | Should -BeTrue
103103
}
104+
105+
It 'Invalid manifest from extension discovery should not fail overall discovery' {
106+
$invalidManifest = @'
107+
{
108+
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
109+
"type": "Test/InvalidManifest",
110+
"version": "0.1.0",
111+
"get": {
112+
"executable": "dsctest",
113+
"unexpectedField": "This field is not expected in the get section and should be ignored by the discovery process"
114+
},
115+
"newProperty": "This property is not expected in the manifest and should be ignored by the discovery process"
116+
}
117+
'@
118+
$resourceScript = @'
119+
$resource = @{
120+
manifestPath = "$env:TestDrive" + [System.IO.Path]::DirectorySeparatorChar + 'invalidManifest.dsc.resource.json'
121+
}
122+
$resource | ConvertTo-Json -Compress
123+
'@
124+
$extensionManifest = @'
125+
{
126+
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
127+
"type": "Test/DiscoverInvalid",
128+
"version": "0.1.0",
129+
"description": "Test discover resource, this is a really long description to test that the table can be rendered without truncating the description text from this extension.",
130+
"discover": {
131+
"executable": "pwsh",
132+
"args": [
133+
"-NoLogo",
134+
"-NonInteractive",
135+
"-NoProfile",
136+
"-Command",
137+
"./discover.ps1"
138+
]
139+
}
140+
}
141+
'@
142+
143+
Set-Content -Path "$TestDrive/invalidManifest.dsc.resource.json" -Value $invalidManifest
144+
Set-Content -Path "$TestDrive/discover.ps1" -Value $resourceScript
145+
Set-Content -Path "$TestDrive/extension.dsc.extension.json" -Value $extensionManifest
146+
try {
147+
$env:DSC_RESOURCE_PATH = $TestDrive + [System.IO.Path]::PathSeparator + $env:PATH
148+
$env:TestDrive = $TestDrive
149+
$out = dsc -l info resource list 2> $TestDrive/error.log | ConvertFrom-Json
150+
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Path "$TestDrive/error.log" -Raw | Out-String)
151+
# The invalid manifest should be skipped and not included in the discovered resources
152+
foreach ($resource in $out) {
153+
$resource.type | Should -Not -Be 'Test/InvalidManifest'
154+
}
155+
(Get-Content -Path "$TestDrive/error.log" -Raw) | Should -BeLike "*INFO Extension 'Test/DiscoverInvalid' failed: Manifest: Invalid manifest for resource '*invalidManifest.dsc.resource.json'*" -Because (Get-Content -Path "$TestDrive/error.log" -Raw | Out-String)
156+
} finally {
157+
$env:DSC_RESOURCE_PATH = $null
158+
$env:TestDrive = $null
159+
}
160+
}
104161
}

dsc_lib/locales/en-us.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ extensionResourceFound = "Extension found resource '%{resource}'"
9191
callingExtension = "Calling extension '%{extension}' to discover resources"
9292
extensionFoundResources = "Extension '%{extension}' found %{count} resources"
9393
invalidManifestVersion = "Manifest '%{path}' has invalid version: %{err}"
94-
failedImportManifest = "Failed to import manifest for resource '%{resource}': %{err}"
95-
failedLoadManifest = "Failed to load manifest for resource '%{resource}': %{err}"
94+
failedLoadManifest = "Failed to load manifest: %{err}"
9695

9796
[dscresources.commandResource]
9897
invokeGet = "Invoking get for '%{resource}'"
@@ -175,7 +174,7 @@ resourceManifestSchemaDescription = "Defines the JSON Schema the resource manife
175174
discoverNoResults = "No results returned for discovery extension '%{extension}'"
176175
discoverNotAbsolutePath = "Resource path from extension '%{extension}' is not an absolute path: %{path}"
177176
extensionReturned = "Extension '%{extension}' returned line: %{line}"
178-
failedImportManifest = "Failed to import manifest for resource '%{resource}': %{err}"
177+
failedLoadManifest = "Extension '%{extension}' failed: %{err}"
179178

180179
[extensions.extension_manifest]
181180
extensionManifestSchemaTitle = "Extension manifest schema URI"

dsc_lib/src/discovery/command_discovery.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ impl ResourceDiscovery for CommandDiscovery {
249249
// resource that is requested by resource/config operation
250250
// if it is, then "ResourceNotFound" error will be issued later
251251
// and here we just write as information
252-
info!("{}", t!("discovery.commandDiscovery.failedLoadManifest", resource = path.to_string_lossy(), err = e).to_string());
252+
info!("{}", t!("discovery.commandDiscovery.failedLoadManifest", err = e).to_string());
253253
continue;
254254
},
255255
};
@@ -280,7 +280,7 @@ impl ResourceDiscovery for CommandDiscovery {
280280
let manifest = match import_manifest(manifest.clone()) {
281281
Ok(manifest) => manifest,
282282
Err(err) => {
283-
info!("{}", t!("discovery.commandDiscovery.failedImportManifest", resource = resource.type_name, err = err).to_string());
283+
info!("{}", t!("discovery.commandDiscovery.failedLoadManifest", err = err));
284284
continue;
285285
}
286286
};

dsc_lib/src/extensions/dscextension.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl DscExtension {
107107
}
108108
};
109109
if !Path::new(&discover_result.manifest_path).is_absolute() {
110-
return Err(DscError::Extension(t!("extensions.dscextension.discoverNotAbsolutePath", extension = self.type_name.clone(), path = discover_result.manifest_path.clone()).to_string()));
110+
return Err(DscError::Extension(t!("extensions.dscextension.discoverNotAbsolutePath", extension = self.type_name.clone()).to_string()));
111111
}
112112
let manifest_path = Path::new(&discover_result.manifest_path);
113113
// Currently we don't support extensions discovering other extensions
@@ -116,7 +116,7 @@ impl DscExtension {
116116
Ok(_) => continue,
117117
Err(err) => {
118118
// For invalid manifest, we write an information and skip it
119-
info!("{}", t!("extensions.dscextension.failedImportManifest", resource = manifest_path.to_string_lossy(), err = err).to_string());
119+
info!("{}", t!("extensions.dscextension.failedLoadManifest", extension = self.type_name, path = discover_result.manifest_path.to_string(), err = err));
120120
continue;
121121
}
122122
};

0 commit comments

Comments
 (0)