Michael Blow has submitted this change and it was merged. Change subject: ASTERIXDB-1552: Test Case for Sample Local Cluster ......................................................................
ASTERIXDB-1552: Test Case for Sample Local Cluster Change-Id: I2eacbd033a65661d22dc2a848afd83bbcc43677f Reviewed-on: https://asterix-gerrit.ics.uci.edu/1028 Sonar-Qube: Jenkins <[email protected]> Tested-by: Jenkins <[email protected]> Integration-Tests: Jenkins <[email protected]> Reviewed-by: Till Westmann <[email protected]> --- M asterixdb/asterix-app/src/test/java/org/apache/asterix/test/common/TestHelper.java A asterixdb/asterix-server/src/test/java/org/apache/asterix/server/test/SampleLocalClusterIT.java 2 files changed, 146 insertions(+), 0 deletions(-) Approvals: Till Westmann: Looks good to me, approved Jenkins: Verified; No violations found; Verified diff --git a/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/common/TestHelper.java b/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/common/TestHelper.java index 0804126..5661258 100644 --- a/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/common/TestHelper.java +++ b/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/common/TestHelper.java @@ -18,7 +18,18 @@ */ package org.apache.asterix.test.common; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Enumeration; import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +import org.apache.commons.compress.utils.IOUtils; +import org.apache.commons.lang3.StringUtils; public final class TestHelper { @@ -31,4 +42,34 @@ return false; } + public static String joinPath(String... pathElements) { + return StringUtils.join(pathElements, File.separatorChar); + } + + public static void unzip(String sourceFile, String outputDir) throws IOException { + if (System.getProperty("os.name").toLowerCase().startsWith("win")) { + try (ZipFile zipFile = new ZipFile(sourceFile)) { + Enumeration<? extends ZipEntry> entries = zipFile.entries(); + while (entries.hasMoreElements()) { + ZipEntry entry = entries.nextElement(); + File entryDestination = new File(outputDir, entry.getName()); + if (!entry.isDirectory()) { + entryDestination.getParentFile().mkdirs(); + try (InputStream in = zipFile.getInputStream(entry); + OutputStream out = new FileOutputStream(entryDestination)) { + IOUtils.copy(in, out); + } + } + } + } + } else { + Process process = new ProcessBuilder("unzip", "-d", outputDir, sourceFile).start(); + try { + process.waitFor(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IOException(e); + } + } + } } diff --git a/asterixdb/asterix-server/src/test/java/org/apache/asterix/server/test/SampleLocalClusterIT.java b/asterixdb/asterix-server/src/test/java/org/apache/asterix/server/test/SampleLocalClusterIT.java new file mode 100644 index 0000000..e98262a --- /dev/null +++ b/asterixdb/asterix-server/src/test/java/org/apache/asterix/server/test/SampleLocalClusterIT.java @@ -0,0 +1,105 @@ +/* + * 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.asterix.server.test; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; +import java.net.URL; +import java.util.Collections; +import java.util.logging.Logger; + +import org.apache.asterix.common.utils.ServletUtil.Servlets; +import org.apache.asterix.test.aql.TestExecutor; +import org.apache.asterix.test.common.TestHelper; +import org.apache.asterix.testframework.context.TestCaseContext.OutputFormat; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + +import static org.apache.asterix.test.common.TestHelper.joinPath; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class SampleLocalClusterIT { + + // Important paths and files for this test. + + // The "target" subdirectory of asterix-server. All outputs go here. + private static final String TARGET_DIR = joinPath(/*System.getProperty("basedir"),*/ "target"); + + // Directory where the NCs create and store all data, as configured by + // src/test/resources/NCServiceExecutionIT/cc.conf. + private static final String OUTPUT_DIR = joinPath(TARGET_DIR, "sample-local-cluster"); + + private static final String LOCAL_SAMPLES_DIR = joinPath(OUTPUT_DIR, "samples", "local"); + + private static final Logger LOGGER = Logger.getLogger(SampleLocalClusterIT.class.getName()); + + @BeforeClass + public static void setUp() throws Exception { + // Create actual-results output directory. + File outDir = new File(OUTPUT_DIR); + + // Remove any instance data from previous runs. + if (outDir.isDirectory()) { + FileUtils.deleteDirectory(outDir); + } + outDir.mkdirs(); + + String installerZip = joinPath(TARGET_DIR, + new File(TARGET_DIR).list((dir, name) -> name.matches("asterix-server.*-binary-assembly.zip"))[0]); + + TestHelper.unzip(installerZip, OUTPUT_DIR); + } + + @Test + public void test0_startCluster() throws Exception { + Process process = new ProcessBuilder(joinPath(LOCAL_SAMPLES_DIR, "bin/start-sample-cluster.sh")) + .inheritIO().start(); + Assert.assertEquals(process.waitFor(), 0); + } + + @Test + public void test1_sanityQuery() throws Exception { + TestExecutor testExecutor = new TestExecutor(); + InputStream resultStream = testExecutor.executeQuery("1+1", OutputFormat.ADM, + "http://127.0.0.1:19002" + Servlets.AQL_QUERY.getPath(), Collections.emptyList()); + StringWriter sw = new StringWriter(); + IOUtils.copy(resultStream, sw); + Assert.assertEquals(sw.toString().trim(), "2"); + } + + @Test + public void test2_stopCluster() throws Exception { + Process process = new ProcessBuilder(joinPath(LOCAL_SAMPLES_DIR, "bin/stop-sample-cluster.sh")) + .inheritIO().start(); + Assert.assertEquals(process.waitFor(), 0); + try { + new URL("http://127.0.0.1:19002").openConnection().connect(); + Assert.assertTrue("Expected connection to be refused.", false); + } catch (IOException e) { + // expected + } + } +} -- To view, visit https://asterix-gerrit.ics.uci.edu/1028 To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings Gerrit-MessageType: merged Gerrit-Change-Id: I2eacbd033a65661d22dc2a848afd83bbcc43677f Gerrit-PatchSet: 3 Gerrit-Project: asterixdb Gerrit-Branch: master Gerrit-Owner: Michael Blow <[email protected]> Gerrit-Reviewer: Jenkins <[email protected]> Gerrit-Reviewer: Michael Blow <[email protected]> Gerrit-Reviewer: Till Westmann <[email protected]>
