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
|
#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/agent'
require 'puppet/configurer'
class AgentTestClient
def run
# no-op
end
def stop
# no-op
end
end
def without_warnings
flag = $VERBOSE
$VERBOSE = nil
yield
$VERBOSE = flag
end
describe Puppet::Agent do
before do
Puppet::Status.indirection.stubs(:find).returns Puppet::Status.new("version" => Puppet.version)
@agent = Puppet::Agent.new(AgentTestClient, false)
# So we don't actually try to hit the filesystem.
@agent.stubs(:lock).yields
# make Puppet::Application safe for stubbing; restore in an :after block; silence warnings for this.
without_warnings { Puppet::Application = Class.new(Puppet::Application) }
Puppet::Application.stubs(:clear?).returns(true)
Puppet::Application.class_eval do
class << self
def controlled_run(&block)
block.call
end
end
end
end
after do
# restore Puppet::Application from stub-safe subclass, and silence warnings
without_warnings { Puppet::Application = Puppet::Application.superclass }
end
it "should set its client class at initialization" do
expect(Puppet::Agent.new("foo", false).client_class).to eq("foo")
end
it "should include the Locker module" do
expect(Puppet::Agent.ancestors).to be_include(Puppet::Agent::Locker)
end
it "should create an instance of its client class and run it when asked to run" do
client = mock 'client'
AgentTestClient.expects(:new).returns client
client.expects(:run)
@agent.stubs(:disabled?).returns false
@agent.run
end
it "should initialize the client's transaction_uuid if passed as a client_option" do
client = mock 'client'
transaction_uuid = 'foo'
AgentTestClient.expects(:new).with(anything, transaction_uuid).returns client
client.expects(:run)
@agent.stubs(:disabled?).returns false
@agent.run(:transaction_uuid => transaction_uuid)
end
it "should be considered running if the lock file is locked" do
lockfile = mock 'lockfile'
@agent.expects(:lockfile).returns(lockfile)
lockfile.expects(:locked?).returns true
expect(@agent).to be_running
end
describe "when being run" do
before do
AgentTestClient.stubs(:lockfile_path).returns "/my/lock"
@agent.stubs(:disabled?).returns false
end
it "should splay" do
@agent.expects(:splay)
@agent.run
end
it "should do nothing if disabled" do
@agent.expects(:disabled?).returns(true)
AgentTestClient.expects(:new).never
@agent.run
end
it "(#11057) should notify the user about why a run is skipped" do
Puppet::Application.stubs(:controlled_run).returns(false)
Puppet::Application.stubs(:run_status).returns('MOCK_RUN_STATUS')
# This is the actual test that we inform the user why the run is skipped.
# We assume this information is contained in
# Puppet::Application.run_status
Puppet.expects(:notice).with(regexp_matches(/MOCK_RUN_STATUS/))
@agent.run
end
it "should display an informative message if the agent is administratively disabled" do
@agent.expects(:disabled?).returns true
@agent.expects(:disable_message).returns "foo"
Puppet.expects(:notice).with(regexp_matches(/Skipping run of .*; administratively disabled.*\(Reason: 'foo'\)/))
@agent.run
end
it "should use Puppet::Application.controlled_run to manage process state behavior" do
calls = sequence('calls')
Puppet::Application.expects(:controlled_run).yields.in_sequence(calls)
AgentTestClient.expects(:new).once.in_sequence(calls)
@agent.run
end
it "should not fail if a client class instance cannot be created" do
AgentTestClient.expects(:new).raises "eh"
Puppet.expects(:err)
@agent.run
end
it "should not fail if there is an exception while running its client" do
client = AgentTestClient.new
AgentTestClient.expects(:new).returns client
client.expects(:run).raises "eh"
Puppet.expects(:err)
@agent.run
end
it "should use a filesystem lock to restrict multiple processes running the agent" do
client = AgentTestClient.new
AgentTestClient.expects(:new).returns client
@agent.expects(:lock)
client.expects(:run).never # if it doesn't run, then we know our yield is what triggers it
@agent.run
end
it "should make its client instance available while running" do
client = AgentTestClient.new
AgentTestClient.expects(:new).returns client
client.expects(:run).with { expect(@agent.client).to equal(client); true }
@agent.run
end
it "should run the client instance with any arguments passed to it" do
client = AgentTestClient.new
AgentTestClient.expects(:new).returns client
client.expects(:run).with(:pluginsync => true, :other => :options)
@agent.run(:other => :options)
end
it "should return the agent result" do
client = AgentTestClient.new
AgentTestClient.expects(:new).returns client
@agent.expects(:lock).returns(:result)
expect(@agent.run).to eq(:result)
end
describe "when should_fork is true", :if => Puppet.features.posix? do
before do
@agent = Puppet::Agent.new(AgentTestClient, true)
# So we don't actually try to hit the filesystem.
@agent.stubs(:lock).yields
Kernel.stubs(:fork)
Process.stubs(:waitpid2).returns [123, (stub 'process::status', :exitstatus => 0)]
@agent.stubs(:exit)
end
it "should run the agent in a forked process" do
client = AgentTestClient.new
AgentTestClient.expects(:new).returns client
client.expects(:run)
Kernel.expects(:fork).yields
@agent.run
end
it "should exit child process if child exit" do
client = AgentTestClient.new
AgentTestClient.expects(:new).returns client
client.expects(:run).raises(SystemExit)
Kernel.expects(:fork).yields
@agent.expects(:exit).with(-1)
@agent.run
end
it "should re-raise exit happening in the child" do
Process.stubs(:waitpid2).returns [123, (stub 'process::status', :exitstatus => -1)]
expect { @agent.run }.to raise_error(SystemExit)
end
it "should re-raise NoMoreMemory happening in the child" do
Process.stubs(:waitpid2).returns [123, (stub 'process::status', :exitstatus => -2)]
expect { @agent.run }.to raise_error(NoMemoryError)
end
it "should return the child exit code" do
Process.stubs(:waitpid2).returns [123, (stub 'process::status', :exitstatus => 777)]
expect(@agent.run).to eq(777)
end
it "should return the block exit code as the child exit code" do
Kernel.expects(:fork).yields
@agent.expects(:exit).with(777)
@agent.run_in_fork {
777
}
end
end
describe "on Windows", :if => Puppet.features.microsoft_windows? do
it "should never fork" do
agent = Puppet::Agent.new(AgentTestClient, true)
expect(agent.should_fork).to be_falsey
end
end
end
describe "when checking execution state" do
describe 'with regular run status' do
before :each do
Puppet::Application.stubs(:restart_requested?).returns(false)
Puppet::Application.stubs(:stop_requested?).returns(false)
Puppet::Application.stubs(:interrupted?).returns(false)
Puppet::Application.stubs(:clear?).returns(true)
end
it 'should be false for :stopping?' do
expect(@agent.stopping?).to be_falsey
end
it 'should be false for :needing_restart?' do
expect(@agent.needing_restart?).to be_falsey
end
end
describe 'with a stop requested' do
before :each do
Puppet::Application.stubs(:clear?).returns(false)
Puppet::Application.stubs(:restart_requested?).returns(false)
Puppet::Application.stubs(:stop_requested?).returns(true)
Puppet::Application.stubs(:interrupted?).returns(true)
end
it 'should be true for :stopping?' do
expect(@agent.stopping?).to be_truthy
end
it 'should be false for :needing_restart?' do
expect(@agent.needing_restart?).to be_falsey
end
end
describe 'with a restart requested' do
before :each do
Puppet::Application.stubs(:clear?).returns(false)
Puppet::Application.stubs(:restart_requested?).returns(true)
Puppet::Application.stubs(:stop_requested?).returns(false)
Puppet::Application.stubs(:interrupted?).returns(true)
end
it 'should be false for :stopping?' do
expect(@agent.stopping?).to be_falsey
end
it 'should be true for :needing_restart?' do
expect(@agent.needing_restart?).to be_truthy
end
end
end
end
|