Skip to content

Commit 7985e50

Browse files
authored
Merge pull request #1445 from SteveL-MSFT/discover-ignore
Fix not failing discovery when finding unsupported manifests
2 parents e1d03cd + cace460 commit 7985e50

5 files changed

Lines changed: 96 additions & 3 deletions

File tree

dsc/tests/dsc_discovery.tests.ps1

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,31 @@ Describe 'tests for resource discovery' {
342342
$traceLog | Should -Match "Skipping resource discovery due to 'resourceDiscovery' mode set to 'DuringDeployment'"
343343
}
344344
}
345+
346+
It 'Invalid resource manifest will generate info message' {
347+
$invalidManifest = @'
348+
{
349+
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
350+
"type": "Test/InvalidManifest",
351+
"version": "0.1.0",
352+
"get": {
353+
"executable": "dsctest",
354+
"unexpectedField": "This field is not expected in the get section and should be ignored by the discovery process"
355+
},
356+
"newProperty": "This property is not expected in the manifest and should be ignored by the discovery process"
357+
}
358+
'@
359+
Set-Content -Path "$testdrive/test.dsc.resource.json" -Value $invalidManifest
360+
try {
361+
$env:DSC_RESOURCE_PATH = $testdrive + [System.IO.Path]::PathSeparator + $env:PATH
362+
363+
$out = dsc -l info resource list 'Test/InvalidManifest' 2> "$testdrive/error.txt" | ConvertFrom-Json
364+
$LASTEXITCODE | Should -Be 0
365+
$out | Should -BeNullOrEmpty -Because (Get-Content -Raw -Path "$testdrive/error.txt")
366+
$errorLog = Get-Content -Raw -Path "$testdrive/error.txt"
367+
$errorLog | Should -BeLike "*INFO Failed to load manifest: Invalid manifest for resource '*test.dsc.resource.json'*" -Because $errorLog
368+
} finally {
369+
$env:DSC_RESOURCE_PATH = $null
370+
}
371+
}
345372
}

dsc/tests/dsc_extension_discover.tests.ps1

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,61 @@ Describe 'Discover extension tests' {
156156
$env:DSC_RESOURCE_PATH = $null
157157
}
158158
}
159+
160+
It 'Invalid manifest from extension discovery should not fail overall discovery' {
161+
$invalidManifest = @'
162+
{
163+
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
164+
"type": "Test/InvalidManifest",
165+
"version": "0.1.0",
166+
"get": {
167+
"executable": "dsctest",
168+
"unexpectedField": "This field is not expected in the get section and should be ignored by the discovery process"
169+
},
170+
"newProperty": "This property is not expected in the manifest and should be ignored by the discovery process"
171+
}
172+
'@
173+
$resourceScript = @'
174+
$resource = @{
175+
manifestPath = "$env:TestDrive" + [System.IO.Path]::DirectorySeparatorChar + 'invalidManifest.dsc.resource.json'
176+
}
177+
$resource | ConvertTo-Json -Compress
178+
'@
179+
$extensionManifest = @'
180+
{
181+
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
182+
"type": "Test/DiscoverInvalid",
183+
"version": "0.1.0",
184+
"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.",
185+
"discover": {
186+
"executable": "pwsh",
187+
"args": [
188+
"-NoLogo",
189+
"-NonInteractive",
190+
"-NoProfile",
191+
"-Command",
192+
"./discover.ps1"
193+
]
194+
}
195+
}
196+
'@
197+
198+
Set-Content -Path "$TestDrive/invalidManifest.dsc.resource.json" -Value $invalidManifest
199+
Set-Content -Path "$TestDrive/discover.ps1" -Value $resourceScript
200+
Set-Content -Path "$TestDrive/extension.dsc.extension.json" -Value $extensionManifest
201+
try {
202+
$env:DSC_RESOURCE_PATH = $TestDrive + [System.IO.Path]::PathSeparator + $env:PATH
203+
$env:TestDrive = $TestDrive
204+
$out = dsc -l info resource list 2> $TestDrive/error.log | ConvertFrom-Json
205+
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Path "$TestDrive/error.log" -Raw | Out-String)
206+
# The invalid manifest should be skipped and not included in the discovered resources
207+
foreach ($resource in $out) {
208+
$resource.type | Should -Not -Be 'Test/InvalidManifest'
209+
}
210+
(Get-Content -Path "$TestDrive/error.log" -Raw) | Should -BeLike "*INFO Extension 'Test/DiscoverInvalid' failed to load manifest: Invalid manifest for resource '*invalidManifest.dsc.resource.json'*" -Because (Get-Content -Path "$TestDrive/error.log" -Raw | Out-String)
211+
} finally {
212+
$env:DSC_RESOURCE_PATH = $null
213+
$env:TestDrive = $null
214+
}
215+
}
159216
}

lib/dsc-lib/locales/en-us.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ conditionNotBoolean = "Condition '%{condition}' did not evaluate to a boolean"
138138
conditionNotMet = "Condition '%{condition}' not met, skipping manifest at '%{path}' for resource '%{resource}'"
139139
adaptedResourcePathNotFound = "Adapted resource '%{resource}' path not found: %{path}"
140140
invalidManifestFileName = "Invalid manifest file name '%{path}'"
141+
failedLoadManifest = "Failed to load manifest: %{err}"
141142

142143
[dscresources.commandResource]
143144
invokeGet = "Invoking get for '%{resource}'"
@@ -250,6 +251,7 @@ importNoResults = "Extension '%{extension}' returned no results for import"
250251
secretMultipleLinesReturned = "Extension '%{extension}' returned multiple lines which is not supported for secrets"
251252
importProcessingOutput = "Processing output from extension '%{extension}'"
252253
deprecationMessage = "Extension '%{extension}' is deprecated: %{message}"
254+
failedLoadManifest = "Extension '%{extension}' failed to load manifest: %{err}"
253255

254256
[extensions.extension_manifest]
255257
extensionManifestSchemaTitle = "Extension manifest schema URI"

lib/dsc-lib/src/discovery/command_discovery.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ impl ResourceDiscovery for CommandDiscovery {
267267
// At this point we can't determine whether or not the bad manifest contains
268268
// resource that is requested by resource/config operation
269269
// if it is, then "ResouceNotFound" error will be issued later
270-
// and here we just write as warning
271-
warn!("{e}");
270+
// and here we just write as info
271+
info!("{}", t!("discovery.commandDiscovery.failedLoadManifest", err = e));
272272
continue;
273273
},
274274
};

lib/dsc-lib/src/extensions/discover.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,14 @@ impl DscExtension {
9999
return Err(DscError::Extension(t!("extensions.dscextension.discoverNotAbsolutePath", extension = self.type_name.clone(), path = discover_result.manifest_path.display()).to_string()));
100100
}
101101
// Currently we don't support extensions discovering other extensions
102-
for imported_manifest in load_manifest(&discover_result.manifest_path)? {
102+
let manifests = match load_manifest(&discover_result.manifest_path) {
103+
Ok(manifests) => manifests,
104+
Err(err) => {
105+
info!("{}", t!("extensions.dscextension.failedLoadManifest", extension = self.type_name, err = err));
106+
continue;
107+
}
108+
};
109+
for imported_manifest in manifests {
103110
if let ImportedManifest::Resource(resource) = imported_manifest {
104111
resources.push(resource);
105112
}

0 commit comments

Comments
 (0)