Refactor the "all or single" behavior of many commands into the base.

This commit is contained in:
Mitchell Hashimoto 2010-05-21 23:38:44 -07:00
parent dd7f471a3b
commit 12b8ab4516
14 changed files with 53 additions and 216 deletions

View File

@ -94,6 +94,23 @@ module Vagrant
# Methods below are not meant to be overriden/implemented by subclasses
#-------------------------------------------------------------------
# Parses the options for a given command and if a name was
# given, it calls the single method, otherwise it calls the all
# method. This helper is an abstraction which allows commands to
# easily be used in both regular and multi-VM environments.
def all_or_single(args, method_prefix)
args = parse_options(args)
single_method = "#{method_prefix}_single".to_sym
if args[0]
send(single_method, args[0])
else
env.vms.keys.each do |name|
send(single_method, name)
end
end
end
# Shows the version
def puts_version
File.open(File.join(PROJECT_ROOT, "VERSION"), "r") do |f|

View File

@ -11,13 +11,7 @@ module Vagrant
description "Destroys the vagrant environment"
def execute(args=[])
args = parse_options(args)
if args[0]
destroy_single(args[0])
else
destroy_all
end
all_or_single(args, :destroy)
end
# Destroys a single VM by name.
@ -35,13 +29,6 @@ module Vagrant
end
end
# Destroys all VMs represented by the current environment.
def destroy_all
env.vms.each do |name, vm|
destroy_single(name)
end
end
def options_spec(opts)
opts.banner = "Usage: vagrant destroy"
end

View File

@ -11,13 +11,7 @@ module Vagrant
description "Halts the currently running vagrant environment"
def execute(args=[])
args = parse_options(args)
if args[0]
halt_single(args[0])
else
halt_all
end
all_or_single(args, :halt)
end
def halt_single(name)
@ -34,12 +28,6 @@ module Vagrant
end
end
def halt_all
env.vms.keys.each do |name|
halt_single(name)
end
end
def options_spec(opts)
opts.banner = "Usage: vagrant halt"

View File

@ -10,13 +10,7 @@ module Vagrant
description "Reload the vagrant environment"
def execute(args=[])
args = parse_options(args)
if args[0]
reload_single(args[0])
else
reload_all
end
all_or_single(args, :reload)
end
def reload_single(name)
@ -33,12 +27,6 @@ module Vagrant
end
end
def reload_all
env.vms.keys.each do |name|
reload_single(name)
end
end
def options_spec(opts)
opts.banner = "Usage: vagrant reload"
end

View File

@ -10,13 +10,7 @@ module Vagrant
description "Resumes a suspend vagrant environment"
def execute(args=[])
args = parse_options(args)
if args[0]
resume_single(args[0])
else
resume_all
end
all_or_single(args, :resume)
end
def resume_single(name)
@ -33,12 +27,6 @@ module Vagrant
end
end
def resume_all
env.vms.keys.each do |name|
resume_single(name)
end
end
def options_spec(opts)
opts.banner = "Usage: vagrant resume"
end

View File

@ -11,13 +11,7 @@ module Vagrant
description "Suspends the currently running vagrant environment"
def execute(args=[])
args = parse_options(args)
if args[0]
suspend_single(args[0])
else
suspend_all
end
all_or_single(args, :suspend)
end
def suspend_single(name)
@ -34,12 +28,6 @@ module Vagrant
end
end
def suspend_all
env.vms.keys.each do |name|
suspend_single(name)
end
end
def options_spec(opts)
opts.banner = "Usage: vagrant suspend"
end

View File

@ -9,13 +9,7 @@ module Vagrant
description "Creates the vagrant environment"
def execute(args=[])
args = parse_options(args)
if args[0]
up_single(args[0])
else
up_all
end
all_or_single(args, :up)
end
def up_single(name)
@ -36,16 +30,6 @@ module Vagrant
end
end
def up_all
# First verify that all VMs have valid boxes
env.vms.each { |name, vm| vm.env.require_box unless vm.created? }
# Next, handle each VM
env.vms.keys.each do |name|
up_single(name)
end
end
def options_spec(opts)
opts.banner = "Usage: vagrant up"
end

View File

@ -78,6 +78,24 @@ class CommandsBaseTest < Test::Unit::TestCase
end
end
context "all or single methods" do
should "call the single method if a name is given" do
name = "bar"
@instance.expects(:foo_single).with(name).once
@instance.all_or_single(["bar"], :foo)
end
should "call the single method for each VM if no name is given" do
vms = { :foo => nil, :bar => nil }
vms.keys.each do |name|
@instance.expects(:foo_single).with(name).once
end
@env.stubs(:vms).returns(vms)
@instance.all_or_single([], :foo)
end
end
context "getting the option parser" do
should "create it with the options spec if it hasn't been created yet" do
opts = mock("opts")

View File

@ -10,28 +10,10 @@ class CommandsDestroyTest < Test::Unit::TestCase
end
context "executing" do
should "call destroy_all if no name is given" do
@instance.expects(:destroy_all).once
should "call all or single for the method" do
@instance.expects(:all_or_single).with([], :destroy)
@instance.execute
end
should "call destroy_single if a name is given" do
@instance.expects(:destroy_single).with("foo").once
@instance.execute(["foo"])
end
end
context "destroying all" do
should "destroy each VM" do
vms = { :foo => nil, :bar => nil, :baz => nil }
@env.stubs(:vms).returns(vms)
vms.each do |name, value|
@instance.expects(:destroy_single).with(name).once
end
@instance.destroy_all
end
end
context "destroying a single VM" do

View File

@ -9,28 +9,10 @@ class CommandsHaltTest < Test::Unit::TestCase
end
context "executing" do
should "call halt_all if no name is given" do
@instance.expects(:halt_all).once
should "call all or single for the method" do
@instance.expects(:all_or_single).with([], :halt)
@instance.execute
end
should "call halt_single if a name is given" do
@instance.expects(:halt_single).with("foo").once
@instance.execute(["foo"])
end
end
context "halting all" do
should "halt each VM" do
vms = { :foo => nil, :bar => nil, :baz => nil }
@env.stubs(:vms).returns(vms)
vms.each do |name, value|
@instance.expects(:halt_single).with(name).once
end
@instance.halt_all
end
end
context "halting a single VM" do

View File

@ -9,28 +9,10 @@ class CommandsReloadTest < Test::Unit::TestCase
end
context "executing" do
should "call on all if no name is given" do
@instance.expects(:reload_all).once
should "call all or single for the method" do
@instance.expects(:all_or_single).with([], :reload)
@instance.execute
end
should "call on single if a name is given" do
@instance.expects(:reload_single).with("foo").once
@instance.execute(["foo"])
end
end
context "reloading all" do
should "reload each VM" do
vms = { :foo => nil, :bar => nil, :baz => nil }
@env.stubs(:vms).returns(vms)
vms.each do |name, value|
@instance.expects(:reload_single).with(name).once
end
@instance.reload_all
end
end
context "reloading a single VM" do

View File

@ -9,28 +9,10 @@ class CommandsResumeTest < Test::Unit::TestCase
end
context "executing" do
should "call on all if no name is given" do
@instance.expects(:resume_all).once
should "call all or single for the method" do
@instance.expects(:all_or_single).with([], :resume)
@instance.execute
end
should "call on single if a name is given" do
@instance.expects(:resume_single).with("foo").once
@instance.execute(["foo"])
end
end
context "resume all" do
should "resume each VM" do
vms = { :foo => nil, :bar => nil, :baz => nil }
@env.stubs(:vms).returns(vms)
vms.each do |name, value|
@instance.expects(:resume_single).with(name).once
end
@instance.resume_all
end
end
context "resuming a single VM" do

View File

@ -9,28 +9,10 @@ class CommandsSuspendTest < Test::Unit::TestCase
end
context "executing" do
should "call on all if no name is given" do
@instance.expects(:suspend_all).once
should "call all or single for the method" do
@instance.expects(:all_or_single).with([], :suspend)
@instance.execute
end
should "call on single if a name is given" do
@instance.expects(:suspend_single).with("foo").once
@instance.execute(["foo"])
end
end
context "suspending all" do
should "suspend each VM" do
vms = { :foo => nil, :bar => nil, :baz => nil }
@env.stubs(:vms).returns(vms)
vms.each do |name, value|
@instance.expects(:suspend_single).with(name).once
end
@instance.suspend_all
end
end
context "suspending a single VM" do

View File

@ -9,15 +9,10 @@ class CommandsUpTest < Test::Unit::TestCase
end
context "executing" do
should "call up_all if no name is given" do
@instance.expects(:up_all).once
should "call all or single for the method" do
@instance.expects(:all_or_single).with([], :up)
@instance.execute
end
should "call up_single if a name is given" do
@instance.expects(:up_single).with("foo").once
@instance.execute(["foo"])
end
end
context "upping a single VM" do
@ -49,30 +44,4 @@ class CommandsUpTest < Test::Unit::TestCase
@instance.up_single(:foo)
end
end
context "upping all VMs" do
setup do
@vms = {}
@env.stubs(:vms).returns(@vms)
end
def create_vm
vm = mock("vm")
vm.stubs(:env).returns(mock_environment)
vm.stubs(:created?).returns(false)
vm
end
should "require a box for all VMs" do
@vms[:foo] = create_vm
@vms[:bar] = create_vm
@vms.each do |name, vm|
vm.env.expects(:require_box).once
@instance.expects(:up_single).with(name).once
end
@instance.up_all
end
end
end