Port collision with SSH test.

This commit is contained in:
Mitchell Hashimoto 2011-12-24 22:25:02 -08:00
parent 1f0720dfc2
commit 919f7adaa3
6 changed files with 62 additions and 17 deletions

View File

@ -25,7 +25,7 @@ end
# Enable logging if requested # Enable logging if requested
if ENV["ACCEPTANCE_LOG"] if ENV["ACCEPTANCE_LOG"]
logger = Log4r::Logger.new("acceptance") logger = Log4r::Logger.new("test")
logger.outputters = Log4r::Outputter.stdout logger.outputters = Log4r::Outputter.stdout
logger.level = Log4r.const_get(ENV["ACCEPTANCE_LOG"].upcase) logger.level = Log4r.const_get(ENV["ACCEPTANCE_LOG"].upcase)
logger = nil logger = nil

View File

@ -6,15 +6,23 @@ require "uri"
require "vagrant/util/retryable" require "vagrant/util/retryable"
require "acceptance/support/shared/command_examples" require "acceptance/support/shared/command_examples"
require "support/tempdir"
describe "vagrant port forwarding" do describe "vagrant port forwarding" do
include Vagrant::Util::Retryable include Vagrant::Util::Retryable
include_context "acceptance" include_context "acceptance"
it "forwards ports properly" do def initialize_environment(env=nil)
require_box("default") require_box("default")
env ||= environment
env.execute("vagrant", "box", "add", "base", box_path("default")).should succeed
end
it "forwards ports properly" do
initialize_environment
guest_port = 3000 guest_port = 3000
host_port = 5000 host_port = 5000
@ -27,7 +35,6 @@ end
VFILE VFILE
end end
assert_execute("vagrant", "box", "add", "base", box_path("default"))
assert_execute("vagrant", "up") assert_execute("vagrant", "up")
thr = nil thr = nil
@ -49,4 +56,36 @@ VFILE
thr.kill if thr thr.kill if thr
end end
end end
it "detects and corrects port collisions" do
# The two environments need to share a VBOX_USER_HOME so that the
# VM's go into the same place.
env_vars = { "VBOX_USER_HOME" => Tempdir.new("vagrant").to_s }
environment = new_environment(env_vars)
environment2 = new_environment(env_vars)
# For this test we create two isolated environments and `vagrant up`
# in each. SSH would collide, so this verifies that it won't!
begin
initialize_environment(environment)
initialize_environment(environment2)
# Build both environments up.
environment.execute("vagrant", "init").should succeed
environment.execute("vagrant", "up").should succeed
environment2.execute("vagrant", "init").should succeed
environment2.execute("vagrant", "up").should succeed
# Touch files in both environments
environment.execute("vagrant", "ssh", "-c", "touch /vagrant/foo").should succeed
environment2.execute("vagrant", "ssh", "-c", "touch /vagrant/bar").should succeed
# Verify that the files exist in each folder, properly
environment.workdir.join("foo").exist?.should be
environment2.workdir.join("bar").exist?.should be
ensure
environment.close
environment2.close
end
end
end end

View File

@ -11,7 +11,7 @@ module Acceptance
attr_reader :box_directory attr_reader :box_directory
def initialize(path) def initialize(path)
@logger = Log4r::Logger.new("acceptance::config") @logger = Log4r::Logger.new("test::acceptance::config")
@logger.info("Loading configuration from: #{path}") @logger.info("Loading configuration from: #{path}")
options = YAML.load_file(path) options = YAML.load_file(path)
@logger.info("Loaded: #{options.inspect}") @logger.info("Loaded: #{options.inspect}")

View File

@ -14,15 +14,15 @@ module Acceptance
def initialize(apps=nil, env=nil) def initialize(apps=nil, env=nil)
super() super()
@logger = Log4r::Logger.new("acceptance::isolated_environment") @logger = Log4r::Logger.new("test::acceptance::isolated_environment")
@apps = apps || {} @apps = apps.clone || {}
@env = env || {} @env = env.clone || {}
# Set the home directory and virtualbox home directory environmental # Set the home directory and virtualbox home directory environmental
# variables so that Vagrant and VirtualBox see the proper paths here. # variables so that Vagrant and VirtualBox see the proper paths here.
@env["HOME"] = @homedir.to_s @env["HOME"] ||= @homedir.to_s
@env["VBOX_USER_HOME"] = @homedir.to_s @env["VBOX_USER_HOME"] ||= @homedir.to_s
end end
# Executes a command in the context of this isolated environment. # Executes a command in the context of this isolated environment.
@ -32,8 +32,6 @@ module Acceptance
# Create the command # Create the command
command = replace_command(command) command = replace_command(command)
@logger.info("Executing: #{[command].concat(argN).inspect}")
# Determine the options # Determine the options
options = argN.last.is_a?(Hash) ? argN.pop : {} options = argN.last.is_a?(Hash) ? argN.pop : {}
options = { options = {
@ -45,6 +43,7 @@ module Acceptance
argN << options argN << options
# Execute, logging out the stdout/stderr as we get it # Execute, logging out the stdout/stderr as we get it
@logger.info("Executing: #{[command].concat(argN).inspect}")
Vagrant::Util::Subprocess.execute(command, *argN) do |type, data| Vagrant::Util::Subprocess.execute(command, *argN) do |type, data|
@logger.debug("#{type}: #{data}") if type == :stdout || type == :stderr @logger.debug("#{type}: #{data}") if type == :stdout || type == :stderr
yield type, data if block_given? yield type, data if block_given?

View File

@ -7,7 +7,7 @@ shared_context "acceptance" do
# create more verbose logs for tests which can be useful in the case # create more verbose logs for tests which can be useful in the case
# that a test fails. # that a test fails.
let(:logger_name) { "logger" } let(:logger_name) { "logger" }
let(:logger) { Log4r::Logger.new("acceptance::#{logger_name}") } let(:logger) { Log4r::Logger.new("test::acceptance::#{logger_name}") }
# This is the global configuration given by the acceptance test # This is the global configuration given by the acceptance test
# configurations. # configurations.
@ -18,10 +18,7 @@ shared_context "acceptance" do
# to replace "vagrant" with the proper path to Vagrant as well # to replace "vagrant" with the proper path to Vagrant as well
# as tell the isolated environment about custom environmental # as tell the isolated environment about custom environmental
# variables to pass in. # variables to pass in.
let!(:environment) do let!(:environment) { new_environment }
apps = { "vagrant" => config.vagrant_path }
Acceptance::IsolatedEnvironment.new(apps, config.env)
end
before(:each) do before(:each) do
# Wait for VBoxSVC to disappear, since each test requires its # Wait for VBoxSVC to disappear, since each test requires its
@ -33,6 +30,16 @@ shared_context "acceptance" do
environment.close environment.close
end end
# Creates a new isolated environment instance each time it is called.
#
# @return [Acceptance::IsolatedEnvironment]
def new_environment(env=nil)
apps = { "vagrant" => config.vagrant_path }
env = config.env.merge(env || {})
Acceptance::IsolatedEnvironment.new(apps, env)
end
# Executes the given command in the context of the isolated environment. # Executes the given command in the context of the isolated environment.
# #
# @return [Object] # @return [Object]

View File

@ -24,7 +24,7 @@ class IsolatedEnvironment
# @param [Hash] env Additional environmental variables to inject # @param [Hash] env Additional environmental variables to inject
# into the execution environments. # into the execution environments.
def initialize def initialize
@logger = Log4r::Logger.new("isolated_environment") @logger = Log4r::Logger.new("test::isolated_environment")
# Create a temporary directory for our work # Create a temporary directory for our work
@tempdir = Tempdir.new("vagrant") @tempdir = Tempdir.new("vagrant")