def test_collect_vm_info(self, mock_get_ssh_client): """Test collect_vm_info method. """ distro = MagicMock() client = MagicMock() distro.get_vm_info.return_value = \ 'Failed to collect VM info: Does not exist.' mock_get_ssh_client.return_value = client cloud = IpaCloud(*args, **self.kwargs) cloud.distro = distro cloud.log_file = 'fake_file.name' cloud.logger = MagicMock() with patch('builtins.open', create=True) as mock_open: mock_open.return_value = MagicMock(spec=io.IOBase) file_handle = mock_open.return_value.__enter__.return_value cloud._collect_vm_info() file_handle.write.assert_has_calls([ call('\n'), call('Failed to collect VM info: Does not exist.') ]) cloud.logger.info.assert_called_once_with( 'Collecting basic info about VM') assert mock_get_ssh_client.call_count == 1
def test_cloud_bad_connect_soft_reboot( self, mock_soft_reboot, mock_get_host_key, mock_get_ssh_client, mock_start_instance, mock_set_image_id, mock_set_instance_ip ): """Test exception when connection not established after hard reboot.""" mock_soft_reboot.return_value = None mock_get_host_key.return_value = b'04820482' mock_get_ssh_client.side_effect = [ None, None, IpaSSHException('ERROR!') ] mock_start_instance.return_value = None mock_set_image_id.return_value = None mock_set_instance_ip.return_value = None self.kwargs['running_instance_id'] = 'fakeinstance' self.kwargs['test_files'] = ['test_soft_reboot'] cloud = IpaCloud(**self.kwargs) cloud.ssh_private_key_file = 'tests/data/ida_test' cloud.ssh_user = '******' cloud.logger = MagicMock() cloud.test_image() cloud.logger.error.assert_called_once_with( 'Unable to connect to instance after soft reboot: ERROR!' ) cloud.logger.error.reset_mock() assert mock_get_ssh_client.call_count > 0 assert mock_soft_reboot.call_count == 1 mock_soft_reboot.reset_mock() cloud.results_dir = self.results_dir.name mock_get_ssh_client.side_effect = [None, None, Exception('ERROR!')] cloud.test_image() cloud.logger.error.assert_called_once_with( 'Instance failed soft reboot: ERROR!' ) assert mock_get_ssh_client.call_count > 0 assert mock_soft_reboot.call_count == 1 mock_soft_reboot.reset_mock()