33
44Describe ' tests for resource discovery' {
55 BeforeAll {
6- $env: DSC_RESOURCE_PATH = $testdrive
7-
86 $script :lookupTableFilePath = if ($IsWindows ) {
97 Join-Path $env: LocalAppData " dsc\AdaptedResourcesLookupTable.json"
108 } else {
@@ -16,10 +14,6 @@ Describe 'tests for resource discovery' {
1614 Remove-Item - Path " $testdrive /test.dsc.resource.*" - ErrorAction SilentlyContinue
1715 }
1816
19- AfterAll {
20- $env: DSC_RESOURCE_PATH = $null
21- }
22-
2317 It ' Use DSC_RESOURCE_PATH instead of PATH when defined' {
2418 $resourceJson = @'
2519 {
@@ -31,75 +25,85 @@ Describe 'tests for resource discovery' {
3125 }
3226 }
3327'@
34-
35- Set-Content - Path " $testdrive /test.dsc.resource.json" - Value $resourceJson
36- $resources = dsc resource list | ConvertFrom-Json
37- $resources.Count | Should - Be 1
38- $resources.type | Should - BeExactly ' DSC/TestPathResource'
28+ try {
29+ $oldPath = $env: PATH
30+ $env: DSC_RESOURCE_PATH = $testdrive
31+ Set-Content - Path " $testdrive /test.dsc.resource.json" - Value $resourceJson
32+ $resources = dsc resource list | ConvertFrom-Json
33+ $resources.Count | Should - Be 1
34+ $resources.type | Should - BeExactly ' DSC/TestPathResource'
35+ }
36+ finally {
37+ $env: PATH = $oldPath
38+ $env: DSC_RESOURCE_PATH = $null
39+ }
3940 }
4041
41- It ' support discovering <extension>' - TestCases @ (
42- @ { extension = ' yaml' }
43- @ { extension = ' yml' }
44- ) {
45- param ($extension )
46-
47- $resourceYaml = @'
48- $schema: https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json
49- type: DSC/TestYamlResource
50- version: 0.1.0
51- get:
52- executable: dsc
42+ Context ' Forced discovery using $testdrive' {
43+ BeforeAll {
44+ $env: DSC_RESOURCE_PATH = $testdrive
45+ }
46+
47+ AfterAll {
48+ $env: DSC_RESOURCE_PATH = $null
49+ }
50+
51+ It ' support discovering <extension>' - TestCases @ (
52+ @ { extension = ' yaml' }
53+ @ { extension = ' yml' }
54+ ) {
55+ param ($extension )
56+
57+ $resourceYaml = @'
58+ $schema: https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json
59+ type: DSC/TestYamlResource
60+ version: 0.1.0
61+ get:
62+ executable: dsc
5363'@
5464
55- Set-Content - Path " $testdrive /test.dsc.resource.$extension " - Value $resourceYaml
56- $resources = dsc resource list | ConvertFrom-Json
57- $resources.Count | Should - Be 1
58- $resources.type | Should - BeExactly ' DSC/TestYamlResource'
59- }
65+ Set-Content - Path " $testdrive /test.dsc.resource.$extension " - Value $resourceYaml
66+ $resources = dsc resource list | ConvertFrom-Json
67+ $resources.Count | Should - Be 1
68+ $resources.type | Should - BeExactly ' DSC/TestYamlResource'
69+ }
6070
61- It ' does not support discovering a file with an extension that is not json or yaml' {
62- param ($extension )
71+ It ' does not support discovering a file with an extension that is not json or yaml' {
72+ param ($extension )
6373
64- $resourceInput = @'
65- $schema: https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json
66- type: DSC/TestYamlResource
67- version: 0.1.0
68- get:
69- executable: dsc
74+ $resourceInput = @'
75+ $schema: https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json
76+ type: DSC/TestYamlResource
77+ version: 0.1.0
78+ get:
79+ executable: dsc
7080'@
7181
72- Set-Content - Path " $testdrive /test.dsc.resource.txt" - Value $resourceInput
73- $resources = dsc resource list | ConvertFrom-Json
74- $resources.Count | Should - Be 0
75- }
82+ Set-Content - Path " $testdrive /test.dsc.resource.txt" - Value $resourceInput
83+ $resources = dsc resource list | ConvertFrom-Json
84+ $resources.Count | Should - Be 0
85+ }
7686
77- It ' warns on invalid semver' {
78- $manifest = @'
79- {
80- "$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
81- "type": "Test/InvalidSemver",
82- "version": "1.1.0..1",
83- "get": {
84- "executable": "dsctest"
85- },
86- "schema": {
87- "command": {
87+ It ' warns on invalid semver' {
88+ $manifest = @'
89+ {
90+ "$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
91+ "type": "Test/InvalidSemver",
92+ "version": "1.1.0..1",
93+ "get": {
8894 "executable": "dsctest"
95+ },
96+ "schema": {
97+ "command": {
98+ "executable": "dsctest"
99+ }
89100 }
90101 }
91- }
92102'@
93- $oldPath = $env: DSC_RESOURCE_PATH
94- try {
95- $env: DSC_RESOURCE_PATH = $testdrive
96103 Set-Content - Path " $testdrive /test.dsc.resource.json" - Value $manifest
97104 $null = dsc resource list 2> " $testdrive /error.txt"
98105 " $testdrive /error.txt" | Should - FileContentMatchExactly ' WARN.*?does not use semver' - Because (Get-Content - Raw " $testdrive /error.txt" )
99106 }
100- finally {
101- $env: DSC_RESOURCE_PATH = $oldPath
102- }
103107 }
104108
105109 It ' Ensure List operation populates adapter lookup table' {
@@ -296,4 +300,47 @@ Describe 'tests for resource discovery' {
296300 $env: DSC_RESOURCE_PATH = $null
297301 }
298302 }
303+
304+ It ' Resource discovery can be set to <mode>' - TestCases @ (
305+ @ { namespace = ' Microsoft.DSC' ; mode = ' preDeployment' }
306+ @ { namespace = ' Microsoft.DSC' ; mode = ' duringDeployment' }
307+ @ { namespace = ' Ignore' ; mode = ' ignore' }
308+ ) {
309+ param ($namespace , $mode )
310+
311+ $guid = (New-Guid ).Guid.Replace(' -' , ' ' )
312+ $manifestPath = Join-Path (Split-Path (Get-Command dscecho - ErrorAction Stop).Source - Parent) echo.dsc.resource.json
313+
314+ $config_yaml = @"
315+ `$ schema: https://aka.ms/dsc/schemas/v3/bundled/config/manifest.json
316+ metadata:
317+ ${namespace} :
318+ resourceDiscovery: $mode
319+ resources:
320+ - type: Test/CopyResource
321+ name: This should be found and executed
322+ properties:
323+ sourceFile: $manifestPath
324+ typeName: "Test/$guid "
325+ - type: Test/$guid
326+ name: This is the new resource
327+ properties:
328+ output: Hello World
329+ "@
330+ $out = dsc - l trace config get - i $config_yaml 2> " $testdrive /tracing.txt"
331+ $traceLog = Get-Content - Raw - Path " $testdrive /tracing.txt"
332+ if ($mode -ne ' duringDeployment' ) {
333+ $LASTEXITCODE | Should - Be 2
334+ $out | Should - BeNullOrEmpty
335+ $traceLog | Should -Match " ERROR.*?Resource not found: Test/$guid "
336+ $traceLog | Should -Not -Match " Invoking get for 'Test/CopyResource'"
337+ } else {
338+ $LASTEXITCODE | Should - Be 0 - Because (Get-Content - Raw - Path " $testdrive /tracing.txt" )
339+ $output = $out | ConvertFrom-Json
340+ $output.results [0 ].result.actualState.typeName | Should - BeExactly " Test/$guid " - Because $out
341+ $output.results [1 ].result.actualState.output | Should - BeExactly ' Hello World' - Because $out
342+ $traceLog | Should -Match " Invoking get for 'Test/$guid '"
343+ $traceLog | Should -Match " Skipping resource discovery due to 'resourceDiscovery' mode set to 'DuringDeployment'"
344+ }
345+ }
299346}
0 commit comments