Skip to content

Commit

Permalink
Allow to set env options to the Asadmin
Browse files Browse the repository at this point in the history
- And use them to set the first start-domain timeout to get at least some
  response on timeout. There are two timeouts now.

Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Jun 13, 2023
1 parent c805d45 commit 87ac14c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import org.glassfish.admin.rest.client.ClientWrapper;
import org.glassfish.main.itest.tools.asadmin.Asadmin;
import org.glassfish.main.itest.tools.asadmin.AsadminResult;
import org.glassfish.main.itest.tools.asadmin.AsadminResultMatcher;

import static org.glassfish.main.itest.tools.asadmin.AsadminResultMatcher.asadminOK;
import static org.hamcrest.MatcherAssert.assertThat;

/**
Expand All @@ -62,6 +62,7 @@ public class GlassFishTestEnvironment {
private static final File PASSWORD_FILE_FOR_UPDATE = findPasswordFile("password_update.txt");
private static final File PASSWORD_FILE = findPasswordFile("password.txt");

private static final int ASADMIN_START_DOMAIN_TIMEOUT = 30_000;

static {
LOG.log(Level.INFO, "Using basedir: {0}", BASEDIR);
Expand All @@ -71,9 +72,17 @@ public class GlassFishTestEnvironment {
getAsadmin().exec(10_000, "stop-domain", "--kill", "--force");
});
Runtime.getRuntime().addShutdownHook(hook);
assertThat(getAsadmin().exec(30_000, "start-domain", "--debug"), AsadminResultMatcher.asadminOK());
Asadmin asadmin = getAsadmin().withEnv(ADMIN_USER, ADMIN_PASSWORD);
if (System.getenv("AS_START_TIMEOUT") == null) {
// AS_START_TIMEOUT for the detection that "the server is running!"
// START_DOMAIN_TIMEOUT for us waiting for the end of the asadmin start-domain process.
asadmin.withEnv("AS_START_TIMEOUT", Integer.toString(ASADMIN_START_DOMAIN_TIMEOUT - 5000));
}
// This is the absolutely first start - if it fails, all other starts will fail too.
assertThat(asadmin.exec(ASADMIN_START_DOMAIN_TIMEOUT, "start-domain", "--debug"), asadminOK());
}


/**
* @return {@link Asadmin} command api for tests.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Eclipse Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2023 Eclipse Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -23,7 +23,10 @@
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.function.Function;
import java.util.logging.Level;
Expand Down Expand Up @@ -55,6 +58,7 @@ public class Asadmin {
private final File asadmin;
private final String adminUser;
private final File adminPasswordFile;
private final Map<String, String> environment = new HashMap<>();


/**
Expand All @@ -71,6 +75,19 @@ public Asadmin(final File asadmin, final String adminUser, final File adminPassw
}


/**
* Adds environment property set for the asadmin execution.
*
* @param name
* @param value
* @return this
*/
public Asadmin withEnv(final String name, final String value) {
this.environment.put(name, value);
return this;
}


/**
* @return asadmin command file name
*/
Expand Down Expand Up @@ -124,8 +141,8 @@ public AsadminResult exec(final String... args) {
/**
* Executes the command with arguments synchronously with given timeout in millis.
*
* @param timeout
* @param args
* @param timeout timeout in millis
* @param args command and arguments.
* @return {@link AsadminResult} never null.
*/
public AsadminResult exec(final int timeout, final String... args) {
Expand All @@ -136,8 +153,8 @@ public AsadminResult exec(final int timeout, final String... args) {
* Executes the command with arguments.
*
* @param timeout timeout in millis
* @param detachedAndTerse - detached command is executed asynchronously, can be attached later by the attach command.
* @param args - command and arguments.
* @param detachedAndTerse detached command is executed asynchronously, can be attached later by the attach command.
* @param args command and arguments.
* @return {@link AsadminResult} never null.
*/
private AsadminResult exec(final int timeout, final boolean detachedAndTerse, final String... args) {
Expand All @@ -159,16 +176,19 @@ private AsadminResult exec(final int timeout, final boolean detachedAndTerse, fi
final ProcessManager processManager = new ProcessManager(command);
processManager.setTimeoutMsec(timeout);
processManager.setEcho(false);
if (LOG.isLoggable(Level.FINEST)) {
processManager.setEnvironment("AS_TRACE","true");
for (Entry<String, String> env : this.environment.entrySet()) {
processManager.setEnvironment(env.getKey(), env.getValue());
}
if (System.getenv("AS_TRACE") == null && LOG.isLoggable(Level.FINEST)) {
processManager.setEnvironment("AS_TRACE", "true");
}

int exitCode;
String asadminErrorMessage = "";
try {
exitCode = processManager.execute();
} catch (final ProcessManagerTimeoutException e) {
asadminErrorMessage = "ProcessManagerTimeoutException: command timed out after " + timeout + " ms.\n";
asadminErrorMessage = e.getMessage();
exitCode = 1;
} catch (final ProcessManagerException e) {
LOG.log(Level.SEVERE, "The execution failed.", e);
Expand All @@ -183,7 +203,12 @@ private AsadminResult exec(final int timeout, final boolean detachedAndTerse, fi
} else {
result = new AsadminResult(args[0], exitCode, processManager.getStdout(), stdErr);
}
System.out.print(result.getStdOut());
if (!result.getStdOut().isEmpty()) {
System.out.print(result.getStdOut());
}
if (!result.getStdErr().isEmpty()) {
System.err.println(result.getStdErr());
}
return result;
}
}

0 comments on commit 87ac14c

Please sign in to comment.