Skip to content

Commit

Permalink
Merge pull request #47 from trevor-vaughan/fix_docker_connection
Browse files Browse the repository at this point in the history
Be more aggressive about picking a connection
  • Loading branch information
bastelfreak authored Aug 6, 2021
2 parents 1b5cf95 + cc8d9c9 commit d7621ca
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
38 changes: 36 additions & 2 deletions lib/beaker/hypervisor/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def initialize(hosts, options)
else
@registry = ENV['DOCKER_REGISTRY']
end
elsif ::Docker.respond_to?(:podman?) && ::Docker.podman?
@docker_type = 'podman'
else
@docker_type = 'docker'
end
Expand Down Expand Up @@ -150,9 +152,41 @@ def get_ssh_connection_info(container)
gw = network_settings['Gateway']
ip = gw unless (gw.nil? || gw.empty?)
else
port22 = network_settings.dig('Ports','22/tcp')
ip = port22[0]["HostIp"] if port22
# The many faces of container networking

# Host to Container
port22 = network_settings.dig('PortBindings','22/tcp')
ip = port22[0]['HostIp'] if port22
port = port22[0]['HostPort'] if port22

# Container to container
unless ip && port
ip = nil
port = nil

ip = network_settings['IPAddress']
port = 22 if ip && !ip.empty?
end

# Container through gateway
unless ip && port
ip = nil
port = nil

ip = network_settings['Gateway']

if ip && !ip.empty?
port22 = network_settings.dig('PortBindings','22/tcp')
port = port22[0]['HostPort'] if port22
end
end

# Legacy fallback
unless ip && port
port22 = network_settings.dig('Ports','22/tcp')
ip = port22[0]["HostIp"] if port22
port = port22[0]['HostPort'] if port22
end
end
end

Expand Down
9 changes: 5 additions & 4 deletions spec/beaker/hypervisor/docker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ module Beaker
allow( ::Docker ).to receive(:options).and_return(docker_options)
allow( ::Docker ).to receive(:options=)
allow( ::Docker ).to receive(:logger=)
allow( ::Docker ).to receive(:podman?).and_return(false)
allow( ::Docker ).to receive(:version).and_return(version)
allow( ::Docker::Image ).to receive(:build).and_return(image)
allow( ::Docker::Image ).to receive(:create).and_return(image)
Expand Down Expand Up @@ -490,7 +491,7 @@ module Beaker
ENV['DOCKER_HOST'] = nil
docker.provision

expect( hosts[0]['ip'] ).to be === '127.0.1.1'
expect( hosts[0]['ip'] ).to be === '192.0.2.1'
expect( hosts[0]['port'] ).to be === 8022
end

Expand All @@ -506,7 +507,7 @@ module Beaker
ENV['DOCKER_HOST'] = nil
docker.provision

expect( hosts[0]['ip'] ).to be === '127.0.1.1'
expect( hosts[0]['ip'] ).to be === '192.0.2.1'
expect( hosts[0]['port'] ).to be === 8022
expect( hosts[0]['ssh'][:password] ).to be === 'root'
expect( hosts[0]['ssh'][:port] ).to be === 8022
Expand Down Expand Up @@ -536,8 +537,8 @@ module Beaker
ENV['DOCKER_HOST'] = nil
docker.provision

expect( hosts[0]['ip'] ).to be === '127.0.1.1'
expect( hosts[0]['port'] ).to be === 8022
expect( hosts[0]['ip'] ).to be === '192.0.2.1'
expect( hosts[0]['port'] ).to be === 22
end
end

Expand Down

0 comments on commit d7621ca

Please sign in to comment.