diff --git a/spec/unit/application/resource_spec.rb b/spec/unit/application/resource_spec.rb index 826b68d9a..e3e4c6738 100755 --- a/spec/unit/application/resource_spec.rb +++ b/spec/unit/application/resource_spec.rb @@ -1,220 +1,213 @@ #!/usr/bin/env rspec require 'spec_helper' require 'puppet/application/resource' describe Puppet::Application::Resource do before :each do - @resource = Puppet::Application[:resource] + @resource_app = Puppet::Application[:resource] Puppet::Util::Log.stubs(:newdestination) Puppet::Resource.indirection.stubs(:terminus_class=) end it "should ask Puppet::Application to not parse Puppet configuration file" do - @resource.should_parse_config?.should be_false + @resource_app.should_parse_config?.should be_false end describe "in preinit" do it "should init extra_params to empty array", :'fails_on_ruby_1.9.2' => true do - @resource.preinit - - @resource.extra_params.should == [] + @resource_app.preinit + @resource_app.extra_params.should == [] end it "should load Facter facts" do Facter.expects(:loadfacts).once - @resource.preinit + @resource_app.preinit end end describe "when handling options" do - [:debug, :verbose, :edit].each do |option| it "should store argument value when calling handle_#{option}" do - @resource.options.expects(:[]=).with(option, 'arg') - @resource.send("handle_#{option}".to_sym, 'arg') + @resource_app.options.expects(:[]=).with(option, 'arg') + @resource_app.send("handle_#{option}".to_sym, 'arg') end end it "should set options[:host] to given host" do - @resource.handle_host(:whatever) - - @resource.host.should == :whatever + @resource_app.handle_host(:whatever) + @resource_app.host.should == :whatever end it "should load an display all types with types option" do type1 = stub_everything 'type1', :name => :type1 type2 = stub_everything 'type2', :name => :type2 Puppet::Type.stubs(:loadall) Puppet::Type.stubs(:eachtype).multiple_yields(type1,type2) - @resource.expects(:puts).with(['type1','type2']) - expect { @resource.handle_types(nil) }.to exit_with 0 + @resource_app.expects(:puts).with(['type1','type2']) + expect { @resource_app.handle_types(nil) }.to exit_with 0 end it "should add param to extra_params list" do - @resource.extra_params = [ :param1 ] - @resource.handle_param("whatever") + @resource_app.extra_params = [ :param1 ] + @resource_app.handle_param("whatever") - @resource.extra_params.should == [ :param1, :whatever ] + @resource_app.extra_params.should == [ :param1, :whatever ] end end describe "during setup" do before :each do Puppet::Log.stubs(:newdestination) Puppet.stubs(:parse_config) end - it "should set console as the log destination" do Puppet::Log.expects(:newdestination).with(:console) - @resource.setup + @resource_app.setup end it "should set log level to debug if --debug was passed" do - @resource.options.stubs(:[]).with(:debug).returns(true) - @resource.setup + @resource_app.options.stubs(:[]).with(:debug).returns(true) + @resource_app.setup Puppet::Log.level.should == :debug end it "should set log level to info if --verbose was passed" do - @resource.options.stubs(:[]).with(:debug).returns(false) - @resource.options.stubs(:[]).with(:verbose).returns(true) - @resource.setup + @resource_app.options.stubs(:[]).with(:debug).returns(false) + @resource_app.options.stubs(:[]).with(:verbose).returns(true) + @resource_app.setup Puppet::Log.level.should == :info end it "should Parse puppet config" do Puppet.expects(:parse_config) - @resource.setup + @resource_app.setup end end describe "when running" do - before :each do @type = stub_everything 'type', :properties => [] - @resource.command_line.stubs(:args).returns(['mytype']) + @resource_app.command_line.stubs(:args).returns(['mytype']) Puppet::Type.stubs(:type).returns(@type) end it "should raise an error if no type is given" do - @resource.command_line.stubs(:args).returns([]) - lambda { @resource.main }.should raise_error(RuntimeError, "You must specify the type to display") + @resource_app.command_line.stubs(:args).returns([]) + lambda { @resource_app.main }.should raise_error(RuntimeError, "You must specify the type to display") end it "should raise an error when editing a remote host" do - @resource.options.stubs(:[]).with(:edit).returns(true) - @resource.host = 'host' + @resource_app.options.stubs(:[]).with(:edit).returns(true) + @resource_app.host = 'host' - lambda { @resource.main }.should raise_error(RuntimeError, "You cannot edit a remote host") + lambda { @resource_app.main }.should raise_error(RuntimeError, "You cannot edit a remote host") end it "should raise an error if the type is not found" do Puppet::Type.stubs(:type).returns(nil) - lambda { @resource.main }.should raise_error(RuntimeError, 'Could not find type mytype') + lambda { @resource_app.main }.should raise_error(RuntimeError, 'Could not find type mytype') end describe "with a host" do before :each do - @resource.stubs(:puts) - @resource.host = 'host' + @resource_app.stubs(:puts) + @resource_app.host = 'host' Puppet::Resource.indirection.stubs(:find ).never Puppet::Resource.indirection.stubs(:search).never Puppet::Resource.indirection.stubs(:save ).never end it "should search for resources" do - @resource.command_line.stubs(:args).returns(['type']) + @resource_app.command_line.stubs(:args).returns(['type']) Puppet::Resource.indirection.expects(:search).with('https://host:8139/production/resources/type/', {}).returns([]) - @resource.main + @resource_app.main end it "should describe the given resource" do - @resource.command_line.stubs(:args).returns(['type', 'name']) + @resource_app.command_line.stubs(:args).returns(['type', 'name']) x = stub_everything 'resource' Puppet::Resource.indirection.expects(:find).with('https://host:8139/production/resources/type/name').returns(x) - @resource.main + @resource_app.main end it "should add given parameters to the object" do - @resource.command_line.stubs(:args).returns(['type','name','param=temp']) + @resource_app.command_line.stubs(:args).returns(['type','name','param=temp']) res = stub "resource" Puppet::Resource.indirection.expects(:save).with(res, 'https://host:8139/production/resources/type/name').returns(res) res.expects(:collect) res.expects(:to_manifest) Puppet::Resource.expects(:new).with('type', 'name', :parameters => {'param' => 'temp'}).returns(res) - @resource.main + @resource_app.main end - end describe "without a host" do before :each do - @resource.stubs(:puts) - @resource.host = nil + @resource_app.stubs(:puts) + @resource_app.host = nil Puppet::Resource.indirection.stubs(:find ).never Puppet::Resource.indirection.stubs(:search).never Puppet::Resource.indirection.stubs(:save ).never end it "should search for resources" do Puppet::Resource.indirection.expects(:search).with('mytype/', {}).returns([]) - @resource.main + @resource_app.main end it "should describe the given resource" do - @resource.command_line.stubs(:args).returns(['type','name']) + @resource_app.command_line.stubs(:args).returns(['type','name']) x = stub_everything 'resource' Puppet::Resource.indirection.expects(:find).with('type/name').returns(x) - @resource.main + @resource_app.main end it "should add given parameters to the object" do - @resource.command_line.stubs(:args).returns(['type','name','param=temp']) + @resource_app.command_line.stubs(:args).returns(['type','name','param=temp']) res = stub "resource" Puppet::Resource.indirection.expects(:save).with(res, 'type/name').returns(res) res.expects(:collect) res.expects(:to_manifest) Puppet::Resource.expects(:new).with('type', 'name', :parameters => {'param' => 'temp'}).returns(res) - @resource.main + @resource_app.main end - end end describe "when handling file type" do before :each do Facter.stubs(:loadfacts) - @resource.preinit + @resource_app.preinit end it "should raise an exception if no file specified" do - @resource.command_line.stubs(:args).returns(['file']) + @resource_app.command_line.stubs(:args).returns(['file']) - lambda { @resource.main }.should raise_error(RuntimeError, /Listing all file instances is not supported/) + lambda { @resource_app.main }.should raise_error(RuntimeError, /Listing all file instances is not supported/) end it "should output a file resource when given a file path" do path = File.expand_path('/etc') res = Puppet::Type.type(:file).new(:path => path).to_resource Puppet::Resource.indirection.expects(:find).returns(res) - @resource.command_line.stubs(:args).returns(['file', path]) - @resource.expects(:puts).with do |args| + @resource_app.command_line.stubs(:args).returns(['file', path]) + @resource_app.expects(:puts).with do |args| args.should =~ /file \{ '#{Regexp.escape(path)}'/m end - @resource.main + @resource_app.main end end end