This is an automated email from the ASF dual-hosted git repository. dklco pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-app-cms.git
commit f669145cb8faf09518476d2eb6dee2af6074b7aa Author: Dan Klco <[email protected]> AuthorDate: Mon Jul 13 17:20:26 2020 -0400 Minor - Re-adding support for smoke-testing the build artifact --- builder/pom.xml | 3 +- .../apache/sling/launchpad/LaunchpadReadyRule.java | 121 +++++++++++++ .../java/org/apache/sling/launchpad/SmokeIT.java | 195 +++++++++++++++++++++ .../org/apache/sling/launchpad/package-info.java | 30 ++++ 4 files changed, 347 insertions(+), 2 deletions(-) diff --git a/builder/pom.xml b/builder/pom.xml index d3a2898..a2277ba 100644 --- a/builder/pom.xml +++ b/builder/pom.xml @@ -26,7 +26,7 @@ <properties> <sling.java.version>8</sling.java.version> - <IT.expected.bundles.count>125</IT.expected.bundles.count> + <IT.expected.bundles.count>208</IT.expected.bundles.count> <cms.version>${project.parent.version}</cms.version> </properties> @@ -163,4 +163,3 @@ </dependency> </dependencies> </project> - diff --git a/builder/src/test/java/org/apache/sling/launchpad/LaunchpadReadyRule.java b/builder/src/test/java/org/apache/sling/launchpad/LaunchpadReadyRule.java new file mode 100644 index 0000000..5b7815d --- /dev/null +++ b/builder/src/test/java/org/apache/sling/launchpad/LaunchpadReadyRule.java @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.launchpad; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.ConnectException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.junit.rules.ExternalResource; + +public class LaunchpadReadyRule extends ExternalResource { + + private static final int TRIES = 60; + private static final int WAIT_BETWEEN_TRIES_MILLIS = 1000; + + private final List<Check> checks = new ArrayList<>(); + + public LaunchpadReadyRule(int launchpadPort) { + + checks.add(new Check("http://localhost:" + launchpadPort + "/server/default/jcr:root/content")); + checks.add(new Check("http://localhost:" + launchpadPort + "/content/apache/sling-apache-org/index.html") { + @Override + public String runCheck(HttpResponse response) throws Exception { + try (InputStreamReader isr = new InputStreamReader(response.getEntity().getContent()); + BufferedReader reader = new BufferedReader(isr)) { + + String line; + while ((line = reader.readLine()) != null) { + if (line.contains("Apache Sling - Bringing Back the Fun!")) { + return null; + } + } + } + + return "Did not find 'ready' marker in the response body"; + } + }); + } + + @Override + protected void before() throws Throwable { + + try (CloseableHttpClient client = HttpClients.createDefault()) { + for (Check check : checks) { + runCheck(client, check); + } + } + } + + private void runCheck(CloseableHttpClient client, Check check) throws Exception { + + String lastFailure = null; + HttpGet get = new HttpGet(check.getUrl()); + + for (int i = 0; i < TRIES; i++) { + try (CloseableHttpResponse response = client.execute(get)) { + + if (response.getStatusLine().getStatusCode() != 200) { + lastFailure = "Status code is " + response.getStatusLine(); + Thread.sleep(WAIT_BETWEEN_TRIES_MILLIS); + continue; + } + + lastFailure = check.runCheck(response); + if (lastFailure == null) { + return; + } + } catch ( ConnectException e ) { + lastFailure = e.getClass().getName() + " : " + e.getMessage(); + } + + Thread.sleep(WAIT_BETWEEN_TRIES_MILLIS); + } + + throw new RuntimeException(String.format("Launchpad not ready. Failed check for URL %s with message '%s'", + check.getUrl(), lastFailure)); + } + + static class Check { + private String url; + + public Check(String url) { + this.url = url; + } + + public String getUrl() { + return url; + } + + /** + * @param response the HttpResponse + * @return null if check check was successful, an error description otherwise + * @throws Exception + */ + public String runCheck(HttpResponse response) throws Exception { + return null; + } + } + +} diff --git a/builder/src/test/java/org/apache/sling/launchpad/SmokeIT.java b/builder/src/test/java/org/apache/sling/launchpad/SmokeIT.java new file mode 100644 index 0000000..71676dc --- /dev/null +++ b/builder/src/test/java/org/apache/sling/launchpad/SmokeIT.java @@ -0,0 +1,195 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.launchpad; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +import java.util.List; +import java.util.Map; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.apache.felix.utils.json.JSONParser; +import org.apache.http.Header; +import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.impl.auth.BasicScheme; +import org.apache.http.impl.client.BasicAuthCache; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.hamcrest.CoreMatchers; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; + +public class SmokeIT { + + private static final int LAUNCHPAD_PORT = Integer.getInteger("launchpad.http.port", 8080); + private static final int EXPECTED_BUNDLES_COUNT = Integer.getInteger("IT.expected.bundles.count", Integer.MAX_VALUE); + + @ClassRule + public static LaunchpadReadyRule LAUNCHPAD = new LaunchpadReadyRule(LAUNCHPAD_PORT); + private HttpClientContext httpClientContext; + + @Before + public void prepareHttpContext() { + + CredentialsProvider credsProvider = new BasicCredentialsProvider(); + UsernamePasswordCredentials creds = new UsernamePasswordCredentials("admin", "admin"); + credsProvider.setCredentials(new AuthScope("localhost", LAUNCHPAD_PORT), creds); + + BasicAuthCache authCache = new BasicAuthCache(); + BasicScheme basicAuth = new BasicScheme(); + authCache.put(new HttpHost("localhost", LAUNCHPAD_PORT, "http"), basicAuth); + + httpClientContext = HttpClientContext.create(); + httpClientContext.setCredentialsProvider(credsProvider); + httpClientContext.setAuthCache(authCache); + } + + private CloseableHttpClient newClient() { + + return HttpClientBuilder.create() + .setDefaultCredentialsProvider(httpClientContext.getCredentialsProvider()) + .build(); + } + + @Test + public void verifyAllBundlesStarted() throws Exception { + + try ( CloseableHttpClient client = newClient() ) { + + HttpGet get = new HttpGet("http://localhost:" + LAUNCHPAD_PORT + "/system/console/bundles.json"); + + // pass the context to ensure preemptive basic auth is used + // https://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html + try ( CloseableHttpResponse response = client.execute(get, httpClientContext) ) { + + if ( response.getStatusLine().getStatusCode() != 200 ) { + fail("Unexpected status line " + response.getStatusLine()); + } + + Header contentType = response.getFirstHeader("Content-Type"); + assertThat("Content-Type header", contentType.getValue(), CoreMatchers.startsWith("application/json")); + + Map<String, Object> obj = new JSONParser(response.getEntity().getContent()).getParsed(); + + @SuppressWarnings("unchecked") + List<Object> status = (List<Object>) obj.get("s"); + + @SuppressWarnings("unchecked") + List<Object> bundles = (List<Object>) obj.get("data"); + if(bundles.size() < EXPECTED_BUNDLES_COUNT) { + fail("Expected at least " + EXPECTED_BUNDLES_COUNT + " bundles, got " + bundles.size()); + } + + BundleStatus bs = new BundleStatus(status); + + if ( bs.resolvedBundles != 0 || bs.installedBundles != 0 ) { + + StringBuilder out = new StringBuilder(); + out.append("Expected all bundles to be active, but instead got ") + .append(bs.resolvedBundles).append(" resolved bundles, ") + .append(bs.installedBundles).append(" installed bundlles: "); + + for ( int i = 0 ; i < bundles.size(); i++ ) { + @SuppressWarnings("unchecked") + Map<String, Object> bundle = (Map<String, Object>) bundles.get(i); + + String bundleState = (String) bundle.get("state"); + String bundleSymbolicName = (String) bundle.get("symbolicName"); + String bundleVersion = (String) bundle.get("version"); + + switch ( bundleState ) { + case "Active": + case "Fragment": + continue; + + default: + out.append("\n- ").append(bundleSymbolicName).append(" ").append(bundleVersion).append(" is in state " ).append(bundleState); + } + } + + fail(out.toString()); + } + } + } + } + + @Test + public void ensureRepositoryIsStarted() throws Exception { + try ( CloseableHttpClient client = newClient() ) { + + HttpGet get = new HttpGet("http://localhost:" + LAUNCHPAD_PORT + "/server/default/jcr:root/content"); + + try ( CloseableHttpResponse response = client.execute(get) ) { + + if ( response.getStatusLine().getStatusCode() != 200 ) { + fail("Unexpected status line " + response.getStatusLine()); + } + + Header contentType = response.getFirstHeader("Content-Type"); + assertThat("Content-Type header", contentType.getValue(), equalTo("text/xml")); + + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + dbf.setNamespaceAware(true); + DocumentBuilder db = dbf.newDocumentBuilder(); + Document document = db.parse(response.getEntity().getContent()); + + Element docElement = document.getDocumentElement(); + NamedNodeMap attrs = docElement.getAttributes(); + + Node nameAttr = attrs.getNamedItemNS("http://www.jcp.org/jcr/sv/1.0", "name"); + assertThat("no 'name' attribute found", nameAttr, notNullValue()); + assertThat("Invalid name attribute value", nameAttr.getNodeValue(), equalTo("content")); + } + } + } + + static class BundleStatus { + + long totalBundles; + long activeBundles; + long activeFragments; + long resolvedBundles; + long installedBundles; + + public BundleStatus(List<Object> array) { + + totalBundles = (Long)array.get(0); + activeBundles = (Long)array.get(1); + activeFragments = (Long)array.get(2); + resolvedBundles = (Long)array.get(3); + installedBundles = (Long)array.get(4); + + } + } +} diff --git a/builder/src/test/java/org/apache/sling/launchpad/package-info.java b/builder/src/test/java/org/apache/sling/launchpad/package-info.java new file mode 100644 index 0000000..f70e5e8 --- /dev/null +++ b/builder/src/test/java/org/apache/sling/launchpad/package-info.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * <h1>Smoke tests for the Sling Launchpad</h1> + * + * <p>This package contains a minimal set of tests for the Sling launchpad. The + * tests validate that the launchpad is correctly assembled and that there are + * no obvious mistakes such as bundles that can't be wired. More extensive + * tests must be placed in specific test projects.</p> + * + * <p>The launchpad tests don't depend on other Sling bundles,to prevent circular + * dependencies. As such, there is some duplication of code ( at least intent, if + * not implementation ) with some of the testing projects. This is another + * argument for keeping the tests minimal.</p> + */ +package org.apache.sling.launchpad; \ No newline at end of file
