forked from hashicorp/vagrant
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a check for VM accessibility before every action
- Loading branch information
Showing
6 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Vagrant | ||
class Action | ||
module VM | ||
class CheckAccessible | ||
def initialize(app, env) | ||
@app = app | ||
end | ||
|
||
def call(env) | ||
if env["vm"] && env["vm"].created? && !env["vm"].vm.accessible? | ||
# The VM we are attempting to manipulate is inaccessible. This | ||
# is a very bad situation and can only be fixed by the user. It | ||
# also prohibits us from actually doing anything with the virtual | ||
# machine, so we raise an error. | ||
raise Errors::VMInaccessible | ||
end | ||
|
||
@app.call(env) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require 'test_helper' | ||
|
||
class CheckAccessibleVMActionTest < Test::Unit::TestCase | ||
setup do | ||
@klass = Vagrant::Action::VM::CheckAccessible | ||
end | ||
|
||
context "calling" do | ||
setup do | ||
@app, @env = action_env | ||
@instance = @klass.new(@app, @env) | ||
end | ||
|
||
should "continue up the chain if the VM is nil" do | ||
@env["vm"] = nil | ||
|
||
@app.expects(:call).once | ||
|
||
assert_nothing_raised { | ||
@instance.call(@env) | ||
} | ||
end | ||
|
||
should "continue up the chain if the VM is not created" do | ||
@env["vm"] = mock("vm") | ||
@env["vm"].stubs(:created?).returns(false) | ||
|
||
@app.expects(:call).once | ||
|
||
assert_nothing_raised { | ||
@instance.call(@env) | ||
} | ||
end | ||
|
||
should "continue up the chain if the VM is created and accessible" do | ||
@env["vm"] = mock("vm") | ||
@env["vm"].stubs(:created?).returns(true) | ||
@env["vm"].stubs(:vm).returns(mock("real_vm")) | ||
@env["vm"].vm.stubs(:accessible?).returns(true) | ||
|
||
@app.expects(:call).once | ||
|
||
assert_nothing_raised { | ||
@instance.call(@env) | ||
} | ||
end | ||
|
||
should "fail if the VM is not accessible" do | ||
@env["vm"] = mock("vm") | ||
@env["vm"].stubs(:created?).returns(true) | ||
@env["vm"].stubs(:vm).returns(mock("real_vm")) | ||
@env["vm"].vm.stubs(:accessible?).returns(false) | ||
|
||
@app.expects(:call).never | ||
|
||
assert_raises(Vagrant::Errors::VMInaccessible) { | ||
@instance.call(@env) | ||
} | ||
end | ||
end | ||
end |