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
|
# Spec Tests for the Launchd provider
#
require 'spec_helper'
describe Puppet::Type.type(:service).provider(:launchd) do
let (:plistlib) { Puppet::Util::Plist }
let (:joblabel) { "com.foo.food" }
let (:provider) { subject.class }
let (:resource) { Puppet::Type.type(:service).new(:name => joblabel, :provider => :launchd) }
let (:launchd_overrides_6_9) { '/var/db/launchd.db/com.apple.launchd/overrides.plist' }
let (:launchd_overrides_10_) { '/var/db/com.apple.xpc.launchd/disabled.plist' }
subject { resource.provider }
if Puppet.features.microsoft_windows?
# Get a pid for $CHILD_STATUS to latch on to
command = "cmd.exe /c \"exit 0\""
Puppet::Util::Execution.execute(command, {:failonfail => false})
end
describe "the type interface" do
%w{ start stop enabled? enable disable status}.each do |method|
it { is_expected.to respond_to method.to_sym }
end
end
describe 'the status of the services' do
it "should call the external command 'launchctl list' once" do
provider.expects(:launchctl).with(:list).returns(joblabel)
provider.expects(:jobsearch).with(nil).returns({joblabel => "/Library/LaunchDaemons/#{joblabel}"})
provider.prefetch({})
end
it "should return stopped if not listed in launchctl list output" do
provider.expects(:launchctl).with(:list).returns('com.bar.is_running')
provider.expects(:jobsearch).with(nil).returns({'com.bar.is_not_running' => "/Library/LaunchDaemons/com.bar.is_not_running"})
expect(provider.prefetch({}).last.status).to eq :stopped
end
it "should return running if listed in launchctl list output" do
provider.expects(:launchctl).with(:list).returns('com.bar.is_running')
provider.expects(:jobsearch).with(nil).returns({'com.bar.is_running' => "/Library/LaunchDaemons/com.bar.is_running"})
expect(provider.prefetch({}).last.status).to eq :running
end
after :each do
provider.instance_variable_set(:@job_list, nil)
end
describe "when hasstatus is set to false" do
before :each do
resource[:hasstatus] = :false
end
it "should use the user-provided status command if present and return running if true" do
resource[:status] = '/bin/true'
subject.expects(:texecute).with(:status, ["/bin/true"], false).returns(0)
$CHILD_STATUS.stubs(:exitstatus).returns(0)
expect(subject.status).to eq(:running)
end
it "should use the user-provided status command if present and return stopped if false" do
resource[:status] = '/bin/false'
subject.expects(:texecute).with(:status, ["/bin/false"], false).returns(nil)
$CHILD_STATUS.stubs(:exitstatus).returns(1)
expect(subject.status).to eq(:stopped)
end
it "should fall back to getpid if no status command is provided" do
subject.expects(:getpid).returns(123)
expect(subject.status).to eq(:running)
end
end
end
[[10, '10.6'], [13, '10.9']].each do |kernel, version|
describe "when checking whether the service is enabled on OS X #{version}" do
it "should return true if the job plist says disabled is true and the global overrides says disabled is false" do
provider.expects(:get_os_version).returns(kernel).at_least_once
subject.expects(:plist_from_label).returns([joblabel, {"Disabled" => true}])
plistlib.expects(:read_plist_file).with(launchd_overrides_6_9).returns({joblabel => {"Disabled" => false}})
FileTest.expects(:file?).with(launchd_overrides_6_9).returns(true)
expect(subject.enabled?).to eq(:true)
end
it "should return false if the job plist says disabled is false and the global overrides says disabled is true" do
provider.expects(:get_os_version).returns(kernel).at_least_once
subject.expects(:plist_from_label).returns([joblabel, {"Disabled" => false}])
plistlib.expects(:read_plist_file).with(launchd_overrides_6_9).returns({joblabel => {"Disabled" => true}})
FileTest.expects(:file?).with(launchd_overrides_6_9).returns(true)
expect(subject.enabled?).to eq(:false)
end
it "should return true if the job plist and the global overrides have no disabled keys" do
provider.expects(:get_os_version).returns(kernel).at_least_once
subject.expects(:plist_from_label).returns([joblabel, {}])
plistlib.expects(:read_plist_file).with(launchd_overrides_6_9).returns({})
FileTest.expects(:file?).with(launchd_overrides_6_9).returns(true)
expect(subject.enabled?).to eq(:true)
end
end
end
describe "when checking whether the service is enabled on OS X 10.10" do
it "should return true if the job plist says disabled is true and the global overrides says disabled is false" do
provider.expects(:get_os_version).returns(14).at_least_once
subject.expects(:plist_from_label).returns([joblabel, {"Disabled" => true}])
plistlib.expects(:read_plist_file).with(launchd_overrides_10_).returns({joblabel => false})
FileTest.expects(:file?).with(launchd_overrides_10_).returns(true)
expect(subject.enabled?).to eq(:true)
end
it "should return false if the job plist says disabled is false and the global overrides says disabled is true" do
provider.expects(:get_os_version).returns(14).at_least_once
subject.expects(:plist_from_label).returns([joblabel, {"Disabled" => false}])
plistlib.expects(:read_plist_file).with(launchd_overrides_10_).returns({joblabel => true})
FileTest.expects(:file?).with(launchd_overrides_10_).returns(true)
expect(subject.enabled?).to eq(:false)
end
it "should return true if the job plist and the global overrides have no disabled keys" do
provider.expects(:get_os_version).returns(14).at_least_once
subject.expects(:plist_from_label).returns([joblabel, {}])
plistlib.expects(:read_plist_file).with(launchd_overrides_10_).returns({})
FileTest.expects(:file?).with(launchd_overrides_10_).returns(true)
expect(subject.enabled?).to eq(:true)
end
end
describe "when starting the service" do
it "should call any explicit 'start' command" do
resource[:start] = "/bin/false"
subject.expects(:texecute).with(:start, ["/bin/false"], true)
subject.start
end
it "should look for the relevant plist once" do
subject.expects(:plist_from_label).returns([joblabel, {}]).once
subject.expects(:enabled?).returns :true
subject.expects(:execute).with([:launchctl, :load, "-w", joblabel])
subject.start
end
it "should execute 'launchctl load' once without writing to the plist if the job is enabled" do
subject.expects(:plist_from_label).returns([joblabel, {}])
subject.expects(:enabled?).returns :true
subject.expects(:execute).with([:launchctl, :load, "-w", joblabel]).once
subject.start
end
it "should execute 'launchctl load' with writing to the plist once if the job is disabled" do
subject.expects(:plist_from_label).returns([joblabel, {}])
subject.expects(:enabled?).returns(:false)
subject.expects(:execute).with([:launchctl, :load, "-w", joblabel]).once
subject.start
end
it "should disable the job once if the job is disabled and should be disabled at boot" do
resource[:enable] = false
subject.expects(:plist_from_label).returns([joblabel, {"Disabled" => true}])
subject.expects(:enabled?).returns :false
subject.expects(:execute).with([:launchctl, :load, "-w", joblabel])
subject.expects(:disable).once
subject.start
end
it "(#2773) should execute 'launchctl load -w' if the job is enabled but stopped" do
subject.expects(:plist_from_label).returns([joblabel, {}])
subject.expects(:enabled?).returns(:true)
subject.expects(:status).returns(:stopped)
subject.expects(:execute).with([:launchctl, :load, '-w', joblabel])
subject.start
end
it "(#16271) Should stop and start the service when a restart is called" do
subject.expects(:stop)
subject.expects(:start)
subject.restart
end
end
describe "when stopping the service" do
it "should call any explicit 'stop' command" do
resource[:stop] = "/bin/false"
subject.expects(:texecute).with(:stop, ["/bin/false"], true)
subject.stop
end
it "should look for the relevant plist once" do
subject.expects(:plist_from_label).returns([joblabel, {}]).once
subject.expects(:enabled?).returns :true
subject.expects(:execute).with([:launchctl, :unload, '-w', joblabel])
subject.stop
end
it "should execute 'launchctl unload' once without writing to the plist if the job is disabled" do
subject.expects(:plist_from_label).returns([joblabel, {}])
subject.expects(:enabled?).returns :false
subject.expects(:execute).with([:launchctl, :unload, joblabel]).once
subject.stop
end
it "should execute 'launchctl unload' with writing to the plist once if the job is enabled" do
subject.expects(:plist_from_label).returns([joblabel, {}])
subject.expects(:enabled?).returns :true
subject.expects(:execute).with([:launchctl, :unload, '-w', joblabel]).once
subject.stop
end
it "should enable the job once if the job is enabled and should be enabled at boot" do
resource[:enable] = true
subject.expects(:plist_from_label).returns([joblabel, {"Disabled" => false}])
subject.expects(:enabled?).returns :true
subject.expects(:execute).with([:launchctl, :unload, "-w", joblabel])
subject.expects(:enable).once
subject.stop
end
end
describe "when enabling the service" do
it "should look for the relevant plist once" do ### Do we need this test? Differentiating it?
resource[:enable] = true
subject.expects(:plist_from_label).returns([joblabel, {}]).once
subject.expects(:enabled?).returns :false
subject.expects(:execute).with([:launchctl, :unload, joblabel])
subject.stop
end
it "should check if the job is enabled once" do
resource[:enable] = true
subject.expects(:plist_from_label).returns([joblabel, {}]).once
subject.expects(:enabled?).once
subject.expects(:execute).with([:launchctl, :unload, joblabel])
subject.stop
end
end
describe "when disabling the service" do
it "should look for the relevant plist once" do
resource[:enable] = false
subject.expects(:plist_from_label).returns([joblabel, {}]).once
subject.expects(:enabled?).returns :true
subject.expects(:execute).with([:launchctl, :unload, '-w', joblabel])
subject.stop
end
end
[[10, "10.6"], [13, "10.9"]].each do |kernel, version|
describe "when enabling the service on OS X #{version}" do
it "should write to the global launchd overrides file once" do
resource[:enable] = true
provider.expects(:get_os_version).returns(kernel).at_least_once
plistlib.expects(:read_plist_file).with(launchd_overrides_6_9).returns({})
plistlib.expects(:write_plist_file).with(has_entry(resource[:name], {'Disabled' => false}), launchd_overrides_6_9).once
subject.enable
end
end
describe "when disabling the service on OS X #{version}" do
it "should write to the global launchd overrides file once" do
resource[:enable] = false
provider.expects(:get_os_version).returns(kernel).at_least_once
plistlib.expects(:read_plist_file).with(launchd_overrides_6_9).returns({})
plistlib.expects(:write_plist_file).with(has_entry(resource[:name], {'Disabled' => true}), launchd_overrides_6_9).once
subject.disable
end
end
end
describe "when enabling the service on OS X 10.10" do
it "should write to the global launchd overrides file once" do
resource[:enable] = true
provider.expects(:get_os_version).returns(14).at_least_once
plistlib.expects(:read_plist_file).with(launchd_overrides_10_).returns({})
plistlib.expects(:write_plist_file).with(has_entry(resource[:name], false), launchd_overrides_10_).once
subject.enable
end
end
describe "when disabling the service on OS X 10.10" do
it "should write to the global launchd overrides file once" do
resource[:enable] = false
provider.expects(:get_os_version).returns(14).at_least_once
plistlib.expects(:read_plist_file).with(launchd_overrides_10_).returns({})
plistlib.expects(:write_plist_file).with(has_entry(resource[:name], true), launchd_overrides_10_).once
subject.disable
end
end
describe "make_label_to_path_map" do
before do
# clear out this class variable between runs
if provider.instance_variable_defined? :@label_to_path_map
provider.send(:remove_instance_variable, :@label_to_path_map)
end
end
describe "when encountering malformed plists" do
let(:plist_without_label) do
{
'LimitLoadToSessionType' => 'Aqua'
}
end
let(:busted_plist_path) { '/Library/LaunchAgents/org.busted.plist' }
let(:binary_plist_path) { '/Library/LaunchAgents/org.binary.plist' }
it "[17624] should warn that the plist in question is being skipped" do
provider.expects(:launchd_paths).returns(['/Library/LaunchAgents'])
provider.expects(:return_globbed_list_of_file_paths).with('/Library/LaunchAgents').returns([busted_plist_path])
plistlib.expects(:read_plist_file).with(busted_plist_path).returns(plist_without_label)
Puppet.expects(:debug).with("Reading launchd plist #{busted_plist_path}")
Puppet.expects(:debug).with("The #{busted_plist_path} plist does not contain a 'label' key; Puppet is skipping it")
provider.make_label_to_path_map
end
end
it "should return the cached value when available" do
provider.instance_variable_set(:@label_to_path_map, {'xx'=>'yy'})
expect(provider.make_label_to_path_map).to eq({'xx'=>'yy'})
end
describe "when successful" do
let(:launchd_dir) { '/Library/LaunchAgents' }
let(:plist) { launchd_dir + '/foo.bar.service.plist' }
let(:label) { 'foo.bar.service' }
before do
provider.instance_variable_set(:@label_to_path_map, nil)
provider.expects(:launchd_paths).returns([launchd_dir])
provider.expects(:return_globbed_list_of_file_paths).with(launchd_dir).returns([plist])
plistlib.expects(:read_plist_file).with(plist).returns({'Label'=>'foo.bar.service'})
end
it "should read the plists and return their contents" do
expect(provider.make_label_to_path_map).to eq({label=>plist})
end
it "should re-read the plists and return their contents when refreshed" do
provider.instance_variable_set(:@label_to_path_map, {'xx'=>'yy'})
expect(provider.make_label_to_path_map(true)).to eq({label=>plist})
end
end
end
describe "jobsearch" do
let(:map) { {"org.mozilla.puppet" => "/path/to/puppet.plist",
"org.mozilla.python" => "/path/to/python.plist"} }
it "returns the entire map with no args" do
provider.expects(:make_label_to_path_map).returns(map)
expect(provider.jobsearch).to eq(map)
end
it "returns a singleton hash when given a label" do
provider.expects(:make_label_to_path_map).returns(map)
expect(provider.jobsearch("org.mozilla.puppet")).to eq({ "org.mozilla.puppet" => "/path/to/puppet.plist" })
end
it "refreshes the label_to_path_map when label is not found" do
provider.expects(:make_label_to_path_map).with().returns({})
provider.expects(:make_label_to_path_map).with(true).returns(map)
expect(provider.jobsearch("org.mozilla.puppet")).to eq({ "org.mozilla.puppet" => "/path/to/puppet.plist" })
end
it "raises Puppet::Error when the label is still not found" do
provider.expects(:make_label_to_path_map).with().returns(map)
provider.expects(:make_label_to_path_map).with(true).returns(map)
expect { provider.jobsearch("NOSUCH") }.to raise_error(Puppet::Error)
end
end
describe "read_overrides" do
before do
Kernel.stubs(:sleep)
end
it "should read overrides" do
provider.expects(:read_plist).once.returns({})
expect(provider.read_overrides).to eq({})
end
it "should retry if read_plist fails" do
provider.expects(:read_plist).once.returns({})
provider.expects(:read_plist).once.returns(nil)
expect(provider.read_overrides).to eq({})
end
it "raises Puppet::Error after 20 attempts" do
provider.expects(:read_plist).times(20).returns(nil)
expect { provider.read_overrides }.to raise_error(Puppet::Error)
end
end
end
|