|
266 | 266 | end |
267 | 267 | end |
268 | 268 |
|
| 269 | + # Test Kitchen 4 asks the driver whether the instance is actually alive, for |
| 270 | + # `kitchen list --live` and the `kitchen status` alias. Drivers that do not |
| 271 | + # answer inherit "unknown" from Kitchen::Driver::Base. |
| 272 | + describe "#status" do |
| 273 | + # Kitchen::Instance#driver_status checks the arity before calling, and |
| 274 | + # silently reports "unknown" for a status method that takes no state. A |
| 275 | + # correct-looking implementation with the wrong signature is never called. |
| 276 | + it "accepts the Kitchen state hash" do |
| 277 | + expect(driver.method(:status).arity).to eq(1) |
| 278 | + end |
| 279 | + |
| 280 | + it "reports nothing created when the state file records no server" do |
| 281 | + expect(compute).not_to receive(:get_instance) |
| 282 | + |
| 283 | + expect(driver.status({})).to include(live: false, state: "not created") |
| 284 | + end |
| 285 | + |
| 286 | + context "with a running instance" do |
| 287 | + before do |
| 288 | + allow(compute).to receive(:get_instance).and_return(ComputeApi.instance(status: "RUNNING")) |
| 289 | + end |
| 290 | + |
| 291 | + it "reports it as live" do |
| 292 | + expect(driver.status(server_name: "tk-test-1", zone: "test-zone-1a")) |
| 293 | + .to include(live: true, state: "running") |
| 294 | + end |
| 295 | + |
| 296 | + it "identifies the instance it asked about" do |
| 297 | + expect(driver.status(server_name: "tk-test-1", zone: "test-zone-1a")) |
| 298 | + .to include(resource_id: "tk-test-1") |
| 299 | + end |
| 300 | + |
| 301 | + it "records when it checked, as an RFC 3339 timestamp" do |
| 302 | + checked_at = driver.status(server_name: "tk-test-1", zone: "test-zone-1a")[:checked_at] |
| 303 | + |
| 304 | + expect { Time.iso8601(checked_at) }.not_to raise_error |
| 305 | + end |
| 306 | + |
| 307 | + it "asks the zone recorded in the state file, not the configured one" do |
| 308 | + expect(compute).to receive(:get_instance) |
| 309 | + .with("test-project", "recorded-zone", "tk-test-1") |
| 310 | + .and_return(ComputeApi.instance) |
| 311 | + |
| 312 | + driver.status(server_name: "tk-test-1", zone: "recorded-zone") |
| 313 | + end |
| 314 | + end |
| 315 | + |
| 316 | + # A preemptible instance GCE has reclaimed still exists, and the state file |
| 317 | + # still says "Created". Reporting its real state is the point of the hook. |
| 318 | + context "with an instance that exists but is not running" do |
| 319 | + before do |
| 320 | + allow(compute).to receive(:get_instance).and_return(ComputeApi.instance(status: "TERMINATED")) |
| 321 | + end |
| 322 | + |
| 323 | + it "reports GCE's own state and does not call it live" do |
| 324 | + expect(driver.status(server_name: "tk-test-1", zone: "test-zone-1a")) |
| 325 | + .to include(live: false, state: "terminated") |
| 326 | + end |
| 327 | + end |
| 328 | + |
| 329 | + context "with a server recorded that no longer exists" do |
| 330 | + before { allow(compute).to receive(:get_instance).and_raise(ComputeApi.client_error) } |
| 331 | + |
| 332 | + it "reports it as gone rather than raising" do |
| 333 | + expect(driver.status(server_name: "tk-gone", zone: "test-zone-1a")) |
| 334 | + .to include(live: false, state: "not found") |
| 335 | + end |
| 336 | + |
| 337 | + it "says the state file is out of date" do |
| 338 | + expect(driver.status(server_name: "tk-gone", zone: "test-zone-1a")[:message]) |
| 339 | + .to match(/tk-gone/) |
| 340 | + end |
| 341 | + end |
| 342 | + end |
| 343 | + |
269 | 344 | describe "#generate_server_name" do |
270 | 345 | it "derives a name from the Test Kitchen instance name" do |
271 | 346 | expect(driver.generate_server_name).to match(/\Atk-default-ubuntu-2204-[0-9a-f]{6}\z/) |
|
0 commit comments