Skip to content

Commit

Permalink
Merge pull request #25171 from dmatej/terse
Browse files Browse the repository at this point in the history
Added parameter for "terse" to Asadmin used in tests
  • Loading branch information
dmatej authored Oct 8, 2024
2 parents 20d8b1a + 8521a2a commit c30ecb4
Show file tree
Hide file tree
Showing 16 changed files with 173 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
* after tests are finished.
*
* @author David Matejcek
* @author Ondro Mihalyi
*/
public class GlassFishTestEnvironment {
private static final Logger LOG = Logger.getLogger(GlassFishTestEnvironment.class.getName());
Expand Down Expand Up @@ -84,7 +85,7 @@ public class GlassFishTestEnvironment {
// 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.
// Note: --suspend implicitly enables --debug
assertThat(asadmin.exec(ASADMIN_START_DOMAIN_TIMEOUT, "start-domain",
Expand All @@ -96,17 +97,26 @@ public class GlassFishTestEnvironment {
* @return {@link Asadmin} command api for tests.
*/
public static Asadmin getAsadmin() {
return new Asadmin(ASADMIN, ADMIN_USER, PASSWORD_FILE);
return getAsadmin(true);
}


/**
* @param terse true means suitable and minimized for easy parsing.
* @return {@link Asadmin} command api for tests.
*/
public static Asadmin getAsadmin(boolean terse) {
return new Asadmin(ASADMIN, ADMIN_USER, PASSWORD_FILE, terse);
}

/**
* @return {@link Asadmin} command api for tests.
*/
public static StartServ getStartServ() {
return new StartServ(STARTSERV);
}


/**
* @return {@link Asadmin} command api for tests.
*/
Expand Down Expand Up @@ -258,6 +268,18 @@ public static void deleteJobsFile() {
}


/** Default is org.apache.derby.jdbc.ClientDataSource */
public static void switchDerbyPoolToEmbededded() {
final AsadminResult result = getAsadmin(true).exec(5_000, "set",
"resources.jdbc-connection-pool.DerbyPool.datasource-classname=org.apache.derby.jdbc.EmbeddedDataSource",
"resources.jdbc-connection-pool.DerbyPool.property.PortNumber=",
"resources.jdbc-connection-pool.DerbyPool.property.serverName=",
"resources.jdbc-connection-pool.DerbyPool.property.URL=");
assertThat(result, asadminOK());
// Just to see the result in log.
assertThat(getAsadmin(true).exec(5_000, "get", "resources.jdbc-connection-pool.DerbyPool.*"), asadminOK());
}

/**
* Useful for a heuristic inside Eclipse and other environments.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Eclipse Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024 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 Down Expand Up @@ -57,6 +57,7 @@ public class Asadmin {
private final File asadmin;
private final String adminUser;
private final File adminPasswordFile;
private final boolean terse;
private final Map<String, String> environment = new HashMap<>();


Expand All @@ -68,9 +69,23 @@ public class Asadmin {
* @param adminPasswordFile - a file containing admin's password set as <code>AS_ADMIN_PASSWORD=...</code>
*/
public Asadmin(final File asadmin, final String adminUser, final File adminPasswordFile) {
this(asadmin, adminUser, adminPasswordFile, false);
}


/**
* Creates a stateless instance of the tool.
*
* @param asadmin - executable file
* @param adminUser - username authorized to use the domain
* @param adminPasswordFile - a file containing admin's password set as <code>AS_ADMIN_PASSWORD=...</code>
* @param terse - to produce output, minimized and suitable for parsing.
*/
public Asadmin(final File asadmin, final String adminUser, final File adminPasswordFile, final boolean terse) {
this.asadmin = asadmin;
this.adminUser = adminUser;
this.adminPasswordFile = adminPasswordFile;
this.terse = terse;
}


Expand Down Expand Up @@ -167,8 +182,10 @@ private AsadminResult exec(final int timeout, final boolean detachedAndTerse, fi
command.add("--passwordfile");
command.add(adminPasswordFile.getAbsolutePath());
if (detachedAndTerse) {
command.add("--terse");
command.add("--terse=true");
command.add("--detach");
} else {
command.add("--terse=" + terse);
}
command.addAll(parameters);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2024 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.itest.tools.asadmin;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

/**
* Tool to simplify properties backup so you can symmetrically set some subset of properties before
* and after the test
*
* @author Ondro Mihalyi
* @author David Matejcek
*/
public class DomainPropertiesBackup {

private final List<String> settingsBackup;

/**
* Creates the backup by calling asadmin get keyFilter.
* From retrieved list excludes entries with the specified key.
*
* @param keyFilter
* @param excludedKeys
*/
public DomainPropertiesBackup(String keyFilter, String... excludedKeys) {
final AsadminResult result = getAsadmin(true).exec(5_000, "get", keyFilter);
assertThat(result, asadminOK());
Predicate<String> inclusionFilter = s -> {
for (String key : excludedKeys) {
if (s.startsWith(key + '=')) {
return false;
}
}
return true;
};
settingsBackup = Stream.of(result.getStdOut().split("\n")).filter(inclusionFilter).collect(Collectors.toList());
}

/**
* Restore properties from the backup by calling asadmin set.
*/
public void restore() {
String[] args = new String[settingsBackup.size() + 1];
args[0] = "set";
for (int i = 1; i < args.length; i++) {
args[i] = settingsBackup.get(i - 1);
}
final AsadminResult result = getAsadmin(true).exec(5_000, args);
assertThat(result, asadminOK());
}


/**
* @return backup for keys starting by
* <code>resources.jdbc-connection-pool.DerbyPool.</code>
*/
public static DomainPropertiesBackup backupDerbyPool() {
return new DomainPropertiesBackup("resources.jdbc-connection-pool.DerbyPool.*",
"resources.jdbc-connection-pool.DerbyPool.name");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Eclipse Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2024 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 Down Expand Up @@ -38,7 +38,7 @@
@TestMethodOrder(OrderAnnotation.class)
public class AsadminVersionITest {

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

@AfterAll
public static void startDomainAgain() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class ClusterITest {
private static final String INSTANCE_NAME_2 = "eein2";
private static final String URL1 = "http://localhost:" + PORT1;
private static final String URL2 = "http://localhost:" + PORT2;
private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin();
private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(false);
private static final AtomicBoolean INSTANCES_REACHABLE = new AtomicBoolean();
private static final AtomicBoolean APP_DEPLOYED = new AtomicBoolean();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -47,7 +47,7 @@
*/
public class OSGiCommandsITest {

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

@BeforeAll
public static void waitOsgiReady() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -52,7 +52,7 @@
@ExtendWith(JobTestExtension.class)
public class DetachAttachITest {
private static final Logger LOG = Logger.getLogger(DetachAttachITest.class.getName());
private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin();
private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin(false);


@Test
Expand All @@ -69,7 +69,7 @@ public void uptimePeriodically() throws Exception {
}
Thread.sleep(1000L);
{
AsadminResult result = ASADMIN.exec("--terse", "attach", id);
AsadminResult result = GlassFishTestEnvironment.getAsadmin(true).exec("attach", id);
assertThat(result, asadminOK());
assertTrue(result.getStdOut().contains("uptime"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class JobTestExtension implements BeforeAllCallback, AfterAllCallback {
private static final String ORIG_INITIAL_DELAY = "origInitialDelay";
private static final String ORIG_RETENTION_PERIOD = "origRetentionPeriod";

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

@Override
public void beforeAll(ExtensionContext context) throws Exception {
Expand Down
Loading

0 comments on commit c30ecb4

Please sign in to comment.