diff options
author | dheerajd-msys <dheeraj.dubey@msystechnologies.com> | 2016-10-25 16:47:07 +0530 |
---|---|---|
committer | Bryan McLellan <btm@loftninjas.org> | 2016-11-11 08:02:44 -0500 |
commit | 9c8dac1b11bb9d420824398c903f71dbf659bf06 (patch) | |
tree | 98e0835d9f8c5cfd0ff5ee3f58bf93d27151e03b | |
parent | 1b05baf3cdf6f0bcf8fd5aeabf5a34695937adac (diff) | |
download | chef-9c8dac1b11bb9d420824398c903f71dbf659bf06.tar.gz |
Added unit specs for powershell package
Signed-off-by: dheerajd-msys <dheeraj.dubey@msystechnologies.com>
-rw-r--r-- | lib/chef/provider/package.rb | 3 | ||||
-rw-r--r-- | lib/chef/provider/package/powershell.rb | 71 | ||||
-rw-r--r-- | lib/chef/resource/powershell_package.rb | 4 | ||||
-rw-r--r-- | spec/unit/provider/package/powershell_spec.rb | 367 | ||||
-rw-r--r-- | spec/unit/resource/powershell_package_spec.rb | 68 |
5 files changed, 478 insertions, 35 deletions
diff --git a/lib/chef/provider/package.rb b/lib/chef/provider/package.rb index f22d20dbbc..048807dd05 100644 --- a/lib/chef/provider/package.rb +++ b/lib/chef/provider/package.rb @@ -453,9 +453,6 @@ class Chef elsif current_version.nil? Chef::Log.debug("#{new_resource} #{package_name} not installed, installing #{candidate_version}") target_version_array.push(candidate_version) - elsif !version_requirement_satisfied?(current_version, candidate_version) - Chef::Log.debug("#{new_resource} #{package_name} #{candidate_version} not installed, installing #{candidate_version}") - target_version_array.push(candidate_version) else Chef::Log.debug("#{new_resource} #{package_name} #{current_version} already installed") target_version_array.push(nil) diff --git a/lib/chef/provider/package/powershell.rb b/lib/chef/provider/package/powershell.rb index 84e8c53fc1..047c5768fb 100644 --- a/lib/chef/provider/package/powershell.rb +++ b/lib/chef/provider/package/powershell.rb @@ -28,7 +28,7 @@ class Chef provides :powershell_package, os: "windows" def load_current_resource - @current_resource = Chef::Resource::PowershellPackage.new(new_resource.name) + @current_resource = Chef::Resource::PowershellPackage.new(new_resource.name) current_resource.package_name(new_resource.package_name) current_resource.version(build_current_versions) current_resource @@ -38,7 +38,7 @@ class Chef super if powershell_out("$PSVersionTable.PSVersion.Major").stdout.strip().to_i < 5 raise "Minimum installed Powershell Version required is 5" - end + end requirements.assert(:install) do |a| a.assertion { candidates_exist_for_all_uninstalled? } a.failure_message(Chef::Exceptions::Package, "No candidate version available for #{packages_missing_candidates.join(", ")}") @@ -49,40 +49,50 @@ class Chef def candidate_version @candidate_version ||= build_candidate_versions end - + def install_package(names, versions) # Installs the package specified with the version passed else latest version will be installed - names.each_with_index do |name,index| - powershell_out("Install-Package #{name} -Force -ForceBootstrap -RequiredVersion #{versions[index]}", { :timeout => @new_resource.timeout }).stdout + names.each_with_index do |name, index| + powershell_out("Install-Package #{name} -Force -ForceBootstrap -RequiredVersion #{versions[index]}", { :timeout => @new_resource.timeout }) end end def remove_package(names, versions) # Removes the package, if no version is passed, all installed version will be removed - names.each_with_index do |name,index| - if !versions.nil? - if versions.length > index - powershell_out( "Uninstall-Package #{name} -Force -RequiredVersion #{versions[index]}", { :timeout => @new_resource.timeout }).stdout + names.each_with_index do |name, index| + if !versions.nil? + if versions[index] != nil + powershell_out( "Uninstall-Package #{name} -Force -RequiredVersion #{versions[index]}", { :timeout => @new_resource.timeout }) + else + powershell_out( "Uninstall-Package #{name} -Force", { :timeout => @new_resource.timeout }) end end if new_resource.version.nil? stdout = 0 - while stdout!="" do + while stdout != "" stdout = powershell_out( "Uninstall-Package #{name} -Force", { :timeout => @new_resource.timeout }).stdout - if !stdout.empty? + if !stdout.empty? stdout = stdout.split(" ") - Chef::Log.debug("Removed package #{name} with version #{stdout[stdout.index(name)+1]}") + Chef::Log.debug("Removed package #{name} with version #{stdout[stdout.index(name) + 1]}") end end - end + end end end - # return array of latest available packages online - def build_candidate_versions + # return array of latest available packages online + def build_candidate_versions versions = [] - new_resource.package_name.map do |name| - stdout = powershell_out("Find-Package #{name}", { :timeout => @new_resource.timeout }).stdout + new_resource.package_name.each_with_index do |name, index| + if !new_resource.version.nil? + if new_resource.version[index] != nil + stdout = powershell_out("Find-Package #{name} -RequiredVersion #{new_resource.version[index]}", { :timeout => @new_resource.timeout }).stdout + else + stdout = powershell_out("Find-Package #{name}", { :timeout => @new_resource.timeout }).stdout + end + else + stdout = powershell_out("Find-Package #{name}", { :timeout => @new_resource.timeout }).stdout + end versions.push(find_version(stdout)) end versions @@ -91,34 +101,35 @@ class Chef #return array of currently installed version def build_current_versions version_list = [] - new_resource.package_name.each_with_index do |name,index| + new_resource.package_name.each_with_index do |name, index| if !new_resource.version.nil? - if new_resource.version.length >= index+1 - stdout = powershell_out("Get-Package -Name #{name} -RequiredVersion #{new_resource.version[index]}", { :timeout => @new_resource.timeout }).stdout + if new_resource.version[index] != nil + stdout = powershell_out("Get-Package -Name #{name} -RequiredVersion #{new_resource.version[index]}", { :timeout => @new_resource.timeout }).stdout else - stdout = powershell_out("Get-Package -Name #{name}", { :timeout => @new_resource.timeout }).stdout + stdout = powershell_out("Get-Package -Name #{name}", { :timeout => @new_resource.timeout }).stdout end else - stdout = powershell_out("Get-Package -Name #{name}", { :timeout => @new_resource.timeout }).stdout - end + stdout = powershell_out("Get-Package -Name #{name}", { :timeout => @new_resource.timeout }).stdout + end if stdout.empty? - version_list.push(nil) - else + version_list.push(nil) + else stdout = stdout.split(" ") - version_list.push(stdout[stdout.index(name)+1]) + version_list.push(stdout[stdout.index(name) + 1]) end end version_list end - def find_version(stdout) + def find_version(stdout) if stdout.empty? - raise "Version speicified by user not found for the package" + nil + #raise Chef::Exceptions::Package, "Invalid Package name pecified or proper version not passed" else - stdout.split(" ")[-2] + stdout.split(" ")[-2] end end - + end end end diff --git a/lib/chef/resource/powershell_package.rb b/lib/chef/resource/powershell_package.rb index 17d0fed555..4d658d3cc5 100644 --- a/lib/chef/resource/powershell_package.rb +++ b/lib/chef/resource/powershell_package.rb @@ -33,9 +33,9 @@ class Chef end property :package_name, [String, Array], coerce: proc { |x| [x].flatten } - + property :version, [String, Array], coerce: proc { |x| [x].flatten } - + end end end diff --git a/spec/unit/provider/package/powershell_spec.rb b/spec/unit/provider/package/powershell_spec.rb new file mode 100644 index 0000000000..5461077b48 --- /dev/null +++ b/spec/unit/provider/package/powershell_spec.rb @@ -0,0 +1,367 @@ +# +# Author:: Dheeraj Dubey(<dheeraj.dubey@msystechnologies.com>) +# Copyright:: Copyright 2008-2016, Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require "spec_helper" +require "chef/mixin/powershell_out" + +describe Chef::Provider::Package::Powershell do + include Chef::Mixin::PowershellOut + let(:timeout) { 900 } + + let(:new_resource) { Chef::Resource::PowershellPackage.new("windows_test_pkg") } + + let(:provider) do + node = Chef::Node.new + events = Chef::EventDispatch::Dispatcher.new + run_context = Chef::RunContext.new(node, {}, events) + Chef::Provider::Package::Powershell.new(new_resource, run_context) + end + + let(:installed_package_stdout) do + <<-EOF +"\r\nName Version Source Summary \r\n---- ------- ------ ------- \r\nxCertificate 2.1.0.0 https://www.p... This module includes DSC resources that simplify ad...\r\n\r\n\r\n" + EOF + end + + let(:package_version_stdout) do + <<-EOF +"\r\nName Version Source Summary \r\n---- ------- ------ ------- \r\nxCertificate 2.1.0.0 https://www.p..." + EOF + end + + let(:installed_package_stdout_xnetworking) do + <<-EOF +"\r\nName Version Source Summary \r\n---- ------- ------ ------- \r\nxNetworking 2.12.0.0 https://www.p... This module includes DSC resources that simplify ad..." + EOF + end + + let(:package_version_stdout_xnetworking) do + <<-EOF +"\r\nName Version Source Summary \r\n---- ------- ------ ------- \r\nxNetworking 2.12.0.0 https://www.p..." + EOF + end + + describe "#initialize" do + it "should return the correct class" do + expect(provider).to be_kind_of(Chef::Provider::Package::Powershell) + end + end + + describe "#candidate_version" do + + it "should set the candidate_version to the latest version when not pinning" do + my_double = double("powershellout_double") + allow(my_double).to receive(:stdout).and_return(package_version_stdout) + allow(provider).to receive(:powershell_out).with("Find-Package xCertificate", { :timeout => new_resource.timeout }).and_return(my_double) + new_resource.package_name(["xCertificate"]) + new_resource.version(nil) + expect(provider.candidate_version).to eql(["2.1.0.0"]) + end + + it "should set the candidate_version to pinned version if available" do + my_double = double("powershellout_double") + allow(my_double).to receive(:stdout).and_return(package_version_stdout) + allow(provider).to receive(:powershell_out).with("Find-Package xCertificate -RequiredVersion 2.1.0.0", { :timeout => new_resource.timeout }).and_return(my_double) + new_resource.package_name(["xCertificate"]) + new_resource.version(["2.1.0.0"]) + expect(provider.candidate_version).to eql(["2.1.0.0"]) + end + + it "should set the candidate_version to nil if there is no candidate" do + my_double = double("powershellout_double") + allow(my_double).to receive(:stdout).and_return("") + allow(provider).to receive(:powershell_out).with("Find-Package xCertificate", { :timeout => new_resource.timeout }).and_return(my_double) + new_resource.package_name(["xCertificate"]) + expect(provider.candidate_version).to eql([nil]) + end + + it "should set the candidate_version correctly when there are two packages to install" do + my_double = double("powershellout_double") + allow(my_double).to receive(:stdout).and_return(package_version_stdout) + my_double1 = double("powershellout_double") + allow(my_double1).to receive(:stdout).and_return(package_version_stdout_xnetworking) + allow(provider).to receive(:powershell_out).with("Find-Package xCertificate", { :timeout => new_resource.timeout }).and_return(my_double) + allow(provider).to receive(:powershell_out).with("Find-Package xNetworking", { :timeout => new_resource.timeout }).and_return(my_double1) + new_resource.package_name(%w{xCertificate xNetworking}) + new_resource.version(nil) + expect(provider.candidate_version).to eql(["2.1.0.0", "2.12.0.0"]) + end + + it "should set the candidate_version correctly when only the first is installable" do + my_double = double("powershellout_double") + allow(my_double).to receive(:stdout).and_return(package_version_stdout) + my_double1 = double("powershellout_double") + allow(my_double1).to receive(:stdout).and_return("") + allow(provider).to receive(:powershell_out).with("Find-Package xCertificate", { :timeout => new_resource.timeout }).and_return(my_double) + allow(provider).to receive(:powershell_out).with("Find-Package xNetworking", { :timeout => new_resource.timeout }).and_return(my_double1) + new_resource.package_name(%w{xCertificate xNetworking}) + new_resource.version(nil) + expect(provider.candidate_version).to eql(["2.1.0.0", nil]) + end + + it "should set the candidate_version correctly when only the last is installable" do + my_double = double("powershellout_double") + allow(my_double).to receive(:stdout).and_return("") + my_double1 = double("powershellout_double") + allow(my_double1).to receive(:stdout).and_return(package_version_stdout_xnetworking) + allow(provider).to receive(:powershell_out).with("Find-Package xCertificate", { :timeout => new_resource.timeout }).and_return(my_double) + allow(provider).to receive(:powershell_out).with("Find-Package xNetworking", { :timeout => new_resource.timeout }).and_return(my_double1) + new_resource.package_name(%w{xCertificate xNetworking}) + new_resource.version(nil) + expect(provider.candidate_version).to eql([nil, "2.12.0.0"]) + end + + it "should set the candidate_version correctly when neither are is installable" do + my_double = double("powershellout_double") + allow(my_double).to receive(:stdout).and_return("") + my_double1 = double("powershellout_double") + allow(my_double1).to receive(:stdout).and_return("") + allow(provider).to receive(:powershell_out).with("Find-Package xCertificate", { :timeout => new_resource.timeout }).and_return(my_double) + allow(provider).to receive(:powershell_out).with("Find-Package xNetworking", { :timeout => new_resource.timeout }).and_return(my_double1) + new_resource.package_name(%w{xNetworking xCertificate}) + new_resource.version(nil) + expect(provider.candidate_version).to eql([nil, nil]) + end + end + + describe "#action_install" do + it "should install a single package" do + my_double = double("powershellout_double") + allow(my_double).to receive(:stdout).and_return(package_version_stdout) + my_double1 = double("powershellout_double") + allow(my_double1).to receive(:stdout).and_return("") + my_double2 = double("powershellout_double") + allow(my_double2).to receive(:stdout).and_return("5") + provider.load_current_resource + new_resource.package_name(["xCertificate"]) + new_resource.version(nil) + allow(provider).to receive(:powershell_out).with("Find-Package xCertificate", { :timeout => new_resource.timeout }).and_return(my_double) + allow(provider).to receive(:powershell_out).with("Get-Package -Name xCertificate", { :timeout => new_resource.timeout }).and_return(my_double1) + allow(provider).to receive(:powershell_out).with("$PSVersionTable.PSVersion.Major").and_return(my_double2) + expect(provider).to receive(:powershell_out).with("Install-Package xCertificate -Force -ForceBootstrap -RequiredVersion 2.1.0.0", { :timeout => new_resource.timeout }) + provider.run_action(:install) + expect(new_resource).to be_updated_by_last_action + end + + context "when changing the timeout to 3600" do + let(:timeout) { 3600 } + it "sets the timeout on shell_out commands" do + my_double = double("powershellout_double") + allow(my_double).to receive(:stdout).and_return(package_version_stdout) + my_double1 = double("powershellout_double") + allow(my_double1).to receive(:stdout).and_return("") + my_double2 = double("powershellout_double") + allow(my_double2).to receive(:stdout).and_return("5") + new_resource.timeout(timeout) + provider.load_current_resource + new_resource.package_name(["xCertificate"]) + new_resource.version(nil) + allow(provider).to receive(:powershell_out).with("Find-Package xCertificate", { :timeout => new_resource.timeout }).and_return(my_double) + allow(provider).to receive(:powershell_out).with("Get-Package -Name xCertificate", { :timeout => new_resource.timeout }).and_return(my_double1) + allow(provider).to receive(:powershell_out).with("$PSVersionTable.PSVersion.Major").and_return(my_double2) + expect(provider).to receive(:powershell_out).with("Install-Package xCertificate -Force -ForceBootstrap -RequiredVersion 2.1.0.0", { :timeout => new_resource.timeout }) + provider.run_action(:install) + expect(new_resource).to be_updated_by_last_action + end + end + + it "should not install packages that are up-to-date" do + my_double = double("powershellout_double") + allow(my_double).to receive(:stdout).and_return(package_version_stdout) + my_double1 = double("powershellout_double") + allow(my_double1).to receive(:stdout).and_return(installed_package_stdout) + my_double2 = double("powershellout_double") + allow(my_double2).to receive(:stdout).and_return("5") + + new_resource.package_name(["xCertificate"]) + new_resource.version(nil) + allow(provider).to receive(:powershell_out).with("Find-Package xCertificate", { :timeout => new_resource.timeout }).and_return(my_double) + allow(provider).to receive(:powershell_out).with("Get-Package -Name xCertificate", { :timeout => new_resource.timeout }).and_return(my_double1) + allow(provider).to receive(:powershell_out).with("$PSVersionTable.PSVersion.Major").and_return(my_double2) + + provider.load_current_resource + expect(provider).not_to receive(:install_package) + provider.run_action(:install) + expect(new_resource).not_to be_updated_by_last_action + end + + it "should handle complicated cases when the name/version array is pruned" do + # implicitly test that we correctly pick up new_resource.version[1] instead of + # new_version.resource[0] + my_double = double("powershellout_double") + allow(my_double).to receive(:stdout).and_return(package_version_stdout) + my_double1 = double("powershellout_double") + allow(my_double1).to receive(:stdout).and_return("") + my_double2 = double("powershellout_double") + allow(my_double2).to receive(:stdout).and_return("5") + my_double3 = double("powershellout_double") + allow(my_double3).to receive(:stdout).and_return(package_version_stdout_xnetworking) + my_double4 = double("powershellout_double") + allow(my_double4).to receive(:stdout).and_return("") + + new_resource.package_name(%w{xCertificate xNetworking}) + new_resource.version([nil, "2.12.0.0"]) + allow(provider).to receive(:powershell_out).with("Find-Package xCertificate", { :timeout => new_resource.timeout }).and_return(my_double) + allow(provider).to receive(:powershell_out).with("Get-Package -Name xCertificate", { :timeout => new_resource.timeout }).and_return(my_double1) + allow(provider).to receive(:powershell_out).with("Find-Package xNetworking -RequiredVersion 2.12.0.0", { :timeout => new_resource.timeout }).and_return(my_double3) + allow(provider).to receive(:powershell_out).with("Get-Package -Name xNetworking -RequiredVersion 2.12.0.0", { :timeout => new_resource.timeout }).and_return(my_double4) + allow(provider).to receive(:powershell_out).with("$PSVersionTable.PSVersion.Major").and_return(my_double2) + expect(provider).to receive(:powershell_out).with("Install-Package xCertificate -Force -ForceBootstrap -RequiredVersion 2.1.0.0", { :timeout => new_resource.timeout }) + expect(provider).to receive(:powershell_out).with("Install-Package xNetworking -Force -ForceBootstrap -RequiredVersion 2.12.0.0", { :timeout => new_resource.timeout }) + provider.load_current_resource + provider.run_action(:install) + expect(new_resource).to be_updated_by_last_action + end + + it "should split up commands when given two packages, one with a version pin" do + my_double = double("powershellout_double") + allow(my_double).to receive(:stdout).and_return(package_version_stdout) + my_double1 = double("powershellout_double") + allow(my_double1).to receive(:stdout).and_return("") + my_double2 = double("powershellout_double") + allow(my_double2).to receive(:stdout).and_return("5") + my_double3 = double("powershellout_double") + allow(my_double3).to receive(:stdout).and_return(package_version_stdout_xnetworking) + my_double4 = double("powershellout_double") + allow(my_double4).to receive(:stdout).and_return("") + new_resource.package_name(%w{xCertificate xNetworking}) + new_resource.version(["2.1.0.0", nil]) + allow(provider).to receive(:powershell_out).with("Find-Package xCertificate -RequiredVersion 2.1.0.0", { :timeout => new_resource.timeout }).and_return(my_double) + allow(provider).to receive(:powershell_out).with("Get-Package -Name xCertificate -RequiredVersion 2.1.0.0", { :timeout => new_resource.timeout }).and_return(my_double1) + allow(provider).to receive(:powershell_out).with("Find-Package xNetworking", { :timeout => new_resource.timeout }).and_return(my_double3) + allow(provider).to receive(:powershell_out).with("Get-Package -Name xNetworking", { :timeout => new_resource.timeout }).and_return(my_double4) + allow(provider).to receive(:powershell_out).with("$PSVersionTable.PSVersion.Major").and_return(my_double2) + expect(provider).to receive(:powershell_out).with("Install-Package xCertificate -Force -ForceBootstrap -RequiredVersion 2.1.0.0", { :timeout => new_resource.timeout }) + expect(provider).to receive(:powershell_out).with("Install-Package xNetworking -Force -ForceBootstrap -RequiredVersion 2.12.0.0", { :timeout => new_resource.timeout }) + provider.load_current_resource + provider.run_action(:install) + expect(new_resource).to be_updated_by_last_action + end + + it "should do multipackage installs when given two packages without constraints" do + my_double = double("powershellout_double") + allow(my_double).to receive(:stdout).and_return(package_version_stdout) + my_double1 = double("powershellout_double") + allow(my_double1).to receive(:stdout).and_return("") + my_double2 = double("powershellout_double") + allow(my_double2).to receive(:stdout).and_return("5") + my_double3 = double("powershellout_double") + allow(my_double3).to receive(:stdout).and_return(package_version_stdout_xnetworking) + my_double4 = double("powershellout_double") + allow(my_double4).to receive(:stdout).and_return("") + new_resource.package_name(%w{xCertificate xNetworking}) + new_resource.version(nil) + allow(provider).to receive(:powershell_out).with("Find-Package xCertificate", { :timeout => new_resource.timeout }).and_return(my_double) + allow(provider).to receive(:powershell_out).with("Get-Package -Name xCertificate", { :timeout => new_resource.timeout }).and_return(my_double1) + allow(provider).to receive(:powershell_out).with("Find-Package xNetworking", { :timeout => new_resource.timeout }).and_return(my_double3) + allow(provider).to receive(:powershell_out).with("Get-Package -Name xNetworking", { :timeout => new_resource.timeout }).and_return(my_double4) + allow(provider).to receive(:powershell_out).with("$PSVersionTable.PSVersion.Major").and_return(my_double2) + expect(provider).to receive(:powershell_out).with("Install-Package xCertificate -Force -ForceBootstrap -RequiredVersion 2.1.0.0", { :timeout => new_resource.timeout }) + expect(provider).to receive(:powershell_out).with("Install-Package xNetworking -Force -ForceBootstrap -RequiredVersion 2.12.0.0", { :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 + it "does nothing when the package is already removed" do + my_double = double("powershellout_double") + allow(my_double).to receive(:stdout).and_return(package_version_stdout) + my_double1 = double("powershellout_double") + allow(my_double1).to receive(:stdout).and_return("") + my_double2 = double("powershellout_double") + allow(my_double2).to receive(:stdout).and_return("5") + provider.load_current_resource + new_resource.package_name(["xCertificate"]) + new_resource.version(["2.1.0.0"]) + allow(provider).to receive(:powershell_out).with("Find-Package xCertificate -RequiredVersion 2.1.0.0", { :timeout => new_resource.timeout }).and_return(my_double) + allow(provider).to receive(:powershell_out).with("Get-Package -Name xCertificate -RequiredVersion 2.1.0.0", { :timeout => new_resource.timeout }).and_return(my_double1) + allow(provider).to receive(:powershell_out).with("$PSVersionTable.PSVersion.Major").and_return(my_double2) + expect(provider).not_to receive(:remove_package) + provider.run_action(:remove) + expect(new_resource).not_to be_updated_by_last_action + end + + it "does nothing when all the packages are already removed" do + my_double = double("powershellout_double") + allow(my_double).to receive(:stdout).and_return(package_version_stdout) + my_double1 = double("powershellout_double") + allow(my_double1).to receive(:stdout).and_return("") + my_double2 = double("powershellout_double") + allow(my_double2).to receive(:stdout).and_return("5") + my_double3 = double("powershellout_double") + allow(my_double3).to receive(:stdout).and_return(package_version_stdout_xnetworking) + my_double4 = double("powershellout_double") + allow(my_double4).to receive(:stdout).and_return("") + new_resource.package_name(%w{xCertificate xNetworking}) + new_resource.version(nil) + allow(provider).to receive(:powershell_out).with("Find-Package xCertificate", { :timeout => new_resource.timeout }).and_return(my_double) + allow(provider).to receive(:powershell_out).with("Get-Package -Name xCertificate", { :timeout => new_resource.timeout }).and_return(my_double1) + allow(provider).to receive(:powershell_out).with("Find-Package xNetworking", { :timeout => new_resource.timeout }).and_return(my_double3) + allow(provider).to receive(:powershell_out).with("Get-Package -Name xNetworking", { :timeout => new_resource.timeout }).and_return(my_double4) + allow(provider).to receive(:powershell_out).with("$PSVersionTable.PSVersion.Major").and_return(my_double2) + provider.load_current_resource + expect(provider).not_to receive(:remove_package) + provider.run_action(:remove) + expect(new_resource).not_to be_updated_by_last_action + end + + it "removes a package when version is specified" do + my_double = double("powershellout_double") + allow(my_double).to receive(:stdout).and_return(package_version_stdout) + my_double1 = double("powershellout_double") + allow(my_double1).to receive(:stdout).and_return(installed_package_stdout) + my_double2 = double("powershellout_double") + allow(my_double2).to receive(:stdout).and_return("5") + + new_resource.package_name(["xCertificate"]) + new_resource.version(["2.1.0.0"]) + allow(provider).to receive(:powershell_out).with("Find-Package xCertificate -RequiredVersion 2.1.0.0", { :timeout => new_resource.timeout }).and_return(my_double) + allow(provider).to receive(:powershell_out).with("Get-Package -Name xCertificate -RequiredVersion 2.1.0.0", { :timeout => new_resource.timeout }).and_return(my_double1) + allow(provider).to receive(:powershell_out).with("$PSVersionTable.PSVersion.Major").and_return(my_double2) + + provider.load_current_resource + expect(provider).to receive(:powershell_out).with("Uninstall-Package xCertificate -Force -RequiredVersion 2.1.0.0", { :timeout => new_resource.timeout }) + provider.run_action(:remove) + expect(new_resource).to be_updated_by_last_action + end + + it "removes a package when version is not specified" do + my_double = double("powershellout_double") + allow(my_double).to receive(:stdout).and_return(package_version_stdout) + my_double1 = double("powershellout_double") + allow(my_double1).to receive(:stdout).and_return(installed_package_stdout) + my_double2 = double("powershellout_double") + allow(my_double2).to receive(:stdout).and_return("5") + my_double3 = double("powershellout_double") + allow(my_double3).to receive(:stdout).and_return("") + + new_resource.package_name(["xCertificate"]) + new_resource.version(nil) + allow(provider).to receive(:powershell_out).with("Find-Package xCertificate", { :timeout => new_resource.timeout }).and_return(my_double) + allow(provider).to receive(:powershell_out).with("Get-Package -Name xCertificate", { :timeout => new_resource.timeout }).and_return(my_double1) + allow(provider).to receive(:powershell_out).with("$PSVersionTable.PSVersion.Major").and_return(my_double2) + + provider.load_current_resource + expect(provider).to receive(:powershell_out).with("Uninstall-Package xCertificate -Force", { :timeout => new_resource.timeout }).and_return(my_double3) + provider.run_action(:remove) + expect(new_resource).to be_updated_by_last_action + end + end +end diff --git a/spec/unit/resource/powershell_package_spec.rb b/spec/unit/resource/powershell_package_spec.rb new file mode 100644 index 0000000000..ff4aa991a7 --- /dev/null +++ b/spec/unit/resource/powershell_package_spec.rb @@ -0,0 +1,68 @@ +# +# Author:: Dheeraj Dubey(<dheeraj.dubey@msystechnologies.com>) +# Copyright:: Copyright 2008-2016, Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require "spec_helper" + +describe Chef::Resource::PowershellPackage do + + let(:resource) { Chef::Resource::PowershellPackage.new("test_package") } + + it "should create a new Chef::Resource::PowershellPackage" do + expect(resource).to be_a_kind_of(Chef::Resource) + expect(resource).to be_a_kind_of(Chef::Resource::Package) + expect(resource).to be_a_instance_of(Chef::Resource::PowershellPackage) + end + + #to check the value of resource.resource_name + it "should have a resource name of :python" do + expect(resource.resource_name).to eql(:powershell_package) + end + + it "should coerce its name to a package_name array" do + expect(resource.package_name).to eql(["test_package"]) + end + + it "the package_name setter should coerce to arrays" do + resource.package_name("git") + expect(resource.package_name).to eql(["git"]) + end + + it "the package_name setter should accept arrays" do + resource.package_name(%w{git unzip}) + expect(resource.package_name).to eql(%w{git unzip}) + end + + it "the name should accept arrays" do + resource = Chef::Resource::PowershellPackage.new(%w{git unzip}) + expect(resource.package_name).to eql(%w{git unzip}) + end + + it "the default version should be nil" do + expect(resource.version).to eql(nil) + end + + it "the version setter should coerce to arrays" do + resource.version("1.2.3") + expect(resource.version).to eql(["1.2.3"]) + end + + it "the version setter should accept arrays" do + resource.version(["1.2.3", "4.5.6"]) + expect(resource.version).to eql(["1.2.3", "4.5.6"]) + end +end |