From 2850ac176f07bdb422ae059b7172450a7db9fbb2 Mon Sep 17 00:00:00 2001 From: Joel Sallow <32407840+vexx32@users.noreply.github.com> Date: Fri, 24 Jun 2022 09:22:33 -0400 Subject: [PATCH] (#74) Add test for bootstrap script Made sense to move the test package files into a subfolder, so did that in order to keep test files a bit more organised. --- .../setup_win_chocolatey/defaults/main.yml | 1 + .../setup_win_chocolatey/files/bootstrap.ps1 | 25 +++++++++++++++++++ .../files/{ => test-package}/package.nuspec | 0 .../tools/chocolateyUninstall.ps1 | 0 .../tools/chocolateyinstall.ps1 | 0 .../setup_win_chocolatey/tasks/main.yml | 7 +++++- .../targets/win_chocolatey/tasks/tests.yml | 18 +++++++++++++ 7 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 chocolatey/tests/integration/targets/setup_win_chocolatey/files/bootstrap.ps1 rename chocolatey/tests/integration/targets/setup_win_chocolatey/files/{ => test-package}/package.nuspec (100%) rename chocolatey/tests/integration/targets/setup_win_chocolatey/files/{ => test-package}/tools/chocolateyUninstall.ps1 (100%) rename chocolatey/tests/integration/targets/setup_win_chocolatey/files/{ => test-package}/tools/chocolateyinstall.ps1 (100%) diff --git a/chocolatey/tests/integration/targets/setup_win_chocolatey/defaults/main.yml b/chocolatey/tests/integration/targets/setup_win_chocolatey/defaults/main.yml index eb5289d..06916bd 100644 --- a/chocolatey/tests/integration/targets/setup_win_chocolatey/defaults/main.yml +++ b/chocolatey/tests/integration/targets/setup_win_chocolatey/defaults/main.yml @@ -1,5 +1,6 @@ --- test_choco_path: '{{ win_output_dir }}\win_chocolatey' +test_choco_bootstrap_script: '{{ win_output_dir }}\test-bootstrap.ps1' test_choco_source: '{{ test_choco_path }}\packages' test_choco_source2: '{{ test_choco_path }}\packages2' # used to verify source works with the source name and not just the path test_choco_package1: ansible diff --git a/chocolatey/tests/integration/targets/setup_win_chocolatey/files/bootstrap.ps1 b/chocolatey/tests/integration/targets/setup_win_chocolatey/files/bootstrap.ps1 new file mode 100644 index 0000000..6698130 --- /dev/null +++ b/chocolatey/tests/integration/targets/setup_win_chocolatey/files/bootstrap.ps1 @@ -0,0 +1,25 @@ +# This is a minimal bootstrap script which simply pulls the default bootstrap +# script from community.chocolatey.org and then uses that to install Chocolatey. +# Afterwards, a file is created so we can confirm this bootstrap script was +# indeed used to install Chocolatey. + +$protocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12 +[System.Net.ServicePointManager]::SecurityProtocol = $protocol + +$client = New-Object System.Net.WebClient +$scriptContent = $client.DownloadString("https://community.chocolatey.org/install.ps1") +$filePath = "$PSScriptRoot/install.ps1" + +$scriptContent | Set-Content -Path $filePath + +$ErrorActionPreference = 'Stop' + +# Parameters aren't needed; our Ansible installation process sets all the parameters +# with environment variables before running the bootstrap script. +& $filePath + +$temp = "C:\temp" +if (-not (Test-Path $temp)) { + New-Item -ItemType Directory -Path $temp -Force > $null +} +[pscustomobject]@{ bootstrap = $true } | Set-Content -Path "$temp\confirm-bootstrap.txt" diff --git a/chocolatey/tests/integration/targets/setup_win_chocolatey/files/package.nuspec b/chocolatey/tests/integration/targets/setup_win_chocolatey/files/test-package/package.nuspec similarity index 100% rename from chocolatey/tests/integration/targets/setup_win_chocolatey/files/package.nuspec rename to chocolatey/tests/integration/targets/setup_win_chocolatey/files/test-package/package.nuspec diff --git a/chocolatey/tests/integration/targets/setup_win_chocolatey/files/tools/chocolateyUninstall.ps1 b/chocolatey/tests/integration/targets/setup_win_chocolatey/files/test-package/tools/chocolateyUninstall.ps1 similarity index 100% rename from chocolatey/tests/integration/targets/setup_win_chocolatey/files/tools/chocolateyUninstall.ps1 rename to chocolatey/tests/integration/targets/setup_win_chocolatey/files/test-package/tools/chocolateyUninstall.ps1 diff --git a/chocolatey/tests/integration/targets/setup_win_chocolatey/files/tools/chocolateyinstall.ps1 b/chocolatey/tests/integration/targets/setup_win_chocolatey/files/test-package/tools/chocolateyinstall.ps1 similarity index 100% rename from chocolatey/tests/integration/targets/setup_win_chocolatey/files/tools/chocolateyinstall.ps1 rename to chocolatey/tests/integration/targets/setup_win_chocolatey/files/test-package/tools/chocolateyinstall.ps1 diff --git a/chocolatey/tests/integration/targets/setup_win_chocolatey/tasks/main.yml b/chocolatey/tests/integration/targets/setup_win_chocolatey/tasks/main.yml index 67a515d..95ee1f5 100644 --- a/chocolatey/tests/integration/targets/setup_win_chocolatey/tasks/main.yml +++ b/chocolatey/tests/integration/targets/setup_win_chocolatey/tasks/main.yml @@ -14,9 +14,14 @@ - name: copy template package files win_copy: - src: files/ + src: files/test-package/ dest: '{{ test_choco_path }}' +- name: copy test bootstrap script + win_copy: + src: files/bootstrap.ps1 + dest: '{{ test_choco_bootstrap_script }}' + # run the setup in 1 shell script to save on test time - name: set up packages win_shell: | diff --git a/chocolatey/tests/integration/targets/win_chocolatey/tasks/tests.yml b/chocolatey/tests/integration/targets/win_chocolatey/tasks/tests.yml index 9edc3a8..2422e0a 100644 --- a/chocolatey/tests/integration/targets/win_chocolatey/tasks/tests.yml +++ b/chocolatey/tests/integration/targets/win_chocolatey/tasks/tests.yml @@ -694,3 +694,21 @@ that: - not remove_nonexistent_version is changed - '"{{ test_choco_package1 }}|0.1.0" in remove_nonexistent_version_result.stdout_lines' + +- name: remove existing Chocolatey installation + win_shell: Remove-Item -Path "C:/ProgramData/chocolatey" -Recurse -Force + +- name: install Chocolatey using the test bootstrap script + win_chocolatey: + name: chocolatey + state: present + bootstrap_script: "{{ test_choco_bootstrap_script }}" + +- name: checking if the bootstrap file has been created + win_shell: Get-Content -Path "C:/temp/confirm-bootstrap.txt" -Raw + register: bootstrap_file_check + +- name: assert bootstrap file has been created + assert: + that: + - (bootstrap_file_check.stdout|from_json).bootstrap