Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be more aggressive about picking a connection #47

Merged
merged 1 commit into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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