-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathdsc_extension_discover.tests.ps1
More file actions
133 lines (125 loc) · 5.08 KB
/
dsc_extension_discover.tests.ps1
File metadata and controls
133 lines (125 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
BeforeDiscovery {
try {
$windowWidth = [Console]::WindowWidth
} catch {
$consoleUnavailable = $true
}
}
Describe 'Discover extension tests' {
BeforeAll {
$oldPath = $env:PATH
$toolPath = Resolve-Path -Path "$PSScriptRoot/../../extensions/test/discover"
$env:PATH = "$toolPath" + [System.IO.Path]::PathSeparator + $oldPath
$env:DSC_TRACE_LEVEL = 'error'
}
AfterAll {
$env:PATH = $oldPath
$env:DSC_TRACE_LEVEL = $null
}
It 'Discover extensions' {
$out = dsc extension list | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
if ($IsWindows) {
$out.Count | Should -Be 3 -Because ($out | Out-String)
$out[0].type | Should -Be 'Microsoft.DSC.Extension/Bicep'
$out[0].version | Should -Be '0.1.0'
$out[0].capabilities | Should -BeExactly @('import')
$out[0].manifest | Should -Not -BeNullOrEmpty
$out[1].type | Should -Be 'Microsoft.Windows.Appx/Discover'
$out[1].version | Should -Be '0.1.0'
$out[1].capabilities | Should -BeExactly @('discover')
$out[1].manifest | Should -Not -BeNullOrEmpty
$out[2].type | Should -BeExactly 'Test/Discover'
$out[2].version | Should -BeExactly '0.1.0'
$out[2].capabilities | Should -BeExactly @('discover')
$out[2].manifest | Should -Not -BeNullOrEmpty
} else {
$out.Count | Should -Be 2 -Because ($out | Out-String)
$out[0].type | Should -Be 'Microsoft.DSC.Extension/Bicep'
$out[0].version | Should -Be '0.1.0'
$out[0].capabilities | Should -BeExactly @('import')
$out[0].manifest | Should -Not -BeNullOrEmpty
$out[1].type | Should -BeExactly 'Test/Discover'
$out[1].version | Should -BeExactly '0.1.0'
$out[1].capabilities | Should -BeExactly @('discover')
$out[1].manifest | Should -Not -BeNullOrEmpty
}
}
It 'Filtering works for extension discovered resources' {
$out = dsc resource list '*Discovered*' | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.Count | Should -Be 2
$out[0].type | Should -Be 'Test/DiscoveredOne'
$out[1].type | Should -Be 'Test/DiscoveredTwo'
$out[0].kind | Should -Be 'Resource'
$out[1].kind | Should -Be 'Resource'
}
It 'Extension resources can be used in config' {
$config_yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
contentVersion: 1.0.0.0
resources:
- name: One
type: Test/DiscoveredOne
- name: Two
type: Test/DiscoveredTwo
"@
$out = dsc config get -i $config_yaml | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results.Count | Should -Be 2
$out.results[0].type | Should -BeExactly 'Test/DiscoveredOne'
$out.results[0].result.actualState.Output | Should -BeExactly 'Hello One'
$out.results[1].type | Should -BeExactly 'Test/DiscoveredTwo'
$out.results[1].result.actualState.Output | Should -BeExactly 'Hello Two'
}
It 'Relative path from discovery will fail' {
$extension_json = @'
{
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
"type": "Test/DiscoverRelative",
"version": "0.1.0",
"description": "Test discover resource",
"discover": {
"executable": "pwsh",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-Command",
"./discover.ps1",
"-RelativePath"
]
}
}
'@
Set-Content -Path "$TestDrive/test.dsc.extension.json" -Value $extension_json
Copy-Item -Path "$toolPath/discover.ps1" -Destination $TestDrive | Out-Null
Copy-Item -Path "$toolPath/resources" -Destination $TestDrive -Recurse | Out-Null
$env:DSC_RESOURCE_PATH = "$TestDrive" + [System.IO.Path]::PathSeparator + (Split-Path (Get-Command pwsh).Source -Parent)
try {
$out = dsc extension list | ConvertFrom-Json
$out.Count | Should -Be 1 -Because ($out | Out-String)
$out.type | Should -Be 'Test/DiscoverRelative'
$out = dsc resource list 2> $TestDrive/error.log
$LASTEXITCODE | Should -Be 0
$out | Should -BeNullOrEmpty
$errorMessage = Get-Content -Path "$TestDrive/error.log" -Raw
$errorMessage | Should -BeLike '*is not an absolute path*'
} finally {
$env:DSC_RESOURCE_PATH = $null
}
}
It 'Table can be not truncated' -Skip:($consoleUnavailable) {
$output = dsc extension list --output-format table-no-truncate
$LASTEXITCODE | Should -Be 0
$foundWideLine = $false
foreach ($line in $output) {
if ($line.Length -gt $windowWidth) {
$foundWideLine = $true
}
}
$foundWideLine | Should -BeTrue
}
}