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 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
|
require 'spec_helper'
describe 'Puppet::Type::Service::Provider::Smf',
unless: Puppet::Util::Platform.windows? || Puppet::Util::Platform.jruby? do
let(:provider_class) { Puppet::Type.type(:service).provider(:smf) }
def set_resource_params(params = {})
params.each do |param, value|
if value.nil?
@provider.resource.delete(param) if @provider.resource[param]
else
@provider.resource[param] = value
end
end
end
before(:each) do
# Create a mock resource
@resource = Puppet::Type.type(:service).new(
:name => "/system/myservice", :ensure => :running, :enable => :true)
@provider = provider_class.new(@resource)
allow(FileTest).to receive(:file?).with('/usr/sbin/svcadm').and_return(true)
allow(FileTest).to receive(:executable?).with('/usr/sbin/svcadm').and_return(true)
allow(FileTest).to receive(:file?).with('/usr/bin/svcs').and_return(true)
allow(FileTest).to receive(:executable?).with('/usr/bin/svcs').and_return(true)
allow(Facter).to receive(:value).with(:operatingsystem).and_return('Solaris')
allow(Facter).to receive(:value).with(:osfamily).and_return('Solaris')
allow(Facter).to receive(:value).with(:operatingsystemrelease).and_return('11.2')
end
context ".instances" do
it "should have an instances method" do
expect(provider_class).to respond_to :instances
end
it "should get a list of services (excluding legacy)" do
expect(provider_class).to receive(:svcs).with('-H', '-o', 'state,fmri').and_return(File.read(my_fixture('svcs_instances.out')))
instances = provider_class.instances.map { |p| {:name => p.get(:name), :ensure => p.get(:ensure)} }
# we dont manage legacy
expect(instances.size).to eq(3)
expect(instances[0]).to eq({:name => 'svc:/system/svc/restarter:default', :ensure => :running })
expect(instances[1]).to eq({:name => 'svc:/network/cswrsyncd:default', :ensure => :maintenance })
expect(instances[2]).to eq({:name => 'svc:/network/dns/client:default', :ensure => :degraded })
end
end
describe '#service_exists?' do
it 'returns true if the service exists' do
expect(@provider).to receive(:service_fmri)
expect(@provider.service_exists?).to be(true)
end
it 'returns false if the service does not exist' do
expect(@provider).to receive(:service_fmri).and_raise(
Puppet::ExecutionFailure, 'svcs failed!'
)
expect(@provider.service_exists?).to be(false)
end
end
describe '#setup_service' do
it 'noops if the service resource does not have the manifest parameter passed-in' do
expect(@provider).not_to receive(:svccfg)
set_resource_params({ :manifest => nil })
@provider.setup_service
end
context 'when the service resource has a manifest parameter passed-in' do
let(:manifest) { 'foo' }
before(:each) { set_resource_params({ :manifest => manifest }) }
it 'noops if the service resource already exists' do
expect(@provider).not_to receive(:svccfg)
expect(@provider).to receive(:service_exists?).and_return(true)
@provider.setup_service
end
it "imports the service resource's manifest" do
expect(@provider).to receive(:service_exists?).and_return(false)
expect(@provider).to receive(:svccfg).with(:import, manifest)
@provider.setup_service
end
it 'raises a Puppet::Error if SMF fails to import the manifest' do
expect(@provider).to receive(:service_exists?).and_return(false)
failure_reason = 'svccfg failed!'
expect(@provider).to receive(:svccfg).with(:import, manifest).and_raise(Puppet::ExecutionFailure, failure_reason)
expect { @provider.setup_service }.to raise_error do |error|
expect(error).to be_a(Puppet::Error)
expect(error.message).to match(failure_reason)
end
end
end
end
describe '#service_fmri' do
it 'returns the memoized the fmri if it exists' do
@provider.instance_variable_set(:@fmri, 'resource_fmri')
expect(@provider.service_fmri).to eql('resource_fmri')
end
it 'raises a Puppet::Error if the service resource matches multiple FMRIs' do
expect(@provider).to receive(:svcs).with('-l', @provider.resource[:name]).and_return(File.read(my_fixture('svcs_multiple_fmris.out')))
expect { @provider.service_fmri }.to raise_error do |error|
expect(error).to be_a(Puppet::Error)
expect(error.message).to match(@provider.resource[:name])
expect(error.message).to match('multiple')
matched_fmris = ["svc:/application/tstapp:one", "svc:/application/tstapp:two"]
expect(error.message).to match(matched_fmris.join(', '))
end
end
it 'raises a Puppet:ExecutionFailure if svcs fails' do
expect(@provider).to receive(:svcs).with('-l', @provider.resource[:name]).and_raise(
Puppet::ExecutionFailure, 'svcs failed!'
)
expect { @provider.service_fmri }.to raise_error do |error|
expect(error).to be_a(Puppet::ExecutionFailure)
expect(error.message).to match('svcs failed!')
end
end
it "returns the service resource's fmri and memoizes it" do
expect(@provider).to receive(:svcs).with('-l', @provider.resource[:name]).and_return(File.read(my_fixture('svcs_fmri.out')))
expected_fmri = 'svc:/application/tstapp:default'
expect(@provider.service_fmri).to eql(expected_fmri)
expect(@provider.instance_variable_get(:@fmri)).to eql(expected_fmri)
end
end
describe '#enabled?' do
let(:fmri) { 'resource_fmri' }
before(:each) do
allow(@provider).to receive(:service_fmri).and_return(fmri)
end
it 'returns :true if the service is enabled' do
expect(@provider).to receive(:svccfg).with('-s', fmri, 'listprop', 'general/enabled').and_return(
'general/enabled boolean true'
)
expect(@provider.enabled?).to be(:true)
end
it 'return :false if the service is not enabled' do
expect(@provider).to receive(:svccfg).with('-s', fmri, 'listprop', 'general/enabled').and_return(
'general/enabled boolean false'
)
expect(@provider.enabled?).to be(:false)
end
it 'returns :false if the service does not exist' do
expect(@provider).to receive(:service_exists?).and_return(false)
expect(@provider.enabled?).to be(:false)
end
end
describe '#restartcmd' do
let(:fmri) { 'resource_fmri' }
before(:each) do
allow(@provider).to receive(:service_fmri).and_return(fmri)
end
it 'returns the right command for restarting the service for Solaris versions newer than 11.2' do
expect(Facter).to receive(:value).with(:operatingsystemrelease).and_return('11.3')
expect(@provider.restartcmd).to eql([@provider.command(:adm), :restart, '-s', fmri])
end
it 'returns the right command for restarting the service on Solaris 11.2' do
expect(Facter).to receive(:value).with(:operatingsystemrelease).and_return('11.2')
expect(@provider.restartcmd).to eql([@provider.command(:adm), :restart, '-s', fmri])
end
it 'returns the right command for restarting the service for Solaris versions older than Solaris 11.2' do
expect(Facter).to receive(:value).with(:operatingsystemrelease).and_return('10.3')
expect(@provider.restartcmd).to eql([@provider.command(:adm), :restart, fmri])
end
end
describe '#service_states' do
let(:fmri) { 'resource_fmri' }
before(:each) do
allow(@provider).to receive(:service_fmri).and_return(fmri)
end
it 'returns the current and next states of the service' do
expect(@provider).to receive(:svcs).with('-H', '-o', 'state,nstate', fmri).and_return(
'online disabled'
)
expect(@provider.service_states).to eql({ :current => 'online', :next => 'disabled' })
end
it "returns nil for the next state if svcs marks it as '-'" do
expect(@provider).to receive(:svcs).with('-H', '-o', 'state,nstate', fmri).and_return(
'online -'
)
expect(@provider.service_states).to eql({ :current => 'online', :next => nil })
end
end
describe '#wait' do
# TODO: Document this method!
def transition_service(from, to, tries)
intermediate_returns = [{ :current => from, :next => to }] * (tries - 1)
final_return = { :current => to, :next => nil }
allow(@provider).to receive(:service_states).and_return(*intermediate_returns.push(final_return))
end
before(:each) do
allow(Timeout).to receive(:timeout).and_yield
allow(Kernel).to receive(:sleep)
end
it 'waits for the service to enter the desired state' do
transition_service('online', 'disabled', 1)
@provider.wait('offline', 'disabled', 'uninitialized')
end
it 'times out and raises a Puppet::Error after sixty seconds' do
expect(Timeout).to receive(:timeout).with(60).and_raise(Timeout::Error, 'method timed out!')
expect { @provider.wait('online') }.to raise_error do |error|
expect(error).to be_a(Puppet::Error)
expect(error.message).to match(@provider.resource[:name])
end
end
it 'sleeps a bit before querying the service state' do
transition_service('disabled', 'online', 10)
expect(Kernel).to receive(:sleep).with(1).exactly(9).times
@provider.wait('online')
end
end
describe '#restart' do
let(:fmri) { 'resource_fmri' }
before(:each) do
allow(@provider).to receive(:service_fmri).and_return(fmri)
allow(@provider).to receive(:execute)
allow(@provider).to receive(:wait)
end
it 'should restart the service' do
expect(@provider).to receive(:execute)
@provider.restart
end
it 'should wait for the service to restart' do
expect(@provider).to receive(:wait).with('online')
@provider.restart
end
end
describe '#status' do
let(:states) do
{
:current => 'online',
:next => nil
}
end
before(:each) do
allow(@provider).to receive(:service_states).and_return(states)
allow(Facter).to receive(:value).with(:operatingsystemrelease).and_return('10.3')
end
it "should run the status command if it's passed in" do
set_resource_params({ :status => 'status_cmd' })
expect(@provider).to receive(:execute)
.with(["status_cmd"], hash_including(failonfail: false))
.and_return(Puppet::Util::Execution::ProcessOutput.new('', 0))
expect(@provider).not_to receive(:service_states)
expect(@provider.status).to eql(:running)
end
shared_examples 'returns the right status' do |svcs_state, expected_state|
it "returns '#{expected_state}' if the svcs state is '#{svcs_state}'" do
states[:current] = svcs_state
expect(@provider.status).to eql(expected_state)
end
end
include_examples 'returns the right status', 'online', :running
include_examples 'returns the right status', 'offline', :stopped
include_examples 'returns the right status', 'disabled', :stopped
include_examples 'returns the right status', 'uninitialized', :stopped
include_examples 'returns the right status', 'maintenance', :maintenance
include_examples 'returns the right status', 'degraded', :degraded
it "raises a Puppet::Error if the svcs state is 'legacy_run'" do
states[:current] = 'legacy_run'
expect { @provider.status }.to raise_error do |error|
expect(error).to be_a(Puppet::Error)
expect(error.message).to match('legacy')
end
end
it "raises a Puppet::Error if the svcs state is unmanageable" do
states[:current] = 'unmanageable state'
expect { @provider.status }.to raise_error do |error|
expect(error).to be_a(Puppet::Error)
expect(error.message).to match(states[:current])
end
end
it "returns 'stopped' if the service does not exist" do
expect(@provider).to receive(:service_states).and_raise(Puppet::ExecutionFailure, 'service does not exist!')
expect(@provider.status).to eql(:stopped)
end
it "uses the current state for comparison if the next state is not provided" do
states[:next] = 'disabled'
expect(@provider.status).to eql(:stopped)
end
it "should return stopped for an incomplete service on Solaris 11" do
allow(Facter).to receive(:value).with(:operatingsystemrelease).and_return('11.3')
allow(@provider).to receive(:complete_service?).and_return(false)
allow(@provider).to receive(:svcs).with('-l', @provider.resource[:name]).and_return(File.read(my_fixture('svcs_fmri.out')))
expect(@provider.status).to eq(:stopped)
end
end
describe '#maybe_clear_service_then_svcadm' do
let(:fmri) { 'resource_fmri' }
before(:each) do
allow(@provider).to receive(:service_fmri).and_return(fmri)
end
it 'applies the svcadm subcommand with the given flags' do
expect(@provider).to receive(:adm).with('enable', '-rst', fmri)
@provider.maybe_clear_service_then_svcadm(:stopped, 'enable', '-rst')
end
[:maintenance, :degraded].each do |status|
it "clears the service before applying the svcadm subcommand if the service status is #{status}" do
expect(@provider).to receive(:adm).with('clear', fmri)
expect(@provider).to receive(:adm).with('enable', '-rst', fmri)
@provider.maybe_clear_service_then_svcadm(status, 'enable', '-rst')
end
end
end
describe '#flush' do
def mark_property_for_syncing(property, value)
properties_to_sync = @provider.instance_variable_get(:@properties_to_sync)
properties_to_sync[property] = value
end
it 'should noop if enable and ensure do not need to be syncd' do
expect(@provider).not_to receive(:setup_service)
@provider.flush
end
context 'enable or ensure need to be syncd' do
let(:stopped_states) do
['offline', 'disabled', 'uninitialized']
end
let(:fmri) { 'resource_fmri' }
let(:mock_status) { :maintenance }
before(:each) do
allow(@provider).to receive(:setup_service)
allow(@provider).to receive(:service_fmri).and_return(fmri)
# We will update this mock on a per-test basis.
allow(@provider).to receive(:status).and_return(mock_status)
allow(@provider).to receive(:wait)
end
context 'only ensure needs to be syncd' do
it 'stops the service if ensure == stopped' do
mark_property_for_syncing(:ensure, :stopped)
expect(@provider).to receive(:maybe_clear_service_then_svcadm).with(mock_status, 'disable', '-st')
expect(@provider).to receive(:wait).with(*stopped_states)
@provider.flush
end
it 'starts the service if ensure == running' do
mark_property_for_syncing(:ensure, :running)
expect(@provider).to receive(:maybe_clear_service_then_svcadm).with(mock_status, 'enable', '-rst')
expect(@provider).to receive(:wait).with('online')
@provider.flush
end
end
context 'enable needs to be syncd' do
before(:each) do
# We will stub this value out later, this default is useful
# for the final state tests.
mark_property_for_syncing(:enable, true)
end
it 'enables the service' do
mark_property_for_syncing(:enable, true)
expect(@provider).to receive(:maybe_clear_service_then_svcadm).with(mock_status, 'enable', '-rs')
expect(@provider).to receive(:adm).with('mark', '-I', 'maintenance', fmri)
@provider.flush
end
it 'disables the service' do
mark_property_for_syncing(:enable, false)
expect(@provider).to receive(:maybe_clear_service_then_svcadm).with(mock_status, 'disable', '-s')
expect(@provider).to receive(:adm).with('mark', '-I', 'maintenance', fmri)
@provider.flush
end
context 'when the final service state is running' do
before(:each) do
allow(@provider).to receive(:status).and_return(:running)
end
it 'starts the service if enable was false' do
mark_property_for_syncing(:enable, false)
expect(@provider).to receive(:adm).with('disable', '-s', fmri)
expect(@provider).to receive(:adm).with('enable', '-rst', fmri)
expect(@provider).to receive(:wait).with('online')
@provider.flush
end
it 'waits for the service to start if enable was true' do
mark_property_for_syncing(:enable, true)
expect(@provider).to receive(:adm).with('enable', '-rs', fmri)
expect(@provider).to receive(:wait).with('online')
@provider.flush
end
end
context 'when the final service state is stopped' do
before(:each) do
allow(@provider).to receive(:status).and_return(:stopped)
end
it 'stops the service if enable was true' do
mark_property_for_syncing(:enable, true)
expect(@provider).to receive(:adm).with('enable', '-rs', fmri)
expect(@provider).to receive(:adm).with('disable', '-st', fmri)
expect(@provider).to receive(:wait).with(*stopped_states)
@provider.flush
end
it 'waits for the service to stop if enable was false' do
mark_property_for_syncing(:enable, false)
expect(@provider).to_not receive(:adm).with('disable', '-st', fmri)
expect(@provider).to receive(:wait).with(*stopped_states)
@provider.flush
end
end
it 'marks the service as being under maintenance if the final state is maintenance' do
expect(@provider).to receive(:status).and_return(:maintenance)
expect(@provider).to receive(:adm).with('clear', fmri)
expect(@provider).to receive(:adm).with('enable', '-rs', fmri)
expect(@provider).to receive(:adm).with('mark', '-I', 'maintenance', fmri)
expect(@provider).to receive(:wait).with('maintenance')
@provider.flush
end
it 'uses the ensure value as the final state if ensure also needs to be syncd' do
mark_property_for_syncing(:ensure, :running)
expect(@provider).to receive(:status).and_return(:stopped)
expect(@provider).to receive(:adm).with('enable', '-rs', fmri)
expect(@provider).to receive(:wait).with('online')
@provider.flush
end
it 'marks the final state of a degraded service as running' do
expect(@provider).to receive(:status).and_return(:degraded)
expect(@provider).to receive(:adm).with('clear', fmri)
expect(@provider).to receive(:adm).with('enable', '-rs', fmri)
expect(@provider).to receive(:wait).with('online')
@provider.flush
end
end
end
end
end
|