From cc8d9c94f0c696c39a80c90ed7b6bf887f8eea6a Mon Sep 17 00:00:00 2001 From: Trevor Vaughan Date: Tue, 1 Jun 2021 17:19:14 -0400 Subject: [PATCH] Be more aggressive about picking a connection Based on the conversation in #46, this attempts to run down a list of 'normal' connections until one can be found. It also pins the fact that you are operating inside of podman. Closes #46 --- lib/beaker/hypervisor/docker.rb | 38 +++++++++++++++++++++++++-- spec/beaker/hypervisor/docker_spec.rb | 9 ++++--- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/lib/beaker/hypervisor/docker.rb b/lib/beaker/hypervisor/docker.rb index d6ddd7e..cd3718f 100644 --- a/lib/beaker/hypervisor/docker.rb +++ b/lib/beaker/hypervisor/docker.rb @@ -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 @@ -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 diff --git a/spec/beaker/hypervisor/docker_spec.rb b/spec/beaker/hypervisor/docker_spec.rb index a48d561..a5dde4b 100644 --- a/spec/beaker/hypervisor/docker_spec.rb +++ b/spec/beaker/hypervisor/docker_spec.rb @@ -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) @@ -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 @@ -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 @@ -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