-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCloudbaseInit.Tests.ps1
More file actions
460 lines (399 loc) · 17.6 KB
/
Copy pathCloudbaseInit.Tests.ps1
File metadata and controls
460 lines (399 loc) · 17.6 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
$global:here = Split-Path -Parent $MyInvocation.MyCommand.Path
$ErrorActionPreference = "Stop"
Import-Module "${here}/ini.psm1"
$REG_KEY_WOW_FOLDER = "WOW6432Node\"
$REG_KEY_FOLDER = ""
if ($ENV:TEST_ARCHITECTURE -eq "x86") {
$REG_KEY_FOLDER = $REG_KEY_WOW_FOLDER
}
$global:CLOUDBASE_INIT_REGISTRY_PATH = "HKLM:\SOFTWARE\${REG_KEY_FOLDER}Cloudbase Solutions\Cloudbase-Init\b9517879-4e93-4a1a-9073-4ae0ddfac27c\Plugins"
$global:CLOUDBASE_INIT_NET_NAME = "cbs_init_eth0"
$global:CLOUDBASE_INIT_NET_STATIC_IP = "10.196.59.2"
function before.cloudbaseinit.plugins.common.mtu.MTUPlugin {
# NOOP
}
function after.cloudbaseinit.plugins.common.mtu.MTUPlugin {
# NOOP
# MTU plugin retrieves the data from DHCP and is not available
# in the test environment
}
function before.cloudbaseinit.plugins.windows.ntpclient.NTPClientPlugin {
It "w32time service should exist" {
{ Get-Service "w32time" -ErrorAction Stop } | Should -Not -Throw
}
}
function after.cloudbaseinit.plugins.windows.ntpclient.NTPClientPlugin {
It "w32time service should be running" {
$status = (Get-Service "w32time" -ErrorAction Stop).Status
$status | Should -Be "Running"
}
}
function before.cloudbaseinit.plugins.windows.sanpolicy.SANPolicyPlugin {
It "Get-StorageSetting should return" {
{ (Get-StorageSetting).NewDiskPolicy } | Should -Not -Throw
}
}
function after.cloudbaseinit.plugins.windows.sanpolicy.SANPolicyPlugin {
It "Get-StorageSetting should be OnlineAll" {
$sanPolicy = (Get-StorageSetting).NewDiskPolicy
$sanPolicy | Should -Be "OnlineAll"
}
}
function before.cloudbaseinit.plugins.windows.displayidletimeout.DisplayIdleTimeoutConfigPlugin {
It "powercfg returns SUB_VIDEO VIDEOIDLE" {
$powerCfgOut = $(cmd /c 'powercfg /query SCHEME_CURRENT SUB_VIDEO VIDEOIDLE') `
| Select-String -Pattern "Current AC Power Setting Index:"
$powerCfgOut.Matches.Count | Should -Be 1
}
}
function after.cloudbaseinit.plugins.windows.displayidletimeout.DisplayIdleTimeoutConfigPlugin {
It "powercfg returns SUB_VIDEO VIDEOIDLE 0" {
$powerCfgOut = $(cmd /c 'powercfg /query SCHEME_CURRENT SUB_VIDEO VIDEOIDLE') `
| Select-String -Pattern "Current AC Power Setting Index: 0x00000000"
$powerCfgOut.Matches.Count | Should -Be 1
}
}
function before.cloudbaseinit.plugins.windows.bootconfig.BootStatusPolicyPlugin {
It "bcdedit returns base identifier" {
$bcdOut = $(cmd /c 'bcdedit /enum {current}') `
| Select-String -Pattern "identifier"
$bcdOut.Matches.Count | Should -Be 1
}
}
function after.cloudbaseinit.plugins.windows.bootconfig.BootStatusPolicyPlugin {
It "bcdedit returns ignoreallfailures" {
$bcdOut = $(cmd /c 'bcdedit /enum {current}') `
| Select-String -Pattern "ignoreallfailures"
$bcdOut.Matches.Count | Should -Be 1
}
}
function before.cloudbaseinit.plugins.common.sethostname.SetHostNamePlugin {
# NOOP
}
function after.cloudbaseinit.plugins.common.sethostname.SetHostNamePlugin {
# NOOP
# SetHostNamePlugin needs a reboot and the reboot is not available
# on the test environment
}
function before.cloudbaseinit.plugins.windows.extendvolumes.ExtendVolumesPlugin {
# NOOP
}
function after.cloudbaseinit.plugins.windows.extendvolumes.ExtendVolumesPlugin {
# NOOP
# ExtendVolumesPlugin needs a disk that has empty unpartitioned space,
# which is not available on the test environment
}
function before.cloudbaseinit.plugins.common.userdata.UserDataPlugin {
It "UserDataPlugin has a clean environment" {
{
$out = net.exe localgroup "windows" 2>&1
if ($LASTEXITCODE) {
throw "Group not found"
}
} | Should -Throw
{
$out = net.exe localgroup "cloud-users" 2>&1
if ($LASTEXITCODE) {
throw "Group not found"
}
} | Should -Throw
{
$out = net.exe user "cloud-config-user" 2>&1
if ($LASTEXITCODE) {
throw "User not found"
}
} | Should -Throw
{ Get-Content -Raw -Path "c:\test_file" -ErrorAction Stop } | Should -Throw
{ Get-Content -Raw -Path "c:\test_append.ps1" -ErrorAction Stop } | Should -Throw
{ Get-ChildItem -Path "c:\runcmd" -ErrorAction Stop } | Should -Throw
}
}
function after.cloudbaseinit.plugins.common.userdata.UserDataPlugin {
It "UserDataPlugin created windows group" {
{
$out = net.exe localgroup "windows" 2>&1
if ($LASTEXITCODE) {
throw "Group not found"
}
} | Should -Not -Throw
}
It "UserDataPlugin created cloud-users group" {
{
$out = net.exe localgroup "cloud-users" 2>&1
if ($LASTEXITCODE) {
throw "Group not found"
}
} | Should -Not -Throw
}
It "UserDataPlugin created cloud-config-user user" {
{
$out = net.exe user "cloud-config-user" 2>&1
if ($LASTEXITCODE) {
throw "User not found"
}
} | Should -Not -Throw
}
It "UserDataPlugin created file c:\test_file" {
{
Get-Content -Raw -Path "c:\test_file" -ErrorAction Stop
} | Should -Not -Throw
}
It "UserDataPlugin created file c:\test_append" {
{
Get-Content -Raw -Path "c:\test_append.ps1" -ErrorAction Stop
} | Should -Not -Throw
}
It "UserDataPlugin created directory c:\runcmd" {
{
Get-ChildItem -Path "c:\runcmd" -ErrorAction Stop
} | Should -Not -Throw
}
}
function before.cloudbaseinit.plugins.windows.winrmlistener.ConfigWinRMListenerPlugin {
}
function after.cloudbaseinit.plugins.windows.winrmlistener.ConfigWinRMListenerPlugin {
}
function before.cloudbaseinit.plugins.common.localscripts.LocalScriptsPlugin {
}
function after.cloudbaseinit.plugins.common.localscripts.LocalScriptsPlugin {
}
function before.cloudbaseinit.plugins.common.trim.TrimConfigPlugin {
}
function after.cloudbaseinit.plugins.common.trim.TrimConfigPlugin {
}
function before.cloudbaseinit.plugins.windows.createuser.CreateUserPlugin {
It "CreateUserPlugin has a clean environment" {
{
$out = net.exe user "Admin" 2>&1
if ($LASTEXITCODE) {
throw "User not found"
}
} | Should -Throw
}
}
function after.cloudbaseinit.plugins.windows.createuser.CreateUserPlugin {
It "CreateUserPlugin created Admin user" {
{
$out = net.exe user "Admin" 2>&1
if ($LASTEXITCODE) {
throw "User not found"
}
} | Should -Not -Throw
}
}
function before.cloudbaseinit.plugins.common.sshpublickeys.SetUserSSHPublicKeysPlugin{
}
function after.cloudbaseinit.plugins.common.sshpublickeys.SetUserSSHPublicKeysPlugin{
}
function before.cloudbaseinit.plugins.common.setuserpassword.SetUserPasswordPlugin {
}
function after.cloudbaseinit.plugins.common.setuserpassword.SetUserPasswordPlugin {
}
function before.cloudbaseinit.plugins.windows.winrmcertificateauth.ConfigWinRMCertificateAuthPlugin {
}
function after.cloudbaseinit.plugins.windows.winrmcertificateauth.ConfigWinRMCertificateAuthPlugin {
}
function before.cloudbaseinit.plugins.common.networkconfig.NetworkConfigPlugin {
It "a network adapter should not exist" {
{
# if the functional test has been already run before,
# we need to rename the net adapter name to a different name.
Rename-NetAdapter -Name $global:CLOUDBASE_INIT_NET_NAME -NewName "not_cbs_init0" -ErrorAction SilentlyContinue
} | Should -Not -Throw
}
}
function after.cloudbaseinit.plugins.common.networkconfig.NetworkConfigPlugin {
It "a network adapter should exist and have a proper name" {
{
Get-NetAdapter -Name $global:CLOUDBASE_INIT_NET_NAME
if (!($global:CLOUDBASE_INIT_NET_STATIC_IP -in (Get-NetIPAddress -InterfaceAlias $global:CLOUDBASE_INIT_NET_NAME).IPAddress)) {
throw "Failed to set ip address on net adapter"
}
} | Should -Not -Throw
}
}
function prepare.empty {
# NOOP
}
function prepare.openstack {
pushd "$here/../$($env:CLOUD)"
$openVpnTapAdapterMacAddress = Prepare-OpenVPNTapAdapter
$networkTemplateFile = "cloudbase-init-metadata/openstack/latest/network_data.json.template"
if (Test-path $networkTemplateFile) {
$networkTemplateFileContent = (Get-Content -Raw $networkTemplateFile)
$networkTemplateFileContent = $networkTemplateFileContent.Replace("REPLACE_MAC_ADDRESS", $openVpnTapAdapterMacAddress)
$networkTemplateFileContent | Set-Content "cloudbase-init-metadata/openstack/latest/network_data.json" -Encoding Ascii
Write-Host $networkTemplateFileContent
}
try {
Dismount-DiskImage -ErrorAction SilentlyContinue (Resolve-Path "../cloudbase-init-config-drive.iso")
Remove-Item -Force -ErrorAction SilentlyContinue "../cloudbase-init-config-drive.iso"
} catch {}
try {
& "$here/../bin/mkisofs.exe" -o "../cloudbase-init-config-drive.iso" -ignore-error -ldots -allow-lowercase -allow-multidot -l -publisher "cbsl" -quiet -J -r -V "config-2" "cloudbase-init-metadata" 2>&1
} catch {}
Mount-DiskImage -ImagePath (Resolve-Path "../cloudbase-init-config-drive.iso") | Out-Null
Get-PsDrive | Out-Null
popd
}
function prepare.openstack-http {
prepare.openstack
}
function before.cloudbaseinit.plugins.windows.bootconfig.BCDConfigPlugin {
# TBD
}
function after.cloudbaseinit.plugins.windows.bootconfig.BCDConfigPlugin {
# TBD
}
function before.cloudbaseinit.plugins.windows.bootconfig.BootStatusPolicyPlugin {
# TBD
}
function after.cloudbaseinit.plugins.windows.bootconfig.BootStatusPolicyPlugin {
# TBD
}
function Prepare-OpenVPNTapAdapter {
# Use OpenVPN to create a TAP Windows Adapter to be configured.
# On Github Actions, we cannot use the existing network adapter for verifying the
# NetworkConfigPlugin, as resetting the same static network config breaks the worker
# connection to the Github Actions manager, and the action will lose context and
# timeout.
$openVpnUrl = "https://build.openvpn.net/downloads/releases/OpenVPN-2.5.10-I601-amd64.msi"
$wc = New-Object System.Net.WebClient
$msiFilePath = Join-Path $(pwd) "openvpn.msi"
$wc.DownloadFile($openVpnUrl, $msiFilePath)
cmd /c "msiexec.exe -i ${msiFilePath} /qn /norestart /l*v test.log" | Out-Null
if ($LASTEXITCODE) { throw "Failed to install openvpn" }
# Refresh the network adapter list to make sure it is properly updated (Windows quirk)
Get-NetAdapter | Out-Null
$adapter = Get-NetAdapter -InterfaceDescription "TAP-Windows Adapter V9" | Select-Object -First 1
if (!$adapter) { throw "Failed to find adapter"}
# Windows quirk to be able to set static IPs to disconnected network adapters
Set-ItemProperty -Path "HKLM:\\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\$($adapter.DeviceID)" -Name EnableDHCP -Value 0 | Out-Null
# A tap device gets created by default after installing OpenVPN.
# We'll update the network-config template with the according MAC address.
$currentMacAddress = $adapter.macaddress.Replace("-",":")
return $currentMacAddress
}
function prepare.nocloud {
pushd "$here/../$($env:CLOUD)"
$openVpnTapAdapterMacAddress = Prepare-OpenVPNTapAdapter
$networkTemplateFile = "cloudbase-init-metadata\network-config.template"
if (Test-path $networkTemplateFile) {
$networkTemplateFileContent = (Get-Content -Raw $networkTemplateFile)
$networkTemplateFileContent = $networkTemplateFileContent.Replace("REPLACE_MAC_ADDRESS", $openVpnTapAdapterMacAddress)
$networkTemplateFileContent | Set-Content "cloudbase-init-metadata\network-config" -Encoding Ascii
Write-Host $networkTemplateFileContent
}
try {
Dismount-DiskImage -ErrorAction SilentlyContinue (Resolve-Path "../cloudbase-init-config-drive.iso")
Remove-Item -Force -ErrorAction SilentlyContinue "../cloudbase-init-config-drive.iso"
} catch {}
try {
& "$here/../bin/mkisofs.exe" -o "../cloudbase-init-config-drive.iso" -ignore-error -ldots -allow-lowercase -allow-multidot -l -publisher "cbsl" -quiet -J -r -V "cidata" "cloudbase-init-metadata" 2>&1
} catch {}
Mount-DiskImage -ImagePath (Resolve-Path "../cloudbase-init-config-drive.iso") | Out-Null
Get-PsDrive | Out-Null
popd
}
function prepare.proxmox {
& prepare.nocloud
}
function prepare.vmwareguest {
pushd "$here/../$($env:CLOUD)"
$openVpnTapAdapterMacAddress = Prepare-OpenVPNTapAdapter
$rpcToolMockName = "mock-rpctool"
$rpcToolPyPath = (Resolve-Path ".\${rpcToolMockName}.py").Path
if (Test-path $rpcToolPyPath) {
$rpcToolPyContent = (Get-Content -Raw $rpcToolPyPath)
$rpcToolPyContent = $rpcToolPyContent.Replace("REPLACE_MAC_ADDRESS", $openVpnTapAdapterMacAddress)
$rpcToolPyContent | Set-Content $rpcToolPyPath -Encoding Ascii
}
# VmwareGuest service relies on a binary called rpctool.exe.
# This tool returns the metadata or userdata, so we mock it to be as close as
# possible to the real environment.
& pip.exe install pyinstaller
if ($LASTEXITCODE) {
throw "Failed to install pyinstaller"
}
& pyinstaller -F "${rpcToolPyPath}"
if ($LASTEXITCODE) {
throw "Failed to create rpctool.exe using pyinstaller"
}
$rpcToolPath = (Resolve-Path ".\dist\${rpcToolMockName}.exe").Path
$metadataServiceConfigFile = (Resolve-Path "cloudbase-init.conf").Path
Set-IniFileValue -Path $metadataServiceConfigFile -Section "vmwareguestinfo" `
-Key "vmware_rpctool_path" -Value $rpcToolPath
popd
}
BeforeDiscovery {
$env:CLOUD | Should -Not -Be $null
$metadataServiceConfigFile = Resolve-Path "$here/../$($env:CLOUD)/cloudbase-init.conf"
$pluginList = Get-IniFileValue -Path $metadataServiceConfigFile -Section "DEFAULT" `
-Key "plugins" `
-Default ""
$pluginList = $pluginList.Split(",")
& "prepare.$($ENV:CLOUD)"
}
Describe "TestVerifyBeforeAllPlugins" {
$env:CLOUD | Should -Not -Be $null
$metadataServiceConfigFile = Resolve-Path "$here/../$($env:CLOUD)/cloudbase-init.conf"
$pluginList = Get-IniFileValue -Path $metadataServiceConfigFile -Section "DEFAULT" `
-Key "plugins" `
-Default ""
$pluginList = $pluginList.Split(",")
Context "Verify state before running plugin" -ForEach $pluginList {
$plugin = $_
if (!$plugin) {
return
}
& "before.${plugin}"
$propertyName = $_.split(".")[-1]
It "Checks for Registry Key state ${plugin} and property ${propertyName}" {
$propertyName = $_.split(".")[-1]
$propertyValue = "NOT_INITIALIZED"
try {
$propertyValue = Get-ItemProperty -Path $global:CLOUDBASE_INIT_REGISTRY_PATH -Name $propertyName -ErrorAction "Stop" | Select-Object -ExpandProperty $propertyName
} catch {
$propertyValue = "NOT_EXISTENT"
}
$propertyValue | Should -BeExactly "NOT_EXISTENT"
}
}
}
Describe "TestVerifyAfterAllPlugins" {
$env:CLOUD | Should -Not -Be $null
$metadataServiceConfigFile = Resolve-Path "$here/../$($env:CLOUD)/cloudbase-init.conf"
$pluginList = Get-IniFileValue -Path $metadataServiceConfigFile -Section "DEFAULT" `
-Key "plugins" `
-Default ""
$pluginList = $pluginList.Split(",")
Context "Verify state after running plugin" -ForEach $pluginList {
$plugin = $_
if (!$plugin) {
return
}
& "after.${plugin}"
if ($env:CLOUD -ne "empty") {
$propertyName = $_.split(".")[-1]
It "Checks for Registry Key state ${plugin} and property ${propertyName}" {
$propertyName = $_.split(".")[-1]
$propertyValue = "NOT_INITIALIZED"
try {
$propertyValue = Get-ItemProperty -Path $global:CLOUDBASE_INIT_REGISTRY_PATH -Name $propertyName -ErrorAction "Stop" | Select-Object -ExpandProperty $propertyName
} catch {
$propertyValue = "NOT_EXISTENT"
}
$expectedValue = 1
# plugins that run at every boot
if ($propertyName -eq "ExtendVolumesPlugin") {
$expectedValue = 2
}
# plugins that run in the PRE_METADATA_DISCOVERY or PRE_NETWORKING stage
if ($propertyName -in @("MTUPlugin", "NTPClientPlugin")) {
$expectedValue = "NOT_EXISTENT"
}
$propertyValue | Should -BeExactly $expectedValue
}
}
}
}