diff --git a/images/linux/scripts/helpers/Tests.Helpers.psm1 b/images/linux/scripts/helpers/Tests.Helpers.psm1 index 20010a07d..d4040e9fd 100644 --- a/images/linux/scripts/helpers/Tests.Helpers.psm1 +++ b/images/linux/scripts/helpers/Tests.Helpers.psm1 @@ -16,6 +16,11 @@ function Invoke-PesterTests { throw "Unable to find test file '$TestFile' on '$testPath'." } + # Check that Pester module is imported + if (!(Get-Module "Pester")) { + Import-Module Pester + } + $configuration = [PesterConfiguration] @{ Run = @{ Path = $testPath; PassThru = $true } Output = @{ Verbosity = "Detailed" } diff --git a/images/linux/scripts/installers/Install-AzureModules.ps1 b/images/linux/scripts/installers/Install-AzureModules.ps1 new file mode 100644 index 000000000..ca95efa4e --- /dev/null +++ b/images/linux/scripts/installers/Install-AzureModules.ps1 @@ -0,0 +1,31 @@ +$ErrorActionPreference = "Stop" +$ProgressPreference = "SilentlyContinue" + +Import-Module "$env:HELPER_SCRIPTS/Tests.Helpers.psm1" -DisableNameChecking + +# Get modules content from toolset +$modules = (Get-ToolsetContent).azureModules +$installPSModulePath = "/usr/share" + +foreach ($module in $modules) +{ + $moduleName = $module.name + Write-Host "Installing ${moduleName} to the ${installPSModulePath} path..." + foreach ($version in $module.versions) + { + $modulePath = Join-Path -Path $installPSModulePath -ChildPath "${moduleName}_${version}" + Write-Host " - $version [$modulePath]" + Save-Module -Path $modulePath -Name $moduleName -RequiredVersion $version -Force -Verbose + } +} + +# If Az.Accounts > 1.0.0 unable to load module with error: Assembly with same name is already loaded +# Force install Az.Accounts 1.0.0 +$azAccountsPath = "/usr/share/az_1.0.0/Az.Accounts" +if (Test-Path $azAccountsPath) +{ + Remove-Item -Path $azAccountsPath -Force -Recurse + Save-Module -Name Az.Accounts -Path "/usr/share/az_1.0.0" -RequiredVersion 1.0.0 -Force +} + +Invoke-PesterTests -TestFile "PowerShellModules" -TestName "AzureModules" diff --git a/images/linux/scripts/installers/Install-PowerShellModules.ps1 b/images/linux/scripts/installers/Install-PowerShellModules.ps1 index ed100e3c4..95e03545e 100644 --- a/images/linux/scripts/installers/Install-PowerShellModules.ps1 +++ b/images/linux/scripts/installers/Install-PowerShellModules.ps1 @@ -1,14 +1,15 @@ $ErrorActionPreference = "Stop" +$ProgressPreference = "SilentlyContinue" -function Get-ToolsetContent -{ - $toolset = Join-Path $env:INSTALLER_SCRIPT_FOLDER "toolset.json" - Get-Content $toolset -Raw | ConvertFrom-Json -} +Import-Module "$env:HELPER_SCRIPTS/Tests.Helpers.psm1" -DisableNameChecking # Specifies the installation policy Set-PSRepository -InstallationPolicy Trusted -Name PSGallery +# Try to update PowerShellGet before the actual installation +Install-Module -Name PowerShellGet -Force +Update-Module -Name PowerShellGet -Force + # Install PowerShell modules $modules = (Get-ToolsetContent).powershellModules @@ -29,3 +30,5 @@ foreach($module in $modules) Install-Module -Name $moduleName -Scope AllUsers -SkipPublisherCheck -Force } + +Invoke-PesterTests -TestFile "PowerShellModules" -TestName "PowerShellModules" diff --git a/images/linux/scripts/installers/azpowershell.sh b/images/linux/scripts/installers/azpowershell.sh deleted file mode 100644 index 6c299e32b..000000000 --- a/images/linux/scripts/installers/azpowershell.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/bash -e -################################################################################ -## File: azpowershell.sh -## Desc: Installed Azure PowerShell -################################################################################ - -# List of versions -toolset="$INSTALLER_SCRIPT_FOLDER/toolset.json" -versions=$(jq -r '.azureModules[] | select(.name | contains("az")) | .versions[]' $toolset) - -# Try to install and update PowerShellGet before the actual installation -pwsh -Command "Install-Module -Name PowerShellGet -Force" -pwsh -Command "Update-Module -Name PowerShellGet -Force" - -# Install Azure CLI (instructions taken from https://docs.microsoft.com/en-us/cli/azure/install-azure-cli) -for version in ${versions[@]}; do - pwsh -Command "Save-Module -Name Az -LiteralPath /usr/share/az_$version -RequiredVersion $version -Force -Verbose" -done - -# Run tests to determine that the software installed as expected -echo "Testing to make sure that script performed as expected, and basic scenarios work" -for version in ${versions[@]}; do - modulePath="/usr/share/az_$version" - pwsh -Command " - \$env:PSModulePath = '${modulePath}:' + \$env:PSModulePath; - if ( -not (Get-Module -ListAvailable -Name Az.Accounts)) { - Write-Host 'Az Module was not installed' - exit 1 - }" - if [ $? -ne 0 ]; then - echo "Az version $version is not installed" - exit 1 - fi -done diff --git a/images/linux/scripts/installers/docker-compose.sh b/images/linux/scripts/installers/docker-compose.sh index 0fa74fc12..bc335eda5 100644 --- a/images/linux/scripts/installers/docker-compose.sh +++ b/images/linux/scripts/installers/docker-compose.sh @@ -4,15 +4,11 @@ ## Desc: Installs Docker Compose ################################################################################ -URL=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | jq -r '.assets[].browser_download_url | select(contains("docker-compose-Linux-x86_64"))' | head -1) +source $HELPER_SCRIPTS/invoke-tests.sh # Install latest docker-compose from releases +URL=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | jq -r '.assets[].browser_download_url | select(contains("docker-compose-Linux-x86_64"))' | head -1) curl -L $URL -o /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose -# Run tests to determine that the software installed as expected -echo "Testing to make sure that script performed as expected, and basic scenarios work" -if ! command -v docker-compose; then - echo "docker-compose was not installed" - exit 1 -fi +invoke_tests "Tools" "Docker-compose" diff --git a/images/linux/scripts/installers/docker-moby.sh b/images/linux/scripts/installers/docker-moby.sh index 964fbc92c..c5c783530 100644 --- a/images/linux/scripts/installers/docker-moby.sh +++ b/images/linux/scripts/installers/docker-moby.sh @@ -6,6 +6,7 @@ # Source the helpers for use with the script source $HELPER_SCRIPTS/install.sh +source $HELPER_SCRIPTS/invoke-tests.sh # Check to see if docker is already installed docker_package=moby @@ -24,21 +25,9 @@ fi systemctl is-active --quiet docker.service || systemctl start docker.service systemctl is-enabled --quiet docker.service || systemctl enable docker.service -# Run tests to determine that the software installed as expected -echo "Testing to make sure that script performed as expected, and basic scenarios work" -echo "Checking the docker-moby and moby-buildx" -if ! command -v docker; then - echo "docker was not installed" - exit 1 -elif ! [[ $(docker buildx) ]]; then - echo "Docker-Buildx was not installed" - exit 1 -else - echo "Docker-moby and Docker-buildx checking the successfull" - # Docker daemon takes time to come up after installing - sleep 10 - docker info -fi +# Docker daemon takes time to come up after installing +sleep 10 +docker info # Pull images toolset="$INSTALLER_SCRIPT_FOLDER/toolset.json" @@ -46,3 +35,5 @@ images=$(jq -r '.docker.images[]' $toolset) for image in $images; do docker pull "$image" done + +invoke_tests "Tools" "Docker" \ No newline at end of file diff --git a/images/linux/scripts/installers/powershellcore.sh b/images/linux/scripts/installers/powershellcore.sh index c3a2c53d3..a827f4a8e 100644 --- a/images/linux/scripts/installers/powershellcore.sh +++ b/images/linux/scripts/installers/powershellcore.sh @@ -13,7 +13,3 @@ if ! command -v pwsh; then echo "pwsh was not installed" exit 1 fi -if ! pwsh -c 'Write-Host Hello world'; then - echo "pwsh failed to run" - exit 1 -fi diff --git a/images/linux/scripts/tests/PowerShellModules.Tests.ps1 b/images/linux/scripts/tests/PowerShellModules.Tests.ps1 new file mode 100644 index 000000000..e7374742a --- /dev/null +++ b/images/linux/scripts/tests/PowerShellModules.Tests.ps1 @@ -0,0 +1,65 @@ +Describe "PowerShellModules" { + $modules = (Get-ToolsetContent).powershellModules + $withoutVersionsModules = $modules | Where-Object {-not $_.versions} | ForEach-Object { + @{moduleName = $_.name} + } + + $withVersionsModules = $modules | Where-Object {$_.versions} | ForEach-Object { + $moduleName = $_.name + $_.versions | ForEach-Object { + @{moduleName = $moduleName; expectedVersion = $_} + } + } + + It " is installed" -TestCases $withoutVersionsModules { + Get-Module -Name $moduleName -ListAvailable | Should -BeTrue + } + + if ($withVersionsModules) { + It " with is installed" -TestCases $withVersionsModules { + (Get-Module -Name $moduleName -ListAvailable).Version -contains $expectedVersion | Should -BeTrue + } + } +} + +Describe "AzureModules" { + $modules = (Get-ToolsetContent).azureModules + $modulesRootPath = "/usr/share" + + foreach ($module in $modules) { + $moduleName = $module.name + + Context "$moduleName" { + + foreach ($version in $module.versions) { + $modulePath = Join-Path -Path $modulesRootPath -ChildPath "${moduleName}_${version}" + $moduleInfo = @{ moduleName = $moduleName; modulePath = $modulePath; expectedVersion = $version } + It " exists in modules directory" -TestCases $moduleInfo { + $testJob = Start-Job -ScriptBlock { + param ( + $modulePath, + $moduleName + ) + + $env:PSModulePath = "${modulePath}:${env:PSModulePath}" + Import-Module -Name $moduleName + (Get-Module -Name $moduleName).Version.ToString() + + } -ArgumentList $modulePath, $moduleName + + $moduleVersion = $testJob | Wait-Job | Receive-Job + Remove-Job $testJob + $moduleVersion | Should -Match $expectedVersion + } + } + + if ($module.default) { + $moduleInfo = @{ moduleName = $moduleName; moduleDefault = $module.default } + It " set as default" -TestCases $moduleInfo { + $moduleVersion = (Get-Module -ListAvailable -Name $moduleName).Version.ToString() + $moduleVersion | Should -Match $moduleDefault + } + } + } + } +} \ No newline at end of file diff --git a/images/linux/scripts/tests/Tools.Tests.ps1 b/images/linux/scripts/tests/Tools.Tests.ps1 index 19e10d3b5..0f0963587 100644 --- a/images/linux/scripts/tests/Tools.Tests.ps1 +++ b/images/linux/scripts/tests/Tools.Tests.ps1 @@ -16,6 +16,40 @@ Describe "azcopy" { } } +Describe "Docker" { + It "docker" { + "docker --version" | Should -ReturnZeroExitCode + } + + It "docker buildx" { + "docker buildx" | Should -ReturnZeroExitCode + } + + Context "docker images" { + $testCases = (Get-ToolsetContent).docker.images | ForEach-Object { @{ ImageName = $_ } } + + It "" -TestCases $testCases { + sudo docker images "$ImageName" --format "{{.Repository}}" | Should -Not -BeNullOrEmpty + } + } +} + +Describe "Docker-compose" { + It "docker-compose" { + "docker-compose --version"| Should -ReturnZeroExitCode + } +} + +Describe "PowerShell Core" { + It "pwsh" { + "pwsh --version" | Should -ReturnZeroExitCode + } + + It "Execute 2+2 command" { + pwsh -Command "2+2" | Should -BeExactly 4 + } +} + Describe "Ansible" { It "Ansible" { "ansible --version" | Should -ReturnZeroExitCode @@ -84,4 +118,4 @@ Describe "gfortran" { "$GfortranVersion --version" | Should -ReturnZeroExitCode } -} \ No newline at end of file +} diff --git a/images/linux/ubuntu1604.json b/images/linux/ubuntu1604.json index 092100f55..2801570dc 100644 --- a/images/linux/ubuntu1604.json +++ b/images/linux/ubuntu1604.json @@ -156,9 +156,11 @@ { "type": "shell", "scripts": [ - "{{template_dir}}/scripts/installers/Install-PowerShellModules.ps1" + "{{template_dir}}/scripts/installers/Install-PowerShellModules.ps1", + "{{template_dir}}/scripts/installers/Install-AzureModules.ps1" ], "environment_vars": [ + "HELPER_SCRIPTS={{user `helper_script_folder`}}", "INSTALLER_SCRIPT_FOLDER={{user `installer_script_folder`}}" ], "execute_command": "sudo sh -c '{{ .Vars }} pwsh -f {{ .Path }}'" @@ -231,7 +233,6 @@ "{{template_dir}}/scripts/installers/swig.sh", "{{template_dir}}/scripts/installers/netlify.sh", "{{template_dir}}/scripts/installers/android.sh", - "{{template_dir}}/scripts/installers/azpowershell.sh", "{{template_dir}}/scripts/installers/pypy.sh", "{{template_dir}}/scripts/installers/python.sh" ], diff --git a/images/linux/ubuntu1804.json b/images/linux/ubuntu1804.json index 97f3cc303..fcab659ac 100644 --- a/images/linux/ubuntu1804.json +++ b/images/linux/ubuntu1804.json @@ -159,9 +159,11 @@ { "type": "shell", "scripts": [ - "{{template_dir}}/scripts/installers/Install-PowerShellModules.ps1" + "{{template_dir}}/scripts/installers/Install-PowerShellModules.ps1", + "{{template_dir}}/scripts/installers/Install-AzureModules.ps1" ], "environment_vars": [ + "HELPER_SCRIPTS={{user `helper_script_folder`}}", "INSTALLER_SCRIPT_FOLDER={{user `installer_script_folder`}}" ], "execute_command": "sudo sh -c '{{ .Vars }} pwsh -f {{ .Path }}'" @@ -235,7 +237,6 @@ "{{template_dir}}/scripts/installers/swig.sh", "{{template_dir}}/scripts/installers/netlify.sh", "{{template_dir}}/scripts/installers/android.sh", - "{{template_dir}}/scripts/installers/azpowershell.sh", "{{template_dir}}/scripts/installers/pypy.sh", "{{template_dir}}/scripts/installers/python.sh" ], diff --git a/images/linux/ubuntu2004.json b/images/linux/ubuntu2004.json index a15d3d511..0fe4a31ea 100644 --- a/images/linux/ubuntu2004.json +++ b/images/linux/ubuntu2004.json @@ -161,9 +161,11 @@ { "type": "shell", "scripts": [ - "{{template_dir}}/scripts/installers/Install-PowerShellModules.ps1" + "{{template_dir}}/scripts/installers/Install-PowerShellModules.ps1", + "{{template_dir}}/scripts/installers/Install-AzureModules.ps1" ], "environment_vars": [ + "HELPER_SCRIPTS={{user `helper_script_folder`}}", "INSTALLER_SCRIPT_FOLDER={{user `installer_script_folder`}}" ], "execute_command": "sudo sh -c '{{ .Vars }} pwsh -f {{ .Path }}'" @@ -237,7 +239,6 @@ "{{template_dir}}/scripts/installers/swig.sh", "{{template_dir}}/scripts/installers/netlify.sh", "{{template_dir}}/scripts/installers/android.sh", - "{{template_dir}}/scripts/installers/azpowershell.sh", "{{template_dir}}/scripts/installers/pypy.sh", "{{template_dir}}/scripts/installers/python.sh" ],