From 73e3086372d09ca13f73b861022149443c6f118e Mon Sep 17 00:00:00 2001 From: John Date: Fri, 24 Feb 2023 13:49:56 -0800 Subject: Added test and define_resource_requirements block Signed-off-by: John --- lib/chef/resource/chocolatey_installer.rb | 20 ++++++++++++++++--- spec/unit/resource/chocolatey_installer_spec.rb | 26 +++++++++++++++++++++---- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/lib/chef/resource/chocolatey_installer.rb b/lib/chef/resource/chocolatey_installer.rb index aa1f2a60b1..5f94881665 100644 --- a/lib/chef/resource/chocolatey_installer.rb +++ b/lib/chef/resource/chocolatey_installer.rb @@ -21,13 +21,14 @@ class Chef action :uninstall end ``` + **Install Chocolatey with Parameters** ```ruby chocolatey_installer 'latest' do action :install download_url "https://www.contoso.com/foo" - chocolatey_version 2.12.24 + chocolatey_version '2.12.24' end ``` DOC @@ -65,6 +66,18 @@ class Chef ::File.exist?("#{ENV["ALLUSERSPROFILE"]}\\chocolatey\\bin\\choco.exe") end + def define_resource_requirements + [ new_resource.proxy_user, new_resource.proxy_password ].each do + requirements.assert(:install) do |a| + a.assertion do + (!new_resource.proxy_user.nil? && new_resource.proxy_password.nil?) || (new_resource.proxy_user.nil? && !new_resource.proxy_password.nil?) + end + a.failure_message(Chef::Exceptions::ValidationFailed, "You must specify both a proxy_user and a proxy_password") + a.whyrun("Assuming that if you have configured a 'proxy_user' you must also supply a 'proxy_password'") + end + end + end + action :install, description: "Installs Chocolatey package manager" do unless new_resource.download_url.nil? "Set-Item -path env:chocolateyDownloadUrl -Value #{new_resource.download_url}" @@ -88,11 +101,12 @@ class Chef if !new_resource.proxy_user.nil? && !new_resource.proxy_password.nil? "Set-Item -path env:chocolateyProxyUser -Value #{new_resource.proxy_user}; Set-Item -path env:chocolateyProxyPassword -Value #{new_resource.proxy_password}" - elsif (!new_resource.proxy_user.nil? && new_resource.proxy_password.nil?) || (new_resource.proxy_user.nil? && !new_resource.proxy_password.nil?) - Chef::Log.error("Both a Proxy User and a Proxy Password must be set or neither can be set") + # elsif (!new_resource.proxy_user.nil? && new_resource.proxy_password.nil?) || (new_resource.proxy_user.nil? && !new_resource.proxy_password.nil?) + # Chef::Log.error("Both a Proxy User and a Proxy Password must be set or neither can be set") end converge_if_changed do + # the '-bor' parameter below is a Bitwise Or of 2 bytes that is used to create the correct Security Protocol offset with and results in creating TLS 1.2 powershell_code = <<-CODE Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; diff --git a/spec/unit/resource/chocolatey_installer_spec.rb b/spec/unit/resource/chocolatey_installer_spec.rb index 16767db58e..c4f758f385 100644 --- a/spec/unit/resource/chocolatey_installer_spec.rb +++ b/spec/unit/resource/chocolatey_installer_spec.rb @@ -50,7 +50,7 @@ describe Chef::Resource::ChocolateyInstaller do ENV.update(@original_env) end - describe "basic chocolatey settings" do + describe "Basic chocolatey settings" do context "on windows", :windows_only do it "has a resource name of :chocolatey_installer" do expect(resource.resource_name).to eql(:chocolatey_installer) @@ -64,10 +64,14 @@ describe Chef::Resource::ChocolateyInstaller do expect { resource.action :install }.not_to raise_error expect { resource.action :uninstall }.not_to raise_error end + + it "does not support bologna install options" do + expect { resource.action :foo }.to raise_error(Chef::Exceptions::ValidationFailed) + end end end - describe "installing chocolatey" do + describe "Installing chocolatey" do context "on windows", :windows_only do it "can install Chocolatey with parameters" do expect { resource.action :install }.not_to raise_error @@ -80,7 +84,21 @@ describe Chef::Resource::ChocolateyInstaller do end end - describe "chocolatey is idempotent because it" do + describe "Installing chocolatey with broken parameters" do + let(:resource) do + resource = Chef::Resource::ChocolateyInstaller.new("fakey_fakerton") + resource.instance_variable_set(:@proxy_user, "steveb@microsoft.com") + resource.instance_variable_set(:@proxy_password, "") + resource + end + it "should error out if both a proxy user and proxy password are not specified" do + # require "pry" + # binding.pry + expect { resource.action :install }.to raise_error(Chef::Exceptions::ValidationFailed) + end + end + + describe "Chocolatey is idempotent because it" do context "on windows", :windows_only do it "does not install choco again if it is already installed" do install_choco @@ -91,7 +109,7 @@ describe Chef::Resource::ChocolateyInstaller do end end - describe "uinstalling chocolatey" do + describe "Uinstalling chocolatey" do context "on windows", :windows_only do it "doesn't error out uninstalling chocolatey if chocolatey is not installed" do allow(::File).to receive(:exist?).with('C:\ProgramData\chocolatey\bin\choco.exe').and_return(false) -- cgit v1.2.1