diff options
-rw-r--r-- | lib/chef/provider/package/powershell.rb | 5 | ||||
-rw-r--r-- | lib/chef/resource/powershell_package.rb | 1 | ||||
-rw-r--r-- | spec/unit/provider/package/powershell_spec.rb | 51 | ||||
-rw-r--r-- | spec/unit/resource/powershell_package_spec.rb | 9 |
4 files changed, 66 insertions, 0 deletions
diff --git a/lib/chef/provider/package/powershell.rb b/lib/chef/provider/package/powershell.rb index 9fe3e52b24..eb65d80e3d 100644 --- a/lib/chef/provider/package/powershell.rb +++ b/lib/chef/provider/package/powershell.rb @@ -115,9 +115,14 @@ class Chef command.push(arg) end command.push("-RequiredVersion #{version}") if version + command.push("-Source #{new_resource.source}") if new_resource.source && command[1] =~ Regexp.union(/Install-Package/, /Find-Package/) command.push(").Version") command.join(" ") end + + def check_resource_semantics! + # This validation method from Chef::Provider::Package does not apply here, so no-op it. + end end end end diff --git a/lib/chef/resource/powershell_package.rb b/lib/chef/resource/powershell_package.rb index 05c59acf73..829db5f4f8 100644 --- a/lib/chef/resource/powershell_package.rb +++ b/lib/chef/resource/powershell_package.rb @@ -41,6 +41,7 @@ class Chef property :version, [String, Array], coerce: proc { |x| [x].flatten } + property :source, [String] end end end diff --git a/spec/unit/provider/package/powershell_spec.rb b/spec/unit/provider/package/powershell_spec.rb index fc53eea12d..c9fb5b69ca 100644 --- a/spec/unit/provider/package/powershell_spec.rb +++ b/spec/unit/provider/package/powershell_spec.rb @@ -22,6 +22,7 @@ require "chef/mixin/powershell_out" describe Chef::Provider::Package::Powershell do include Chef::Mixin::PowershellOut let(:timeout) { 900 } + let(:source) { nil } let(:new_resource) { Chef::Resource::PowershellPackage.new("windows_test_pkg") } @@ -107,6 +108,14 @@ describe Chef::Provider::Package::Powershell do expect(provider.candidate_version).to eql(["2.12.0.0"]) end + it "should use the candidate_version from the correct source" do + allow(provider).to receive(:powershell_out).with("( Find-Package 'xNetworking' -Force -ForceBootstrap -Source MyGallery ).Version", {:timeout => new_resource.timeout }).and_return(package_xnetworking_available) + new_resource.package_name(["xNetworking"]) + new_resource.version(nil) + new_resource.source("MyGallery") + expect(provider.candidate_version).to eql(["2.12.0.0"]) + end + it "should set the candidate_version to the latest version when not pinning and package name is space seperated" do allow(provider).to receive(:powershell_out).with("( Find-Package '7-Zip 16.02 (x64)' -Force -ForceBootstrap ).Version", { :timeout => new_resource.timeout }).and_return(package_7zip_available) new_resource.package_name(["7-Zip 16.02 (x64)"]) @@ -182,6 +191,19 @@ describe Chef::Provider::Package::Powershell do expect(new_resource).to be_updated_by_last_action end + it "should install a single package from a custom source" do + provider.load_current_resource + new_resource.package_name(["xCertificate"]) + new_resource.version(nil) + new_resource.source("MyGallery") + allow(provider).to receive(:powershell_out).with("( Find-Package 'xCertificate' -Force -ForceBootstrap -Source MyGallery ).Version", { :timeout => new_resource.timeout }).and_return(package_xcertificate_available) + allow(provider).to receive(:powershell_out).with("( Get-Package 'xCertificate' -Force -ForceBootstrap ).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("( Install-Package 'xCertificate' -Force -ForceBootstrap -RequiredVersion 2.1.0.0 -Source MyGallery ).Version", { :timeout => new_resource.timeout }) + provider.run_action(:install) + expect(new_resource).to be_updated_by_last_action + end + it "should install a single package when package name has space in between" do provider.load_current_resource new_resource.package_name(["7-Zip 16.02 (x64)"]) @@ -281,6 +303,22 @@ describe Chef::Provider::Package::Powershell do provider.run_action(:install) expect(new_resource).to be_updated_by_last_action end + + it "should do multipackage installs from a custom source when given two packages without constraints" do + new_resource.package_name(%w{xCertificate xNetworking}) + new_resource.version(nil) + new_resource.source("MyGallery") + allow(provider).to receive(:powershell_out).with("( Find-Package 'xCertificate' -Force -ForceBootstrap -Source MyGallery ).Version", { :timeout => new_resource.timeout }).and_return(package_xcertificate_available) + allow(provider).to receive(:powershell_out).with("( Get-Package 'xCertificate' -Force -ForceBootstrap ).Version", { :timeout => new_resource.timeout }).and_return(package_xcertificate_not_available) + allow(provider).to receive(:powershell_out).with("( Find-Package 'xNetworking' -Force -ForceBootstrap -Source MyGallery ).Version", { :timeout => new_resource.timeout }).and_return(package_xnetworking_available) + allow(provider).to receive(:powershell_out).with("( Get-Package 'xNetworking' -Force -ForceBootstrap ).Version", { :timeout => new_resource.timeout }).and_return(package_xnetworking_not_available) + allow(provider).to receive(:powershell_out).with("$PSVersionTable.PSVersion.Major").and_return(powershell_installed_version) + expect(provider).to receive(:powershell_out).with("( Install-Package 'xCertificate' -Force -ForceBootstrap -RequiredVersion 2.1.0.0 -Source MyGallery ).Version", { :timeout => new_resource.timeout }) + expect(provider).to receive(:powershell_out).with("( Install-Package 'xNetworking' -Force -ForceBootstrap -RequiredVersion 2.12.0.0 -Source MyGallery ).Version", { :timeout => new_resource.timeout }) + provider.load_current_resource + provider.run_action(:install) + expect(new_resource).to be_updated_by_last_action + end end describe "#action_remove" do @@ -296,6 +334,19 @@ describe Chef::Provider::Package::Powershell do expect(new_resource).not_to be_updated_by_last_action end + it "does not pass the source parameter to get or uninstall cmdlets" do + new_resource.package_name(["xCertificate"]) + new_resource.version(["2.1.0.0"]) + new_resource.source("MyGallery") + allow(provider).to receive(:powershell_out).with("( Find-Package 'xCertificate' -Force -ForceBootstrap -RequiredVersion 2.1.0.0 -Source MyGallery).Version", { :timeout => new_resource.timeout }).and_return(package_xcertificate_available) + allow(provider).to receive(:powershell_out).with("( Get-Package 'xCertificate' -Force -ForceBootstrap -RequiredVersion 2.1.0.0 ).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("( Uninstall-Package 'xCertificate' -Force -ForceBootstrap -RequiredVersion 2.1.0.0 ).Version", { :timeout => new_resource.timeout }) + provider.run_action(:remove) + expect(new_resource).to be_updated_by_last_action + end + it "does nothing when all the packages are already removed" do new_resource.package_name(%w{xCertificate xNetworking}) new_resource.version(nil) diff --git a/spec/unit/resource/powershell_package_spec.rb b/spec/unit/resource/powershell_package_spec.rb index 701014bd95..a448d58d5a 100644 --- a/spec/unit/resource/powershell_package_spec.rb +++ b/spec/unit/resource/powershell_package_spec.rb @@ -63,4 +63,13 @@ describe Chef::Resource::PowershellPackage do resource.version(["1.2.3", "4.5.6"]) expect(resource.version).to eql(["1.2.3", "4.5.6"]) end + + it "the default source is nil" do + expect(resource.source).to eql(nil) + end + + it "the source setter accepts strings" do + resource.source("MyGallery") + expect(resource.source).to eql("MyGallery") + end end |