-
Notifications
You must be signed in to change notification settings - Fork 275
chore: add windows CSE helpers file #9194
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
Open
Tim Wright (timmy-wright)
wants to merge
3
commits into
main
Choose a base branch
from
timmy/helpers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+143
−10
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,16 @@ | ||
| if (-not $WINDOWS_SCRIPTS_DIRECTORY) { | ||
| $WINDOWS_SCRIPTS_DIRECTORY = 'c:\AzureData\windows' | ||
| } | ||
|
|
||
| # Dot-source cse scripts with functions that are bundled on the VHD | ||
| . c:\AzureData\windows\azurecnifunc.ps1 | ||
| . c:\AzureData\windows\calicofunc.ps1 | ||
| . c:\AzureData\windows\configfunc.ps1 | ||
| . c:\AzureData\windows\containerdfunc.ps1 | ||
| . c:\AzureData\windows\kubeletfunc.ps1 | ||
| . c:\AzureData\windows\kubernetesfunc.ps1 | ||
| . c:\AzureData\windows\nvidiagpudriverfunc.ps1 | ||
| . c:\AzureData\windows\securetlsbootstrapfunc.ps1 | ||
| . c:\AzureData\windows\windowsciliumnetworkingfunc.ps1 | ||
| . c:\AzureData\windows\networkisolatedclusterfunc.ps1 | ||
| . $WINDOWS_SCRIPTS_DIRECTORY\helpers.ps1 | ||
| . $WINDOWS_SCRIPTS_DIRECTORY\azurecnifunc.ps1 | ||
| . $WINDOWS_SCRIPTS_DIRECTORY\calicofunc.ps1 | ||
| . $WINDOWS_SCRIPTS_DIRECTORY\configfunc.ps1 | ||
| . $WINDOWS_SCRIPTS_DIRECTORY\containerdfunc.ps1 | ||
| . $WINDOWS_SCRIPTS_DIRECTORY\kubeletfunc.ps1 | ||
| . $WINDOWS_SCRIPTS_DIRECTORY\kubernetesfunc.ps1 | ||
| . $WINDOWS_SCRIPTS_DIRECTORY\nvidiagpudriverfunc.ps1 | ||
| . $WINDOWS_SCRIPTS_DIRECTORY\securetlsbootstrapfunc.ps1 | ||
| . $WINDOWS_SCRIPTS_DIRECTORY\windowsciliumnetworkingfunc.ps1 | ||
| . $WINDOWS_SCRIPTS_DIRECTORY\networkisolatedclusterfunc.ps1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # common helper functions | ||
|
|
||
| function Remove-ServiceIfExists | ||
| { | ||
| param( | ||
| [Parameter(Mandatory = $true)][string]$ServiceName | ||
| ) | ||
| # A prior provisioning attempt may have already registered this service (e.g. CSE re-invoked | ||
| # after a partial failure). Best-effort remove it so the subsequent nssm.exe install doesn't | ||
| # fail against an already-existing service. | ||
| $svc = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue | ||
| if ($null -ne $svc) { | ||
| sc.exe delete $ServiceName | ||
| # sc.exe delete can legitimately return non-zero here (e.g. 1072 - service already marked for deletion) | ||
| # since this is best-effort cleanup of a pre-existing service, don't treat that as fatal. | ||
| if ($LASTEXITCODE -ne 0) { Write-Log "sc.exe failed to delete existing $ServiceName service (exit code $LASTEXITCODE), continuing anyway" } | ||
| } | ||
| } | ||
|
|
||
| function Invoke-NssmExe | ||
| { | ||
| # Thin wrapper around the path-qualified nssm.exe invocation so tests can Mock it; | ||
| # Pester cannot intercept a call like `& "$KubeDir\nssm.exe"` directly since path-qualified | ||
| # commands bypass function/command name resolution. | ||
| param( | ||
| [Parameter(Mandatory = $true)][string]$KubeDir, | ||
| [Parameter(Mandatory = $true, ValueFromRemainingArguments = $true)][string[]]$NssmArguments | ||
| ) | ||
| & "$KubeDir\nssm.exe" @NssmArguments | ||
| } | ||
|
|
||
| function Invoke-Nssm | ||
| { | ||
| param( | ||
| [Parameter(Mandatory = $true)][string]$KubeDir, | ||
| [Parameter(Mandatory = $true, ValueFromRemainingArguments = $true)][string[]]$NssmArguments | ||
| ) | ||
| Invoke-NssmExe -KubeDir $KubeDir -NssmArguments $NssmArguments | RemoveNulls | ||
| if ($LASTEXITCODE -ne 0) | ||
| { | ||
| throw "nssm.exe $( $NssmArguments -join ' ' ) failed (exit code $LASTEXITCODE)" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| BeforeAll { | ||
| . $PSScriptRoot\..\..\..\parts\windows\windowscsehelper.ps1 | ||
| . $PSCommandPath.Replace('.tests.ps1', '.ps1') | ||
|
|
||
| # Get-Service and sc.exe are Windows-only; stub them so Mock can override them when | ||
| # tests run in isolation (e.g. locally on non-Windows, outside the full suite). | ||
| function Get-Service {} | ||
| function sc.exe {} | ||
| } | ||
|
|
||
| Describe 'Remove-ServiceIfExists' { | ||
| Context 'when the service does not exist' { | ||
| BeforeEach { | ||
| $script:scExeCallCount = 0 | ||
| Mock Get-Service -MockWith { return $null } | ||
| Mock sc.exe -MockWith { $script:scExeCallCount++ } | ||
| } | ||
|
|
||
| It 'does not call sc.exe' { | ||
| Remove-ServiceIfExists -ServiceName 'some-service' | ||
|
|
||
| $script:scExeCallCount | Should -Be 0 | ||
| } | ||
| } | ||
|
|
||
| Context 'when the service already exists' { | ||
| BeforeEach { | ||
| $script:scExeCallCount = 0 | ||
| $mockExistingSvc = [PSCustomObject]@{Name = 'some-service'; Status = 'Stopped'} | ||
| Mock Get-Service -MockWith { return $mockExistingSvc } | ||
| } | ||
|
|
||
| It 'calls sc.exe delete to remove the existing service' { | ||
| Mock sc.exe -MockWith { $script:scExeCallCount++; $global:LASTEXITCODE = 0 } | ||
|
|
||
| Remove-ServiceIfExists -ServiceName 'some-service' | ||
|
|
||
| $script:scExeCallCount | Should -Be 1 | ||
| } | ||
|
|
||
| It 'does not throw when sc.exe delete succeeds' { | ||
| Mock sc.exe -MockWith { $global:LASTEXITCODE = 0 } | ||
|
|
||
| { Remove-ServiceIfExists -ServiceName 'some-service' } | Should -Not -Throw | ||
| } | ||
|
|
||
| It 'does not throw when sc.exe delete fails (best-effort cleanup)' { | ||
| Mock sc.exe -MockWith { $global:LASTEXITCODE = 1 } | ||
|
|
||
| { Remove-ServiceIfExists -ServiceName 'some-service' } | Should -Not -Throw | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Describe 'Invoke-Nssm' { | ||
| BeforeEach { | ||
| $script:nssmInvocations = @() | ||
| } | ||
|
|
||
| It 'does not throw when nssm.exe succeeds' { | ||
| Mock Invoke-NssmExe -MockWith { | ||
| $script:nssmInvocations += , @($NssmArguments) | ||
| $global:LASTEXITCODE = 0 | ||
| return 'ok' | ||
| } | ||
|
|
||
| { Invoke-Nssm -KubeDir 'C:\k' -NssmArguments 'install', 'some-service', 'C:\k\some-service.exe' } | Should -Not -Throw | ||
|
|
||
| $script:nssmInvocations.Count | Should -Be 1 | ||
| $script:nssmInvocations[0] | Should -Be @('install', 'some-service', 'C:\k\some-service.exe') | ||
| } | ||
|
|
||
| It 'throws with the exit code when nssm.exe fails' { | ||
| Mock Invoke-NssmExe -MockWith { | ||
| $global:LASTEXITCODE = 1 | ||
| return $null | ||
| } | ||
|
|
||
| { Invoke-Nssm -KubeDir 'C:\k' -NssmArguments 'install', 'some-service', 'C:\k\some-service.exe' } | | ||
| Should -Throw '*failed (exit code 1)*' | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.