class TestUpsertOperationUnitTests(unittest.TestCase): def setUp(self): self._conn = mock.MagicMock() self._resource = BaseConfigurationResource(self._conn) def test_get_operation_name(self): operation_a = mock.MagicMock() operation_b = mock.MagicMock() def checker_wrapper(expected_object): def checker(obj, *args, **kwargs): return obj == expected_object return checker operations = { operation_a: "spec", operation_b: "spec" } assert operation_a == self._resource._get_operation_name(checker_wrapper(operation_a), operations) assert operation_b == self._resource._get_operation_name(checker_wrapper(operation_b), operations) assert self._resource._get_operation_name(checker_wrapper(None), operations) is None @mock.patch.object(BaseConfigurationResource, "_get_operation_name") @mock.patch.object(BaseConfigurationResource, "add_object") def test_add_upserted_object(self, add_object_mock, get_operation_mock): model_operations = mock.MagicMock() params = mock.MagicMock() add_op_name = get_operation_mock.return_value assert add_object_mock.return_value == self._resource._add_upserted_object(model_operations, params) get_operation_mock.assert_called_once_with( self._resource._operation_checker.is_add_operation, model_operations) add_object_mock.assert_called_once_with(add_op_name, params) @mock.patch.object(BaseConfigurationResource, "_get_operation_name") @mock.patch.object(BaseConfigurationResource, "add_object") def test_add_upserted_object_with_no_add_operation(self, add_object_mock, get_operation_mock): model_operations = mock.MagicMock() get_operation_mock.return_value = None with pytest.raises(FtdConfigurationError) as exc_info: self._resource._add_upserted_object(model_operations, mock.MagicMock()) assert ADD_OPERATION_NOT_SUPPORTED_ERROR in str(exc_info.value) get_operation_mock.assert_called_once_with(self._resource._operation_checker.is_add_operation, model_operations) add_object_mock.assert_not_called() @mock.patch.object(BaseConfigurationResource, "_get_operation_name") @mock.patch.object(BaseConfigurationResource, "edit_object") @mock.patch("ansible.module_utils.network.ftd.configuration.copy_identity_properties") @mock.patch("ansible.module_utils.network.ftd.configuration._set_default") def test_edit_upserted_object(self, _set_default_mock, copy_properties_mock, edit_object_mock, get_operation_mock): model_operations = mock.MagicMock() existing_object = mock.MagicMock() params = { 'path_params': {}, 'data': {} } result = self._resource._edit_upserted_object(model_operations, existing_object, params) assert result == edit_object_mock.return_value _set_default_mock.assert_has_calls([ mock.call(params, 'path_params', {}), mock.call(params, 'data', {}) ]) get_operation_mock.assert_called_once_with( self._resource._operation_checker.is_edit_operation, model_operations ) copy_properties_mock.assert_called_once_with( existing_object, params['data'] ) edit_object_mock.assert_called_once_with( get_operation_mock.return_value, params ) @mock.patch("ansible.module_utils.network.ftd.configuration.OperationChecker.is_upsert_operation_supported") @mock.patch.object(BaseConfigurationResource, "get_operation_specs_by_model_name") @mock.patch.object(BaseConfigurationResource, "_find_object_matching_params") @mock.patch.object(BaseConfigurationResource, "_add_upserted_object") @mock.patch.object(BaseConfigurationResource, "_edit_upserted_object") def test_upsert_object_successfully_added(self, edit_mock, add_mock, find_object, get_operation_mock, is_upsert_supported_mock): params = mock.MagicMock() is_upsert_supported_mock.return_value = True find_object.return_value = None result = self._resource.upsert_object('upsertFoo', params) assert result == add_mock.return_value self._conn.get_model_spec.assert_called_once_with('Foo') is_upsert_supported_mock.assert_called_once_with(get_operation_mock.return_value) get_operation_mock.assert_called_once_with('Foo') find_object.assert_called_once_with('Foo', params) add_mock.assert_called_once_with(get_operation_mock.return_value, params) edit_mock.assert_not_called() @mock.patch("ansible.module_utils.network.ftd.configuration.equal_objects") @mock.patch("ansible.module_utils.network.ftd.configuration.OperationChecker.is_upsert_operation_supported") @mock.patch.object(BaseConfigurationResource, "get_operation_specs_by_model_name") @mock.patch.object(BaseConfigurationResource, "_find_object_matching_params") @mock.patch.object(BaseConfigurationResource, "_add_upserted_object") @mock.patch.object(BaseConfigurationResource, "_edit_upserted_object") def test_upsert_object_successfully_edited(self, edit_mock, add_mock, find_object, get_operation_mock, is_upsert_supported_mock, equal_objects_mock): params = mock.MagicMock() existing_obj = mock.MagicMock() is_upsert_supported_mock.return_value = True find_object.return_value = existing_obj equal_objects_mock.return_value = False result = self._resource.upsert_object('upsertFoo', params) assert result == edit_mock.return_value self._conn.get_model_spec.assert_called_once_with('Foo') get_operation_mock.assert_called_once_with('Foo') is_upsert_supported_mock.assert_called_once_with(get_operation_mock.return_value) add_mock.assert_not_called() equal_objects_mock.assert_called_once_with(existing_obj, params[ParamName.DATA]) edit_mock.assert_called_once_with(get_operation_mock.return_value, existing_obj, params) @mock.patch("ansible.module_utils.network.ftd.configuration.equal_objects") @mock.patch("ansible.module_utils.network.ftd.configuration.OperationChecker.is_upsert_operation_supported") @mock.patch.object(BaseConfigurationResource, "get_operation_specs_by_model_name") @mock.patch.object(BaseConfigurationResource, "_find_object_matching_params") @mock.patch.object(BaseConfigurationResource, "_add_upserted_object") @mock.patch.object(BaseConfigurationResource, "_edit_upserted_object") def test_upsert_object_returned_without_modifications(self, edit_mock, add_mock, find_object, get_operation_mock, is_upsert_supported_mock, equal_objects_mock): params = mock.MagicMock() existing_obj = mock.MagicMock() is_upsert_supported_mock.return_value = True find_object.return_value = existing_obj equal_objects_mock.return_value = True result = self._resource.upsert_object('upsertFoo', params) assert result == existing_obj self._conn.get_model_spec.assert_called_once_with('Foo') get_operation_mock.assert_called_once_with('Foo') is_upsert_supported_mock.assert_called_once_with(get_operation_mock.return_value) add_mock.assert_not_called() equal_objects_mock.assert_called_once_with(existing_obj, params[ParamName.DATA]) edit_mock.assert_not_called() @mock.patch("ansible.module_utils.network.ftd.configuration.OperationChecker.is_upsert_operation_supported") @mock.patch.object(BaseConfigurationResource, "get_operation_specs_by_model_name") @mock.patch.object(BaseConfigurationResource, "_find_object_matching_params") @mock.patch.object(BaseConfigurationResource, "_add_upserted_object") @mock.patch.object(BaseConfigurationResource, "_edit_upserted_object") def test_upsert_object_not_supported(self, edit_mock, add_mock, find_object, get_operation_mock, is_upsert_supported_mock): params = mock.MagicMock() is_upsert_supported_mock.return_value = False self.assertRaises( FtdInvalidOperationNameError, self._resource.upsert_object, 'upsertFoo', params ) self._conn.get_model_spec.assert_called_once_with('Foo') get_operation_mock.assert_called_once_with('Foo') is_upsert_supported_mock.assert_called_once_with(get_operation_mock.return_value) find_object.assert_not_called() add_mock.assert_not_called() edit_mock.assert_not_called() @mock.patch("ansible.module_utils.network.ftd.configuration.OperationChecker.is_upsert_operation_supported") @mock.patch.object(BaseConfigurationResource, "get_operation_specs_by_model_name") @mock.patch.object(BaseConfigurationResource, "_find_object_matching_params") @mock.patch.object(BaseConfigurationResource, "_add_upserted_object") @mock.patch.object(BaseConfigurationResource, "_edit_upserted_object") def test_upsert_object_when_model_not_supported(self, edit_mock, add_mock, find_object, get_operation_mock, is_upsert_supported_mock): params = mock.MagicMock() self._conn.get_model_spec.return_value = None self.assertRaises( FtdInvalidOperationNameError, self._resource.upsert_object, 'upsertNonExisting', params ) self._conn.get_model_spec.assert_called_once_with('NonExisting') get_operation_mock.assert_not_called() is_upsert_supported_mock.assert_not_called() find_object.assert_not_called() add_mock.assert_not_called() edit_mock.assert_not_called() @mock.patch("ansible.module_utils.network.ftd.configuration.equal_objects") @mock.patch("ansible.module_utils.network.ftd.configuration.OperationChecker.is_upsert_operation_supported") @mock.patch.object(BaseConfigurationResource, "get_operation_specs_by_model_name") @mock.patch.object(BaseConfigurationResource, "_find_object_matching_params") @mock.patch.object(BaseConfigurationResource, "_add_upserted_object") @mock.patch.object(BaseConfigurationResource, "_edit_upserted_object") def test_upsert_object_with_fatal_error_during_edit(self, edit_mock, add_mock, find_object, get_operation_mock, is_upsert_supported_mock, equal_objects_mock): params = mock.MagicMock() existing_obj = mock.MagicMock() is_upsert_supported_mock.return_value = True find_object.return_value = existing_obj equal_objects_mock.return_value = False edit_mock.side_effect = FtdConfigurationError("Some object edit error") self.assertRaises( FtdConfigurationError, self._resource.upsert_object, 'upsertFoo', params ) is_upsert_supported_mock.assert_called_once_with(get_operation_mock.return_value) self._conn.get_model_spec.assert_called_once_with('Foo') get_operation_mock.assert_called_once_with('Foo') find_object.assert_called_once_with('Foo', params) add_mock.assert_not_called() edit_mock.assert_called_once_with(get_operation_mock.return_value, existing_obj, params) @mock.patch("ansible.module_utils.network.ftd.configuration.OperationChecker.is_upsert_operation_supported") @mock.patch.object(BaseConfigurationResource, "get_operation_specs_by_model_name") @mock.patch.object(BaseConfigurationResource, "_find_object_matching_params") @mock.patch.object(BaseConfigurationResource, "_add_upserted_object") @mock.patch.object(BaseConfigurationResource, "_edit_upserted_object") def test_upsert_object_with_fatal_error_during_add(self, edit_mock, add_mock, find_object, get_operation_mock, is_upsert_supported_mock): params = mock.MagicMock() is_upsert_supported_mock.return_value = True find_object.return_value = None error = FtdConfigurationError("Obj duplication error") add_mock.side_effect = error self.assertRaises( FtdConfigurationError, self._resource.upsert_object, 'upsertFoo', params ) is_upsert_supported_mock.assert_called_once_with(get_operation_mock.return_value) self._conn.get_model_spec.assert_called_once_with('Foo') get_operation_mock.assert_called_once_with('Foo') find_object.assert_called_once_with('Foo', params) add_mock.assert_called_once_with(get_operation_mock.return_value, params) edit_mock.assert_not_called()
class TestUpsertOperationUnitTests(unittest.TestCase): def setUp(self): conn = mock.MagicMock() self._resource = BaseConfigurationResource(conn) def test_get_operation_name(self): operation_a = mock.MagicMock() operation_b = mock.MagicMock() def checker_wrapper(expected_object): def checker(obj, *args, **kwargs): return obj == expected_object return checker operations = { operation_a: "spec", operation_b: "spec" } assert operation_a == self._resource._get_operation_name(checker_wrapper(operation_a), operations) assert operation_b == self._resource._get_operation_name(checker_wrapper(operation_b), operations) self.assertRaises( FtdConfigurationError, self._resource._get_operation_name, checker_wrapper(None), operations ) @mock.patch.object(BaseConfigurationResource, "_get_operation_name") @mock.patch.object(BaseConfigurationResource, "add_object") def test_add_upserted_object(self, add_object_mock, get_operation_mock): model_operations = mock.MagicMock() params = mock.MagicMock() add_op_name = get_operation_mock.return_value assert add_object_mock.return_value == self._resource._add_upserted_object(model_operations, params) get_operation_mock.assert_called_once_with( self._resource._operation_checker.is_add_operation, model_operations) add_object_mock.assert_called_once_with(add_op_name, params) @mock.patch.object(BaseConfigurationResource, "_get_operation_name") @mock.patch.object(BaseConfigurationResource, "edit_object") @mock.patch("ansible.module_utils.network.ftd.configuration.copy_identity_properties") @mock.patch("ansible.module_utils.network.ftd.configuration._set_default") def test_edit_upserted_object(self, _set_default_mock, copy_properties_mock, edit_object_mock, get_operation_mock): model_operations = mock.MagicMock() existing_object = mock.MagicMock() params = { 'path_params': {}, 'data': {} } result = self._resource._edit_upserted_object(model_operations, existing_object, params) assert result == edit_object_mock.return_value _set_default_mock.assert_has_calls([ mock.call(params, 'path_params', {}), mock.call(params, 'data', {}) ]) get_operation_mock.assert_called_once_with( self._resource._operation_checker.is_edit_operation, model_operations ) copy_properties_mock.assert_called_once_with( existing_object, params['data'] ) edit_object_mock.assert_called_once_with( get_operation_mock.return_value, params ) @mock.patch.object(BaseConfigurationResource, "get_operation_specs_by_model_name") @mock.patch("ansible.module_utils.network.ftd.configuration.OperationChecker.is_upsert_operation_supported") @mock.patch("ansible.module_utils.network.ftd.configuration._extract_model_from_upsert_operation") def test_is_upsert_operation_supported(self, extract_model_mock, is_upsert_supported_mock, get_operation_spec_mock): op_name = mock.MagicMock() result = self._resource.is_upsert_operation_supported(op_name) assert result == is_upsert_supported_mock.return_value extract_model_mock.assert_called_once_with(op_name) get_operation_spec_mock.assert_called_once_with(extract_model_mock.return_value) is_upsert_supported_mock.assert_called_once_with(get_operation_spec_mock.return_value) @mock.patch.object(BaseConfigurationResource, "is_upsert_operation_supported") @mock.patch.object(BaseConfigurationResource, "get_operation_specs_by_model_name") @mock.patch.object(BaseConfigurationResource, "_add_upserted_object") @mock.patch.object(BaseConfigurationResource, "_edit_upserted_object") @mock.patch("ansible.module_utils.network.ftd.configuration._extract_model_from_upsert_operation") def test_upsert_object_succesfully_added(self, extract_model_mock, edit_mock, add_mock, get_operation_mock, is_upsert_supported_mock): op_name = mock.MagicMock() params = mock.MagicMock() is_upsert_supported_mock.return_value = True result = self._resource.upsert_object(op_name, params) assert result == add_mock.return_value is_upsert_supported_mock.assert_called_once_with(op_name) extract_model_mock.assert_called_once_with(op_name) get_operation_mock.assert_called_once_with(extract_model_mock.return_value) add_mock.assert_called_once_with(get_operation_mock.return_value, params) edit_mock.assert_not_called() @mock.patch.object(BaseConfigurationResource, "is_upsert_operation_supported") @mock.patch.object(BaseConfigurationResource, "get_operation_specs_by_model_name") @mock.patch.object(BaseConfigurationResource, "_add_upserted_object") @mock.patch.object(BaseConfigurationResource, "_edit_upserted_object") @mock.patch("ansible.module_utils.network.ftd.configuration._extract_model_from_upsert_operation") def test_upsert_object_succesfully_edited(self, extract_model_mock, edit_mock, add_mock, get_operation_mock, is_upsert_supported_mock): op_name = mock.MagicMock() params = mock.MagicMock() is_upsert_supported_mock.return_value = True error = FtdConfigurationError("Obj duplication error") error.obj = mock.MagicMock() add_mock.side_effect = error result = self._resource.upsert_object(op_name, params) assert result == edit_mock.return_value is_upsert_supported_mock.assert_called_once_with(op_name) extract_model_mock.assert_called_once_with(op_name) get_operation_mock.assert_called_once_with(extract_model_mock.return_value) add_mock.assert_called_once_with(get_operation_mock.return_value, params) edit_mock.assert_called_once_with(get_operation_mock.return_value, error.obj, params) @mock.patch.object(BaseConfigurationResource, "is_upsert_operation_supported") @mock.patch.object(BaseConfigurationResource, "get_operation_specs_by_model_name") @mock.patch.object(BaseConfigurationResource, "_add_upserted_object") @mock.patch.object(BaseConfigurationResource, "_edit_upserted_object") @mock.patch("ansible.module_utils.network.ftd.configuration._extract_model_from_upsert_operation") def test_upsert_object_not_supported(self, extract_model_mock, edit_mock, add_mock, get_operation_mock, is_upsert_supported_mock): op_name = mock.MagicMock() params = mock.MagicMock() is_upsert_supported_mock.return_value = False self.assertRaises( FtdInvalidOperationNameError, self._resource.upsert_object, op_name, params ) is_upsert_supported_mock.assert_called_once_with(op_name) extract_model_mock.assert_not_called() get_operation_mock.assert_not_called() add_mock.assert_not_called() edit_mock.assert_not_called() @mock.patch.object(BaseConfigurationResource, "is_upsert_operation_supported") @mock.patch.object(BaseConfigurationResource, "get_operation_specs_by_model_name") @mock.patch.object(BaseConfigurationResource, "_add_upserted_object") @mock.patch.object(BaseConfigurationResource, "_edit_upserted_object") @mock.patch("ansible.module_utils.network.ftd.configuration._extract_model_from_upsert_operation") def test_upsert_object_neither_added_nor_edited(self, extract_model_mock, edit_mock, add_mock, get_operation_mock, is_upsert_supported_mock): op_name = mock.MagicMock() params = mock.MagicMock() is_upsert_supported_mock.return_value = True error = FtdConfigurationError("Obj duplication error") error.obj = mock.MagicMock() add_mock.side_effect = error edit_mock.side_effect = FtdConfigurationError("Some object edit error") self.assertRaises( FtdConfigurationError, self._resource.upsert_object, op_name, params ) is_upsert_supported_mock.assert_called_once_with(op_name) extract_model_mock.assert_called_once_with(op_name) get_operation_mock.assert_called_once_with(extract_model_mock.return_value) add_mock.assert_called_once_with(get_operation_mock.return_value, params) edit_mock.assert_called_once_with(get_operation_mock.return_value, error.obj, params) @mock.patch.object(BaseConfigurationResource, "is_upsert_operation_supported") @mock.patch.object(BaseConfigurationResource, "get_operation_specs_by_model_name") @mock.patch.object(BaseConfigurationResource, "_add_upserted_object") @mock.patch.object(BaseConfigurationResource, "_edit_upserted_object") @mock.patch("ansible.module_utils.network.ftd.configuration._extract_model_from_upsert_operation") def test_upsert_object_with_fatal_error_during_add(self, extract_model_mock, edit_mock, add_mock, get_operation_mock, is_upsert_supported_mock): op_name = mock.MagicMock() params = mock.MagicMock() is_upsert_supported_mock.return_value = True error = FtdConfigurationError("Obj duplication error") add_mock.side_effect = error self.assertRaises( FtdConfigurationError, self._resource.upsert_object, op_name, params ) is_upsert_supported_mock.assert_called_once_with(op_name) extract_model_mock.assert_called_once_with(op_name) get_operation_mock.assert_called_once_with(extract_model_mock.return_value) add_mock.assert_called_once_with(get_operation_mock.return_value, params) edit_mock.assert_not_called()