summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-06-30 09:24:45 -0600
committerJohn Keiser <john@johnkeiser.com>2015-06-30 11:18:41 -0600
commit55ac7e57db9e5c419cc469e57e0b8bced993a729 (patch)
treeb4893764a8eeaf22dc98a50bbe6321f0972e72fd
parent7f2efa0bdabee2745b9b61725d88944bedd44052 (diff)
downloadchef-55ac7e57db9e5c419cc469e57e0b8bced993a729.tar.gz
Make ProviderResolver tests work, fix resources to resolve correctly as well as providers
-rw-r--r--lib/chef/provider/group/windows.rb8
-rw-r--r--lib/chef/provider/mount/aix.rb2
-rw-r--r--lib/chef/provider/package/openbsd.rb1
-rw-r--r--lib/chef/provider/user/windows.rb8
-rw-r--r--lib/chef/resource/dsc_script.rb2
-rw-r--r--lib/chef/resource/ips_package.rb1
-rw-r--r--lib/chef/resource/mount.rb8
-rw-r--r--lib/chef/resource/openbsd_package.rb11
-rw-r--r--lib/chef/resource/solaris_package.rb5
-rw-r--r--spec/unit/provider_resolver_spec.rb80
10 files changed, 73 insertions, 53 deletions
diff --git a/lib/chef/provider/group/windows.rb b/lib/chef/provider/group/windows.rb
index 46d8afc7f6..59d1c308ff 100644
--- a/lib/chef/provider/group/windows.rb
+++ b/lib/chef/provider/group/windows.rb
@@ -28,9 +28,11 @@ class Chef
provides :group, os: 'windows'
- def initialize(new_resource,run_context)
- super
- @net_group = Chef::Util::Windows::NetGroup.new(@new_resource.group_name)
+ if RUBY_PLATFORM =~ /mswin|mingw32|windows/
+ def initialize(new_resource,run_context)
+ super
+ @net_group = Chef::Util::Windows::NetGroup.new(@new_resource.group_name)
+ end
end
def load_current_resource
diff --git a/lib/chef/provider/mount/aix.rb b/lib/chef/provider/mount/aix.rb
index 4ad7b24c15..510dfde46d 100644
--- a/lib/chef/provider/mount/aix.rb
+++ b/lib/chef/provider/mount/aix.rb
@@ -32,7 +32,7 @@ class Chef
@new_resource.options.clear
end
if @new_resource.fstype == "auto"
- @new_resource.fstype = nil
+ @new_resource.send(:clear_fstype)
end
end
diff --git a/lib/chef/provider/package/openbsd.rb b/lib/chef/provider/package/openbsd.rb
index f231101390..83fc09c8ae 100644
--- a/lib/chef/provider/package/openbsd.rb
+++ b/lib/chef/provider/package/openbsd.rb
@@ -31,6 +31,7 @@ class Chef
class Openbsd < Chef::Provider::Package
provides :package, os: "openbsd"
+ provides :openbsd_package
include Chef::Mixin::ShellOut
include Chef::Mixin::GetSourceFromPackage
diff --git a/lib/chef/provider/user/windows.rb b/lib/chef/provider/user/windows.rb
index e282a11d45..d8653c6af3 100644
--- a/lib/chef/provider/user/windows.rb
+++ b/lib/chef/provider/user/windows.rb
@@ -29,9 +29,11 @@ class Chef
provides :user, os: "windows"
- def initialize(new_resource,run_context)
- super
- @net_user = Chef::Util::Windows::NetUser.new(@new_resource.username)
+ if RUBY_PLATFORM =~ /mswin|mingw32|windows/
+ def initialize(new_resource,run_context)
+ super
+ @net_user = Chef::Util::Windows::NetUser.new(@new_resource.username)
+ end
end
def load_current_resource
diff --git a/lib/chef/resource/dsc_script.rb b/lib/chef/resource/dsc_script.rb
index 2fcf183375..2877f61eb4 100644
--- a/lib/chef/resource/dsc_script.rb
+++ b/lib/chef/resource/dsc_script.rb
@@ -22,7 +22,7 @@ class Chef
class Resource
class DscScript < Chef::Resource
- provides :dsc_script, platform: "windows"
+ provides :dsc_script, os: "windows"
default_action :run
diff --git a/lib/chef/resource/ips_package.rb b/lib/chef/resource/ips_package.rb
index 8d720dd411..2bf8e1dba8 100644
--- a/lib/chef/resource/ips_package.rb
+++ b/lib/chef/resource/ips_package.rb
@@ -23,6 +23,7 @@ class Chef
class Resource
class IpsPackage < ::Chef::Resource::Package
+ provides :package, os: "solaris2"
provides :ips_package, os: "solaris2"
allowed_actions :install, :remove, :upgrade
diff --git a/lib/chef/resource/mount.rb b/lib/chef/resource/mount.rb
index 79986d127f..a5da0ba329 100644
--- a/lib/chef/resource/mount.rb
+++ b/lib/chef/resource/mount.rb
@@ -174,6 +174,14 @@ class Chef
)
end
+ private
+
+ # Used by the AIX provider to set fstype to nil.
+ # TODO use property to make nil a valid value for fstype
+ def clear_fstype
+ @fstype = nil
+ end
+
end
end
end
diff --git a/lib/chef/resource/openbsd_package.rb b/lib/chef/resource/openbsd_package.rb
index f91fdb37e0..9ae8813d69 100644
--- a/lib/chef/resource/openbsd_package.rb
+++ b/lib/chef/resource/openbsd_package.rb
@@ -29,17 +29,6 @@ class Chef
include Chef::Mixin::ShellOut
provides :package, os: "openbsd"
-
- def after_created
- assign_provider
- end
-
- private
-
- def assign_provider
- @provider = Chef::Provider::Package::Openbsd
- end
-
end
end
end
diff --git a/lib/chef/resource/solaris_package.rb b/lib/chef/resource/solaris_package.rb
index 2dc72d5c47..a98fb8b4fa 100644
--- a/lib/chef/resource/solaris_package.rb
+++ b/lib/chef/resource/solaris_package.rb
@@ -24,10 +24,7 @@ class Chef
class Resource
class SolarisPackage < Chef::Resource::Package
provides :package, os: "solaris2", platform_family: "nexentacore"
- provides :package, os: "solaris2", platform_family: "solaris2" do |node|
- # on >= Solaris 11 we default to IPS packages instead
- node[:platform_version].to_f <= 5.10
- end
+ provides :package, os: "solaris2", platform_family: "solaris2", platform_version: "<= 5.10"
end
end
end
diff --git a/spec/unit/provider_resolver_spec.rb b/spec/unit/provider_resolver_spec.rb
index cd3d7713a7..d20c2b71fa 100644
--- a/spec/unit/provider_resolver_spec.rb
+++ b/spec/unit/provider_resolver_spec.rb
@@ -28,30 +28,37 @@ include Chef::Mixin::ConvertToClassName
describe Chef::ProviderResolver do
+ let(:resource_name) { :service }
+ let(:provider) { nil }
+ let(:action) { :start }
+
let(:node) do
node = Chef::Node.new
- allow(node).to receive(:[]).with(:os).and_return(os)
- allow(node).to receive(:[]).with(:platform_family).and_return(platform_family)
- allow(node).to receive(:[]).with(:platform).and_return(platform)
- allow(node).to receive(:[]).with(:platform_version).and_return(platform_version)
- allow(node).to receive(:is_a?).and_return(Chef::Node)
+ node.automatic[:os] = os
+ node.automatic[:platform_family] = platform_family
+ node.automatic[:platform] = platform
+ node.automatic[:platform_version] = platform_version
+ node.automatic[:kernel] = { machine: 'i386' }
node
end
+ let(:run_context) { Chef::RunContext.new(node, nil, nil) }
let(:provider_resolver) { Chef::ProviderResolver.new(node, resource, action) }
+ let(:resolved_provider) do
+ begin
+ resource ? resource.provider_for_action(action).class : nil
+ rescue Chef::Exceptions::ProviderNotFound
+ nil
+ end
+ end
- let(:action) { :start }
-
- let(:resolved_provider) { provider_resolver.resolve }
-
- let(:provider) { nil }
-
- let(:resource_name) { :service }
-
- let(:resource) { double(Chef::Resource, provider: provider, resource_name: resource_name) }
-
- before do
- allow(resource).to receive(:is_a?).with(Chef::Resource).and_return(true)
+ let(:resource) do
+ resource_class = Chef::ResourceResolver.resolve(resource_name, node: node)
+ if resource_class
+ resource = resource_class.new('test', run_context)
+ resource.provider = provider if provider
+ end
+ resource
end
def self.on_platform(platform, *tags,
@@ -83,16 +90,30 @@ describe Chef::ProviderResolver do
end
def self.expect_providers(**providers)
- providers.each do |name, provider|
+ providers.each do |name, expected_provider|
describe name.to_s do
let(:resource_name) { name }
- if provider
- it "resolves to a #{provider}" do
- expect(resolved_provider).to eql(provider)
+
+ tags = []
+ expected_resource = nil
+ Array(expected_provider).each do |p|
+ if p.is_a?(Class) && p <= Chef::Provider
+ expected_provider = p
+ elsif p.is_a?(Class) && p <= Chef::Resource
+ expected_resource = p
+ else
+ tags << p
+ end
+ end
+
+ if expected_resource || expected_provider
+ it "resolves to #{[ expected_resource, expected_provider ].compact.join(" and ")}", *tags do
+ expect(resource.class).to eql(expected_resource) if expected_resource
+ expect(resolved_provider).to eql(expected_provider) if expected_provider
end
else
- it "Fails to resolve (since #{name.inspect} is unsupported on #{platform} #{platform_version})" do
- expect { resolved_provider }.to raise_error /Cannot find a provider/
+ it "Fails to resolve (since #{name.inspect} is unsupported on #{platform} #{platform_version})", *tags do
+ expect(resolved_provider).to be_nil
end
end
end
@@ -454,7 +475,7 @@ describe Chef::ProviderResolver do
PROVIDERS =
{
- bash: Chef::Provider::Script,
+ bash: [ Chef::Resource::Bash, Chef::Provider::Script ],
breakpoint: Chef::Provider::Breakpoint,
chef_gem: Chef::Provider::Package::Rubygems,
cookbook_file: Chef::Provider::CookbookFile,
@@ -713,7 +734,7 @@ describe Chef::ProviderResolver do
"openbsd" => {
group: Chef::Provider::Group::Usermod,
- package: Chef::Provider::Package::Openbsd,
+ package: [ Chef::Resource::OpenbsdPackage, Chef::Provider::Package::Openbsd ],
"openbsd" => {
"openbsd" => {
@@ -725,10 +746,10 @@ describe Chef::ProviderResolver do
"solaris2" => {
group: Chef::Provider::Group::Usermod,
- ips_package: Chef::Provider::Package::Ips,
- package: Chef::Provider::Package::Ips,
+ ips_package: [ Chef::Resource::IpsPackage, Chef::Provider::Package::Ips ],
+ package: [ Chef::Resource::SolarisPackage, Chef::Provider::Package::Solaris ],
mount: Chef::Provider::Mount::Solaris,
- solaris_package: Chef::Provider::Package::Solaris,
+ solaris_package: [ Chef::Resource::SolarisPackage, Chef::Provider::Package::Solaris ],
"smartos" => {
smartos_package: Chef::Provider::Package::SmartOS,
@@ -743,7 +764,6 @@ describe Chef::ProviderResolver do
"solaris2" => {
"nexentacore" => {
"3.1.4" => {
- package: Chef::Provider::Package::Solaris,
},
},
"omnios" => {
@@ -762,9 +782,9 @@ describe Chef::ProviderResolver do
"solaris2" => {
user: Chef::Provider::User::Solaris,
"5.11" => {
+ package: [ Chef::Resource::IpsPackage, Chef::Provider::Package::Ips ],
},
"5.9" => {
- package: Chef::Provider::Package::Solaris,
},
},
},