summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2021-09-17 08:09:31 -0700
committerGitHub <noreply@github.com>2021-09-17 08:09:31 -0700
commitbcf517dfa13bd06c088dc42b5ebd26c42785f30c (patch)
tree27532c6e925d7ac69156fd33bd527a56cc11f83a
parentae0bd77ccaf7433062a42d292775bed580005648 (diff)
parent42716dfbe123a4558e22160c51be536997600770 (diff)
downloadchef-bcf517dfa13bd06c088dc42b5ebd26c42785f30c.tar.gz
Merge pull request #12054 from chef/package_options
Add user provided options to powershell_package commands
-rw-r--r--lib/chef/provider/package/powershell.rb5
-rw-r--r--spec/unit/provider/package/powershell_spec.rb86
2 files changed, 79 insertions, 12 deletions
diff --git a/lib/chef/provider/package/powershell.rb b/lib/chef/provider/package/powershell.rb
index 1f59360c61..1c123d7952 100644
--- a/lib/chef/provider/package/powershell.rb
+++ b/lib/chef/provider/package/powershell.rb
@@ -124,6 +124,11 @@ class Chef
command.push("-RequiredVersion #{version}") if version
command.push("-Source #{new_resource.source}") if new_resource.source && cmdlet_name =~ Regexp.union(/Install-Package/, /Find-Package/)
command.push("-SkipPublisherCheck") if new_resource.skip_publisher_check && cmdlet_name !~ /Find-Package/
+ if new_resource.options && cmdlet_name !~ Regexp.union(/Get-Package/, /Find-Package/)
+ new_resource.options.each do |arg|
+ command.push(arg) unless command.include?(arg)
+ end
+ end
command.push(").Version")
command.join(" ")
end
diff --git a/spec/unit/provider/package/powershell_spec.rb b/spec/unit/provider/package/powershell_spec.rb
index 2f4da8c179..8bf95b9b14 100644
--- a/spec/unit/provider/package/powershell_spec.rb
+++ b/spec/unit/provider/package/powershell_spec.rb
@@ -105,6 +105,10 @@ describe Chef::Provider::Package::Powershell, :windows_only, :windows_gte_10 do
let(:generated_install_cmdlet) { "#{tls_set_command} ( Install-Package xNetworking -Force -ForceBootstrap -WarningAction SilentlyContinue ).Version" }
let(:generated_install_cmdlet_with_version) { "#{tls_set_command} ( Install-Package xNetworking -Force -ForceBootstrap -WarningAction SilentlyContinue -RequiredVersion 1.0.0.0 ).Version" }
let(:generated_install_cmdlet_with_source) { "#{tls_set_command} ( Install-Package xNetworking -Force -ForceBootstrap -WarningAction SilentlyContinue -Source MyGallery ).Version" }
+ let(:generated_install_cmdlet_with_options) { "#{tls_set_command} ( Install-Package xNetworking -Force -ForceBootstrap -WarningAction SilentlyContinue -AcceptLicense -Verbose ).Version" }
+ let(:generated_install_cmdlet_with_version_and_options) { "#{tls_set_command} ( Install-Package xNetworking -Force -ForceBootstrap -WarningAction SilentlyContinue -RequiredVersion 1.0.0.0 -AcceptLicense -Verbose ).Version" }
+ let(:generated_install_cmdlet_with_source_and_options) { "#{tls_set_command} ( Install-Package xNetworking -Force -ForceBootstrap -WarningAction SilentlyContinue -Source MyGallery -AcceptLicense -Verbose ).Version" }
+ let(:generated_install_cmdlet_with_source_and_version_and_options) { "#{tls_set_command} ( Install-Package xNetworking -Force -ForceBootstrap -WarningAction SilentlyContinue -RequiredVersion 1.0.0.0 -Source MyGallery -AcceptLicense -Verbose ).Version" }
let(:generated_install_cmdlet_with_source_and_version) { "#{tls_set_command} ( Install-Package xNetworking -Force -ForceBootstrap -WarningAction SilentlyContinue -RequiredVersion 1.0.0.0 -Source MyGallery ).Version" }
let(:generated_uninstall_cmdlet) { "#{tls_set_command} ( Uninstall-Package xNetworking -Force -ForceBootstrap -WarningAction SilentlyContinue ).Version" }
let(:generated_uninstall_cmdlet_with_version) { "#{tls_set_command} ( Uninstall-Package xNetworking -Force -ForceBootstrap -WarningAction SilentlyContinue -RequiredVersion 1.0.0.0 ).Version" }
@@ -204,11 +208,11 @@ describe Chef::Provider::Package::Powershell, :windows_only, :windows_gte_10 do
end
context "when source is nil" do
- it "build get commands correctly" do
+ it "builds get commands correctly" do
expect(provider.build_powershell_package_command("Get-Package xNetworking")).to eql(generated_get_cmdlet)
end
- it "build get commands correctly when a version is passed" do
+ it "builds get commands correctly when a version is passed" do
expect(provider.build_powershell_package_command("Get-Package xNetworking", "1.0.0.0")).to eql(generated_get_cmdlet_with_version)
end
@@ -220,30 +224,45 @@ describe Chef::Provider::Package::Powershell, :windows_only, :windows_gte_10 do
expect(provider.build_powershell_package_command("Find-Package xNetworking", "1.0.0.0")).to eql(generated_find_cmdlet_with_version)
end
- it "build install commands correctly" do
+ it "builds install commands correctly" do
expect(provider.build_powershell_package_command("Install-Package xNetworking")).to eql(generated_install_cmdlet)
end
- it "build install commands correctly when a version is passed" do
+ it "builds install commands correctly when a version is passed" do
expect(provider.build_powershell_package_command("Install-Package xNetworking", "1.0.0.0")).to eql(generated_install_cmdlet_with_version)
end
- it "build install commands correctly" do
+ it "builds install commands correctly when options are passed" do
+ new_resource.options("-AcceptLicense -Verbose")
+ expect(provider.build_powershell_package_command("Install-Package xNetworking")).to eql(generated_install_cmdlet_with_options)
+ end
+
+ it "builds install commands correctly when duplicate options are passed" do
+ new_resource.options("-WarningAction SilentlyContinue")
+ expect(provider.build_powershell_package_command("Install-Package xNetworking")).to eql(generated_install_cmdlet)
+ end
+
+ it "builds install commands correctly when a version and options are passed" do
+ new_resource.options("-AcceptLicense -Verbose")
+ expect(provider.build_powershell_package_command("Install-Package xNetworking", "1.0.0.0")).to eql(generated_install_cmdlet_with_version_and_options)
+ end
+
+ it "builds install commands correctly" do
expect(provider.build_powershell_package_command("Uninstall-Package xNetworking")).to eql(generated_uninstall_cmdlet)
end
- it "build install commands correctly when a version is passed" do
+ it "builds install commands correctly when a version is passed" do
expect(provider.build_powershell_package_command("Uninstall-Package xNetworking", "1.0.0.0")).to eql(generated_uninstall_cmdlet_with_version)
end
end
context "when source is set" do
- it "build get commands correctly" do
+ it "builds get commands correctly" do
new_resource.source("MyGallery")
expect(provider.build_powershell_package_command("Get-Package xNetworking")).to eql(generated_get_cmdlet)
end
- it "build get commands correctly when a version is passed" do
+ it "builds get commands correctly when a version is passed" do
new_resource.source("MyGallery")
expect(provider.build_powershell_package_command("Get-Package xNetworking", "1.0.0.0")).to eql(generated_get_cmdlet_with_version)
end
@@ -258,22 +277,40 @@ describe Chef::Provider::Package::Powershell, :windows_only, :windows_gte_10 do
expect(provider.build_powershell_package_command("Find-Package xNetworking", "1.0.0.0")).to eql(generated_find_cmdlet_with_source_and_version)
end
- it "build install commands correctly" do
+ it "builds install commands correctly" do
new_resource.source("MyGallery")
expect(provider.build_powershell_package_command("Install-Package xNetworking")).to eql(generated_install_cmdlet_with_source)
end
- it "build install commands correctly when a version is passed" do
+ it "builds install commands correctly when a version is passed" do
new_resource.source("MyGallery")
expect(provider.build_powershell_package_command("Install-Package xNetworking", "1.0.0.0")).to eql(generated_install_cmdlet_with_source_and_version)
end
- it "build install commands correctly" do
+ it "builds install commands correctly when options are passed" do
+ new_resource.source("MyGallery")
+ new_resource.options("-AcceptLicense -Verbose")
+ expect(provider.build_powershell_package_command("Install-Package xNetworking")).to eql(generated_install_cmdlet_with_source_and_options)
+ end
+
+ it "builds install commands correctly when duplicate options are passed" do
+ new_resource.source("MyGallery")
+ new_resource.options("-Force -ForceBootstrap")
+ expect(provider.build_powershell_package_command("Install-Package xNetworking")).to eql(generated_install_cmdlet_with_source)
+ end
+
+ it "builds install commands correctly when a version and options are passed" do
+ new_resource.source("MyGallery")
+ new_resource.options("-AcceptLicense -Verbose")
+ expect(provider.build_powershell_package_command("Install-Package xNetworking", "1.0.0.0")).to eql(generated_install_cmdlet_with_source_and_version_and_options)
+ end
+
+ it "builds install commands correctly" do
new_resource.source("MyGallery")
expect(provider.build_powershell_package_command("Uninstall-Package xNetworking")).to eql(generated_uninstall_cmdlet)
end
- it "build install commands correctly when a version is passed" do
+ it "builds install commands correctly when a version is passed" do
new_resource.source("MyGallery")
expect(provider.build_powershell_package_command("Uninstall-Package xNetworking", "1.0.0.0")).to eql(generated_uninstall_cmdlet_with_version)
end
@@ -434,6 +471,19 @@ describe Chef::Provider::Package::Powershell, :windows_only, :windows_gte_10 do
provider.run_action(:install)
expect(new_resource).to be_updated_by_last_action
end
+
+ it "should install a package using provided options" do
+ provider.load_current_resource
+ new_resource.package_name(["xCertificate"])
+ new_resource.version(nil)
+ new_resource.options(%w{-AcceptLicense -Verbose})
+ allow(provider).to receive(:powershell_out).with("#{tls_set_command} ( Find-Package 'xCertificate' -Force -ForceBootstrap -WarningAction SilentlyContinue ).Version", { timeout: new_resource.timeout }).and_return(package_xcertificate_available)
+ allow(provider).to receive(:powershell_out).with("#{tls_set_command} ( Get-Package 'xCertificate' -Force -ForceBootstrap -WarningAction SilentlyContinue ).Version", { timeout: new_resource.timeout }).and_return(package_xcertificate_not_available)
+ allow(provider).to receive(:powershell_out).with("$PSVersionTable.PSVersion.Major").and_return(powershell_installed_version)
+ expect(provider).to receive(:powershell_out).with("#{tls_set_command} ( Install-Package 'xCertificate' -Force -ForceBootstrap -WarningAction SilentlyContinue -RequiredVersion 2.1.0.0 -AcceptLicense -Verbose ).Version", { timeout: new_resource.timeout })
+ provider.run_action(:install)
+ expect(new_resource).to be_updated_by_last_action
+ end
end
describe "#action_remove" do
@@ -499,5 +549,17 @@ describe Chef::Provider::Package::Powershell, :windows_only, :windows_gte_10 do
provider.run_action(:remove)
expect(new_resource).to be_updated_by_last_action
end
+
+ it "should remove a package using provided options" do
+ new_resource.package_name(["xCertificate"])
+ new_resource.options(%w{-AllVersions})
+ allow(provider).to receive(:powershell_out).with("#{tls_set_command} ( Find-Package 'xCertificate' -Force -ForceBootstrap -WarningAction SilentlyContinue ).Version", { timeout: new_resource.timeout }).and_return(package_xcertificate_available)
+ allow(provider).to receive(:powershell_out).with("#{tls_set_command} ( Get-Package 'xCertificate' -Force -ForceBootstrap -WarningAction SilentlyContinue ).Version", { timeout: new_resource.timeout }).and_return(package_xcertificate_available)
+ allow(provider).to receive(:powershell_out).with("$PSVersionTable.PSVersion.Major").and_return(powershell_installed_version)
+ provider.load_current_resource
+ expect(provider).to receive(:powershell_out).with("#{tls_set_command} ( Uninstall-Package 'xCertificate' -Force -ForceBootstrap -WarningAction SilentlyContinue -AllVersions ).Version", { timeout: new_resource.timeout }).and_return(package_xcertificate_not_available)
+ provider.run_action(:remove)
+ expect(new_resource).to be_updated_by_last_action
+ end
end
end