Skip to content

Commit

Permalink
Stabilized admin-tests
Browse files Browse the repository at this point in the history
- stop-removefiles-start principle replaced by asadmin commands (junit extension)
- discovery: detached jobs don't survive restarting domain, synchronous do.

Signed-off-by: David Matějček <dmatej@seznam.cz>
  • Loading branch information
dmatej committed Apr 12, 2022
1 parent cc9eb2e commit 11ed0f3
Show file tree
Hide file tree
Showing 14 changed files with 360 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public void createInstancesTest() {
@Test
@Order(3)
public void startInstancesTest() {
assertThat(ASADMIN.exec(30_000, false, "start-local-instance", INSTANCE_NAME_1), asadminOK());
assertThat(ASADMIN.exec(30_000, false, "start-local-instance", INSTANCE_NAME_2), asadminOK());
assertThat(ASADMIN.exec(30_000, "start-local-instance", INSTANCE_NAME_1), asadminOK());
assertThat(ASADMIN.exec(30_000, "start-local-instance", INSTANCE_NAME_2), asadminOK());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,26 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.glassfish.main.admin.test.tool.asadmin.Asadmin;
import org.glassfish.main.admin.test.tool.asadmin.AsadminResult;
import org.glassfish.main.admin.test.tool.asadmin.DetachedTerseAsadminResult;
import org.glassfish.main.admin.test.tool.asadmin.GlassFishTestEnvironment;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.glassfish.main.admin.test.tool.AsadminResultMatcher.asadminOK;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -51,20 +49,22 @@
/**
* @author martinmares
*/
@ExtendWith(JobTestExtension.class)
public class DetachAttachITest {
private static final Logger LOG = Logger.getLogger(DetachAttachITest.class.getName());
private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin();


@Test
public void uptimePeriodically() throws Exception {
Set<String> ids = new HashSet<>();
for (int i = 0; i < 3; i++) {
LOG.log(Level.FINE, "detachAndAttachUptimePeriodically(): round " + i);
final String id;
{
AsadminResult result = ASADMIN.execDetached("--terse", "uptime");
DetachedTerseAsadminResult result = ASADMIN.execDetached("uptime");
assertThat(result, asadminOK());
id = parseJobIdFromEchoTerse(result.getStdOut());
id = result.getJobId();
assertTrue(ids.add(id));
}
Thread.sleep(1000L);
Expand All @@ -79,47 +79,36 @@ public void uptimePeriodically() throws Exception {

@Test
public void commandWithProgressStatus() throws Exception {
AsadminResult result = ASADMIN.execDetached("--terse", "progress-custom", "6x1");
assertThat(result, asadminOK());
String id = parseJobIdFromEchoTerse(result.getStdOut());
final DetachedTerseAsadminResult detached = ASADMIN.execDetached("progress-custom", "6x1");
assertThat(detached, asadminOK());
Thread.sleep(2000L);
// Now attach running
result = ASADMIN.exec("attach", id);
assertThat(result, asadminOK());
assertThat(result.getStdOut(), stringContainsInOrder("progress-custom"));
List<ProgressMessage> prgs = ProgressMessage.grepProgressMessages(result.getStdOut());
final AsadminResult attachResult = ASADMIN.exec("attach", detached.getJobId());
assertThat(attachResult, asadminOK());
assertThat(attachResult.getStdOut(), stringContainsInOrder("progress-custom"));
List<ProgressMessage> prgs = ProgressMessage.grepProgressMessages(attachResult.getStdOut());
assertFalse(prgs.isEmpty());
assertThat(prgs.get(0).getValue(), greaterThan(0));
assertEquals(100, prgs.get(prgs.size() - 1).getValue());
// Now attach finished - must NOT exist - seen progress job is removed
assertThat(ASADMIN.exec("attach", id), not(asadminOK()));
assertThat(ASADMIN.exec("attach", detached.getJobId()), not(asadminOK()));
}


@Test
public void detachOnesAttachMulti() throws Exception {
ExecutorService pool = Executors.newCachedThreadPool(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread result = new Thread(r);
result.setDaemon(true);
return result;
}
ExecutorService pool = Executors.newCachedThreadPool(r -> {
Thread result = new Thread(r);
result.setDaemon(true);
return result;
});
AsadminResult result = ASADMIN.execDetached("--terse", "progress-custom", "8x1");
final DetachedTerseAsadminResult result = ASADMIN.execDetached("progress-custom", "8x1");
assertThat(result, asadminOK());
final String id = parseJobIdFromEchoTerse(result.getStdOut());
assertNotNull(id, "id");
assertNotNull(result.getJobId(), "id");
Thread.sleep(1500L);
final int attachCount = 3;
Collection<Callable<AsadminResult>> attaches = new ArrayList<>(attachCount);
for (int i = 0; i < attachCount; i++) {
attaches.add(new Callable<AsadminResult>() {
@Override
public AsadminResult call() throws Exception {
return ASADMIN.exec("attach", id);
}
});
attaches.add(() -> ASADMIN.exec("attach", result.getJobId()));
}
List<Future<AsadminResult>> results = pool.invokeAll(attaches);
for (Future<AsadminResult> fRes : results) {
Expand All @@ -132,10 +121,4 @@ public AsadminResult call() throws Exception {
assertEquals(100, prgs.get(prgs.size() - 1).getValue());
}
}

private String parseJobIdFromEchoTerse(String str) {
List<Object> stok = Collections.list(new StringTokenizer(str));
assertThat(stok, hasSize(1));
return (String) stok.get(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,17 @@

import org.glassfish.main.admin.test.tool.asadmin.Asadmin;
import org.glassfish.main.admin.test.tool.asadmin.AsadminResult;
import org.junit.jupiter.api.BeforeEach;
import org.glassfish.main.admin.test.tool.asadmin.DetachedTerseAsadminResult;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;

import static org.glassfish.main.admin.test.tool.AsadminResultMatcher.asadminOK;
import static org.glassfish.main.admin.test.tool.asadmin.GlassFishTestEnvironment.deleteJobsFile;
import static org.glassfish.main.admin.test.tool.asadmin.GlassFishTestEnvironment.deleteOsgiDirectory;
import static org.glassfish.main.admin.test.tool.asadmin.GlassFishTestEnvironment.getAsadmin;

import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.glassfish.main.admin.test.tool.AsadminResultMatcher.asadminOK;
import static org.glassfish.main.admin.test.tool.asadmin.GlassFishTestEnvironment.getAsadmin;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.matchesPattern;
import static org.hamcrest.Matchers.stringContainsInOrder;

/**
Expand All @@ -40,19 +38,13 @@
* @author Bhakti Mehta
* @author David Matejcek
*/
@ExtendWith(JobTestExtension.class)
@TestMethodOrder(OrderAnnotation.class)
public class JobManagerITest {

private static final String COMMAND_PROGRESS_SIMPLE = "progress-simple";
private static final Asadmin ASADMIN = getAsadmin();

@BeforeEach
public void setUp() throws Exception {
assertThat(ASADMIN.exec("stop-domain"), asadminOK());
deleteJobsFile();
deleteOsgiDirectory();
assertThat(ASADMIN.exec("start-domain"), asadminOK());
}

@Test
@Order(1)
Expand All @@ -66,46 +58,37 @@ public void noJobsTest() {
@Test
@Order(2)
public void jobSurvivesRestart() throws Exception {
assertThat(ASADMIN.exec("--terse", "progress-simple"), asadminOK());
assertThat(ASADMIN.exec("list-jobs").getStdOut(), stringContainsInOrder(COMMAND_PROGRESS_SIMPLE, "COMPLETED"));
assertThat(ASADMIN.exec("list-jobs", "1").getStdOut(), stringContainsInOrder(COMMAND_PROGRESS_SIMPLE, "COMPLETED"));

assertThat(ASADMIN.exec(COMMAND_PROGRESS_SIMPLE), asadminOK());
assertThat(ASADMIN.exec("stop-domain"), asadminOK());
assertThat(ASADMIN.exec("start-domain"), asadminOK());
AsadminResult result = ASADMIN.exec("list-jobs", "1");
assertThat(result, asadminOK());
assertThat(result.getStdOut(), stringContainsInOrder(COMMAND_PROGRESS_SIMPLE, "COMPLETED"));
assertThat(ASADMIN.exec("list-jobs").getStdOut(), stringContainsInOrder(COMMAND_PROGRESS_SIMPLE, "COMPLETED"));
}


@Test
@Order(3)
public void detachAndAttach() throws Exception {
assertThat(ASADMIN.execDetached(COMMAND_PROGRESS_SIMPLE).getStdOut(), stringContainsInOrder("Job ID: "));
assertThat(ASADMIN.exec("list-jobs", "1").getStdOut(), stringContainsInOrder(COMMAND_PROGRESS_SIMPLE));
assertThat(ASADMIN.exec("attach", "1"), asadminOK());
DetachedTerseAsadminResult detached = ASADMIN.execDetached(COMMAND_PROGRESS_SIMPLE);
assertThat(detached.getJobId(), matchesPattern("[1-9][0-9]*"));
assertThat(ASADMIN.exec("list-jobs", detached.getJobId()).getStdOut(), stringContainsInOrder(COMMAND_PROGRESS_SIMPLE));
assertThat(ASADMIN.exec("attach", detached.getJobId()), asadminOK());

// list-jobs and it should be purged since the user
// starting is the same as the user who attached to it
assertThat(ASADMIN.exec("list-jobs").getOutput(), stringContainsInOrder("Nothing to list"));
assertThat(ASADMIN.exec("list-jobs", detached.getJobId()).getOutput(), stringContainsInOrder("Nothing to list"));
}


@Test
@Order(4)
public void runConfigureManagedJobsTest() throws Exception {
assertThat(ASADMIN.exec("configure-managed-jobs", "--job-retention-period=60s", "--cleanup-initial-delay=1s", "--cleanup-poll-interval=1s"), asadminOK());
public void runSynchronously() throws Exception {
assertThat(ASADMIN.exec(COMMAND_PROGRESS_SIMPLE), asadminOK());
assertThat(ASADMIN.exec("list-jobs", "1").getStdOut(), stringContainsInOrder(COMMAND_PROGRESS_SIMPLE));

// FIXME: Random race condition on Linux caused by some bug in restart-domain; 4848 port is then blocked for start-domain in setUp();
assertThat(ASADMIN.exec("stop-domain"), asadminOK());
assertThat(ASADMIN.exec("start-domain"), asadminOK());
assertThat(ASADMIN.exec("list-jobs", "1").getStdOut(), stringContainsInOrder(COMMAND_PROGRESS_SIMPLE));
assertThat(ASADMIN.exec("list-jobs").getStdOut(), stringContainsInOrder(COMMAND_PROGRESS_SIMPLE));

assertThat(ASADMIN.exec("configure-managed-jobs", "--job-retention-period=1s", "--cleanup-initial-delay=1s", "--cleanup-poll-interval=1s"), asadminOK());
Thread.sleep(2100L);
assertThat(ASADMIN.exec("list-jobs").getOutput(), stringContainsInOrder("Nothing to list"));
assertThat(ASADMIN.exec("configure-managed-jobs", "--job-retention-period=1h", "--cleanup-initial-delay=5m", "--cleanup-poll-interval=20m"), asadminOK());
assertThat(ASADMIN.exec("configure-managed-jobs", "--job-retention-period=1s", "--cleanup-initial-delay=1s",
"--cleanup-poll-interval=1s"), asadminOK());
Thread.sleep(1100L);
assertThat("Completed jobs should be removed", ASADMIN.exec("list-jobs").getOutput(),
stringContainsInOrder("Nothing to list"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright (c) 2022 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
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.main.admin.test.progress;

import org.glassfish.main.admin.test.tool.asadmin.Asadmin;
import org.glassfish.main.admin.test.tool.asadmin.AsadminResult;
import org.glassfish.main.admin.test.tool.asadmin.GlassFishTestEnvironment;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.jupiter.api.extension.ExtensionContext.Store;

import static java.util.function.Function.identity;
import static org.glassfish.main.admin.test.tool.AsadminResultMatcher.asadminOK;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.junit.jupiter.api.Assertions.fail;


/**
* @author David Matejcek
*/
public class JobTestExtension implements BeforeAllCallback, AfterAllCallback {

private static final String ORIG_POLL_INTERVAL = "origPollInterval";
private static final String ORIG_INITIAL_DELAY = "origInitialDelay";
private static final String ORIG_RETENTION_PERIOD = "origRetentionPeriod";

private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin();

@Override
public void beforeAll(ExtensionContext context) throws Exception {
Namespace namespaceClass = Namespace.create(context.getRequiredTestClass());
Store store = context.getStore(namespaceClass);
store.put(ORIG_RETENTION_PERIOD, ASADMIN.getValue("managed-job-config.job-retention-period", identity()).getValue());
store.put(ORIG_INITIAL_DELAY, ASADMIN.getValue("managed-job-config.initial-delay", identity()).getValue());
store.put(ORIG_POLL_INTERVAL, ASADMIN.getValue("managed-job-config.poll-interval", identity()).getValue());
}

@Override
public void afterAll(ExtensionContext context) throws Exception {
waitForCompletition();

// minimal possible values.
assertThat(ASADMIN.exec("configure-managed-jobs",
"--job-retention-period=1s",
"--cleanup-initial-delay=1s",
"--cleanup-poll-interval=1s"), asadminOK());

Thread.sleep(1100L);
// cleanup should be finished.
AsadminResult result = ASADMIN.exec("list-jobs");
assertThat(result.getOutput(), stringContainsInOrder("Nothing to list"));

Namespace namespaceClass = Namespace.create(context.getRequiredTestClass());
Store store = context.getStore(namespaceClass);
// reset original configuration
assertThat(ASADMIN.exec("configure-managed-jobs",
"--job-retention-period=" + store.get(ORIG_RETENTION_PERIOD),
"--cleanup-initial-delay=" + store.get(ORIG_INITIAL_DELAY),
"--cleanup-poll-interval=" + store.get(ORIG_POLL_INTERVAL)), asadminOK());
}


private void waitForCompletition() throws Exception {
final long start = System.currentTimeMillis();
while (true) {
AsadminResult result = ASADMIN.exec("list-jobs");
assertThat(result, asadminOK());
if (!result.getStdOut().contains("RUNNING")) {
return;
}
if (System.currentTimeMillis() > start + 10000L) {
fail("Timed out waiting for completition of all jobs.");
}
Thread.sleep(500);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.glassfish.main.admin.test.tool.asadmin.AsadminResult;
import org.glassfish.main.admin.test.tool.asadmin.GlassFishTestEnvironment;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.glassfish.main.admin.test.progress.ProgressMessage.isIncreasing;
import static org.glassfish.main.admin.test.tool.AsadminResultMatcher.asadminOK;
Expand All @@ -35,10 +36,12 @@
/**
* @author martinmares
*/
@ExtendWith(JobTestExtension.class)
public class ProgressStatusBasicITest {

private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin();


@Test
public void simple() {
AsadminResult result = ASADMIN.exec("progress-simple");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.glassfish.main.admin.test.tool.asadmin.AsadminResult;
import org.glassfish.main.admin.test.tool.asadmin.GlassFishTestEnvironment;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.glassfish.main.admin.test.progress.ProgressMessage.grepProgressMessages;
import static org.glassfish.main.admin.test.progress.ProgressMessage.isIncreasing;
Expand All @@ -41,13 +42,15 @@
/**
* @author martinmares
*/
@ExtendWith(JobTestExtension.class)
public class ProgressStatusComplexITest {

private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin();


@Test
public void executeCommandFromCommand() {
AsadminResult result = ASADMIN.exec(30_000, false, "progress-exec-other");
AsadminResult result = ASADMIN.exec(30_000, "progress-exec-other");
assertThat(result, asadminOK());
assertArrayEquals(new String[] {
"Starting", "Preparing", "Parsing", "Working on main part",
Expand Down

0 comments on commit 11ed0f3

Please sign in to comment.