1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/application/resource'
describe Puppet::Application::Resource do
before :each do
@resource = Puppet::Application[:resource]
Puppet::Util::Log.stubs(:newdestination)
Puppet::Util::Log.stubs(:level=)
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
end
it "should declare a main command" do
@resource.should respond_to(:main)
end
it "should declare a host option" do
@resource.should respond_to(:handle_host)
end
it "should declare a types option" do
@resource.should respond_to(:handle_types)
end
it "should declare a param option" do
@resource.should respond_to(:handle_param)
end
it "should declare a preinit block" do
@resource.should respond_to(:preinit)
end
describe "in preinit" do
it "should set hosts to nil" do
@resource.preinit
@resource.host.should be_nil
end
it "should init extra_params to empty array" do
@resource.preinit
@resource.extra_params.should == []
end
it "should load Facter facts" do
Facter.expects(:loadfacts).once
@resource.preinit
end
end
describe "when handling options" do
[:debug, :verbose, :edit].each do |option|
it "should declare handle_#{option} method" do
@resource.should respond_to("handle_#{option}".to_sym)
end
it "should store argument value when calling handle_#{option}" do
@resource.options.expects(:[]=).with(option, 'arg')
@resource.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
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.stubs(:exit)
@resource.expects(:puts).with(['type1','type2'])
@resource.handle_types(nil)
end
it "should add param to extra_params list" do
@resource.extra_params = [ :param1 ]
@resource.handle_param("whatever")
@resource.extra_params.should == [ :param1, :whatever ]
end
end
describe "during setup" do
before :each do
Puppet::Log.stubs(:newdestination)
Puppet::Log.stubs(:level=)
Puppet.stubs(:parse_config)
end
it "should set console as the log destination" do
Puppet::Log.expects(:newdestination).with(:console)
@resource.setup
end
it "should set log level to debug if --debug was passed" do
@resource.options.stubs(:[]).with(:debug).returns(true)
Puppet::Log.expects(:level=).with(:debug)
@resource.setup
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)
Puppet::Log.expects(:level=).with(:info)
@resource.setup
end
it "should Parse puppet config" do
Puppet.expects(:parse_config)
@resource.setup
end
end
describe "when running" do
before :each do
@type = stub_everything 'type', :properties => []
@resource.command_line.stubs(:args).returns(['type'])
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
end
it "should raise an error when editing a remote host" do
@resource.options.stubs(:[]).with(:edit).returns(true)
@resource.host = 'host'
lambda { @resource.main }.should raise_error
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
end
describe "with a host" do
before :each do
@resource.stubs(:puts)
@resource.host = 'host'
Puppet::Resource.stubs(:find ).never
Puppet::Resource.stubs(:search).never
Puppet::Resource.stubs(:save ).never
end
it "should search for resources" do
@resource.command_line.stubs(:args).returns(['type'])
Puppet::Resource.expects(:search).with('https://host:8139/production/resources/type/', {}).returns([])
@resource.main
end
it "should describe the given resource" do
@resource.command_line.stubs(:args).returns(['type', 'name'])
x = stub_everything 'resource'
Puppet::Resource.expects(:find).with('https://host:8139/production/resources/type/name').returns(x)
@resource.main
end
it "should add given parameters to the object" do
@resource.command_line.stubs(:args).returns(['type','name','param=temp'])
res = stub "resource"
res.expects(:save).with('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
end
end
describe "without a host" do
before :each do
@resource.stubs(:puts)
@resource.host = nil
Puppet::Resource.stubs(:find ).never
Puppet::Resource.stubs(:search).never
Puppet::Resource.stubs(:save ).never
end
it "should search for resources" do
Puppet::Resource.expects(:search).with('type/', {}).returns([])
@resource.main
end
it "should describe the given resource" do
@resource.command_line.stubs(:args).returns(['type','name'])
x = stub_everything 'resource'
Puppet::Resource.expects(:find).with('type/name').returns(x)
@resource.main
end
it "should add given parameters to the object" do
@resource.command_line.stubs(:args).returns(['type','name','param=temp'])
res = stub "resource"
res.expects(:save).with('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
end
end
end
end
|