diff --git a/vhdbuilder/packer/test/linux-vhd-content-test.sh b/vhdbuilder/packer/test/linux-vhd-content-test.sh index 7593df2d85e..48aaf8c64a0 100644 --- a/vhdbuilder/packer/test/linux-vhd-content-test.sh +++ b/vhdbuilder/packer/test/linux-vhd-content-test.sh @@ -216,7 +216,7 @@ testPackagesInstalled() { echo $test "[INFO] File ${downloadedPackage} exists and has the correct size ${fileSizeDownloaded} bytes" # Validate whether package exists in Azure China cloud if [[ $downloadURL == https://acs-mirror.azureedge.net/* ]]; then - mcURL="${downloadURL/https:\/\/acs-mirror.azureedge.net/https:\/\/kubernetesartifacts.blob.core.chinacloudapi.cn}" + mcURL="${downloadURL/https:\/\/acs-mirror.azureedge.net/https:\/\/mirror.azk8s.cn}" echo "Validating: $mcURL" isExist=$(curl -sLI "$mcURL" | grep -i "404 The specified blob does not exist." | awk '{print $2}') if [[ "$isExist" == "404" ]]; then @@ -224,7 +224,7 @@ testPackagesInstalled() { continue fi - fileSizeInMC=$(curl -sLI $mcURL | grep -i Content-Length | tail -n1 | awk '{print $2}' | tr -d '\r') + fileSizeInMC=$(wget -S $mcURL --start-pos=500G 2>&1 | grep -i Content-Length | tail -n1 | awk '{print $2}' | tr -d '\r') if [[ "$fileSizeInMC" != "$fileSizeDownloaded" ]]; then err "$mcURL is valid but the file size is different. Expected file size: ${fileSizeDownloaded} - downloaded file size: ${fileSizeInMC}" continue diff --git a/vhdbuilder/packer/test/windows-files-check.ps1 b/vhdbuilder/packer/test/windows-files-check.ps1 index 25dd097d16f..46a7c7464ee 100644 --- a/vhdbuilder/packer/test/windows-files-check.ps1 +++ b/vhdbuilder/packer/test/windows-files-check.ps1 @@ -197,9 +197,17 @@ function Test-CompareSingleDir { } if ($URL.StartsWith("https://acs-mirror.azureedge.net/")) { - $mcURL = $URL.replace("https://acs-mirror.azureedge.net/", "https://kubernetesartifacts.blob.core.chinacloudapi.cn/") - - $mooncakeFileSize = (Invoke-WebRequest $mcURL -UseBasicParsing -Method Head).Headers.'Content-Length' + $mcURL = $URL.replace("https://acs-mirror.azureedge.net/", "https://mirror.azk8s.cn/") + + Write-Output "Getting the size of the file: $mcURL" + $webRequest = [System.Net.HttpWebRequest]::Create($mcURL) + # Set the 'Range' header using the AddRange method + $webRequest.AddRange(0, 1023) + Retry-Command -ScriptBlock { + # Get the response + $global:response = $webRequest.GetResponse() + } -ErrorMessage "Failed to get the size ofthe file: $mcURL" + $mooncakeFileSize = $global:response.Headers["Content-Range"] -split "/" | Select-Object -Last 1 if ($globalFileSize -ne $mooncakeFileSize) { $MisMatchFiles[$URL]=$mcURL diff --git a/vhdbuilder/packer/test/windows-vhd-content-test.ps1 b/vhdbuilder/packer/test/windows-vhd-content-test.ps1 index 83f68858a8d..b99a6de9dac 100644 --- a/vhdbuilder/packer/test/windows-vhd-content-test.ps1 +++ b/vhdbuilder/packer/test/windows-vhd-content-test.ps1 @@ -107,6 +107,50 @@ function DownloadFileWithRetry } } +function Retry-Command { + [CmdletBinding()] + Param( + [Parameter(Position=0, Mandatory=$true)] + [scriptblock]$ScriptBlock, + + [Parameter(Position=1, Mandatory=$true)] + [string]$ErrorMessage, + + [Parameter(Position=2, Mandatory=$false)] + [int]$Maximum = 5, + + [Parameter(Position=3, Mandatory=$false)] + [int]$Delay = 10 + ) + + Begin { + $cnt = 0 + } + + Process { + do { + $cnt++ + try { + $ScriptBlock.Invoke() + if ($LASTEXITCODE) { + throw "Retry $cnt : $ErrorMessage" + } + return + } catch { + Write-ErrorWithTimestamp $_.Exception.InnerException.Message -ErrorAction Continue + if ($_.Exception.InnerException.Message.Contains("There is not enough space on the disk. (0x70)")) { + throw "Exit retry since there is not enough space on the disk" + } + Start-Sleep $Delay + } + } while ($cnt -lt $Maximum) + + # Throw an error after $Maximum unsuccessful invocations. Doesn't need + # a condition, since the function returns upon successful invocation. + throw 'All retries failed. $ErrorMessage' + } +} + function Test-FilesToCacheOnVHD { $invalidFiles = @() @@ -167,12 +211,22 @@ function Test-FilesToCacheOnVHD if ( $URL.StartsWith("https://acs-mirror.azureedge.net/")) { - $mcURL = $URL.replace("https://acs-mirror.azureedge.net/", "https://kubernetesartifacts.blob.core.chinacloudapi.cn/") + $mcURL = $URL.replace("https://acs-mirror.azureedge.net/", "https://mirror.azk8s.cn/") try { # It's too slow to download the file from the China Cloud. So we only compare the file size. $localFileSize = (Get-Item $dest).length - $remoteFileSize = (Invoke-WebRequest $mcURL -UseBasicParsing -Method Head).Headers.'Content-Length' + + Write-Output "Getting the size of the file: $mcURL" + $webRequest = [System.Net.HttpWebRequest]::Create($mcURL) + # Set the 'Range' header using the AddRange method + $webRequest.AddRange(0, 1023) + Retry-Command -ScriptBlock { + # Get the response + $global:response = $webRequest.GetResponse() + } -ErrorMessage "Failed to get the size ofthe file: $mcURL" + $mooncakeFileSize = $global:response.Headers["Content-Range"] -split "/" | Select-Object -Last 1 + $remoteFileSize = $response.Headers["Content-Range"] -split "/" | Select-Object -Last 1 if ($localFileSize -ne $remoteFileSize) { $isIgnore = $False