Converted more tests to new vagrant env helpers and removed unused code in ResourceLogger
This commit is contained in:
parent
260f1dcec4
commit
f24094bba8
|
@ -1,5 +1,4 @@
|
||||||
require 'thread'
|
require 'thread'
|
||||||
require 'mario'
|
|
||||||
|
|
||||||
module Vagrant
|
module Vagrant
|
||||||
module Util
|
module Util
|
||||||
|
@ -12,14 +11,8 @@ module Vagrant
|
||||||
#
|
#
|
||||||
# This class is thread safe. The backing class which actually does
|
# This class is thread safe. The backing class which actually does
|
||||||
# all the logging IO is protected.
|
# all the logging IO is protected.
|
||||||
#
|
|
||||||
# This class also handles progress meters of multiple resources and
|
|
||||||
# handles all the proper interleaving and console updating to
|
|
||||||
# display the progress meters in a way which doesn't conflict with
|
|
||||||
# other incoming log messages.
|
|
||||||
class ResourceLogger
|
class ResourceLogger
|
||||||
@@singleton_logger = nil
|
@@singleton_logger = nil
|
||||||
@@progress_reporters = nil
|
|
||||||
@@writer_lock = Mutex.new
|
@@writer_lock = Mutex.new
|
||||||
|
|
||||||
# The resource which this logger represents.
|
# The resource which this logger represents.
|
||||||
|
@ -49,12 +42,6 @@ module Vagrant
|
||||||
def reset_singleton_logger!
|
def reset_singleton_logger!
|
||||||
@@singleton_logger = nil
|
@@singleton_logger = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the progress parts array which contains the various
|
|
||||||
# progress reporters.
|
|
||||||
def progress_reporters
|
|
||||||
@@progress_reporters ||= {}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(resource, env)
|
def initialize(resource, env)
|
||||||
|
@ -66,63 +53,9 @@ module Vagrant
|
||||||
[:debug, :info, :error, :fatal].each do |method|
|
[:debug, :info, :error, :fatal].each do |method|
|
||||||
define_method(method) do |message|
|
define_method(method) do |message|
|
||||||
@@writer_lock.synchronize do
|
@@writer_lock.synchronize do
|
||||||
# We clear the line in case progress reports have been going
|
|
||||||
# out.
|
|
||||||
print(cl_reset)
|
|
||||||
logger.send(method, "[#{resource}] #{message}")
|
logger.send(method, "[#{resource}] #{message}")
|
||||||
end
|
end
|
||||||
|
end
|
||||||
# Once again flush the progress reporters since we probably
|
|
||||||
# cleared any existing ones.
|
|
||||||
flush_progress
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Sets a progress report for the resource that this logger
|
|
||||||
# represents. This progress report is interleaved within the output.
|
|
||||||
def report_progress(progress, total, show_parts=true)
|
|
||||||
# Simply add the progress reporter to the list of progress
|
|
||||||
# reporters
|
|
||||||
self.class.progress_reporters[resource] = {
|
|
||||||
:progress => progress,
|
|
||||||
:total => total,
|
|
||||||
:show_parts => show_parts
|
|
||||||
}
|
|
||||||
|
|
||||||
# And force an update to occur
|
|
||||||
flush_progress
|
|
||||||
end
|
|
||||||
|
|
||||||
# Clears the progress report for this resource
|
|
||||||
def clear_progress
|
|
||||||
self.class.progress_reporters.delete(resource)
|
|
||||||
end
|
|
||||||
|
|
||||||
def flush_progress
|
|
||||||
# Don't do anything if there are no progress reporters
|
|
||||||
return if self.class.progress_reporters.length <= 0
|
|
||||||
|
|
||||||
@@writer_lock.synchronize do
|
|
||||||
reports = []
|
|
||||||
|
|
||||||
# First generate all the report percentages and output
|
|
||||||
self.class.progress_reporters.each do |name, data|
|
|
||||||
percent = (data[:progress].to_f / data[:total].to_f) * 100
|
|
||||||
line = "#{name}: #{percent.to_i}%"
|
|
||||||
line << " (#{data[:progress]} / #{data[:total]})" if data[:show_parts]
|
|
||||||
reports << line
|
|
||||||
end
|
|
||||||
|
|
||||||
# Output it to stdout
|
|
||||||
print "#{cl_reset}[progress] #{reports.join(" ")}"
|
|
||||||
$stdout.flush
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def cl_reset
|
|
||||||
reset = "\r"
|
|
||||||
reset += "\e[0K" unless Mario::Platform.windows?
|
|
||||||
reset
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,6 +20,8 @@ module VagrantTestHelpers
|
||||||
str = args.shift || ""
|
str = args.shift || ""
|
||||||
File.open(path.to_s, "w") do |f|
|
File.open(path.to_s, "w") do |f|
|
||||||
f.puts "Vagrant::Config.run do |config|"
|
f.puts "Vagrant::Config.run do |config|"
|
||||||
|
f.puts "config.vagrant.log_output = nil"
|
||||||
|
f.puts "config.vagrant.home = '#{home_path}'"
|
||||||
f.puts str
|
f.puts str
|
||||||
f.puts "end"
|
f.puts "end"
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,8 +1,21 @@
|
||||||
|
require 'fileutils'
|
||||||
|
|
||||||
module VagrantTestHelpers
|
module VagrantTestHelpers
|
||||||
module Path
|
module Path
|
||||||
# Path to the tmp directory for the tests
|
# Path to the tmp directory for the tests
|
||||||
def tmp_path
|
def tmp_path
|
||||||
Vagrant.source_root.join("test", "tmp")
|
Vagrant.source_root.join("test", "tmp")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Path to the "home" directory for the tests
|
||||||
|
def home_path
|
||||||
|
tmp_path.join("home")
|
||||||
|
end
|
||||||
|
|
||||||
|
# Cleans all the test temp paths
|
||||||
|
def clean_paths
|
||||||
|
FileUtils.rm_rf(tmp_path)
|
||||||
|
FileUtils.mkdir_p(tmp_path)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,11 +2,15 @@
|
||||||
$:.unshift(File.dirname(__FILE__))
|
$:.unshift(File.dirname(__FILE__))
|
||||||
|
|
||||||
require 'vagrant'
|
require 'vagrant'
|
||||||
|
require 'mario'
|
||||||
require 'contest'
|
require 'contest'
|
||||||
require 'mocha'
|
require 'mocha'
|
||||||
require 'support/path'
|
require 'support/path'
|
||||||
require 'support/environment'
|
require 'support/environment'
|
||||||
|
|
||||||
|
# Silence Mario by sending log output to black hole
|
||||||
|
Mario::Platform.logger(nil)
|
||||||
|
|
||||||
# Add the I18n locale for tests
|
# Add the I18n locale for tests
|
||||||
I18n.load_path << File.expand_path("../locales/en.yml", __FILE__)
|
I18n.load_path << File.expand_path("../locales/en.yml", __FILE__)
|
||||||
|
|
||||||
|
|
|
@ -373,7 +373,7 @@ class EnvironmentTest < Test::Unit::TestCase
|
||||||
|
|
||||||
context "loading home directory" do
|
context "loading home directory" do
|
||||||
setup do
|
setup do
|
||||||
@env = mock_environment
|
@env = vagrant_env
|
||||||
@home_dir = File.expand_path(@env.config.vagrant.home)
|
@home_dir = File.expand_path(@env.config.vagrant.home)
|
||||||
|
|
||||||
File.stubs(:directory?).returns(true)
|
File.stubs(:directory?).returns(true)
|
||||||
|
@ -398,33 +398,27 @@ class EnvironmentTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
context "loading box" do
|
context "loading box" do
|
||||||
setup do
|
|
||||||
@box = mock("box")
|
|
||||||
@box.stubs(:env=)
|
|
||||||
|
|
||||||
@env = mock_environment
|
|
||||||
@env.stubs(:root_path).returns("foo")
|
|
||||||
end
|
|
||||||
|
|
||||||
should "do nothing if the root path is nil" do
|
should "do nothing if the root path is nil" do
|
||||||
|
env = @klass.new(:cwd => "/")
|
||||||
Vagrant::Box.expects(:find).never
|
Vagrant::Box.expects(:find).never
|
||||||
@env.stubs(:root_path).returns(nil)
|
env.load_box!
|
||||||
@env.load_box!
|
|
||||||
end
|
end
|
||||||
|
|
||||||
should "not load the box if its not set" do
|
should "not load the box if its not set" do
|
||||||
@env = mock_environment do |config|
|
env = vagrant_env
|
||||||
config.vm.box = nil
|
assert env.config.vm.box.nil?
|
||||||
end
|
|
||||||
|
|
||||||
Vagrant::Box.expects(:find).never
|
Vagrant::Box.expects(:find).never
|
||||||
@env.load_box!
|
env.load_box!
|
||||||
end
|
end
|
||||||
|
|
||||||
should "set the box to what is found by the Box class" do
|
should "set the box to what is found by the Box class" do
|
||||||
Vagrant::Box.expects(:find).with(@env, @env.config.vm.box).once.returns(@box)
|
env = vagrant_env(vagrantfile("config.vm.box = 'foo'"))
|
||||||
@env.load_box!
|
|
||||||
assert @box.equal?(@env.box)
|
@box = mock("box")
|
||||||
|
@box.stubs(:env=)
|
||||||
|
Vagrant::Box.expects(:find).with(env, env.config.vm.box).once.returns(@box)
|
||||||
|
env.load_box!
|
||||||
|
assert @box.equal?(env.box)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -475,35 +469,29 @@ class EnvironmentTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
context "loading blank VMs" do
|
context "loading blank VMs" do
|
||||||
setup do
|
|
||||||
@env = mock_environment
|
|
||||||
end
|
|
||||||
|
|
||||||
should "blank the VMs" do
|
should "blank the VMs" do
|
||||||
@env = mock_environment do |config|
|
env = vagrant_env(vagrantfile(<<-vf))
|
||||||
config.vm.define :foo do |foo_config|
|
config.vm.define :foo
|
||||||
end
|
config.vm.define :bar
|
||||||
|
vf
|
||||||
|
|
||||||
config.vm.define :bar do |bar_config|
|
env.load_blank_vms!
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
@env.load_blank_vms!
|
assert_equal 2, env.vms.length
|
||||||
|
assert(env.vms.all? { |name, vm| !vm.created? })
|
||||||
|
|
||||||
assert_equal 2, @env.vms.length
|
sorted_vms = env.vms.keys.sort { |a,b| a.to_s <=> b.to_s }
|
||||||
assert(@env.vms.all? { |name, vm| !vm.created? })
|
|
||||||
|
|
||||||
sorted_vms = @env.vms.keys.sort { |a,b| a.to_s <=> b.to_s }
|
|
||||||
assert_equal [:bar, :foo], sorted_vms
|
assert_equal [:bar, :foo], sorted_vms
|
||||||
end
|
end
|
||||||
|
|
||||||
should "load the default VM blank if no multi-VMs are specified" do
|
should "load the default VM blank if no multi-VMs are specified" do
|
||||||
assert @env.config.vm.defined_vms.empty? # sanity
|
env = vagrant_env
|
||||||
|
assert env.config.vm.defined_vms.empty? # sanity
|
||||||
|
|
||||||
@env.load_blank_vms!
|
env.load_blank_vms!
|
||||||
|
|
||||||
assert_equal 1, @env.vms.length
|
assert_equal 1, env.vms.length
|
||||||
assert !@env.vms.values.first.created?
|
assert !env.vms.values.first.created?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -71,11 +71,6 @@ class ResourceLoggerUtilTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
context "logging methods" do
|
context "logging methods" do
|
||||||
setup do
|
|
||||||
@instance.stubs(:flush_progress)
|
|
||||||
@instance.stubs(:cl_reset).returns("")
|
|
||||||
end
|
|
||||||
|
|
||||||
[:debug, :info, :error, :fatal].each do |method|
|
[:debug, :info, :error, :fatal].each do |method|
|
||||||
should "log with the proper format on #{method}" do
|
should "log with the proper format on #{method}" do
|
||||||
message = "bar"
|
message = "bar"
|
||||||
|
@ -84,62 +79,5 @@ class ResourceLoggerUtilTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "reporting progress" do
|
|
||||||
setup do
|
|
||||||
@instance.stubs(:flush_progress)
|
|
||||||
end
|
|
||||||
|
|
||||||
should "flush progress" do
|
|
||||||
@instance.expects(:flush_progress).once
|
|
||||||
@instance.report_progress(72, 100)
|
|
||||||
end
|
|
||||||
|
|
||||||
should "add the reporter to the progress reporters" do
|
|
||||||
@instance.report_progress(72, 100)
|
|
||||||
assert @klass.progress_reporters.has_key?(@instance.resource)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "clearing progress" do
|
|
||||||
setup do
|
|
||||||
@instance.stubs(:flush_progress)
|
|
||||||
|
|
||||||
@klass.progress_reporters.clear
|
|
||||||
@instance.report_progress(72, 100)
|
|
||||||
end
|
|
||||||
|
|
||||||
should "remove the key from the reporters" do
|
|
||||||
assert @klass.progress_reporters.has_key?(@instance.resource)
|
|
||||||
@instance.clear_progress
|
|
||||||
assert !@klass.progress_reporters.has_key?(@instance.resource)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "command line reset" do
|
|
||||||
setup do
|
|
||||||
Mario::Platform.logger(nil)
|
|
||||||
end
|
|
||||||
|
|
||||||
context "on windows" do
|
|
||||||
setup do
|
|
||||||
Mario::Platform.forced = Mario::Platform::Windows7
|
|
||||||
end
|
|
||||||
|
|
||||||
should "just return \\r for the clear screen" do
|
|
||||||
assert_equal "\r", @instance.cl_reset
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "on other platforms" do
|
|
||||||
setup do
|
|
||||||
Mario::Platform.forced = Mario::Platform::Linux
|
|
||||||
end
|
|
||||||
|
|
||||||
should "return the full clear screen" do
|
|
||||||
assert_equal "\r\e[0K", @instance.cl_reset
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue