From 4cffa15d4e2f5326da2c3a54e795f022dc998a4e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 31 Jan 2010 01:27:18 -0800 Subject: [PATCH] hobo-down and Env.require_persisted_vm --- bin/hobo-down | 26 ++++++++++++++++++++++++++ lib/hobo/env.rb | 12 ++++++++++++ lib/hobo/vm.rb | 8 ++++++++ test/hobo/env_test.rb | 29 +++++++++++++++++++++++------ test/hobo/vm_test.rb | 18 ++++++++++++++++++ 5 files changed, 87 insertions(+), 6 deletions(-) create mode 100755 bin/hobo-down diff --git a/bin/hobo-down b/bin/hobo-down new file mode 100755 index 000000000..7f1cfc931 --- /dev/null +++ b/bin/hobo-down @@ -0,0 +1,26 @@ +#!/usr/bin/env ruby +begin + require File.expand_path(File.join(File.dirname(__FILE__), "../vendor/gems/ruby/1.8/environment")) +rescue LoadError +end + +require 'git-style-binary/command' + +# Get hobo +hobodir = File.join(File.dirname(__FILE__), '..', 'lib') +$:.unshift(hobodir) unless $:.include?(hobodir) +require 'hobo' + +GitStyleBinary.command do + short_desc "destroys the hobo environment" + banner <<-EOS +Usage: #{command.full_name} #{all_options_string} + +Destroys the hobo environment. + +EOS + + run do |command| + Hobo::VM.down + end +end \ No newline at end of file diff --git a/lib/hobo/env.rb b/lib/hobo/env.rb index 0790630fc..d68d13eca 100644 --- a/lib/hobo/env.rb +++ b/lib/hobo/env.rb @@ -80,6 +80,18 @@ msg load_root_path!(path.parent) end + def require_persisted_vm + if !persisted_vm + error_and_exit(<<-error) +The task you're trying to run requires that the hobo environment +already be created, but unfortunately this hobo still appears to +have no box! You can setup the environment by setting up your +Hobofile and running `hobo up` +error + return + end + end + def error_and_exit(error) puts <<-error ===================================================================== diff --git a/lib/hobo/vm.rb b/lib/hobo/vm.rb index 9774f62ab..c5e79682b 100644 --- a/lib/hobo/vm.rb +++ b/lib/hobo/vm.rb @@ -11,6 +11,14 @@ module Hobo setup_shared_folder(vm) end + # Tear down a virtual machine. + def down + Env.require_persisted_vm + + HOBO_LOGGER.info "Destroying VM and associated drives..." + Env.persisted_vm.destroy(:destroy_image => true) + end + def import HOBO_LOGGER.info "Importing base VM (#{Hobo.config[:vm][:base]})..." VirtualBox::VM.import(File.expand_path(Hobo.config[:vm][:base])) diff --git a/test/hobo/env_test.rb b/test/hobo/env_test.rb index 861f69098..7cee5bf30 100644 --- a/test/hobo/env_test.rb +++ b/test/hobo/env_test.rb @@ -14,10 +14,32 @@ class EnvTest < Test::Unit::TestCase "#{dir}/#{hobo_mock_config[:dotfile_name]}" end + def mock_persisted_vm(returnvalue="foovm") + filemock = mock("filemock") + filemock.expects(:read).returns("foo") + VirtualBox::VM.expects(:find).with("foo").returns(returnvalue) + File.expects(:open).with(Hobo::Env.dotfile_path).once.yields(filemock) + Hobo::Env.load_vm! + end + setup do Hobo::Env.stubs(:error_and_exit) end + context "requiring a VM" do + should "error and exit if no persisted VM was found" do + assert_nil Hobo::Env.persisted_vm + Hobo::Env.expects(:error_and_exit).once + Hobo::Env.require_persisted_vm + end + + should "return and continue if persisted VM is found" do + mock_persisted_vm + Hobo::Env.expects(:error_and_exit).never + Hobo::Env.require_persisted_vm + end + end + context "initial load" do test "load! should load the config and set the persisted_uid" do Hobo::Env.expects(:load_config!).once @@ -101,12 +123,7 @@ class EnvTest < Test::Unit::TestCase end test "loading of the uuid from the dotfile" do - VirtualBox::VM.expects(:find).with("foo").returns("foovm") - - filemock = mock("filemock") - filemock.expects(:read).returns("foo") - File.expects(:open).with(Hobo::Env.dotfile_path).once.yields(filemock) - Hobo::Env.load_vm! + mock_persisted_vm assert_equal 'foovm', Hobo::Env.persisted_vm end diff --git a/test/hobo/vm_test.rb b/test/hobo/vm_test.rb index 7ba4ae402..b319d05a9 100644 --- a/test/hobo/vm_test.rb +++ b/test/hobo/vm_test.rb @@ -6,6 +6,24 @@ class VMTest < Test::Unit::TestCase Hobo.config!(hobo_mock_config) end + context "hobo down" do + setup do + @persisted_vm = mock("persisted_vm") + @persisted_vm.stubs(:destroy) + Hobo::Env.stubs(:persisted_vm).returns(@persisted_vm) + end + + should "require a persisted VM" do + Hobo::Env.expects(:require_persisted_vm).once + Hobo::VM.down + end + + should "destroy the persisted VM and the VM image" do + @persisted_vm.expects(:destroy).with(:destroy_image => true).once + Hobo::VM.down + end + end + context "hobo up" do should "create the instance in the proper order" do create_seq = sequence("create_seq")