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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
|
require 'spec_helper'
def safely_load_service_type
before(:each) do
# We have a :confine block that calls execute in our upstart provider, which fails
# on jruby. Thus, we stub it out here since we don't care to do any assertions on it.
# This is only an issue if you're running these unit tests on a platform where upstart
# is a default provider, like Ubuntu trusty.
allow(Puppet::Util::Execution).to receive(:execute)
Puppet::Type.type(:service)
end
end
test_title = 'Puppet::Type::Service'
describe test_title do
safely_load_service_type
it "should have an :enableable feature that requires the :enable, :disable, and :enabled? methods" do
expect(Puppet::Type.type(:service).provider_feature(:enableable).methods).to eq([:disable, :enable, :enabled?])
end
it "should have a :refreshable feature that requires the :restart method" do
expect(Puppet::Type.type(:service).provider_feature(:refreshable).methods).to eq([:restart])
end
end
describe test_title, "when validating attributes" do
safely_load_service_type
[:name, :binary, :hasstatus, :path, :pattern, :start, :restart, :stop, :status, :hasrestart, :control, :timeout].each do |param|
it "should have a #{param} parameter" do
expect(Puppet::Type.type(:service).attrtype(param)).to eq(:param)
end
end
[:ensure, :enable].each do |param|
it "should have an #{param} property" do
expect(Puppet::Type.type(:service).attrtype(param)).to eq(:property)
end
end
end
describe test_title, "when validating attribute values" do
safely_load_service_type
before do
@provider = double('provider', :class => Puppet::Type.type(:service).defaultprovider, :clear => nil, :controllable? => false)
allow(Puppet::Type.type(:service).defaultprovider).to receive(:new).and_return(@provider)
end
it "should support :running as a value to :ensure" do
Puppet::Type.type(:service).new(:name => "yay", :ensure => :running)
end
it "should support :stopped as a value to :ensure" do
Puppet::Type.type(:service).new(:name => "yay", :ensure => :stopped)
end
it "should alias the value :true to :running in :ensure" do
svc = Puppet::Type.type(:service).new(:name => "yay", :ensure => true)
expect(svc.should(:ensure)).to eq(:running)
end
it "should alias the value :false to :stopped in :ensure" do
svc = Puppet::Type.type(:service).new(:name => "yay", :ensure => false)
expect(svc.should(:ensure)).to eq(:stopped)
end
describe "the enable property" do
before :each do
allow(@provider.class).to receive(:supports_parameter?).and_return(true)
end
describe "for value without required features" do
before :each do
allow(@provider).to receive(:satisfies?)
end
it "should not support :mask as a value" do
expect { Puppet::Type.type(:service).new(:name => "yay", :enable => :mask) }.to raise_error(
Puppet::ResourceError,
/Provider .+ must have features 'maskable' to set 'enable' to 'mask'/
)
end
it "should not support :manual as a value" do
expect { Puppet::Type.type(:service).new(:name => "yay", :enable => :manual) }.to raise_error(
Puppet::ResourceError,
/Provider .+ must have features 'manual_startable' to set 'enable' to 'manual'/
)
end
it "should not support :mask as a value" do
expect { Puppet::Type.type(:service).new(:name => "yay", :enable => :delayed) }.to raise_error(
Puppet::ResourceError,
/Provider .+ must have features 'delayed_startable' to set 'enable' to 'delayed'/
)
end
end
describe "for value with required features" do
before :each do
allow(@provider).to receive(:satisfies?).and_return(:true)
end
it "should support :true as a value" do
srv = Puppet::Type.type(:service).new(:name => "yay", :enable => :true)
expect(srv.should(:enable)).to eq(:true)
end
it "should support :false as a value" do
srv = Puppet::Type.type(:service).new(:name => "yay", :enable => :false)
expect(srv.should(:enable)).to eq(:false)
end
it "should support :mask as a value" do
srv = Puppet::Type.type(:service).new(:name => "yay", :enable => :mask)
expect(srv.should(:enable)).to eq(:mask)
end
it "should support :manual as a value on Windows" do
allow(Puppet::Util::Platform).to receive(:windows?).and_return(true)
srv = Puppet::Type.type(:service).new(:name => "yay", :enable => :manual)
expect(srv.should(:enable)).to eq(:manual)
end
it "should support :delayed as a value on Windows" do
allow(Puppet::Util::Platform).to receive(:windows?).and_return(true)
srv = Puppet::Type.type(:service).new(:name => "yay", :enable => :delayed)
expect(srv.should(:enable)).to eq(:delayed)
end
end
end
describe "the timeout parameter" do
before do
provider_class_with_timeout = Puppet::Type.type(:service).provide(:simple) do
has_features :configurable_timeout
end
allow(Puppet::Type.type(:service)).to receive(:defaultprovider).and_return(provider_class_with_timeout)
end
it "should fail when timeout is not an integer" do
expect { Puppet::Type.type(:service).new(:name => "yay", :timeout => 'foobar') }.to raise_error(Puppet::Error)
end
[-999, -1, 0].each do |int|
it "should not support #{int} as a value to :timeout" do
expect { Puppet::Type.type(:service).new(:name => "yay", :timeout => int) }.to raise_error(Puppet::Error)
end
end
[1, 30, 999].each do |int|
it "should support #{int} as a value to :timeout" do
srv = Puppet::Type.type(:service).new(:name => "yay", :timeout => int)
expect(srv[:timeout]).to eq(int)
end
end
it "should default :timeout to 10 when provider has no default value" do
srv = Puppet::Type.type(:service).new(:name => "yay")
expect(srv[:timeout]).to eq(10)
end
it "should default :timeout to provider given default time when it has one" do
provider_class_with_timeout = Puppet::Type.type(:service).provide(:simple) do
has_features :configurable_timeout
def default_timeout
30
end
end
allow(Puppet::Type.type(:service)).to receive(:defaultprovider).and_return(provider_class_with_timeout)
srv = Puppet::Type.type(:service).new(:name => "yay")
expect(srv[:timeout]).to eq(30)
end
it "should accept string as value" do
srv = Puppet::Type.type(:service).new(:name => "yay", :timeout => "25")
expect(srv[:timeout]).to eq(25)
end
it "should not support values that cannot be converted to Integer such as Array" do
expect { Puppet::Type.type(:service).new(:name => "yay", :timeout => [25]) }.to raise_error(Puppet::Error)
end
end
describe "the service logon credentials" do
before do
provider_class_with_logon_credentials = Puppet::Type.type(:service).provide(:simple) do
has_features :manages_logon_credentials
def logonpassword=(value) end
def logonaccount_insync?(current) end
end
allow(Puppet::Type.type(:service)).to receive(:defaultprovider).and_return(provider_class_with_logon_credentials)
end
describe "the 'logonaccount' property" do
let(:service) {Puppet::Type.type(:service).new(:name => "yay", :logonaccount => 'myUser')}
it "should let superclass implementation resolve insyncness when provider does not respond to the 'logonaccount_insync?' method" do
allow(service.provider).to receive(:respond_to?).with(:logonaccount_insync?).and_return(false)
expect(service.property(:logonaccount).insync?('myUser')).to eq(true)
end
it "should let provider resolve insyncness when provider responds to the 'logonaccount_insync?' method" do
allow(service.provider).to receive(:respond_to?).with(:logonaccount_insync?, any_args).and_return(true)
allow(service.provider).to receive(:logonaccount_insync?).and_return(false)
expect(service.property(:logonaccount).insync?('myUser')).to eq(false)
end
end
describe "the logonpassword parameter" do
it "should fail when logonaccount is not being managed as well" do
expect { Puppet::Type.type(:service).new(:name => "yay", :logonpassword => 'myPass') }.to raise_error(Puppet::Error, /The 'logonaccount' parameter is mandatory when setting 'logonpassword'./)
end
it "should default to empty string when only logonaccount is being managed" do
service = Puppet::Type.type(:service).new(:name => "yay", :logonaccount => 'myUser')
expect { service }.not_to raise_error
expect(service[:logonpassword]).to eq("")
end
it "should default to nil when not even logonaccount is being managed" do
service = Puppet::Type.type(:service).new(:name => "yay")
expect(service[:logonpassword]).to eq(nil)
end
it "should fail when logonpassword includes the ':' character" do
expect { Puppet::Type.type(:service).new(:name => "yay", :logonaccount => 'myUser', :logonpassword => 'my:Pass') }.to raise_error(Puppet::Error, /Passwords cannot include ':'/)
end
end
end
it "should support :true as a value to :hasstatus" do
srv = Puppet::Type.type(:service).new(:name => "yay", :hasstatus => :true)
expect(srv[:hasstatus]).to eq(:true)
end
it "should support :false as a value to :hasstatus" do
srv = Puppet::Type.type(:service).new(:name => "yay", :hasstatus => :false)
expect(srv[:hasstatus]).to eq(:false)
end
it "should specify :true as the default value of hasstatus" do
srv = Puppet::Type.type(:service).new(:name => "yay")
expect(srv[:hasstatus]).to eq(:true)
end
it "should support :true as a value to :hasrestart" do
srv = Puppet::Type.type(:service).new(:name => "yay", :hasrestart => :true)
expect(srv[:hasrestart]).to eq(:true)
end
it "should support :false as a value to :hasrestart" do
srv = Puppet::Type.type(:service).new(:name => "yay", :hasrestart => :false)
expect(srv[:hasrestart]).to eq(:false)
end
it "should allow setting the :enable parameter if the provider has the :enableable feature" do
allow(Puppet::Type.type(:service).defaultprovider).to receive(:supports_parameter?).and_return(true)
expect(Puppet::Type.type(:service).defaultprovider).to receive(:supports_parameter?).with(Puppet::Type.type(:service).attrclass(:enable)).and_return(true)
svc = Puppet::Type.type(:service).new(:name => "yay", :enable => true)
expect(svc.should(:enable)).to eq(:true)
end
it "should not allow setting the :enable parameter if the provider is missing the :enableable feature" do
allow(Puppet::Type.type(:service).defaultprovider).to receive(:supports_parameter?).and_return(true)
expect(Puppet::Type.type(:service).defaultprovider).to receive(:supports_parameter?).with(Puppet::Type.type(:service).attrclass(:enable)).and_return(false)
svc = Puppet::Type.type(:service).new(:name => "yay", :enable => true)
expect(svc.should(:enable)).to be_nil
end
it "should split paths on '#{File::PATH_SEPARATOR}'" do
allow(Puppet::FileSystem).to receive(:exist?).and_return(true)
allow(FileTest).to receive(:directory?).and_return(true)
svc = Puppet::Type.type(:service).new(:name => "yay", :path => "/one/two#{File::PATH_SEPARATOR}/three/four")
expect(svc[:path]).to eq(%w{/one/two /three/four})
end
it "should accept arrays of paths joined by '#{File::PATH_SEPARATOR}'" do
allow(Puppet::FileSystem).to receive(:exist?).and_return(true)
allow(FileTest).to receive(:directory?).and_return(true)
svc = Puppet::Type.type(:service).new(:name => "yay", :path => ["/one#{File::PATH_SEPARATOR}/two", "/three#{File::PATH_SEPARATOR}/four"])
expect(svc[:path]).to eq(%w{/one /two /three /four})
end
end
describe test_title, "when setting default attribute values" do
safely_load_service_type
it "should default to the provider's default path if one is available" do
allow(FileTest).to receive(:directory?).and_return(true)
allow(Puppet::FileSystem).to receive(:exist?).and_return(true)
allow(Puppet::Type.type(:service).defaultprovider).to receive(:respond_to?).and_return(true)
allow(Puppet::Type.type(:service).defaultprovider).to receive(:defpath).and_return("testing")
svc = Puppet::Type.type(:service).new(:name => "other")
expect(svc[:path]).to eq(["testing"])
end
it "should default 'pattern' to the binary if one is provided" do
svc = Puppet::Type.type(:service).new(:name => "other", :binary => "/some/binary")
expect(svc[:pattern]).to eq("/some/binary")
end
it "should default 'pattern' to the name if no pattern is provided" do
svc = Puppet::Type.type(:service).new(:name => "other")
expect(svc[:pattern]).to eq("other")
end
it "should default 'control' to the upcased service name with periods replaced by underscores if the provider supports the 'controllable' feature" do
provider = double('provider', :controllable? => true, :class => Puppet::Type.type(:service).defaultprovider, :clear => nil)
allow(Puppet::Type.type(:service).defaultprovider).to receive(:new).and_return(provider)
svc = Puppet::Type.type(:service).new(:name => "nfs.client")
expect(svc[:control]).to eq("NFS_CLIENT_START")
end
end
describe test_title, "when retrieving the host's current state" do
safely_load_service_type
before do
@service = Puppet::Type.type(:service).new(:name => "yay")
end
it "should use the provider's status to determine whether the service is running" do
expect(@service.provider).to receive(:status).and_return(:yepper)
@service[:ensure] = :running
expect(@service.property(:ensure).retrieve).to eq(:yepper)
end
it "should ask the provider whether it is enabled" do
allow(@service.provider.class).to receive(:supports_parameter?).and_return(true)
expect(@service.provider).to receive(:enabled?).and_return(:yepper)
@service[:enable] = true
expect(@service.property(:enable).retrieve).to eq(:yepper)
end
end
describe test_title, "when changing the host" do
safely_load_service_type
before do
@service = Puppet::Type.type(:service).new(:name => "yay")
end
it "should start the service if it is supposed to be running" do
@service[:ensure] = :running
expect(@service.provider).to receive(:start)
@service.property(:ensure).sync
end
it "should stop the service if it is supposed to be stopped" do
@service[:ensure] = :stopped
expect(@service.provider).to receive(:stop)
@service.property(:ensure).sync
end
it "should enable the service if it is supposed to be enabled" do
allow(@service.provider.class).to receive(:supports_parameter?).and_return(true)
@service[:enable] = true
expect(@service.provider).to receive(:enable)
@service.property(:enable).sync
end
it "should disable the service if it is supposed to be disabled" do
allow(@service.provider.class).to receive(:supports_parameter?).and_return(true)
@service[:enable] = false
expect(@service.provider).to receive(:disable)
@service.property(:enable).sync
end
it "should let superclass implementation resolve insyncness when provider does not respond to the 'enabled_insync?' method" do
allow(@service.provider.class).to receive(:supports_parameter?).and_return(true)
@service[:enable] = true
allow(@service.provider).to receive(:respond_to?).with(:enabled_insync?).and_return(false)
expect(@service.property(:enable).insync?(:true)).to eq(true)
end
it "insyncness should be resolved by provider instead of superclass implementation when provider responds to the 'enabled_insync?' method" do
allow(@service.provider.class).to receive(:supports_parameter?).and_return(true)
@service[:enable] = true
allow(@service.provider).to receive(:respond_to?).with(:enabled_insync?, any_args).and_return(true)
allow(@service.provider).to receive(:enabled_insync?).and_return(false)
expect(@service.property(:enable).insync?(:true)).to eq(false)
end
it "should sync the service's enable state when changing the state of :ensure if :enable is being managed" do
allow(@service.provider.class).to receive(:supports_parameter?).and_return(true)
@service[:enable] = false
@service[:ensure] = :stopped
expect(@service.property(:enable)).to receive(:retrieve).and_return("whatever")
expect(@service.property(:enable)).to receive(:insync?).and_return(false)
expect(@service.property(:enable)).to receive(:sync)
allow(@service.provider).to receive(:stop)
@service.property(:ensure).sync
end
it "should sync the service's logonaccount state when changing the state of :ensure if :logonaccount is being managed" do
allow(@service.provider.class).to receive(:supports_parameter?).and_return(true)
allow(Puppet::Util::Platform).to receive(:windows?).and_return(false)
@service[:ensure] = :stopped
@service[:logonaccount] = 'LocalSystem'
expect(@service.property(:logonaccount)).to receive(:retrieve).and_return("MyUser")
expect(@service.property(:logonaccount)).to receive(:insync?).and_return(false)
expect(@service.property(:logonaccount)).to receive(:sync)
allow(@service.provider).to receive(:stop)
@service.property(:ensure).sync
end
end
describe test_title, "when refreshing the service" do
safely_load_service_type
before do
@service = Puppet::Type.type(:service).new(:name => "yay")
end
it "should restart the service if it is running" do
@service[:ensure] = :running
expect(@service.provider).to receive(:status).and_return(:running)
expect(@service.provider).to receive(:restart)
@service.refresh
end
it "should restart the service if it is running, even if it is supposed to stopped" do
@service[:ensure] = :stopped
expect(@service.provider).to receive(:status).and_return(:running)
expect(@service.provider).to receive(:restart)
@service.refresh
end
it "should not restart the service if it is not running" do
@service[:ensure] = :running
expect(@service.provider).to receive(:status).and_return(:stopped)
@service.refresh
end
it "should add :ensure as a property if it is not being managed" do
expect(@service.provider).to receive(:status).and_return(:running)
expect(@service.provider).to receive(:restart)
@service.refresh
end
end
|