Skip to content

Commit

Permalink
MID-8842 ninja - stop on verify error implemented
Browse files Browse the repository at this point in the history
(cherry picked from commit 41868d2)
  • Loading branch information
1azyman committed Jul 19, 2023
1 parent bf182ca commit 8c949b1
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ public boolean isEmpty() {
public boolean hasChanges() {
return items.stream().anyMatch(i -> i.isChanged());
}

public boolean hasCritical() {
return items.stream().anyMatch(i -> i.getPriority() != null && i.getPriority() == UpgradePriority.CRITICAL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;

import com.evolveum.midpoint.schema.validator.UpgradeValidationResult;

import org.apache.commons.io.FileUtils;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -80,15 +82,19 @@ private VerifyResult verifyFiles() throws IOException {

for (File file : options.getFiles()) {
if (!file.isDirectory()) {
verifyFile(file, reporter, writer);
if (!verifyFile(file, reporter, writer)) {
break;
}
} else {
Collection<File> children = FileUtils.listFiles(file, new String[] { "xml" }, true);
for (File child : children) {
if (child.isDirectory()) {
continue;
}

verifyFile(child, reporter, writer);
if (!verifyFile(child, reporter, writer)) {
break;
}
}
}
}
Expand All @@ -104,18 +110,24 @@ private VerifyResult verifyFiles() throws IOException {
return reporter.getResult();
}

private void verifyFile(File file, VerificationReporter reporter, Writer writer) {
private boolean verifyFile(File file, VerificationReporter reporter, Writer writer) {
PrismContext prismContext = context.getPrismContext();
ParsingContext parsingContext = prismContext.createParsingContextForCompatibilityMode();
PrismParser parser = prismContext.parserFor(file).language(PrismContext.LANG_XML).context(parsingContext);

boolean shouldContinue = true;
try {
List<PrismObject<? extends Objectable>> objects = parser.parseObjects();
for (PrismObject<? extends Objectable> object : objects) {
reporter.verify(writer, object);
UpgradeValidationResult result = reporter.verify(writer, object);
if (options.isStopOnCriticalError() && result.hasCritical()) {
shouldContinue = false;
}
}
} catch (Exception ex) {
log.error("Couldn't verify file '{}'", ex, file.getPath());
}

return shouldContinue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public VerificationCategoryConverter() {

public static final String P_REPORT_STYLE = "--report-style";

public static final String P_CONTINUE_VERIFICATION_ON_ERROR = "--continue-verification-on-error";
public static final String P_STOP_ON_CRITICAL_ERROR = "--stop-on-critical-error";

public static final String P_FILE = "--file";
public static final String P_PLANNED_REMOVAL_VERSION = "--planned-removal-version";
Expand All @@ -71,8 +71,8 @@ public VerificationCategoryConverter() {
converter = ReportStyleConverter.class, validateWith = ReportStyleConverter.class)
private ReportStyle reportStyle = ReportStyle.PLAIN;

@Parameter(names = { P_CONTINUE_VERIFICATION_ON_ERROR }, descriptionKey = "verify.continueVerificationOnError")
private boolean continueVerificationOnError = true;
@Parameter(names = { P_STOP_ON_CRITICAL_ERROR }, descriptionKey = "verify.stopOnCriticalError")
private boolean stopOnCriticalError;

@Parameter(names = { P_FILE }, descriptionKey = "verify.files", variableArity = true)
private List<File> files = new ArrayList<>();
Expand All @@ -96,13 +96,12 @@ public void setReportStyle(ReportStyle reportStyle) {
this.reportStyle = reportStyle;
}

// todo make use of this
public boolean isContinueVerificationOnError() {
return continueVerificationOnError;
public boolean isStopOnCriticalError() {
return stopOnCriticalError;
}

public void setContinueVerificationOnError(boolean continueVerificationOnError) {
this.continueVerificationOnError = continueVerificationOnError;
public void setStopOnCriticalError(boolean stopOnCriticalError) {
this.stopOnCriticalError = stopOnCriticalError;
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.stream.Collectors;

import com.evolveum.midpoint.ninja.action.upgrade.UpgradeConstants;

import org.apache.commons.io.FileUtils;
import org.fusesource.jansi.Ansi;

Expand Down Expand Up @@ -45,7 +46,7 @@ public Void execute() throws Exception {
if (!options.isSkipVerification()) {
VerifyOptions verifyOptions = new VerifyOptions();
verifyOptions.setMultiThread(options.getVerificationThreads());
verifyOptions.setContinueVerificationOnError(options.isContinueVerificationOnError());
verifyOptions.setStopOnCriticalError(options.isStopOnCriticalError());

VerifyAction verifyAction = new VerifyAction();
verifyAction.init(context, verifyOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ public class UpgradeDistributionOptions {
public static final String P_INSTALLATION_DIRECTORY = "--installation-directory";

public static final String P_SKIP_VERIFICATION = "--skip-verification";

public static final String P_VERIFICATION_THREADS = "--verification-threads";

public static final String P_SKIP_PRE_CHECK = "--skip-pre-check";

public static final String P_CONTINUE_VERIFICATION_ON_ERROR = "--continue-verification-on-error";
public static final String P_STOP_ON_CRITICAL_ERROR = "--stop-on-critical-error";

@Parameter(names = { P_TEMP_DIR_LONG }, descriptionKey = "upgradeDistribution.tempDir")
private File tempDirectory;
Expand All @@ -42,8 +39,8 @@ public class UpgradeDistributionOptions {
@Parameter(names = { P_VERIFICATION_THREADS }, descriptionKey = "upgradeDistribution.verificationThreads")
private int verificationThreads = 1;

@Parameter(names = { P_CONTINUE_VERIFICATION_ON_ERROR }, descriptionKey = "upgradeDistribution.continueVerificationOnError")
private boolean continueVerificationOnError = true;
@Parameter(names = { P_STOP_ON_CRITICAL_ERROR }, descriptionKey = "upgradeDistribution.stopOnCriticalError")
private boolean stopOnCriticalError = true;

@Parameter(names = { P_SKIP_PRE_CHECK }, descriptionKey = "upgradeDistribution.skipPreCheck")
private boolean skipPreCheck;
Expand Down Expand Up @@ -96,12 +93,12 @@ public void setVerificationThreads(int verificationThreads) {
this.verificationThreads = verificationThreads;
}

public boolean isContinueVerificationOnError() {
return continueVerificationOnError;
public boolean isStopOnCriticalError() {
return stopOnCriticalError;
}

public void setContinueVerificationOnError(boolean continueVerificationOnError) {
this.continueVerificationOnError = continueVerificationOnError;
public void setStopOnCriticalError(boolean stopOnCriticalError) {
this.stopOnCriticalError = stopOnCriticalError;
}

public boolean isSkipPreCheck() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.schema.DeltaConversionOptions;
import com.evolveum.midpoint.schema.DeltaConvertor;
import com.evolveum.midpoint.schema.validator.UpgradeValidationResult;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;

/**
Expand Down Expand Up @@ -70,7 +71,10 @@ protected void write(Writer writer, ObjectType object) throws IOException {
try {
PrismObject<?> cloned = prismObject.clone();

reporter.verify(writer, cloned);
UpgradeValidationResult result = reporter.verify(writer, cloned);
if (options.isStopOnCriticalError() && result.hasCritical()) {
shouldConsumerStop();
}
ObjectDelta delta = prismObject.diff(cloned);

if (delta.isEmpty()) {
Expand Down
4 changes: 2 additions & 2 deletions tools/ninja/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ search.filter=Value of object filter used to search objects. If you start the fi
letter @, the rest should be a filename. Start the filter with % to use Axiom query language.
upgradeDistribution.skipVerification=Skip verification before upgrade
upgradeDistribution.verificationThreads=Number of threads used for verification
upgradeDistribution.continueVerificationOnError=Whether to stop verification on first error
upgradeDistribution.stopOnCriticalError=Stop verification when critical upgrade item is found.
preUpgradeCheck=Pre-upgrade check
preUpgradeCheck.skipNodesVersionCheck=Skip check of nodes version
preUpgradeCheck.skipDatabaseVersionCheck=Skip check of database version
verify.continueVerificationOnError=Continue verification when error is found.
verify.stopOnCriticalError=Stop verification when critical upgrade item is found.
verify.files=Files/Directories for verification.
upgradeDistribution.skipPreCheck=Skip pre-upgrade checks.
upgradeObjects.files=Files/Directories marked for upgrade.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
*/
public interface NinjaTestMixin {

StreamValidator EMPTY_STREAM_VALIDATOR = list -> Assertions.assertThat(list).isEmpty();
StreamValidator EMPTY_STREAM_VALIDATOR = list -> Assertions.assertThat(list)
.withFailMessage(() -> StringUtils.join(list, "\n"))
.isEmpty();

StreamValidator NOOP_STREAM_VALIDATOR = list -> {
};
Expand Down Expand Up @@ -158,8 +160,10 @@ private <R> R executeWithWrappedPrintStreams(
PrintStream out = new PrintStream(bosOut);
PrintStream err = new PrintStream(bosErr)
) {

return function.apply(out, err);
} catch (Exception ex) {
LOGGER.error("Exception during test execution", ex);
throw ex;
} finally {
List<String> outLines = processTestOutputStream(bosOut, "OUT");
List<String> errLines = processTestOutputStream(bosErr, "ERR");
Expand Down

0 comments on commit 8c949b1

Please sign in to comment.