def test_one_element_content(self): obj_mock = MagicMock() # obj.propSet propSet_prop = PropertyMock(return_value=[]) type(obj_mock).propSet = propSet_prop # obj.obj inner_obj_mock = MagicMock() obj_prop = PropertyMock(return_value=inner_obj_mock) type(obj_mock).obj = obj_prop get_content = MagicMock(return_value=[obj_mock]) with patch('salt.utils.vmware.get_content', get_content): ret = salt.utils.vmware.get_mors_with_properties( self.si, self.obj_type, self.prop_list, self.container_ref, self.traversal_spec) get_content.assert_called_once_with( self.si, self.obj_type, property_list=self.prop_list, container_ref=self.container_ref, traversal_spec=self.traversal_spec, local_properties=False) self.assertEqual(propSet_prop.call_count, 1) self.assertEqual(obj_prop.call_count, 1) self.assertEqual(len(ret), 1) self.assertDictEqual(ret[0], {'object': inner_obj_mock})
def test_local_properties_set(self): obj_mock = MagicMock() # obj.propSet propSet_prop = PropertyMock(return_value=[]) type(obj_mock).propSet = propSet_prop # obj.obj inner_obj_mock = MagicMock() obj_prop = PropertyMock(return_value=inner_obj_mock) type(obj_mock).obj = obj_prop get_content = MagicMock(return_value=[obj_mock]) with patch('salt.utils.vmware.get_content', get_content): ret = salt.utils.vmware.get_mors_with_properties( self.si, self.obj_type, self.prop_list, self.container_ref, self.traversal_spec, local_properties=True) get_content.assert_called_once_with(self.si, self.obj_type, property_list=self.prop_list, container_ref=self.container_ref, traversal_spec=self.traversal_spec, local_properties=True)
def test_info_state_success(self): mock_task = MagicMock() prop_mock_state = PropertyMock(return_value='success') prop_mock_result = PropertyMock() type(mock_task.info).state = prop_mock_state type(mock_task.info).result = prop_mock_result salt.utils.vmware.wait_for_task(mock_task, 'fake_instance_name', 'task_type') self.assertEqual(prop_mock_state.call_count, 3) self.assertEqual(prop_mock_result.call_count, 1)
def test_info_error_exception(self): mock_task = MagicMock() prop_mock_state = PropertyMock(return_value='error') prop_mock_error = PropertyMock(side_effect=Exception('error exc')) type(mock_task.info).state = prop_mock_state type(mock_task.info).error = prop_mock_error with self.assertRaises(Exception) as excinfo: salt.utils.vmware.wait_for_task(mock_task, 'fake_instance_name', 'task_type') self.assertEqual(excinfo.exception.message, 'error exc')
def test_inner_loop_task_info_raise_runtime_fault(self): exc = vmodl.RuntimeFault() exc.msg = 'RuntimeFault msg' mock_task = MagicMock() mock_info1 = MagicMock() type(mock_task).info = PropertyMock(side_effect=[mock_info1, exc]) type(mock_info1).state = PropertyMock(side_effect=['running', 'bad']) with self.assertRaises(excs.VMwareRuntimeError) as excinfo: salt.utils.vmware.wait_for_task(mock_task, 'fake_instance_name', 'task_type') self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
def test_info_state_queued(self): mock_task = MagicMock() # The 'bad' values are invalid in the while loop prop_mock_state = PropertyMock( side_effect=['bad', 'queued', 'bad', 'bad', 'success']) prop_mock_result = PropertyMock() type(mock_task.info).state = prop_mock_state type(mock_task.info).result = prop_mock_result salt.utils.vmware.wait_for_task(mock_task, 'fake_instance_name', 'task_type') self.assertEqual(prop_mock_state.call_count, 5) self.assertEqual(prop_mock_result.call_count, 1)
def test_info_error_system_fault(self): exc = vmodl.fault.SystemError() exc.msg = 'SystemError msg' mock_task = MagicMock() prop_mock_state = PropertyMock(return_value='error') prop_mock_error = PropertyMock(side_effect=exc) type(mock_task.info).state = prop_mock_state type(mock_task.info).error = prop_mock_error with self.assertRaises(excs.VMwareSystemError) as excinfo: salt.utils.vmware.wait_for_task(mock_task, 'fake_instance_name', 'task_type') self.assertEqual(excinfo.exception.strerror, 'SystemError msg')
def test_info_state_different_no_error_attr(self): mock_task = MagicMock() # The 'bad' values are invalid in the while loop prop_mock_state = PropertyMock(return_value='error') prop_mock_error = PropertyMock(side_effect=Exception('error exc')) type(mock_task.info).state = prop_mock_state type(mock_task.info).error = prop_mock_error with self.assertRaises(Exception) as excinfo: salt.utils.vmware.wait_for_task(mock_task, 'fake_instance_name', 'task_type') self.assertEqual(prop_mock_state.call_count, 3) self.assertEqual(prop_mock_error.call_count, 1) self.assertEqual('error exc', excinfo.exception.message)
def test_info_state_running_continues_loop(self): mock_task = MagicMock() # The 'fake' values are required to match all the lookups and end the # loop prop_mock_state = PropertyMock( side_effect=['running', 'fake', 'fake', 'success']) prop_mock_result = PropertyMock() type(mock_task.info).state = prop_mock_state type(mock_task.info).result = prop_mock_result salt.utils.vmware.wait_for_task(mock_task, 'fake_instance_name', 'task_type') self.assertEqual(prop_mock_state.call_count, 4) self.assertEqual(prop_mock_result.call_count, 1)
def test_info_error_invalid_argument_no_fault_message(self): exc = vmodl.fault.InvalidArgument() exc.faultMessage = None exc.msg = 'InvalidArgumentFault msg' mock_task = MagicMock() prop_mock_state = PropertyMock(return_value='error') prop_mock_error = PropertyMock(side_effect=exc) type(mock_task.info).state = prop_mock_state type(mock_task.info).error = prop_mock_error with self.assertRaises(excs.VMwareApiError) as excinfo: salt.utils.vmware.wait_for_task(mock_task, 'fake_instance_name', 'task_type') self.assertEqual(excinfo.exception.strerror, 'InvalidArgumentFault msg')
def test_raise_runtime_fault(self): exc = vmodl.RuntimeFault() exc.msg = 'RuntimeFault msg' type(self.mock_content).rootFolder = PropertyMock(side_effect=exc) with self.assertRaises(excs.VMwareRuntimeError) as excinfo: salt.utils.vmware.get_root_folder(self.mock_si) self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
def test_api_type_raise_runtime_fault(self): exc = vmodl.RuntimeFault() exc.msg = 'RuntimeFault msg' mock_si = MagicMock() type(mock_si.content.about).apiType = PropertyMock(side_effect=exc) with self.assertRaises(excs.VMwareRuntimeError) as excinfo: salt.utils.vmware.is_connection_to_a_vcenter(mock_si) self.assertEqual(excinfo.exception.strerror, 'RuntimeFault msg')
def test_first_task_info_raise_vim_fault(self): exc = vim.fault.VimFault() exc.msg = 'VimFault msg' mock_task = MagicMock() type(mock_task).info = PropertyMock(side_effect=exc) with self.assertRaises(excs.VMwareApiError) as excinfo: salt.utils.vmware.wait_for_task(mock_task, 'fake_instance_name', 'task_type') self.assertEqual(excinfo.exception.strerror, 'VimFault msg')
def test_one_elem_multiple_properties(self): obj_mock = MagicMock() # property1 mock prop_set_obj1_mock = MagicMock() prop_set_obj1_name_prop = PropertyMock(return_value='prop_name1') prop_set_obj1_val_prop = PropertyMock(return_value='prop_value1') type(prop_set_obj1_mock).name = prop_set_obj1_name_prop type(prop_set_obj1_mock).val = prop_set_obj1_val_prop # property2 mock prop_set_obj2_mock = MagicMock() prop_set_obj2_name_prop = PropertyMock(return_value='prop_name2') prop_set_obj2_val_prop = PropertyMock(return_value='prop_value2') type(prop_set_obj2_mock).name = prop_set_obj2_name_prop type(prop_set_obj2_mock).val = prop_set_obj2_val_prop # obj.propSet propSet_prop = PropertyMock( return_value=[prop_set_obj1_mock, prop_set_obj2_mock]) type(obj_mock).propSet = propSet_prop # obj.obj inner_obj_mock = MagicMock() obj_prop = PropertyMock(return_value=inner_obj_mock) type(obj_mock).obj = obj_prop get_content = MagicMock(return_value=[obj_mock]) with patch('salt.utils.vmware.get_content', get_content): ret = salt.utils.vmware.get_mors_with_properties( self.si, self.obj_type, self.prop_list, self.container_ref, self.traversal_spec) get_content.assert_called_once_with(self.si, self.obj_type, property_list=self.prop_list, container_ref=self.container_ref, traversal_spec=self.traversal_spec, local_properties=False) self.assertEqual(propSet_prop.call_count, 1) self.assertEqual(prop_set_obj1_name_prop.call_count, 1) self.assertEqual(prop_set_obj1_val_prop.call_count, 1) self.assertEqual(prop_set_obj2_name_prop.call_count, 1) self.assertEqual(prop_set_obj2_val_prop.call_count, 1) self.assertEqual(obj_prop.call_count, 1) self.assertEqual(len(ret), 1) self.assertDictEqual( ret[0], { 'prop_name1': 'prop_value1', 'prop_name2': 'prop_value2', 'object': inner_obj_mock })
def setUp(self): # setup the service instance self.si_mock = MagicMock() # RootFolder self.root_folder_mock = MagicMock() self.root_folder_prop = PropertyMock( return_value=self.root_folder_mock) type(self.si_mock.content).rootFolder = self.root_folder_prop # CreateContainerView() self.container_view_mock = MagicMock() self.create_container_view_mock = \ MagicMock(return_value=self.container_view_mock) self.si_mock.content.viewManager.CreateContainerView = \ self.create_container_view_mock # RetrieveContents() self.result_mock = MagicMock() self.retrieve_contents_mock = MagicMock(return_value=self.result_mock) self.si_mock.content.propertyCollector.RetrieveContents = \ self.retrieve_contents_mock # Destroy() self.destroy_mock = MagicMock() self.container_view_mock.Destroy = self.destroy_mock # override mocks self.obj_type_mock = MagicMock() self.traversal_spec_ret_mock = MagicMock() self.traversal_spec_mock = \ MagicMock(return_value=self.traversal_spec_ret_mock) self.property_spec_ret_mock = MagicMock() self.property_spec_mock = \ MagicMock(return_value=self.property_spec_ret_mock) self.obj_spec_ret_mock = MagicMock() self.obj_spec_mock = \ MagicMock(return_value=self.obj_spec_ret_mock) self.filter_spec_ret_mock = MagicMock() self.filter_spec_mock = \ MagicMock(return_value=self.filter_spec_ret_mock)
def setUp(self): self.mock_si = MagicMock() self.mock_stub = PropertyMock() self.mock_mo_ref = MagicMock(_stub=self.mock_stub)