2013-03-26 23:00:05 +00:00
|
|
|
require 'unit_helper'
|
|
|
|
|
2013-07-22 01:45:03 +00:00
|
|
|
require 'tmpdir'
|
2014-03-11 22:50:28 +00:00
|
|
|
require 'vagrant-lxc/provider'
|
2013-03-26 23:00:05 +00:00
|
|
|
require 'vagrant-lxc/action/forward_ports'
|
|
|
|
|
|
|
|
describe Vagrant::LXC::Action::ForwardPorts do
|
2013-07-13 16:41:39 +00:00
|
|
|
let(:app) { double(:app, call: true) }
|
2014-05-04 02:31:43 +00:00
|
|
|
let(:env) { {machine: machine, ui: double(info: true, warn: true)} }
|
2013-07-13 16:41:39 +00:00
|
|
|
let(:machine) { double(:machine) }
|
2013-03-26 23:00:05 +00:00
|
|
|
let!(:data_dir) { Pathname.new(Dir.mktmpdir) }
|
2014-03-11 22:50:28 +00:00
|
|
|
let(:provider) { double(Vagrant::LXC::Provider, ssh_info: {host: container_ip}) }
|
2013-07-27 23:58:33 +00:00
|
|
|
let(:host_ip) { '127.0.0.1' }
|
|
|
|
let(:host_port) { 8080 }
|
|
|
|
let(:guest_port) { 80 }
|
2013-03-26 23:00:05 +00:00
|
|
|
let(:container_ip) { '10.0.1.234' }
|
|
|
|
let(:pid) { 'a-pid' }
|
2013-07-27 23:58:33 +00:00
|
|
|
let(:forward_conf) { {guest: guest_port, host: host_port, host_ip: host_ip} }
|
|
|
|
let(:networks) { [[:other_config, {}], [:forwarded_port, forward_conf]] }
|
2013-03-26 23:00:05 +00:00
|
|
|
|
|
|
|
subject { described_class.new(app, env) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
machine.stub_chain(:config, :vm, :networks).and_return(networks)
|
|
|
|
machine.stub(provider: provider, data_dir: data_dir)
|
|
|
|
|
2018-03-09 18:55:09 +00:00
|
|
|
subject.stub(redir_version: 3)
|
2013-03-26 23:00:05 +00:00
|
|
|
subject.stub(exec: true)
|
2013-04-23 11:03:29 +00:00
|
|
|
subject.stub(spawn: pid)
|
2013-03-26 23:00:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
after { FileUtils.rm_rf data_dir.to_s }
|
|
|
|
|
|
|
|
it 'forwards ports using redir' do
|
2013-07-16 04:56:40 +00:00
|
|
|
subject.stub(system: true)
|
|
|
|
subject.call(env)
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(subject).to have_received(:spawn).with(
|
2018-07-04 19:00:46 +00:00
|
|
|
"redir -n #{host_ip}:#{host_port} #{container_ip}:#{guest_port} 2>/dev/null"
|
2013-07-27 23:58:33 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2015-07-19 15:05:53 +00:00
|
|
|
it 'Uses 127.0.0.1 as default if host_ip is nil' do
|
2013-07-27 23:58:33 +00:00
|
|
|
forward_conf.delete(:host_ip)
|
|
|
|
subject.stub(system: true)
|
|
|
|
subject.call(env)
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(subject).to have_received(:spawn).with(
|
2018-07-04 19:00:46 +00:00
|
|
|
"redir -n 127.0.0.1:#{host_port} #{container_ip}:#{guest_port} 2>/dev/null"
|
2013-03-26 23:00:05 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2015-07-19 15:05:53 +00:00
|
|
|
it 'Uses 127.0.0.1 by default if host_ip is a blank string' do
|
2013-11-06 22:24:24 +00:00
|
|
|
forward_conf[:host_ip] = ' '
|
|
|
|
subject.stub(system: true)
|
|
|
|
subject.call(env)
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(subject).to have_received(:spawn).with(
|
2018-07-04 19:00:46 +00:00
|
|
|
"redir -n 127.0.0.1:#{host_port} #{container_ip}:#{guest_port} 2>/dev/null"
|
2013-11-06 22:24:24 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-03-26 23:00:05 +00:00
|
|
|
it "stores redir pids on machine's data dir" do
|
2013-07-16 04:56:40 +00:00
|
|
|
subject.stub(system: true)
|
|
|
|
subject.call(env)
|
2013-03-26 23:00:05 +00:00
|
|
|
pid_file = data_dir.join('pids', "redir_#{host_port}.pid").read
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(pid_file).to eq(pid)
|
2013-03-26 23:00:05 +00:00
|
|
|
end
|
2013-07-16 04:56:40 +00:00
|
|
|
|
2013-11-06 23:01:19 +00:00
|
|
|
it 'allows disabling a previously forwarded port' do
|
|
|
|
forward_conf[:disabled] = true
|
|
|
|
subject.stub(system: true)
|
|
|
|
subject.call(env)
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(subject).not_to have_received(:spawn)
|
2013-11-06 23:01:19 +00:00
|
|
|
end
|
|
|
|
|
2018-03-09 18:55:09 +00:00
|
|
|
it 'uses redir 2.x command line interface' do
|
|
|
|
subject.stub(system: true)
|
|
|
|
subject.stub(redir_version: 2)
|
|
|
|
subject.call(env)
|
|
|
|
expect(subject).to have_received(:spawn).with(
|
|
|
|
"redir --laddr=#{host_ip} --lport=#{host_port} --caddr=#{container_ip} --cport=#{guest_port} 2>/dev/null"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-07-16 04:56:40 +00:00
|
|
|
it 'raises RedirNotInstalled error if `redir` is not installed' do
|
|
|
|
subject.stub(system: false)
|
2014-03-14 14:37:50 +00:00
|
|
|
expect { subject.call(env) }.to raise_error(Vagrant::LXC::Errors::RedirNotInstalled)
|
2013-07-16 04:56:40 +00:00
|
|
|
end
|
2014-05-04 02:31:43 +00:00
|
|
|
|
|
|
|
context 'when a privileged port is used' do
|
|
|
|
let(:host_port) { 80 }
|
|
|
|
|
|
|
|
it 'forwards ports using redir' do
|
|
|
|
subject.stub(system: true)
|
|
|
|
subject.call(env)
|
|
|
|
expect(subject).to have_received(:spawn).with(
|
2018-07-04 19:00:46 +00:00
|
|
|
"sudo redir -n #{host_ip}:#{host_port} #{container_ip}:#{guest_port} 2>/dev/null"
|
2014-05-04 02:31:43 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2015-07-19 15:05:53 +00:00
|
|
|
it 'Uses 127.0.0.1 by default if host_ip is nil' do
|
2014-05-04 02:31:43 +00:00
|
|
|
forward_conf.delete(:host_ip)
|
|
|
|
subject.stub(system: true)
|
|
|
|
subject.call(env)
|
|
|
|
expect(subject).to have_received(:spawn).with(
|
2018-07-04 19:00:46 +00:00
|
|
|
"sudo redir -n 127.0.0.1:#{host_port} #{container_ip}:#{guest_port} 2>/dev/null"
|
2014-05-04 02:31:43 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2015-07-19 15:05:53 +00:00
|
|
|
it 'Uses 127.0.0.1 by default if host_ip is a blank string' do
|
2014-05-04 02:31:43 +00:00
|
|
|
forward_conf[:host_ip] = ' '
|
|
|
|
subject.stub(system: true)
|
|
|
|
subject.call(env)
|
|
|
|
expect(subject).to have_received(:spawn).with(
|
2018-07-04 19:00:46 +00:00
|
|
|
"sudo redir -n 127.0.0.1:#{host_port} #{container_ip}:#{guest_port} 2>/dev/null"
|
2014-05-04 02:31:43 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2013-03-26 23:00:05 +00:00
|
|
|
end
|