def get_ip(self, vm, default_network, match_function, cancellation_context, timeout, logger): self._validate_vmware_tools_installed(logger, vm) ip = None reason = IpReason.Success if not timeout: ip = self._obtain_ip(vm, default_network, match_function, logger) if not ip: reason = IpReason.NotFound else: time_elapsed = 0 interval = self.INTERVAL while time_elapsed < timeout and not ip: if cancellation_context.is_cancelled: reason = IpReason.Cancelled break ip = self._obtain_ip(vm, default_network, match_function, logger) if not ip: time_elapsed += interval time.sleep(interval) if time_elapsed >= timeout: reason = IpReason.Timeout return IpResult(ip, reason)
def test_get_ip_no_timeout_found(self): self.vm.guest.ipAddress = '1.1.1.1' self.vm.guest.net = [] res = self.ip_manager.get_ip(self.vm, self.default_net, self.function, self.cancel, None, self.logger) expected = IpResult('1.1.1.1', IpReason.Success) self.assertEqual(expected.ip_address, res.ip_address) self.assertEqual(expected.reason, res.reason)
def test_get_ip_with_timeout(self): TestVMIPManager.counter = 0 self.ip_manager._obtain_ip = TestVMIPManager.mock_func res = self.ip_manager.get_ip(self.vm, self.default_net, self.function, self.cancel, self.timeout, self.logger) expected = IpResult('1.1.1.1', IpReason.Success) self.assertEqual(expected.ip_address, res.ip_address) self.assertEqual(expected.reason, res.reason)
def test_get_ip_cancelled(self): TestVMIPManager.counter = 0 cancel = Mock() cancel.is_cancelled = True res = self.ip_manager.get_ip(self.vm, self.default_net, self.function, cancel, self.timeout, self.logger) expected = IpResult(None, IpReason.Cancelled) self.assertEqual(expected.ip_address, res.ip_address) self.assertEqual(expected.reason, res.reason)
def test_get_ip_with_timeout_raised(self): TestVMIPManager.counter = 0 self.ip_manager._obtain_ip = TestVMIPManager.mock_func self.ip_manager.INTERVAL = 1.1 res = self.ip_manager.get_ip(self.vm, self.default_net, self.function, self.cancel, self.timeout, self.logger) expected = IpResult(None, IpReason.Timeout) self.assertEqual(expected.ip_address, res.ip_address) self.assertEqual(expected.reason, res.reason) self.ip_manager.INTERVAL = 0.001
def test_get_ip_no_timeout_multi_ip_regex_pattern(self): self.vm.guest.ipAddress = '1.1.1.1' nic = Mock() nic.network = None nic.ipAddress = ['2.2.2.2'] self.vm.guest.net = [nic] res = self.ip_manager.get_ip( self.vm, self.default_net, self.ip_manager.get_ip_match_function('^2\.2\.2\.2$'), self.cancel, None, self.logger) expected = IpResult('2.2.2.2', IpReason.Success) self.assertEqual(expected.ip_address, res.ip_address) self.assertEqual(expected.reason, res.reason)