Skip to content

Commit

Permalink
use files to store expected test config content
Browse files Browse the repository at this point in the history
  • Loading branch information
Qingchuan Hao committed Apr 13, 2024
1 parent 19c2620 commit 116932e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 58 deletions.
33 changes: 23 additions & 10 deletions staging/cse/windows/configfunc.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,16 @@ function Install-OpenSSH {

function Config-CredentialProvider {
Param(
[Parameter(Mandatory=$true)][string]
$KubeDir,
[Parameter(Mandatory = $false)][string]
$CustomCloudContainerRegistryDNSSuffix
)

$CredentialProviderConfPATH = [Io.path]::Combine("$global:KubeDir", "credential-provider-config.yaml")
$CredentialProviderConfPATH = [Io.path]::Combine("$KubeDir", "credential-provider-config.yaml")

Write-Log "Configuring kubelet credential provider"
$azureConfigFile = [io.path]::Combine($global:KubeDir, "azure.json")
$azureConfigFile = [io.path]::Combine("$KubeDir", "azure.json")

$credentialProviderConfig = @"
apiVersion: kubelet.config.k8s.io/v1
Expand All @@ -363,26 +365,37 @@ providers:
- "*.azurecr.cn"
- "*.azurecr.de"
- "*.azurecr.us"
defaultCacheDuration: "10m"
apiVersion: credentialprovider.kubelet.k8s.io/v1
args:
- $azureConfigFile
"@
if ($CustomCloudContainerRegistryDNSSuffix) {
$credentialProviderConfig += @"

if ($CustomCloudContainerRegistryDNSSuffix) {
$credentialProviderConfig = @"
apiVersion: kubelet.config.k8s.io/v1
kind: CredentialProviderConfig
providers:
- name: acr-credential-provider
matchImages:
- "*.azurecr.io"
- "*.azurecr.cn"
- "*.azurecr.de"
- "*.azurecr.us"
- "*$CustomCloudContainerRegistryDNSSuffix"
"@
}

$credentialProviderConfig+=@"
defaultCacheDuration: "10m"
apiVersion: credentialprovider.kubelet.k8s.io/v1
args:
- $azureConfigFile
"@
}
$credentialProviderConfig | Out-File -encoding ASCII -filepath "$CredentialProviderConfPATH"
}

function Install-CredentialProvider {
Param(
[Parameter(Mandatory=$true)][string]
$KubeDir,
[Parameter(Mandatory = $false)][string]
$CustomCloudContainerRegistryDNSSuffix
)
Expand All @@ -395,7 +408,7 @@ function Install-CredentialProvider {
Logs-To-Event -TaskName "AKS.WindowsCSE.Install-CredentialProvider" -TaskMessage "Start to install out of tree credential provider"

Write-Log "Create credential provider configuration file"
Config-CredentialProvider -CustomCloudContainerRegistryDNSSuffix $CustomCloudContainerRegistryDNSSuffix
Config-CredentialProvider -KubeDir $KubeDir -CustomCloudContainerRegistryDNSSuffix $CustomCloudContainerRegistryDNSSuffix

$CredentialProviderBinDir = "c:\var\lib\kubelet\credential-provider"
Write-Log "Download credential provider binary from $global:CredentialProviderURL to $CredentialProviderBinDir"
Expand Down
62 changes: 14 additions & 48 deletions staging/cse/windows/configfunc.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,13 @@ Describe 'Resize-OSDrive' {

Describe 'Config-CredentialProvider' {
BeforeEach {
$global:KubeDir = "$PSScriptRoot"
$CredentialProviderConfPATH=[Io.path]::Combine("$global:KubeDir", "credential-provider-config.yaml")
$global:credentialProviderConfigDir = "staging\cse\windows\credentialProvider.tests.suites"
$CredentialProviderConfPATH=[Io.path]::Combine("$global:credentialProviderConfigDir", "credential-provider-config.yaml")
function Read-Format-Yaml ([string]$YamlFile) {
$yaml = Get-Content $YamlFile | ConvertFrom-Yaml
$yaml = $yaml | ConvertTo-Yaml
return $yaml
}
}

AfterEach {
Expand All @@ -118,59 +123,20 @@ Describe 'Config-CredentialProvider' {

Context 'CustomCloudContainerRegistryDNSSuffix is empty' {
It "should match the expected config file content" {
$expectedCredentialProviderConfig = @"
apiVersion: kubelet.config.k8s.io/v1
kind: CredentialProviderConfig
providers:
- name: acr-credential-provider
matchImages:
- "*.azurecr.io"
- "*.azurecr.cn"
- "*.azurecr.de"
- "*.azurecr.us"
defaultCacheDuration: "10m"
apiVersion: credentialprovider.kubelet.k8s.io/v1
args:
- $global:KubeDir\azure.json
"@
Config-CredentialProvider -CustomCloudContainerRegistryDNSSuffix ""
$acutalCredentialProviderConfigStr = Get-Content $CredentialProviderConfPATH -Raw | Out-String
$acutalCredentialProviderConfig = @"
$acutalCredentialProviderConfigStr
"@
$expectedCredentialProviderConfig = Read-Format-Yaml ([Io.path]::Combine($credentialProviderConfigDir, "DNSSuffixEmpty.config.yaml"))
Config-CredentialProvider -KubeDir $credentialProviderConfigDir -CustomCloudContainerRegistryDNSSuffix ""
$acutalCredentialProviderConfig = Read-Format-Yaml $CredentialProviderConfPATH
$diffence = Compare-Object $acutalCredentialProviderConfig $expectedCredentialProviderConfig
$diffence | Should -Be $null
}
}
Context 'CustomCloudContainerRegistryDNSSuffix is not empty' {
It "should match the expected config file content" {
$expectedCredentialProviderConfig = @"
apiVersion: kubelet.config.k8s.io/v1
kind: CredentialProviderConfig
providers:
- name: acr-credential-provider
matchImages:
- "*.azurecr.io"
- "*.azurecr.cn"
- "*.azurecr.de"
- "*.azurecr.us"
- "*.azurecr.microsoft.fakecloud"
defaultCacheDuration: "10m"
apiVersion: credentialprovider.kubelet.k8s.io/v1
args:
- $global:KubeDir\azure.json
"@
Config-CredentialProvider -CustomCloudContainerRegistryDNSSuffix ".azurecr.microsoft.fakecloud"
$acutalCredentialProviderConfigStr = Get-Content $CredentialProviderConfPATH -Raw | Out-String
$acutalCredentialProviderConfig = @"
$acutalCredentialProviderConfigStr
"@
$expectedCredentialProviderConfig = Read-Format-Yaml ([Io.path]::Combine($credentialProviderConfigDir, "DNSSuffixNotEmpty.config.yaml"))
Config-CredentialProvider -KubeDir $credentialProviderConfigDir -CustomCloudContainerRegistryDNSSuffix ".azurecr.microsoft.fakecloud"
$acutalCredentialProviderConfig = Read-Format-Yaml $CredentialProviderConfPATH
$diffence = Compare-Object $acutalCredentialProviderConfig $expectedCredentialProviderConfig
$diffence | Should -Be $null
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: kubelet.config.k8s.io/v1
kind: CredentialProviderConfig
providers:
- name: acr-credential-provider
matchImages:
- "*.azurecr.io"
- "*.azurecr.cn"
- "*.azurecr.de"
- "*.azurecr.us"
defaultCacheDuration: "10m"
apiVersion: credentialprovider.kubelet.k8s.io/v1
args:
- staging\cse\windows\credentialProvider.tests.suites\azure.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: kubelet.config.k8s.io/v1
kind: CredentialProviderConfig
providers:
- name: acr-credential-provider
matchImages:
- "*.azurecr.io"
- "*.azurecr.cn"
- "*.azurecr.de"
- "*.azurecr.us"
- "*.azurecr.microsoft.fakecloud"
defaultCacheDuration: "10m"
apiVersion: credentialprovider.kubelet.k8s.io/v1
args:
- staging\cse\windows\credentialProvider.tests.suites\azure.json

0 comments on commit 116932e

Please sign in to comment.