Skip to content

Commit 1f99adf

Browse files
authored
Merge pull request voxpupuli#126 from h0tw1r3/fix-remote
Fix privileged remote docker port forward resolution
2 parents ba52a82 + bb7c0c6 commit 1f99adf

File tree

2 files changed

+38
-50
lines changed

2 files changed

+38
-50
lines changed

lib/beaker/hypervisor/docker.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ def get_ssh_connection_info(container)
188188
end
189189
end
190190

191-
if host_config['NetworkMode'] != 'slirp4netns' && network_settings['IPAddress'] && !network_settings['IPAddress'].empty?
192-
ip = network_settings['IPAddress'] if ip.nil?
193-
else
191+
ip = network_settings['IPAddress'] if ip.nil? && host_config['NetworkMode'] != 'slirp4netns' && network_settings['IPAddress'] && !network_settings['IPAddress'].empty?
192+
193+
if port.nil?
194194
port22 = network_settings.dig('Ports', '22/tcp')
195195
port = port22[0]['HostPort'] if port22
196196
end

spec/beaker/hypervisor/docker_spec.rb

+35-47
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ module Beaker
110110

111111
before do
112112
allow(::Docker).to receive(:rootless?).and_return(true)
113+
@docker_host = ENV.fetch('DOCKER_HOST', nil)
114+
ENV.delete('DOCKER_HOST') if @docker_host
115+
end
116+
117+
after do
118+
ENV['DOCKER_HOST'] = @docker_host if @docker_host
113119
end
114120

115121
context 'with connection failure' do
@@ -476,64 +482,46 @@ module Beaker
476482
end
477483

478484
context 'when connecting to ssh' do
479-
context 'when rootless' do
480-
before { @docker_host = ENV.fetch('DOCKER_HOST', nil) }
481-
482-
after { ENV['DOCKER_HOST'] = @docker_host }
483-
484-
it 'exposes port 22 to beaker' do
485-
ENV['DOCKER_HOST'] = nil
486-
docker.provision
487-
488-
expect(hosts[0]['ip']).to eq '127.0.0.1'
489-
expect(hosts[0]['port']).to eq 8022
490-
end
491-
492-
it 'exposes port 22 to beaker when using DOCKER_HOST' do
493-
ENV['DOCKER_HOST'] = 'tcp://192.0.2.2:2375'
494-
docker.provision
495-
496-
expect(hosts[0]['ip']).to eq '192.0.2.2'
497-
expect(hosts[0]['port']).to eq 8022
498-
end
485+
%w[rootless privileged].each do |mode|
486+
context "when #{mode}" do
487+
let(:container_mode) do
488+
mode
489+
end
499490

500-
it 'has ssh agent forwarding enabled' do
501-
ENV['DOCKER_HOST'] = nil
502-
docker.provision
491+
it 'exposes port 22 to beaker' do
492+
docker.provision
503493

504-
expect(hosts[0]['ip']).to eq '127.0.0.1'
505-
expect(hosts[0]['port']).to eq 8022
506-
expect(hosts[0]['ssh'][:password]).to eq 'root'
507-
expect(hosts[0]['ssh'][:port]).to eq 8022
508-
expect(hosts[0]['ssh'][:forward_agent]).to be true
509-
end
494+
expect(hosts[0]['ip']).to eq '127.0.0.1'
495+
expect(hosts[0]['port']).to eq 8022
496+
end
510497

511-
it 'connects to gateway ip' do
512-
FakeFS do
513-
FileUtils.touch('/.dockerenv')
498+
it 'exposes port 22 to beaker when using DOCKER_HOST' do
499+
ENV['DOCKER_HOST'] = 'tcp://192.0.2.2:2375'
514500
docker.provision
515501

516-
expect(hosts[0]['ip']).to eq '192.0.2.254'
502+
expect(hosts[0]['ip']).to eq '192.0.2.2'
517503
expect(hosts[0]['port']).to eq 8022
518504
end
519-
end
520-
end
521-
522-
context 'when rootful' do
523-
before { @docker_host = ENV.fetch('DOCKER_HOST', nil) }
524505

525-
after { ENV['DOCKER_HOST'] = @docker_host }
506+
it 'has ssh agent forwarding enabled' do
507+
docker.provision
526508

527-
let(:container_mode) do
528-
'rootful'
529-
end
509+
expect(hosts[0]['ip']).to eq '127.0.0.1'
510+
expect(hosts[0]['port']).to eq 8022
511+
expect(hosts[0]['ssh'][:password]).to eq 'root'
512+
expect(hosts[0]['ssh'][:port]).to eq 8022
513+
expect(hosts[0]['ssh'][:forward_agent]).to be true
514+
end
530515

531-
it 'exposes port 22 to beaker' do
532-
ENV['DOCKER_HOST'] = nil
533-
docker.provision
516+
it 'connects to gateway ip' do
517+
FakeFS do
518+
FileUtils.touch('/.dockerenv')
519+
docker.provision
534520

535-
expect(hosts[0]['ip']).to eq '127.0.0.1'
536-
expect(hosts[0]['port']).to eq 8022
521+
expect(hosts[0]['ip']).to eq '192.0.2.254'
522+
expect(hosts[0]['port']).to eq 8022
523+
end
524+
end
537525
end
538526
end
539527
end

0 commit comments

Comments
 (0)