dombizita commented on code in PR #11074: URL: https://github.com/apache/ozone/pull/11074#discussion_r3862530611
########## hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/upgrade/TestOMFinalizeUpgradeRequestBase.java: ########## @@ -0,0 +1,288 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.om.request.upgrade; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import org.apache.hadoop.hdds.scm.exceptions.SCMException; +import org.apache.hadoop.ozone.OzoneConsts; +import org.apache.hadoop.ozone.OzoneManagerVersion; +import org.apache.hadoop.ozone.om.exceptions.OMException; +import org.apache.hadoop.ozone.om.execution.flowcontrol.ExecutionContext; +import org.apache.hadoop.ozone.om.helpers.OMNodeDetails; +import org.apache.hadoop.ozone.om.protocolPB.OMAdminProtocolClientSideImpl; +import org.apache.hadoop.ozone.om.request.key.OMKeyRequestTests; +import org.apache.hadoop.ozone.om.response.OMClientResponse; +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest; +import org.apache.hadoop.security.UserGroupInformation; +import org.apache.ratis.server.protocol.TermIndex; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; + +/** + * Shared tests for the client-initiated finalize requests handled by + * {@link OMFinalizeUpgradeRequestBase}: admin authorization, SCM finalization + * triggering and error mapping, OM peer software version validation, and writing + * the finalization-in-progress marker. Subclasses supply the concrete request via + * {@link #newRequest()} and add tests for behavior unique to their command type. + */ +public abstract class TestOMFinalizeUpgradeRequestBase extends OMKeyRequestTests { + + /** + * A fresh, non-forced initiate request for the command type under test. + */ + protected abstract OMFinalizeUpgradeRequestBase newRequest(); + + @BeforeEach + public void stubPeerNodes() { + when(ozoneManager.getPeerNodes()).thenReturn(Collections.emptyList()); + } + + @Test + public void testPreExecuteCallsScmFinalizeUpgrade() throws IOException { + doNothing().when(scmContainerLocationProtocol).finalizeUpgrade(); + + OMFinalizeUpgradeRequestBase request = newRequest(); + OMRequest original = request.getOmRequest(); + + OMRequest modified = request.preExecute(ozoneManager); + + // UserInfo must have been added by the base class preExecute. + assertNotEquals(original, modified); + assertNotNull(modified.getUserInfo()); + + // A non-forced initiate request must route to SCM's non-force finalize path. + verify(scmContainerLocationProtocol).finalizeUpgrade(); + verify(scmContainerLocationProtocol, never()).forceFinalizeUpgrade(); + } + + @Test + public void testScmFinalizeFailurePropagatesToClient() throws IOException { + IOException scmFailure = new IOException("SCM finalize upgrade failed"); + doThrow(scmFailure).when(scmContainerLocationProtocol).finalizeUpgrade(); + + OMFinalizeUpgradeRequestBase request = newRequest(); + + // The exception raised by SCM must propagate out of preExecute so the OM + // client sees the failure instead of a successful finalize. + IOException ex = assertThrows(IOException.class, () -> request.preExecute(ozoneManager)); + assertSame(scmFailure, ex); + + verify(scmContainerLocationProtocol).finalizeUpgrade(); + } + + @Test + public void testScmUnsupportedOperationBecomesOmNotSupportedOperation() throws IOException { + SCMException scmFailure = + new SCMException("SCM version mismatch", SCMException.ResultCodes.UNSUPPORTED_OPERATION); + doThrow(scmFailure).when(scmContainerLocationProtocol).finalizeUpgrade(); + + OMFinalizeUpgradeRequestBase request = newRequest(); + + // An SCM UNSUPPORTED_OPERATION is re-mapped to an OM NOT_SUPPORTED_OPERATION, + // preserving the original message and chaining the SCM exception as the cause. + OMException ex = assertThrows(OMException.class, () -> request.preExecute(ozoneManager)); + assertEquals(OMException.ResultCodes.NOT_SUPPORTED_OPERATION, ex.getResult()); + assertEquals(scmFailure.getMessage(), ex.getMessage()); + assertSame(scmFailure, ex.getCause()); + + verify(scmContainerLocationProtocol).finalizeUpgrade(); + } + + // TODO check if this is desired behavior Review Comment: nit: I believe this is the desired behavior, right? This comment can be removed then. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
