@@ -244,4 +244,80 @@ Describe 'tests for function expressions' {
244244 $LASTEXITCODE | Should - Be 0 - Because (Get-Content $TestDrive / error.log - Raw)
245245 ($out.results [0 ].result.actualState.output | Out-String ) | Should - BeExactly ($expected | Out-String )
246246 }
247+
248+ It ' utcNow function works for: utcNow(<format>)' - TestCases @ (
249+ @ { format = $null }
250+ @ { format = " yyyy-MM-dd" }
251+ @ { format = " yyyy-MM-ddTHH" }
252+ @ { format = " yyyy-MM-ddTHHZ" }
253+ @ { format = " MMM dd, yyyy HH" }
254+ @ { format = " yy-MMMM-dddd tt H" }
255+ @ { format = " MMM ddd zzz" }
256+ @ { format = " YY YYYY MM MMM MMMM" }
257+ ) {
258+ param ($format )
259+
260+ if ($null -eq $format ) {
261+ $expected = (Get-Date - AsUTC).ToString(" o" )
262+ } else {
263+ $expected = (Get-Date - AsUTC).ToString($format )
264+ $format = " '$format '"
265+ }
266+
267+ $config_yaml = @"
268+ `$ schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
269+ parameters:
270+ test:
271+ type: string
272+ defaultValue: "[utcNow($format )]"
273+ resources:
274+ - name: Echo
275+ type: Microsoft.DSC.Debug/Echo
276+ properties:
277+ output: "[parameters('test')]"
278+ "@
279+ $out = dsc - l trace config get - i $config_yaml 2> $TestDrive / error.log
280+ $LASTEXITCODE | Should - Be 0 - Because (Get-Content $TestDrive / error.log - Raw)
281+ # ConvertFrom-Json will convert the date to a DateTime object, so we use regex to capture the string
282+ $out -match ' "output":"(?<date>.*?)"' | Should - BeTrue - Because " Output should contain a date"
283+ $actual = $matches [' date' ]
284+ # since the datetimes might slightly differ, we remove the seconds and milliseconds
285+ $expected = $expected -replace ' :\d+\.\d+Z$' , ' Z'
286+ $actual = $actual -replace ' :\d+\.\d+Z$' , ' Z'
287+ $actual | Should - BeExactly $expected - Because " Expected: '$expected ', Actual: '$actual '"
288+ }
289+
290+ It ' utcNow errors if used not as a parameter default' {
291+ $config_yaml = @"
292+ `$ schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
293+ resources:
294+ - name: Echo
295+ type: Microsoft.DSC.Debug/Echo
296+ properties:
297+ output: "[utcNow()]"
298+ "@
299+ $out = dsc - l trace config get - i $config_yaml 2> $TestDrive / error.log | ConvertFrom-Json
300+ $LASTEXITCODE | Should - Be 2 - Because (Get-Content $TestDrive / error.log - Raw)
301+ (Get-Content $TestDrive / error.log - Raw) | Should -Match ' utcNow function can only be used as a parameter default'
302+ }
303+
304+ It ' uniqueString function works for: <expression>' - TestCases @ (
305+ @ { expression = " [uniqueString('a')]" ; expected = ' cfvwxu6sc4lqo' }
306+ @ { expression = " [uniqueString('a', 'b', 'c')]" ; expected = ' bhw7m6t6ntwd6' }
307+ @ { expression = " [uniqueString('a', 'b', 'c', 'd')]" ; expected = ' yxzg7ur4qetcy' }
308+ ) {
309+ param ($expression , $expected )
310+
311+ $config_yaml = @"
312+ `$ schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
313+ resources:
314+ - name: Echo
315+ type: Microsoft.DSC.Debug/Echo
316+ properties:
317+ output: "$expression "
318+ "@
319+ $out = dsc - l trace config get - i $config_yaml 2> $TestDrive / error.log | ConvertFrom-Json
320+ $LASTEXITCODE | Should - Be 0 - Because (Get-Content $TestDrive / error.log - Raw)
321+ $out.results [0 ].result.actualState.output | Should - BeExactly $expected
322+ }
247323}
0 commit comments