bobpaulin commented on code in PR #11872:
URL: https://github.com/apache/nifi/pull/11872#discussion_r4606465584


##########
nifi-system-tests/nifi-system-test-suite/src/test/java/org/apache/nifi/tests/system/connectors/AbstractConnectorUpgradeDrainIT.java:
##########
@@ -0,0 +1,406 @@
+/*
+ * 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.nifi.tests.system.connectors;
+
+import org.apache.nifi.components.connector.ConnectorState;
+import org.apache.nifi.flow.Bundle;
+import org.apache.nifi.flow.VersionedConfigurationStep;
+import org.apache.nifi.flow.VersionedConnector;
+import org.apache.nifi.flow.VersionedProcessGroup;
+import org.apache.nifi.flow.VersionedProcessor;
+import org.apache.nifi.nar.NarState;
+import org.apache.nifi.tests.system.AbstractNarSwapMigrationIT;
+import org.apache.nifi.tests.system.FlowDefinitionFiles;
+import org.apache.nifi.tests.system.NiFiInstance;
+import org.apache.nifi.tests.system.nar.NarUploadUtil;
+import org.apache.nifi.toolkit.client.ConnectorClient;
+import org.apache.nifi.toolkit.client.NiFiClientException;
+import org.apache.nifi.web.api.dto.BundleDTO;
+import org.apache.nifi.web.api.dto.ConnectorDTO;
+import org.apache.nifi.web.api.dto.NarSummaryDTO;
+import org.apache.nifi.web.api.entity.ConnectionEntity;
+import org.apache.nifi.web.api.entity.ConnectorEntity;
+import org.apache.nifi.web.api.entity.ProcessGroupFlowEntity;
+import org.apache.nifi.web.api.entity.ProcessorEntity;
+import org.junit.jupiter.api.BeforeEach;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Upgrade-drain scenarios that hold for every way a Connector can move to a 
new version. Each subclass supplies the
+ * trigger: a NAR swap followed by a restart, or a version change while the 
Connector is stopped.
+ */
+public abstract class AbstractConnectorUpgradeDrainIT extends 
AbstractNarSwapMigrationIT {
+
+    private static final String GATE_FILE = "upgrade-drain-gate.txt";
+    static final String PREPARATION_FAILED = "flow evolution preparation 
failed";
+    static final String REMOVED_CONNECTION = "Removed Connection";
+    static final String REMOVED_PROCESSOR_CHAIN = "Removed Processor Chain";
+    static final String RELAY_PROCESSOR = "RelayFlowFile";
+    static final String REMOVED_PROCESSOR = "RemovedFlowFile";
+    static final String SWAPPED_PROCESSOR = "SwappedFlowFile";
+
+    static final String DRAIN_CONNECTOR_TYPE = 
"org.apache.nifi.connectors.tests.system.UpgradeDrainConnector";
+    static final String DRAIN_BUNDLE_GROUP = "org.apache.nifi";
+    static final String DRAIN_BUNDLE_ARTIFACT = 
"nifi-connector-upgrade-drain-nar";
+    static final String DRAIN_V1 = "1.0.0";
+    static final String DRAIN_V2 = "2.0.0";
+    private static final String DRAIN_NARS_LOCATION = 
"target/nifi-connector-upgrade-drain-nars";
+    private static final Map<String, String> DRAIN_NAR_IDS = Map.of(
+        DRAIN_V1, "nifi-connector-upgrade-drain-v1-nar",
+        DRAIN_V2, "nifi-connector-upgrade-drain-v2-nar");
+
+    /**
+     * Makes the version 2 UpgradeDrainConnector misbehave. The Connector 
reads the named file from its node's working
+     * directory, and the file contents select the behavior. Faults that share 
a file replace each other.
+     */
+    protected enum UpgradeDrainFault {
+        
PREPARATION_RETURNS_NULL_FUTURE("upgrade-drain-preparation-result.txt", 
"NULL_FUTURE"),
+        PREPARATION_FAILS("upgrade-drain-preparation-result.txt", 
"FAILED_FUTURE"),
+        
PREPARATION_UPDATES_PRE_UPGRADE_FLOW("upgrade-drain-preparation-result.txt", 
"UPDATE_PRE_UPGRADE_FLOW"),
+        ACTIVE_FLOW_RETURNS_NULL("upgrade-drain-active-flow.txt", "NULL"),
+        ACTIVE_FLOW_THROWS("upgrade-drain-active-flow.txt", "THROW"),
+        APPLY_FAILS("upgrade-drain-apply-fail.txt", "");
+
+        private final String fileName;
+        private final String contents;
+
+        UpgradeDrainFault(final String fileName, final String contents) {
+            this.fileName = fileName;
+            this.contents = contents;
+        }
+    }
+
+    /**
+     * Identifies a Connector bundle in assertions. A null version matches any 
version of the artifact.
+     */
+    protected record ExpectedBundle(String artifact, String version) {
+
+        String recordPrefix() {
+            return "org.apache.nifi:" + artifact + ":" + (version == null ? "" 
: version);
+        }
+
+        void assertMatches(final Bundle bundle, final String description) {
+            assertNotNull(bundle, description);
+            assertTrue(bundle.getArtifact().contains(artifact), description + 
" " + bundle.getArtifact());
+            if (version != null) {
+                assertEquals(version, bundle.getVersion(), description);
+            }
+        }
+    }
+
+    /**
+     * Creates the Connector on its outgoing version and applies the given 
configuration.
+     */
+    protected abstract String createOutgoingConnector(Map<String, String> 
properties) throws Exception;
+
+    /**
+     * Moves the stopped Connector to its incoming version, leaving it STOPPED 
with the upgrade pending.
+     */
+    protected abstract void triggerUpgrade(String connectorId) throws 
Exception;
+
+    protected abstract ExpectedBundle outgoingBundle();
+
+    protected abstract ExpectedBundle incomingBundle();
+
+    @BeforeEach
+    public void clearFaults() {
+        for (final UpgradeDrainFault fault : UpgradeDrainFault.values()) {
+            clearFault(fault);
+        }
+    }
+
+    protected void injectFault(final UpgradeDrainFault fault) throws 
IOException {
+        for (int nodeIndex = 1; nodeIndex <= nodeCount(); nodeIndex++) {
+            injectFault(nodeIndex, fault);
+        }
+    }
+
+    protected void injectFault(final int nodeIndex, final UpgradeDrainFault 
fault) throws IOException {
+        writeToNodeDirectory(nodeIndex, fault.fileName, fault.contents);
+    }
+
+    protected void clearFault(final UpgradeDrainFault fault) {
+        deleteFromNodeDirectories(fault.fileName);
+    }
+
+    protected String createOutgoingConnector(final String mode, final boolean 
removeConnection, final String comment) throws Exception {
+        return createOutgoingConnector(properties(mode, removeConnection, 
comment));
+    }
+
+    protected String queueStoppedUpgrade(final String mode, final boolean 
removeConnection, final String comment) throws Exception {
+        return queueAtLeastOneFlowFileAndStop(createOutgoingConnector(mode, 
removeConnection, comment));
+    }
+
+    protected String queueAtLeastOneFlowFileAndStop(final String connectorId) 
throws Exception {
+        holdFlowFiles();
+        getClientUtil().startConnector(connectorId);
+        waitForConnectorMinQueueCount(connectorId, 1);
+        getClientUtil().stopConnector(connectorId);
+        return connectorId;
+    }
+
+    protected void restartKeepingIncomingVersion() {
+        getNiFiInstance().stop();
+        getNiFiInstance().start(true);
+        waitForCluster();
+    }
+
+    protected void uploadDrainNars(final String... versions) throws Exception {
+        final File narsLocation = new File(DRAIN_NARS_LOCATION);
+        if (!narsLocation.exists()) {
+            throw new IllegalStateException("NARs location does not exist at: 
" + narsLocation.getAbsolutePath());
+        }
+
+        final NarUploadUtil narUploadUtil = new NarUploadUtil(getNifiClient());
+        for (final String version : versions) {
+            final NarSummaryDTO nar = narUploadUtil.uploadNar(narsLocation, 
DRAIN_NAR_IDS.get(version));
+            
waitFor(narUploadUtil.getWaitForNarStateSupplier(nar.getIdentifier(), 
NarState.INSTALLED));
+        }
+    }
+
+    protected String createDrainConnector(final String version, final 
Map<String, String> properties) throws NiFiClientException, IOException, 
InterruptedException {
+        final ConnectorEntity connector = 
getClientUtil().createConnector(DRAIN_CONNECTOR_TYPE, DRAIN_BUNDLE_GROUP, 
DRAIN_BUNDLE_ARTIFACT, version);
+        getClientUtil().configureConnector(connector, "Upgrade", properties);
+        getClientUtil().applyConnectorUpdate(connector);
+        getClientUtil().waitForValidConnector(connector.getId());
+        return connector.getId();
+    }
+
+    /**
+     * Sends the version change the way the UI does: a PUT carrying only the 
new bundle.
+     */
+    protected ConnectorEntity changeVersion(final String connectorId, final 
String version) throws NiFiClientException, IOException {
+        final ConnectorClient connectorClient = 
getNifiClient().getConnectorClient();
+        final ConnectorEntity current = 
connectorClient.getConnector(connectorId);
+        final ConnectorDTO component = new ConnectorDTO();
+        component.setId(connectorId);
+        component.setBundle(new BundleDTO(DRAIN_BUNDLE_GROUP, 
DRAIN_BUNDLE_ARTIFACT, version));
+
+        final ConnectorEntity update = new ConnectorEntity();
+        update.setId(connectorId);
+        update.setRevision(current.getRevision());
+        update.setComponent(component);
+        return connectorClient.updateConnector(update);
+    }
+
+    /**
+     * Stops the generator and lets the terminator drop queued FlowFiles on 
every node.
+     */
+    protected void releaseFlowFiles() throws IOException {
+        for (int nodeIndex = 1; nodeIndex <= nodeCount(); nodeIndex++) {
+            releaseFlowFiles(nodeIndex);
+        }
+    }
+
+    /**
+     * Stops the generator and lets the terminator drop queued FlowFiles on 
one node.
+     */
+    protected void releaseFlowFiles(final int nodeIndex) throws IOException {
+        writeToNodeDirectory(nodeIndex, GATE_FILE, "");
+    }
+
+    /**
+     * Lets the generator run and makes the terminator hold every FlowFile on 
every node.
+     */
+    protected void holdFlowFiles() {
+        deleteFromNodeDirectories(GATE_FILE);
+    }
+
+    private void writeToNodeDirectory(final int nodeIndex, final String 
fileName, final String contents) throws IOException {
+        Files.writeString(new File(nodeDirectory(nodeIndex), 
fileName).toPath(), contents);
+    }
+
+    private void deleteFromNodeDirectories(final String fileName) {
+        for (final File directory : instanceDirectories()) {
+            final File file = new File(directory, fileName);
+            if (file.exists()) {
+                assertTrue(file.delete(), file.getAbsolutePath());
+            }
+        }
+    }
+
+    protected void stopAndAssertNotPending(final String connectorId) throws 
IOException {

Review Comment:
   [Claude Opus 5.5] Done. I removed the stop-and-read helper. The tests now 
re-read flow.json.gz inside waitFor, with NiFi still running, until it shows 
the expected bundle, or they check through REST. The stop-then-read calls at 
the end of the ConnectorChangeVersionDrainIT tests are gone too.
   
   Two tests restart NiFi now: 
ConnectorChangeVersionDrainIT.testRestartKeepsPendingUpgrades and the clustered 
NAR-swap test in ClusteredConnectorUpgradeDrainIT.



-- 
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]

Reply via email to