summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorVasu1105 <vasundhara.jagdale@clogeny.com>2016-09-07 11:32:01 +0530
committerVasu1105 <vasundhara.jagdale@clogeny.com>2016-09-30 15:10:46 +0530
commit09b44d56bc3f9285360b6b93f7798e6de4dd8dfd (patch)
treef50f4ad4403c80f8bb3db301b6c5c1bab8610055 /spec
parent09886bad1411faa86b9e716712b4dad62b8330bd (diff)
downloadchef-09b44d56bc3f9285360b6b93f7798e6de4dd8dfd.tar.gz
Added resource and provider for cab package
Reverted changes Adding resource and provider for cab file support Added method identifying package version for installation and removal of package Updated code to idetitfy installed version refactored some code Refactored code for parsing dism get package output Fixed rubocop issue, working on specs created spec files Working on unit specs Added unit specs and working on functional specs Working on functional specs and fixed rubocop errors Fixed failing unit test cases Resolved rubucop style error Removed functional specs Fixed error and working on functional specs Added missing functional spec file
Diffstat (limited to 'spec')
-rw-r--r--spec/functional/assets/dummy-cab-23448.cabbin0 -> 1455340 bytes
-rw-r--r--spec/functional/resource/cab_package_spec.rb54
-rw-r--r--spec/unit/provider/package/cab_spec.rb191
-rw-r--r--spec/unit/resource/cab_package_spec.rb38
4 files changed, 283 insertions, 0 deletions
diff --git a/spec/functional/assets/dummy-cab-23448.cab b/spec/functional/assets/dummy-cab-23448.cab
new file mode 100644
index 0000000000..41eea0b77c
--- /dev/null
+++ b/spec/functional/assets/dummy-cab-23448.cab
Binary files differ
diff --git a/spec/functional/resource/cab_package_spec.rb b/spec/functional/resource/cab_package_spec.rb
new file mode 100644
index 0000000000..c0750ed480
--- /dev/null
+++ b/spec/functional/resource/cab_package_spec.rb
@@ -0,0 +1,54 @@
+#
+# Author:: Vasundhara Jagdale (<vasundhara.jagdale@msystechnologies.com>)
+# Copyright:: Copyright 2015-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::Resource::CabPackage, :windows_only do
+ include Chef::Mixin::PowershellOut
+
+ let(:package_source) { File.join(CHEF_SPEC_ASSETS, "dummy-cab-23448.cab") }
+
+ let(:new_resource) do
+ new_resource = Chef::Resource::CabPackage.new("test-package", run_context)
+ new_resource.source = package_source
+ new_resource
+ end
+
+ let(:installed_version) { proc { stdout = powershell_out!("dism.exe /Online /Get-PackageInfo /PackagePath:\"#{new_resource.source}\" /NoRestart").stdout } }
+
+ context "installing package" do
+ after { remove_package }
+
+ it "installs the package" do
+ new_resource.run_action(:install)
+ expect(new_resource).to be_updated_by_last_action
+ end
+
+ it "raises error if package is not found" do
+ new_resource.source = File.join(CHEF_SPEC_ASSETS, "test.cab")
+ expect { new_resource.run_action(:install) }.to raise_error Chef::Exceptions::Package
+ end
+ end
+end
+
+def remove_package
+ pkg_to_remove = Chef::Resource::CabPackage.new("test-package", run_context)
+ pkg_to_remove.source = package_source
+ pkg_to_remove.run_action(:remove)
+end
diff --git a/spec/unit/provider/package/cab_spec.rb b/spec/unit/provider/package/cab_spec.rb
new file mode 100644
index 0000000000..9ce0c77485
--- /dev/null
+++ b/spec/unit/provider/package/cab_spec.rb
@@ -0,0 +1,191 @@
+#
+# Author:: Vasundhara Jagdale (<vasundhara.jagdale@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::Provider::Package::Cab do
+ let(:timeout) {}
+
+ let(:new_resource) { Chef::Resource::CabPackage.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::Cab.new(new_resource, run_context)
+ end
+
+ let(:installed_package_list_stdout) do
+ <<-EOF
+Packages listing:
+Package Identity : Package_for_KB2999486~31bf3856ad364e35~amd64~~6.1.9768.0
+Package Identity : Package_for_KB2994825~31bf3856ad364e35~amd64~~6.1.7601.0
+ EOF
+ end
+
+ let(:package_version_stdout) do
+ <<-EOF
+Package information:
+Package Identity : Package_for_KB2664825~31bf3856ad364e35~amd64~~6.1.3.0
+State : Installed
+Dependency : Language Pack
+The operation completed successfully
+ EOF
+ end
+
+ before do
+ new_resource.source = "C:\\Temp\\Test6.1-KB2664825-v3-x64.cab"
+ installed_package_list_obj = double(stdout: installed_package_list_stdout)
+ allow(provider).to receive(:powershell_out).with("dism.exe /Online /Get-Packages /NoRestart", { timeout: timeout }).and_return(installed_package_list_obj)
+ package_version_obj = double(stdout: package_version_stdout)
+ allow(provider).to receive(:powershell_out).with("dism.exe /Online /Get-PackageInfo /PackagePath:\"#{new_resource.source}\" /NoRestart", { timeout: timeout }).and_return(package_version_obj)
+ end
+
+ def allow_package_info(package_path = nil, package_name = nil)
+ get_pakage_info_stdout = <<-EOF
+Deployment Image Servicing and Management tool
+Version: 6.1.7600.16385
+
+Image Version: 6.1.7600.16385
+
+Package information:
+Package Identity : Package_for_KB2664825~31bf3856ad364e35~amd64~~6.1.3.0
+Applicable : Yes
+Copyright : Microsoft Corporation
+Company : Microsoft Corporation
+State : Installed
+Dependency : Language Pack
+The operation completed successfully
+ EOF
+ get_pakage_info_obj = double(stdout: get_pakage_info_stdout)
+ if package_path
+ allow(provider).to receive(:powershell_out).with("dism.exe /Online /Get-PackageInfo /PackagePath:\"#{package_path}\" /NoRestart", { timeout: timeout }).and_return(get_pakage_info_obj)
+ else
+ allow(provider).to receive(:powershell_out).with("dism.exe /Online /Get-PackageInfo /PackageName:\"#{package_name}\" /NoRestart", { timeout: timeout }).and_return(get_pakage_info_obj)
+ end
+ end
+
+ def allow_get_packags
+ get_packages_stdout = <<-EOF
+Deployment Image Servicing and Management tool
+Version: 6.1.7600.16385
+
+Image Version: 6.1.7600.16385
+
+Packages listing:
+
+Package Identity : Package_for_KB2999486~31bf3856ad364e35~amd64~~6.1.9768.0
+State : Installed
+Release Type : Language Pack
+Install Time : 2/11/2015 11:33 PM
+
+Package Identity : Package_for_KB2994825~31bf3856ad364e35~amd64~~6.1.7601.0
+State : Installed
+Release Type : Language Pack
+Install Time : 2/11/2015 11:33 PM
+
+Package Identity : Package_for_KB2664825~31bf3856ad364e35~amd64~~6.1.3.0
+State : Installed
+Release Type : Feature Pack
+Install Time : 11/21/2010 3:40 AM
+
+The operation completed successfully.
+ EOF
+ get_packages_obj = double(stdout: get_packages_stdout)
+ allow(provider).to receive(:powershell_out).with("dism.exe /Online /Get-Packages /NoRestart", { timeout: timeout }).and_return(get_packages_obj)
+ end
+
+ describe "#load_current_resource" do
+ it "returns a current_resource" do
+ expect(provider.load_current_resource).to be_kind_of(Chef::Resource::CabPackage)
+ end
+
+ it "sets the current_resource.version to nil when the package is not installed" do
+ provider.load_current_resource
+ expect(provider.current_resource.version).to eql(nil)
+ end
+
+ it "sets the new resource package version" do
+ provider.load_current_resource
+ expect(provider.new_resource.version).to eql("6.1.3.0")
+ end
+ end
+
+ describe "#initialize" do
+ it "returns the correct class" do
+ expect(provider).to be_kind_of(Chef::Provider::Package::Cab)
+ end
+ end
+
+ describe "#package_version" do
+ it "returns the new package version" do
+ allow_package_info(new_resource.source, nil)
+ expect(provider.package_version).to eql("6.1.3.0")
+ end
+ end
+
+ describe "#installed_version" do
+ it "returns the current installed version of package" do
+ allow_package_info(new_resource.source, nil)
+ allow_get_packags
+ allow_package_info(nil, "Package_for_KB2664825~31bf3856ad364e35~amd64~~6.1.3.0")
+ expect(provider.installed_version).to eql("6.1.3.0")
+ end
+ end
+
+ describe "#action_remove" do
+ it "does nothing when the package is already removed" do
+ 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 packages if package is installed" do
+ allow_package_info(new_resource.source, nil)
+ allow_get_packags
+ allow_package_info(nil, "Package_for_KB2664825~31bf3856ad364e35~amd64~~6.1.3.0")
+ provider.load_current_resource
+ expect(provider.installed_version).not_to eql(nil)
+ expect(provider).to receive(:remove_package)
+ provider.run_action(:remove)
+ expect(new_resource).to be_updated_by_last_action
+ end
+ end
+
+ describe "#action_install" do
+ it "installs package if already not installed" do
+ provider.load_current_resource
+ expect(provider.installed_version).to eql(nil)
+ expect(provider).to receive(:install_package)
+ provider.run_action(:install)
+ expect(new_resource).to be_updated_by_last_action
+ end
+
+ it "does not install package if already installed" do
+ allow_package_info(new_resource.source, nil)
+ allow_get_packags
+ allow_package_info(nil, "Package_for_KB2664825~31bf3856ad364e35~amd64~~6.1.3.0")
+ provider.load_current_resource
+ expect(provider.installed_version).not_to eql(nil)
+ expect(provider).not_to receive(:install_package)
+ provider.run_action(:install)
+ expect(new_resource).not_to be_updated_by_last_action
+ end
+ end
+end
diff --git a/spec/unit/resource/cab_package_spec.rb b/spec/unit/resource/cab_package_spec.rb
new file mode 100644
index 0000000000..aa4890f171
--- /dev/null
+++ b/spec/unit/resource/cab_package_spec.rb
@@ -0,0 +1,38 @@
+#
+# Author:: Vasundhara Jagdale (<vasundhara.jagdale@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::CabPackage do
+
+ let(:resource) { Chef::Resource::CabPackage.new("test_pkg") }
+
+ it "creates a new Chef::Resource::CabPackage" 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::CabPackage)
+ end
+
+ it "sets resource name as :cab_package" do
+ expect(resource.resource_name).to eql(:cab_package)
+ end
+
+ it "coerce its name to a package_name" do
+ expect(resource.package_name).to eql("test_pkg")
+ end
+end