Skip to content

Commit

Permalink
Multiple fixes (#24)
Browse files Browse the repository at this point in the history
* Reduce verbosity of environment reload read files
* Ignore Linux hidden files (starting with dot) when reading environment files
* Restore SSH connection test
* Improve test coverage for environment warm reload
  • Loading branch information
david-garcia-garcia authored Jul 19, 2024
1 parent 3e84e07 commit 5ae529c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ function SbsPrepareEnv {

$configuration = "";
$configDir = "C:\environment.d";
$confFiles = @();

if (Test-Path $configDir) {
$mergedConfig = @{}

# We make this recursive to allow mounting full configmap without subpaths in K8S
# see https://github.com/Azure/AKS/issues/4309
$confFiles = Get-ChildItem -Recurse -Path $configDir -Include *.json, *.yaml, *.yml | Sort-Object Name
$confFiles = Get-ChildItem -Recurse -Path $configDir -Include *.json, *.yaml, *.yml | `
Where-object { -not ($_.Name -match "^\.") } | `
Sort-Object Name | Select-Object -ExpandProperty "FullName";

foreach ($file in $confFiles) {
SbsWriteDebug "Reading environment configuration file $file"
$fileContent = Get-Content -Path $file.FullName -Raw | ConvertFrom-Yaml
$fileContent = Get-Content -Path $file -Raw | ConvertFrom-Yaml
foreach ($key in $fileContent.Keys) {
$mergedConfig[$key] = $fileContent[$key];
}
Expand All @@ -42,6 +44,14 @@ function SbsPrepareEnv {
if ($md5HashString -eq $currentHash -and $md5HashString -eq $Env:ENVHASH) {
return $false;
}

$configChangeCount = SbsGetEnvInt -name "SBS_CONFIG_CHANGECOUNT" -defaultValue 0
Write-Host "Configuration change count $($configChangeCount)"
$Env:SBS_CONFIG_CHANGECOUNT = ($configChangeCount + 1);

foreach ($file in $confFiles) {
Write-Host "Read environment configuration file $file"
}

# Store to avoid reprocessing
$md5HashString | Set-Content -Path $hashFilePath;
Expand Down
90 changes: 54 additions & 36 deletions servercore2022/tests/Compose.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
Describe 'compose.yaml' {
BeforeAll {
. ./../bootstraptest.ps1
$Env:imageName = "servercore2022-servercore-1";
}

It 'Container starts' {
docker compose -f servercore2022/compose.yaml up -d;
WaitForLog "servercore2022-servercore-1" "Initialization Completed"
WaitForLog $Env:imageName "Initialization Completed"
}

It 'LogRotate runs at 5AM Daily' {
docker exec servercore2022-servercore-1 powershell "(Get-ScheduledTask LogRotate).Triggers[0].DaysInterval" | Should -Be "1";
docker exec servercore2022-servercore-1 powershell "[DateTime]::Parse((Get-ScheduledTask LogRotate).Triggers[0].StartBoundary).ToLocalTime().ToString('s')" | Should -Be "2023-01-01T05:00:00";
docker exec $Env:imageName powershell "(Get-ScheduledTask LogRotate).Triggers[0].DaysInterval" | Should -Be "1";
docker exec $Env:imageName powershell "[DateTime]::Parse((Get-ScheduledTask LogRotate).Triggers[0].StartBoundary).ToLocalTime().ToString('s')" | Should -Be "2023-01-01T05:00:00";
}

It 'Env variable is protected' {
$sbsTestProtect = docker exec servercore2022-servercore-1 powershell '$Env:SBS_TESTPROTECT';
$sbsTestProtectProtected = docker exec servercore2022-servercore-1 powershell '$Env:SBS_TESTPROTECT_PROTECTED';
$sbsTestProtect = docker exec $Env:imageName powershell '$Env:SBS_TESTPROTECT';
$sbsTestProtectProtected = docker exec $Env:imageName powershell '$Env:SBS_TESTPROTECT_PROTECTED';
$sbsTestProtect | Should -Not -Be $sbsTestProtectProtected;
$sbsTestProtectProtected | Should -Be -Empty;
}

It 'Timezone is set' {
docker exec servercore2022-servercore-1 powershell "(Get-TimeZone).Id" | Should -Be "Pacific Standard Time";
docker exec $Env:imageName powershell "(Get-TimeZone).Id" | Should -Be "Pacific Standard Time";
}

It 'sshd service is started' {
docker exec servercore2022-servercore-1 powershell "(Get-Service -Name 'sshd').Status" | Should -Be "Running"
docker exec $Env:imageName powershell "(Get-Service -Name 'sshd').Status" | Should -Be "Running"
}

It 'sshd service has automatic startup' {
docker exec servercore2022-servercore-1 powershell "(Get-Service -Name 'sshd').StartType" | Should -Be "Automatic"
docker exec $Env:imageName powershell "(Get-Service -Name 'sshd').StartType" | Should -Be "Automatic"
}

It 'DPAPI encode/decode works' {
docker exec servercore2022-servercore-1 powershell '$Env:SBS_TESTPROTECT_PROTECT' | Should -Be "supersecretekey"
$encoded = docker exec servercore2022-servercore-1 powershell '$Env:SBS_TESTPROTECT'
docker exec $Env:imageName powershell '$Env:SBS_TESTPROTECT_PROTECT' | Should -Be "supersecretekey"
$encoded = docker exec $Env:imageName powershell '$Env:SBS_TESTPROTECT'
$encoded | Should -Not -Be "supersecretekey"
$decoded = docker exec servercore2022-servercore-1 powershell 'Import-Module Sbs; return SbsDpapiDecode -EncodedValue $Env:SBS_TESTPROTECT';
$decoded = docker exec $Env:imageName powershell 'Import-Module Sbs; return SbsDpapiDecode -EncodedValue $Env:SBS_TESTPROTECT';
$decoded | Should -Be "supersecretekey"
}

Expand Down Expand Up @@ -63,44 +67,58 @@ Describe 'compose.yaml' {
It 'Env warm reload' {
$jsonString = @{
"SBS_TESTVALUE" = "value1"
"SBS_OVERRIDE" = "originalValue"
"SBS_OVERRIDE" = "originalValue"
} | ConvertTo-Json

# Create directory and set environment
docker exec servercore2022-servercore-1 powershell "New-Item -ItemType Directory -Force -Path 'C:\environment.d'; Set-Content -Path 'C:\environment.d\env0.json' -Value '$jsonString'"
docker exec $Env:imageName powershell "New-Item -ItemType Directory -Force -Path 'C:\environment.d'; Set-Content -Path 'C:\environment.d\env0.json' -Value '$jsonString'"

WaitForLog $Env:imageName "Configuration change count 1"

$jsonString2 = @{
"SBS_TESTVALUE2" = "value2"
"SBS_OVERRIDE" = "overridenValue"
"SBS_OVERRIDE" = "overridenValue"
} | ConvertTo-Json

# Create directory and set environment
docker exec servercore2022-servercore-1 powershell "New-Item -ItemType Directory -Force -Path 'C:\environment.d'; Set-Content -Path 'C:\environment.d\env1.json' -Value '$jsonString2'"
docker exec $Env:imageName powershell "New-Item -ItemType Directory -Force -Path 'C:\environment.d'; Set-Content -Path 'C:\environment.d\env1.json' -Value '$jsonString2'"

# Force refresh
docker exec servercore2022-servercore-1 powershell "Import-Module Sbs; SbsPrepareEnv;"
# Refresh should happen automatically, wait for it
WaitForLog $Env:imageName "Configuration change count 2"

$yamlString = @{
"SBS_TESTVALUE3" = "value3"
} | ConvertTo-Yaml

# Create directory and set environment
docker exec $Env:imageName powershell "New-Item -ItemType Directory -Force -Path 'C:\environment.d\subdir'; Set-Content -Path 'C:\environment.d\subdir\env1.yml' -Value '$yamlString'"

docker exec servercore2022-servercore-1 powershell '$Env:SBS_TESTVALUE' | Should -Be "value1"
docker exec servercore2022-servercore-1 powershell '$Env:SBS_TESTVALUE2' | Should -Be "value2"
docker exec servercore2022-servercore-1 powershell '$Env:SBS_OVERRIDE' | Should -Be "overridenValue"
# Refresh should happen automatically, wait for it
WaitForLog $Env:imageName "Configuration change count 3"

docker exec $Env:imageName powershell '$Env:SBS_TESTVALUE' | Should -Be "value1"
docker exec $Env:imageName powershell '$Env:SBS_TESTVALUE2' | Should -Be "value2"
docker exec $Env:imageName powershell '$Env:SBS_TESTVALUE3' | Should -Be "value3"
docker exec $Env:imageName powershell '$Env:SBS_OVERRIDE' | Should -Be "overridenValue"
}

#It 'Can SSH to container' {
# docker exec servercore2022-servercore-1 powershell "Set-Service -Name sshd -StartupType Manual; Start-Service -Name sshd; net user localadmin ""@MyP@assw0rd"";"
# # Define the SSH parameters
# $Server = "172.18.8.8"
# $UserName = "localadmin"
# $Password = "@MyP@assw0rd" | ConvertTo-SecureString -AsPlainText -Force
# $Credential = New-Object System.Management.Automation.PSCredential ($UserName, $Password)
# Import-Module Posh-SSH
# # Create SSH session
# try {
# $Session = New-SSHSession -ComputerName $Server -Credential $Credential -AcceptKey
# Write-Host "SSH session created successfully."
# } catch {
# Write-Host "Failed to create SSH session: $_"
# }
#}
It 'Can SSH to container' {
docker exec $Env:imageName powershell "Set-Service -Name sshd -StartupType Manual; Start-Service -Name sshd; net user localadmin ""@MyP@assw0rd"";"
# Define the SSH parameters
$Server = "172.18.8.8"
$UserName = "localadmin"
$Password = "@MyP@assw0rd" | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ($UserName, $Password)
Import-Module Posh-SSH
# Create SSH session
try {
$Session = New-SSHSession -ComputerName $Server -Credential $Credential -AcceptKey
Write-Host "SSH session created successfully."
}
catch {
Write-Host "Failed to create SSH session: $_"
}
}

AfterAll {
docker compose -f servercore2022/compose.yaml down;
Expand Down

0 comments on commit 5ae529c

Please sign in to comment.