Skip to content

Commit

Permalink
MID-8842 ninja - ninja now can output non-zer error code in some case…
Browse files Browse the repository at this point in the history
…s, actions still need to be updated

(cherry picked from commit bbe3377)
  • Loading branch information
1azyman committed Jul 31, 2023
1 parent 81ee183 commit 4f5dbeb
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 50 deletions.
38 changes: 27 additions & 11 deletions tools/ninja/src/main/java/com/evolveum/midpoint/ninja/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.io.InputStream;
import java.io.PrintStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -32,7 +33,12 @@
public class Main {

public static void main(String[] args) {
new Main().run(args);
MainResult result = new Main().run(args);

int exitCode = result.getExitCode();
if (exitCode != 0) {
System.exit(exitCode);
}
}

private PrintStream out = System.out;
Expand All @@ -56,7 +62,7 @@ public void setErr(@NotNull PrintStream err) {
}

// todo main doesn't return error code non zero if error occurrs, fix this
protected <T> Object run(String[] args) {
protected <T> @NotNull MainResult run(String[] args) {
AnsiConsole.systemInstall();

JCommander jc = NinjaUtils.setupCommandLineParser();
Expand All @@ -65,7 +71,7 @@ protected <T> Object run(String[] args) {
jc.parse(args);
} catch (ParameterException ex) {
err.println(ex.getMessage());
return null;
return MainResult.EMPTY_ERROR;
}

String parsedCommand = jc.getParsedCommand();
Expand All @@ -78,17 +84,17 @@ protected <T> Object run(String[] args) {
err.println("Cant' use " + BaseOptions.P_VERBOSE + " and " + BaseOptions.P_SILENT
+ " together (verbose and silent)");
printHelp(jc, parsedCommand);
return null;
return MainResult.EMPTY_ERROR;
}

if (BooleanUtils.isTrue(base.isVersion())) {
printVersion(base.isVerbose());
return null;
return MainResult.EMPTY_SUCCESS;
}

if (base.isHelp() || parsedCommand == null) {
printHelp(jc, parsedCommand);
return null;
return MainResult.EMPTY_SUCCESS;
}

NinjaContext context = null;
Expand All @@ -97,7 +103,7 @@ protected <T> Object run(String[] args) {

if (action == null) {
err.println("Action for command '" + parsedCommand + "' not found");
return null;
return MainResult.EMPTY_ERROR;
}

//noinspection unchecked
Expand All @@ -113,21 +119,25 @@ protected <T> Object run(String[] args) {

context.getLog().info(ConsoleFormat.formatActionStartMessage(action));

return action.execute();
Object result = action.execute();

return new MainResult(result);
} finally {
action.destroy();
}
} catch (InputParameterException ex) {
err.println("ERROR: " + ex.getMessage());

return MainResult.EMPTY_ERROR;
} catch (Exception ex) {
handleException(base, ex);

return MainResult.EMPTY_ERROR;
} finally {
cleanupResources(base, context);

AnsiConsole.systemUninstall();
}

return null;
}

private void cleanupResources(BaseOptions opts, NinjaContext context) {
Expand Down Expand Up @@ -158,7 +168,13 @@ private void handleException(BaseOptions opts, Exception ex) {
}

private void printVersion(boolean verbose) {
try (InputStream is = Main.class.getResource("/version").openStream()) {
URL url = Main.class.getResource("/version");
if (url == null) {
err.println("Couldn't obtain version");
return;
}

try (InputStream is = url.openStream()) {
String version = IOUtils.toString(is, StandardCharsets.UTF_8).trim();
out.println(version);
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2010-2023 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/

package com.evolveum.midpoint.ninja;

public class MainResult {

public static final MainResult EMPTY_ERROR = new MainResult(null, 1);

public static final MainResult EMPTY_SUCCESS = new MainResult(null, 0);

private Object object;

private int exitCode;

public MainResult(Object object) {
this(object, 0);
}

public MainResult(Object object, int exitCode) {
this.object = object;
this.exitCode = exitCode;
}

public Object getObject() {
return object;
}

public int getExitCode() {
return exitCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import java.io.*;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.*;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
Expand Down Expand Up @@ -319,29 +316,52 @@ private List<String> createReportRecord(UpgradeValidationItem item, PrismObject<
);
}

private void writeValidationItem(Writer writer, PrismObject<?> object, UpgradeValidationItem validationItem) throws IOException {
if (validationItem.getItem().getStatus() != null) {
writer.append(validationItem.getItem().getStatus().toString());
writer.append(" ");
private void writeValidationItem(Writer writer, PrismObject<?> object, UpgradeValidationItem item) throws IOException {
ValidationItem validationItem = item.getItem();

List<Object> items = new ArrayList<>();

if (validationItem.getStatus() != null) {
items.add(validationItem.getStatus());
} else {
writer.append("INFO ");
}
writer.append(object.toString());
writer.append(" ");
if (validationItem.getItem().getItemPath() != null) {
writer.append(validationItem.getItem().getItemPath().toString());
writer.append(" ");

items.add(object.toDebugName());

UpgradePhase phase = item.getPhase();
if (phase != null) {
items.add(phase);
}

UpgradePriority priority = item.getPriority();
if (priority != null) {
items.add(priority);
}

UpgradeType type = item.getType();
if (type != null) {
items.add(type);
}
writeMessage(writer, validationItem.getItem().getMessage());
writer.append("\n");

if (validationItem.getItemPath() != null) {
items.add(validationItem.getItemPath());
}

String msg = writeMessage(validationItem.getMessage());
if (msg != null) {
items.add(msg);
}

writer.write(StringUtils.join(items, " "));
}

private void writeMessage(Writer writer, LocalizableMessage message) throws IOException {
private String writeMessage(LocalizableMessage message) throws IOException {
if (message == null) {
return;
return null;
}
// TODO: localization?
writer.append(message.getFallbackMessage());
return message.getFallbackMessage();
}

public VerifyResult getResult() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.api.Assertions;
Expand Down Expand Up @@ -64,19 +63,13 @@ interface StreamValidator {
* This is not enough to support tests on other DB (it doesn't run dbtest profile properly)
* or for Native repository, but {@link #clearMidpointTestDatabase(ApplicationContext)} can be used in the preExecute block.
*/
default void setupMidpointHome() throws IOException {
// FileUtils.deleteDirectory(TARGET_HOME);
//
// File baseHome = new File(RESOURCES_DIRECTORY, "midpoint-home");
//
// FileUtils.copyDirectory(baseHome, TARGET_HOME);
//
// // This tells Ninja to use the right config XML for Native repo.
// // Ninja tests don't support test.config.file property as other midPoint tests.
// String testConfigFile = System.getProperty("test.config.file");
// if (testConfigFile != null) {
// System.setProperty(MidpointConfiguration.MIDPOINT_CONFIG_FILE_PROPERTY, testConfigFile);
// }
default void setupMidpointHome() {
// This tells Ninja to use the right config XML for Native repo.
// Ninja tests don't support test.config.file property as other midPoint tests.
String testConfigFile = System.getProperty("test.config.file");
if (testConfigFile != null) {
System.setProperty(MidpointConfiguration.MIDPOINT_CONFIG_FILE_PROPERTY, testConfigFile);
}
}

default void clearMidpointTestDatabase(ApplicationContext context) {
Expand Down Expand Up @@ -178,7 +171,7 @@ private <R> R executeWithWrappedPrintStreams(
}
}

default Object executeTest(
default MainResult executeTest(
@Nullable StreamValidator validateOut, @Nullable StreamValidator validateErr, @NotNull String... args)
throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void test100TestNoNodes() throws Exception {

when();

Boolean result = (Boolean) executeTest(
MainResult result = executeTest(
list -> {
boolean found = list.stream().anyMatch(
s -> s.contains("There are zero nodes in cluster to validate current midPoint version"));
Expand All @@ -35,7 +35,8 @@ public void test100TestNoNodes() throws Exception {

then();

Assertions.assertThat(result)
Boolean shouldContinue = (Boolean) result.getObject();
Assertions.assertThat(shouldContinue)
.isTrue()
.withFailMessage("Upgrade pre-check - should continue (true).");
}
Expand All @@ -54,7 +55,7 @@ public void test200TestWrongSchemaVersion() throws Exception {

when();

Boolean result = (Boolean) executeTest(
MainResult result = executeTest(
list -> {
boolean found = list.stream().anyMatch(
s -> s.contains("There are zero nodes in cluster to validate current midPoint version"));
Expand All @@ -66,7 +67,8 @@ public void test200TestWrongSchemaVersion() throws Exception {

then();

Assertions.assertThat(result)
Boolean shouldContinue = (Boolean) result.getObject();
Assertions.assertThat(shouldContinue)
.isFalse()
.withFailMessage("Upgrade pre-check - DB schema version doesn't match.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void test100VerifyFiles() throws Exception {
VerifyResult result = (VerifyResult) mainResult.getObject();

Assertions.assertThat(result).isNotNull();
Assertions.assertThat(result.getItemPriorityCount(UpgradePriority.OPTIONAL)).isEqualTo(1L);
Assertions.assertThat(result.getItemPriorityCount(UpgradePriority.OPTIONAL)).isEqualTo(11L);

Assertions.assertThat(OUTPUT)
.exists()
Expand Down Expand Up @@ -126,11 +126,15 @@ public void test200UpgradeFiles() throws Exception {
public void test300VerifyObjects() throws Exception {
given();

RepoAddOptions opts = new RepoAddOptions();
opts.setAllowUnencryptedValues(true);
opts.setOverwrite(true);

Collection<File> files = FileUtils.listFiles(TARGET_FILES, new String[] { "xml" }, true);
for (File file : files) {
List<PrismObject<? extends Objectable>> objects = PrismTestUtil.parseObjectsCompat(file);
for (PrismObject<? extends Objectable> object : objects) {
repository.addObject((PrismObject) object, RepoAddOptions.createOverwrite(), new OperationResult("add object"));
repository.addObject((PrismObject) object, opts, new OperationResult("add object"));
}
}

Expand Down Expand Up @@ -159,4 +163,33 @@ public void test300VerifyObjects() throws Exception {

// todo more asserts
}

@Test
public void test400VerifyObjects() throws Exception {
given();

RepoAddOptions opts = new RepoAddOptions();
opts.setAllowUnencryptedValues(true);
opts.setOverwrite(true);

Collection<File> files = FileUtils.listFiles(TARGET_FILES, new String[] { "xml" }, true);
for (File file : files) {
List<PrismObject<? extends Objectable>> objects = PrismTestUtil.parseObjectsCompat(file);
for (PrismObject<? extends Objectable> object : objects) {
repository.addObject((PrismObject) object, opts, new OperationResult("add object"));
}
}

when();

MainResult mainResult = executeTest(NOOP_STREAM_VALIDATOR, NOOP_STREAM_VALIDATOR,
"-v",
"-m", getMidpointHome(),
"verify");

VerifyResult result = (VerifyResult) mainResult.getObject();
Assertions.assertThat(result).isNotNull();

// todo more asserts
}
}

0 comments on commit 4f5dbeb

Please sign in to comment.