2013-03-27 00:45:20 +00:00
|
|
|
require 'unit_helper'
|
|
|
|
|
2013-07-13 17:02:13 +00:00
|
|
|
require 'tmpdir'
|
2013-03-27 00:45:20 +00:00
|
|
|
require 'vagrant-lxc/action/clear_forwarded_ports'
|
|
|
|
|
|
|
|
describe Vagrant::LXC::Action::ClearForwardedPorts do
|
2013-07-13 16:41:39 +00:00
|
|
|
let(:app) { double(:app, call: true) }
|
|
|
|
let(:env) { {machine: machine, ui: double(info: true)} }
|
|
|
|
let(:machine) { double(:machine, data_dir: data_dir) }
|
2013-03-27 00:45:20 +00:00
|
|
|
let!(:data_dir) { Pathname.new(Dir.mktmpdir) }
|
|
|
|
let(:pids_dir) { data_dir.join('pids') }
|
|
|
|
let(:pid) { 'a-pid' }
|
|
|
|
let(:pid_cmd) { 'redir' }
|
|
|
|
|
|
|
|
subject { described_class.new(app, env) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
pids_dir.mkdir
|
|
|
|
pids_dir.join('redir_1234.pid').open('w') { |f| f.write(pid) }
|
|
|
|
subject.stub(system: true, :` => pid_cmd)
|
|
|
|
subject.call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
after { FileUtils.rm_rf data_dir.to_s }
|
|
|
|
|
|
|
|
it 'removes all files under pid directory' do
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(Dir[pids_dir.to_s + "/redir_*.pid"]).to be_empty
|
2013-03-27 00:45:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a valid redir pid' do
|
|
|
|
it 'kills known processes' do
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(subject).to have_received(:system).with("pkill -TERM -P #{pid}")
|
2013-03-27 00:45:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an invalid pid' do
|
2013-04-23 10:55:37 +00:00
|
|
|
let(:pid_cmd) { 'sudo ls' }
|
2013-03-27 00:45:20 +00:00
|
|
|
|
|
|
|
it 'does not kill the process' do
|
2014-03-14 14:37:50 +00:00
|
|
|
expect(subject).not_to have_received(:system).with("pkill -TERM -P #{pid}")
|
2013-03-27 00:45:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|