diff options
author | John Keiser <john@johnkeiser.com> | 2015-09-25 17:46:30 -0700 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2015-09-28 17:34:54 -0700 |
commit | fd01105a5214cf90eea59c09d4fce14f0abb586e (patch) | |
tree | 48204d3276d17a3b8b002ca9102a5086bdb44418 | |
parent | 857b159344ccbc07af06d45854eb8b2f6dc77d11 (diff) | |
download | chef-fd01105a5214cf90eea59c09d4fce14f0abb586e.tar.gz |
Test more of provider resolution by mocking the filesystem and commands
-rw-r--r-- | spec/support/shared/unit/double_world.rb | 60 | ||||
-rw-r--r-- | spec/unit/provider_resolver_spec.rb | 42 |
2 files changed, 96 insertions, 6 deletions
diff --git a/spec/support/shared/unit/double_world.rb b/spec/support/shared/unit/double_world.rb new file mode 100644 index 0000000000..3b528f06e2 --- /dev/null +++ b/spec/support/shared/unit/double_world.rb @@ -0,0 +1,60 @@ +# +# Author:: John Keiser <jkeiser@chef.io> +# Copyright:: Copyright (c) 2015 John Keiser. +# 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. +# + +# +# Mocks File and ShellOut. +# +# world.files["/etc/rc.d/init.d"] = "blah" means that /etc/rc.d/init.d is a file with contents "blah." +# world.commands["shellme"] = "result" means that when shell_out! calls "shellme", it will return "result" in stdout. +# world.commands["shellme"] = 10 means that when shell_out! calls "shellme", it will return exit code 10 +# world.commands["shellme"] = { code: 10, stdout: "result", stderr: "result2"} +# +shared_context "double world" do + let(:world) { DoubleWorld.new } + before do + allow(::File).to receive(:exists?) { |f| world.files.has_key?(f) } + allow(::File).to receive(:executable?) { |f| world.files.has_key?(f) } + allow(::File).to receive(:open) { |f| StringIO.new(world.files[f]) } + + allow(::Chef::Mixin::ShellOut).to receive(:shell_out!) do |c| + raise "hi" + result = world.commands[c] + case result + when String + Mash.new(stdout: result) + when Integer + Mash.new(exitstatus: result) + when Hash + Mash.new(result) + else + raise ArgumentError, result + end + end + end + + private + + class DoubleWorld + def initialize + @files = {} + @commands = {} + end + attr_reader :files + attr_reader :commands + end +end diff --git a/spec/unit/provider_resolver_spec.rb b/spec/unit/provider_resolver_spec.rb index 88df4a20cc..aa485d1fe1 100644 --- a/spec/unit/provider_resolver_spec.rb +++ b/spec/unit/provider_resolver_spec.rb @@ -20,6 +20,7 @@ require 'spec_helper' require 'chef/mixin/convert_to_class_name' require 'chef/provider_resolver' require 'chef/platform/service_helpers' +require 'support/shared/unit/double_world' include Chef::Mixin::ConvertToClassName @@ -27,6 +28,7 @@ include Chef::Mixin::ConvertToClassName #module Chef::Provider describe Chef::ProviderResolver do + include_context "double world" let(:resource_name) { :service } let(:provider) { nil } @@ -139,9 +141,37 @@ describe Chef::ProviderResolver do end def stub_service_configs(*configs) - configs ||= [] - allow(Chef::Platform::ServiceHelpers).to receive(:config_for_service).with("ntp") - .and_return(configs) + configs.each do |config| + case config + when :initd + world.files["/etc/init.d/test"] ||= "" + when :upstart + world.files["/etc/init/test.conf"] ||= "" + when :xinetd + world.files["/etc/xinetd.d/test"] ||= "" + when :etc_rcd + world.files["/etc/rc.d/test"] ||= "" + when :usr_local_etc_rcd + world.files["/usr/local/etc/rc.d/test"] ||= "" + when :systemd + world.files["/bin/systemd"] ||= "" + world.files["/proc/1/comm"] ||= "systemd\n" + world.commands["/bin/systemd --all"] ||= <<-EOM +superv loaded +stinky something-else +test loaded +blargh not-found +EOM + world.commands["/bin/systemd list-unit-files"] ||= <<-EOM +usuperv loaded +ustinky something-else +utest loaded +ublargh not-found +EOM + else + raise ArgumentError, config + end + end end before do @@ -413,7 +443,7 @@ describe Chef::ProviderResolver do it "always returns a Solaris provider" do # no matter what we stub on the next two lines we should get a Solaris provider stub_service_providers(:debian, :invokercd, :insserv, :upstart, :redhat, :systemd) - stub_service_configs(:initd, :upstart, :xinetd, :user_local_etc_rcd, :systemd) + stub_service_configs(:initd, :upstart, :xinetd, :usr_local_etc_rcd, :systemd) expect(resolved_provider).to eql(Chef::Provider::Service::Solaris) end end @@ -428,7 +458,7 @@ describe Chef::ProviderResolver do it "always returns a Windows provider" do # no matter what we stub on the next two lines we should get a Windows provider stub_service_providers(:debian, :invokercd, :insserv, :upstart, :redhat, :systemd) - stub_service_configs(:initd, :upstart, :xinetd, :user_local_etc_rcd, :systemd) + stub_service_configs(:initd, :upstart, :xinetd, :usr_local_etc_rcd, :systemd) expect(resolved_provider).to eql(Chef::Provider::Service::Windows) end end @@ -443,7 +473,7 @@ describe Chef::ProviderResolver do it "always returns a Macosx provider" do # no matter what we stub on the next two lines we should get a Macosx provider stub_service_providers(:debian, :invokercd, :insserv, :upstart, :redhat, :systemd) - stub_service_configs(:initd, :upstart, :xinetd, :user_local_etc_rcd, :systemd) + stub_service_configs(:initd, :upstart, :xinetd, :usr_local_etc_rcd, :systemd) expect(resolved_provider).to eql(Chef::Provider::Service::Macosx) end end |