Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nocloud: enable network config testing #14

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions functional-tests/CloudbaseInit.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ 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
Expand Down Expand Up @@ -230,9 +231,24 @@ function after.cloudbaseinit.plugins.windows.winrmcertificateauth.ConfigWinRMCer
}
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this renames the "cbs_init_eth0" adapter to "not_cbs_init0" before executing NetworkConfigPlugin, probably to verify that the plugin changes the adapter name back to "cbs_init_eth0".

If the plugin hasn't run yet, it won't find the given adapter and silently ignore the error.

I guess it's worth adding an inline comment.

} | 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 {
Expand Down Expand Up @@ -287,6 +303,38 @@ function after.cloudbaseinit.plugins.windows.bootconfig.BootStatusPolicyPlugin {

function prepare.nocloud {
pushd "$here/../$($env:CLOUD)"

# 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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need OpenVPN or can we just install the tap driver from here? https://github.com/OpenVPN/tap-windows6/releases

We should add an inline comment, saying why we need OpenVPN.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need OpenVPN. Installing the driver only does not do anything.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can simply unzip the archive and then manually create the tap device like so:

PS E:\tools\tap> .\devcon.exe install .\OemVista.inf tap0901
Device node created. Install is complete when drivers are installed...
Updating drivers for tap0901 from E:\tools\tap\OemVista.inf.
Drivers installed successfully.

PS E:\tools\tap> get-netadapter -InterfaceDescription "*TAP*"

Name                      InterfaceDescription                    ifIndex Status       MacAddress             LinkSpeed
----                      --------------------                    ------- ------       ----------             ---------
Local Area Connection     TAP-Windows Adapter V9                       25 Disconnected 00-FF-92-31-A1-57         1 Gbps

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to leave the MSI installation, as it is only 4MB and requires one liner (simplicity ++). Also, if the internal ZIP contents change, a fix would be required. Hopefully, the MSI installation will be good for a long time.

$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"
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
ader1990 marked this conversation as resolved.
Show resolved Hide resolved
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

# 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("-",":")
$networkTemplateFile = "cloudbase-init-metadata\network-config.template"
if (Test-path $networkTemplateFile) {
$networkTemplateFileContent = (Get-Content -Raw $networkTemplateFile)
$networkTemplateFileContent = $networkTemplateFileContent.Replace("REPLACE_MAC_ADDRESS", $currentMacAddress)
$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"
Expand Down
12 changes: 12 additions & 0 deletions nocloud/cloudbase-init-metadata/network-config.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
config:
- mac_address: REPLACE_MAC_ADDRESS
name: cbs_init_eth0
type: physical
subnets:
- address: 10.196.59.2/24
dns_nameservers:
- 1.1.1.1
- 8.8.8.8
gateway: 10.196.59.1
type: static
version: 1
2 changes: 1 addition & 1 deletion nocloud/cloudbase-init.conf
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ metadata_services = cloudbaseinit.metadata.services.nocloudservice.NoCloudConfig

# List of enabled plugin classes, to be executed in the provided order (list
# value)
plugins = cloudbaseinit.plugins.common.mtu.MTUPlugin,cloudbaseinit.plugins.windows.ntpclient.NTPClientPlugin,cloudbaseinit.plugins.windows.sanpolicy.SANPolicyPlugin,cloudbaseinit.plugins.windows.displayidletimeout.DisplayIdleTimeoutConfigPlugin,cloudbaseinit.plugins.windows.bootconfig.BootStatusPolicyPlugin,cloudbaseinit.plugins.common.sethostname.SetHostNamePlugin,cloudbaseinit.plugins.windows.createuser.CreateUserPlugin,cloudbaseinit.plugins.common.sshpublickeys.SetUserSSHPublicKeysPlugin,cloudbaseinit.plugins.windows.extendvolumes.ExtendVolumesPlugin,cloudbaseinit.plugins.common.userdata.UserDataPlugin,cloudbaseinit.plugins.common.setuserpassword.SetUserPasswordPlugin,cloudbaseinit.plugins.windows.winrmlistener.ConfigWinRMListenerPlugin,cloudbaseinit.plugins.windows.winrmcertificateauth.ConfigWinRMCertificateAuthPlugin,cloudbaseinit.plugins.common.localscripts.LocalScriptsPlugin,cloudbaseinit.plugins.common.trim.TrimConfigPlugin,
plugins = cloudbaseinit.plugins.common.mtu.MTUPlugin,cloudbaseinit.plugins.windows.ntpclient.NTPClientPlugin,cloudbaseinit.plugins.windows.sanpolicy.SANPolicyPlugin,cloudbaseinit.plugins.windows.displayidletimeout.DisplayIdleTimeoutConfigPlugin,cloudbaseinit.plugins.windows.bootconfig.BootStatusPolicyPlugin,cloudbaseinit.plugins.common.sethostname.SetHostNamePlugin,cloudbaseinit.plugins.windows.createuser.CreateUserPlugin,cloudbaseinit.plugins.common.sshpublickeys.SetUserSSHPublicKeysPlugin,cloudbaseinit.plugins.windows.extendvolumes.ExtendVolumesPlugin,cloudbaseinit.plugins.common.userdata.UserDataPlugin,cloudbaseinit.plugins.common.setuserpassword.SetUserPasswordPlugin,cloudbaseinit.plugins.windows.winrmlistener.ConfigWinRMListenerPlugin,cloudbaseinit.plugins.windows.winrmcertificateauth.ConfigWinRMCertificateAuthPlugin,cloudbaseinit.plugins.common.localscripts.LocalScriptsPlugin,cloudbaseinit.plugins.common.trim.TrimConfigPlugin,cloudbaseinit.plugins.common.networkconfig.NetworkConfigPlugin

# List of enabled userdata content plugins (list value)
#user_data_plugins = cloudbaseinit.plugins.common.userdataplugins.parthandler.PartHandlerPlugin,cloudbaseinit.plugins.common.userdataplugins.cloudconfig.CloudConfigPlugin,cloudbaseinit.plugins.common.userdataplugins.cloudboothook.CloudBootHookPlugin,cloudbaseinit.plugins.common.userdataplugins.shellscript.ShellScriptPlugin,cloudbaseinit.plugins.common.userdataplugins.multipartmixed.MultipartMixedPlugin,cloudbaseinit.plugins.common.userdataplugins.heat.HeatPlugin
Expand Down