From 08bc1ed8145ac1b689c5d849c2eff489a34a6929 Mon Sep 17 00:00:00 2001 From: Jake Spain Date: Wed, 22 Feb 2023 11:31:22 -0500 Subject: [PATCH] Added mock info for guest ip address, fix existing tests, add dns config --- spec/rbvmomi_helper.rb | 24 +++++++++++++++++-- spec/unit/providers/vsphere_spec.rb | 36 +++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/spec/rbvmomi_helper.rb b/spec/rbvmomi_helper.rb index e0acbaf..ace62b6 100644 --- a/spec/rbvmomi_helper.rb +++ b/spec/rbvmomi_helper.rb @@ -198,13 +198,24 @@ MockVirtualDiskManager = Object MockVirtualMachine = Struct.new( # https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.VirtualMachine.html # From VirtualMachine - :config, :runtime, :snapshot, :summary, + :config, :runtime, :snapshot, :summary, :guest, # From ManagedEntity :name, # From RbVmomi::VIM::ManagedEntity # https://github.com/vmware/rbvmomi/blob/master/lib/rbvmomi/vim/ManagedEntity.rb :path -) +) do + # From RbVmomi::VIM::VirtualMachine + # https://github.com/ManageIQ/rbvmomi2/blob/59a5904031a0d7558f306e8d2a05001f9b6822de/lib/rbvmomi/vim/VirtualMachine.rb#L20 + def guest_ip + g = self.guest + if g.ipAddress + g.ipAddress + else + nil + end + end +end MockVirtualMachineSnapshot = Struct.new( # https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.Snapshot.html @@ -347,6 +358,12 @@ MockVirtualMachineGuestSummary = Struct.new( :hostName ) +MockVirtualMachineGuestInfo = Struct.new( + # https://developer.vmware.com/apis/1311/vsphere + # From Data Object - GuestInfo(vim.vm.GuestInfo) + :ipAddress +) + MockVirtualMachineRuntimeInfo = Struct.new( # https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/vim.vm.RuntimeInfo.html # From VirtualMachineRuntimeInfo @@ -801,6 +818,7 @@ end def mock_RbVmomi_VIM_VirtualMachine(options = {}) options[:snapshot_tree] = nil if options[:snapshot_tree].nil? options[:name] = 'VM' + rand(65536).to_s if options[:name].nil? + options[:ip] = '169.254.255.255' if options[:ip].nil? options[:path] = [] if options[:path].nil? mock = MockVirtualMachine.new() @@ -810,11 +828,13 @@ def mock_RbVmomi_VIM_VirtualMachine(options = {}) mock.summary.runtime = MockVirtualMachineRuntimeInfo.new() mock.summary.guest = MockVirtualMachineGuestSummary.new() mock.runtime = mock.summary.runtime + mock.guest = MockVirtualMachineGuestInfo.new() mock.name = options[:name] mock.summary.guest.hostName = options[:hostname] mock.runtime.bootTime = options[:boottime] mock.runtime.powerState = options[:powerstate] + mock.guest.ipAddress = options[:ip] unless options[:snapshot_tree].nil? mock.snapshot = MockVirtualMachineSnapshotInfo.new() diff --git a/spec/unit/providers/vsphere_spec.rb b/spec/unit/providers/vsphere_spec.rb index 14d9097..2dbc22e 100644 --- a/spec/unit/providers/vsphere_spec.rb +++ b/spec/unit/providers/vsphere_spec.rb @@ -50,6 +50,11 @@ describe 'Vmpooler::PoolManager::Provider::VSphere' do :config: max_tries: 3 retry_factor: 10 +:dns_configs: + :gcp-clouddns: + project: vmpooler-test + domain: vmpooler.example.com + dns_zone_resource_name: vmpooler-example-com :providers: :vsphere: server: "vcenter.domain.local" @@ -71,6 +76,7 @@ describe 'Vmpooler::PoolManager::Provider::VSphere' do ready_ttl: 1440 clone_target: 'cluster1' provider: 'vsphere' + dns_config: 'gcp-clouddns' EOT ) } @@ -535,7 +541,7 @@ EOT end end - context 'when VM exists but is missing information' do + context 'when VM exists but is missing hostname, boottime, powerstate' do let(:vm_object) { mock_RbVmomi_VIM_VirtualMachine({ :name => vmname, }) @@ -560,10 +566,25 @@ EOT end end + context 'when VM exists but is missing ip' do + let(:vm_object) { mock_RbVmomi_VIM_VirtualMachine({ + :name => vmname, + :ip => '', + }) + } + + it 'should return empty for ip' do + result = subject.get_vm(poolname,vmname) + + expect(result['ip']).to eq('') + end + end + context 'when VM exists and contains all information' do let(:vm_hostname) { "#{vmname}.demo.local" } let(:boot_time) { Time.now } let(:power_state) { 'MockPowerState' } + let(:ip) { '169.254.255.255' } let(:vm_object) { mock_RbVmomi_VIM_VirtualMachine({ :name => vmname, @@ -622,6 +643,11 @@ EOT expect(result['powerstate']).to eq(power_state) end + it 'should return the ip' do + result = subject.get_vm(poolname,vmname) + + expect(result['ip']).to eq(ip) + end end end @@ -1056,14 +1082,15 @@ EOT end describe '#vm_ready?' do - let(:domain) { nil } + let(:domain) { 'vmpooler.example.com' } context 'When a VM is ready' do before(:each) do + allow(subject).to receive(:domain).and_return('vmpooler.example.com') expect(subject).to receive(:open_socket).with(vmname, domain) end it 'should return true' do - expect(subject.vm_ready?(poolname,vmname)).to be true + expect(subject.vm_ready?(poolname, vmname)).to be true end end @@ -1075,6 +1102,7 @@ EOT end it 'should return true' do + allow(subject).to receive(:domain).and_return('vmpooler.example.com') expect(subject.vm_ready?('missing_pool',vmname)).to be true end end @@ -1390,7 +1418,7 @@ EOT let(:TCPSocket) { double('tcpsocket') } let(:socket) { double('tcpsocket') } let(:hostname) { 'host' } - let(:domain) { 'domain.local'} + let(:domain) { 'vmpooler.example.com' } let(:default_socket) { 22 } before do