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

Fix bug where ip address is not returned from guest tools #85

Merged
merged 1 commit into from
Jun 5, 2018
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
21 changes: 11 additions & 10 deletions lib/chef/provisioning/vsphere_driver/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -916,30 +916,31 @@ def create_ssh_transport(host, options)
def ip_to_bootstrap(bootstrap_options, vm)
start_time = Time.now.utc
timeout = bootstrap_ip_ready_timeout(bootstrap_options)
@vm_helper.find_port?(vm, bootstrap_options) unless vm_helper.port?
vm_helper.find_port?(vm, bootstrap_options) unless vm_helper.port?

# First get the IP to be tested
if has_static_ip(bootstrap_options)
if bootstrap_options[:customization_spec].is_a?(String)
spec = vsphere_helper.find_customization_spec(bootstrap_options[:customization_spec])
vm_ip = spec.nicSettingMap[0].adapter.ip.ipAddress # Use a direct return here?
vm_ip = spec.nicSettingMap[0].adapter.ip.ipAddress
else
## Check if true available
vm_ip = bootstrap_options[:customization_spec][:ipsettings][:ip] unless vm_helper.ip?
nb_attempts = 0
until @vm_helper.open_port?(vm_ip, @vm_helper.port, 1) || (nb_attempts > (bootstrap_options[:ready_timeout] || 90))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed port checking as we're already checking that at the end of the method.

print '.'
nb_attempts += 1
end
vm_ip = bootstrap_options[:customization_spec][:ipsettings][:ip]
end
elsif use_ipv4_during_bootstrap?(bootstrap_options)
vm_ip = wait_for_ipv4(bootstrap_ipv4_timeout(bootstrap_options), vm)
else
vm_ip = vm.guest.ipAddress until vm_guest_ip?(vm) || Time.now.utc - start_time > timeout
until vm_guest_ip?(vm) || Time.now.utc - start_time > timeout
print '.'
sleep 1
end
vm_ip = vm.guest.ipAddress
end

# Then check that it is reachable
until Time.now.utc - start_time > timeout
print '.'
return vm_ip.to_s if @vm_helper.open_port?(vm_ip, @vm_helper.port, 1)
return vm_ip.to_s if vm_helper.open_port?(vm_ip, vm_helper.port, 1)
sleep 1
end
raise "Timed out (#{timeout}s) waiting for ip #{vm_ip} to be connectable"
Expand Down
6 changes: 0 additions & 6 deletions lib/chef/provisioning/vsphere_driver/vm_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ class VmHelper
Timeout::Error, IPAddr::AddressFamilyError
].freeze

# If the IP is true
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed code as there was only 1 reference to it, in the ip_to_bootstrap method

#
def ip?
@ip
end

# If the port is true
#
def port?
Expand Down
90 changes: 90 additions & 0 deletions spec/unit_tests/VsphereDriver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,94 @@
end
end
end

context "#ip_to_bootstrap" do
let(:metal_config) { {} }
let(:vm) { double("vm") }
let(:vm_helper) do
double("vm_helper")
end
before do
allow_any_instance_of(Kernel).to receive(:print)
end

before do
allow(subject).to receive(:vm_helper).and_return(vm_helper)

allow(vm_helper).to receive(:find_port?)
allow(vm_helper).to receive(:port?)
allow(vm_helper).to receive(:port).and_return(port)
end

let(:port) { 22 }
let(:bootstrap_conf) { {} }

context "has_static_ip" do
let(:bootstrap_conf) { { customization_spec: "some spec" } }
context "customization_spec is named" do
let(:vsphere_helper) { double("vsphere_helper") }
let(:fake_spec) { double("fake_spec") }
let(:fake_adapter) { double("fake_adapter") }
let(:fake_ip) do
RbVmomi::VIM::CustomizationFixedIp(
ipAddress: "1.1.1.1"
)
end

it "return ip address" do
allow(subject).to receive(:vsphere_helper).and_return(vsphere_helper)
allow(fake_adapter).to receive_message_chain(:adapter, :ip).and_return(fake_ip)
allow(fake_spec).to receive(:nicSettingMap).and_return([fake_adapter])
allow(vsphere_helper).to receive(:find_customization_spec).and_return(fake_spec)
allow(vm_helper).to receive(:open_port?).with("1.1.1.1", port, 1).and_return(true)

result = subject.send :ip_to_bootstrap, bootstrap_conf, vm
expect(result).to eq "1.1.1.1"
end
end

context "customization_spec has an ip address" do
let(:bootstrap_conf) do
{
customization_spec: {
ipsettings: {
ip: "2.2.2.2"
}
}
}
end

it "returns ip address" do
allow(vm_helper).to receive(:ip?)
allow(vm_helper).to receive(:open_port?).with("2.2.2.2", port, 1).and_return(true)
result = subject.send :ip_to_bootstrap, bootstrap_conf, vm
expect(result).to eq "2.2.2.2"
end
end
end

context "use_ipv4_during_bootstrap" do
let(:bootstrap_conf) { { bootstrap_ipv4: true } }

it "returns ip address" do
allow(subject).to receive(:wait_for_ipv4).and_return("3.3.3.3")
allow(vm_helper).to receive(:open_port?).with("3.3.3.3", port, 1).and_return(true)

result = subject.send :ip_to_bootstrap, bootstrap_conf, vm
expect(result).to eq "3.3.3.3"
end
end

context "wait until guest tools returns an IP address" do
it "returns ip address" do
allow_any_instance_of(Kernel).to receive(:sleep)
expect(subject).to receive(:vm_guest_ip?).exactly(3).times.and_return(false, false, true)
allow(vm).to receive_message_chain(:guest, :ipAddress).and_return("4.4.4.4")

allow(vm_helper).to receive(:open_port?).exactly(1).times.with("4.4.4.4", port, 1).and_return(true)
result = subject.send :ip_to_bootstrap, bootstrap_conf, vm
expect(result).to eq "4.4.4.4"
end
end
end
end