Skip to content

Commit

Permalink
Add ballerina commands test
Browse files Browse the repository at this point in the history
  • Loading branch information
suganyasuven committed Feb 5, 2021
1 parent 14a5acd commit e6ff2be
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 1 deletion.
3 changes: 3 additions & 0 deletions ballerina-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ task distributionTest {
systemProperty "examples.dir", "$buildDir/../../examples"
systemProperty "line.check.extensions", ".java, .bal"
systemProperty "short.version", "$shortVersion"
systemProperty "version.display.text", "$versionDisplayText"
systemProperty "spec.version", "$specVersion"
systemProperty "tool.version", "$ballerinaCommandVersion"

useTestNG() {
suites 'src/test/resources/testng.xml'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. 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.ballerinalang.distribution.test;

import org.ballerinalang.distribution.utils.TestUtils;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.IOException;

/**
* Test ballerina commands.
*/
public class BallerinaCommandTest {
private static final String DIST_NAME = "ballerina-" + TestUtils.MAVEN_VERSION;
public static final String VERSION = System.getProperty("maven.version");
private static final String SPEC_VERSION = System.getProperty("spec.version");
public static final String VERSION_DISPLAY_TEXT = System.getProperty("version.display.text");
private static final String TOOL_VERSION = System.getProperty("tool.version");
private static final String path = TestUtils.TEST_DISTRIBUTION_PATH.resolve(DIST_NAME).resolve("bin").resolve("bal").toString();

@BeforeClass
public void setupDistributions() throws IOException {
TestUtils.cleanDistribution();
TestUtils.prepareDistribution(TestUtils.DISTRIBUTIONS_DIR.resolve(DIST_NAME + ".zip"));
}

@Test(description = "Execute smoke testing to verify installation.")
public void testVersionCommand() throws IOException {
TestUtils.testInstallation(path, VERSION, SPEC_VERSION, TOOL_VERSION, VERSION_DISPLAY_TEXT);
}

@Test(description = "Execute smoke testing to verify dist commands.", dependsOnMethods = {"testVersionCommand"})
public void testDistCommands() throws IOException {
// test bal dist list
String actualOutput = TestUtils.executeCommand(path + " dist list");
Assert.assertTrue(actualOutput.contains("Distributions available locally:"));
Assert.assertTrue(actualOutput.contains(VERSION_DISPLAY_TEXT));
Assert.assertTrue(actualOutput.contains("Distributions available remotely:"));
Assert.assertTrue(actualOutput.contains("1.* channel"));
Assert.assertTrue(actualOutput.contains("1.0.0"));
Assert.assertTrue(actualOutput.contains("1.1.0"));
Assert.assertTrue(actualOutput.contains("1.2.0"));
Assert.assertTrue(actualOutput.contains("Swan Lake channel"));
Assert.assertTrue(actualOutput.contains("slp1"));
Assert.assertTrue(actualOutput.contains("slp8"));
// test bal dist pull and fetching dependencies
actualOutput = TestUtils.executeCommand(path + " dist pull 1.2.3");
Assert.assertTrue(actualOutput.contains("Fetching the '1.2.3' distribution from the remote server..."));
Assert.assertTrue(actualOutput.contains("Fetching the dependencies for '1.2.3' from the remote server..."));
Assert.assertTrue(actualOutput.contains("Downloading jdk8u202-b08-jre"));
Assert.assertTrue(actualOutput.contains("'1.2.3' successfully set as the active distribution"));
TestUtils.testInstallation(path, "1.2.3", "2020R1", TOOL_VERSION, "1.2.3");
// test bal dist update
actualOutput = TestUtils.executeCommand(path + " dist update");
Assert.assertTrue(actualOutput.contains("Fetching the latest patch distribution for 'jballerina-1.2.3' from the remote server..."));
Assert.assertTrue(actualOutput.contains("Successfully set the latest patch distribution"));
actualOutput = TestUtils.executeCommand(path + " dist update");
Assert.assertTrue(actualOutput.contains("is already the active distribution"));
// test bal dist use
actualOutput = TestUtils.executeCommand(path + " dist use 1.2.3");
Assert.assertTrue(actualOutput.contains("'1.2.3' successfully set as the active distribution"));
TestUtils.testInstallation(path, "1.2.3", "2020R1", TOOL_VERSION, "1.2.3");
actualOutput = TestUtils.executeCommand(path + " dist pull slp7");
Assert.assertTrue(actualOutput.contains("Fetching the 'slp7' distribution from the remote server..."));
Assert.assertTrue(actualOutput.contains("Fetching the dependencies for 'slp7' from the remote server..."));
Assert.assertTrue(actualOutput.contains("Downloading jdk-11.0.8+10-jre"));
Assert.assertTrue(actualOutput.contains("'slp7' successfully set as the active distribution"));
TestUtils.testInstallation(path, "swan-lake-preview7", "v2020-09-22", TOOL_VERSION, "Preview 7");
// test bal dist remove <version>
actualOutput = TestUtils.executeCommand(path + " dist remove slp7");
Assert.assertTrue(actualOutput.contains("The active Ballerina distribution cannot be removed"));
actualOutput = TestUtils.executeCommand(path + " dist remove 1.2.3");
Assert.assertTrue(actualOutput.contains("Distribution '1.2.3' successfully removed"));
// test bal update
actualOutput = TestUtils.executeCommand(path + " update");
Assert.assertTrue(actualOutput.contains("Fetching the latest tool version from the remote server..."));
// test bal dist remove -a
actualOutput = TestUtils.executeCommand(path + " dist remove -a");
Assert.assertTrue(actualOutput.contains("All non-active distributions are successfully removed"));
}

@AfterClass
public void cleanUp() throws IOException {
TestUtils.cleanDistribution();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.lingala.zip4j.exception.ZipException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.testng.Assert;

import java.io.BufferedReader;
import java.io.File;
Expand All @@ -30,12 +31,15 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.sql.Timestamp;

/**
* Utility class for tests
Expand All @@ -50,6 +54,7 @@ public class TestUtils {
public static final Path EXAMPLES_DIR = Paths.get(System.getProperty("examples.dir"));
public static final String EXTENSTIONS_TO_BE_FILTERED_FOR_LINE_CHECKS = System.getProperty("line.check.extensions");
public static final Path RESOURCES_PATH = TARGET_DIR.resolve("resources/test");
private static final String SWAN_LAKE_KEYWORD = "swan-lake";

/**
* Log the output of an input stream.
Expand Down Expand Up @@ -209,4 +214,113 @@ public static void deleteGeneratedFiles(String generatedFileName) {
}
}
}

/**
* Execute the given command.
*
* @param command command needs to be execute
* @return output of the executed command
*/
public static String executeCommand(String command) throws IOException {
String output = "";
File file = new File(getUserHome() + File.separator
+ "temp-" + new Timestamp(System.currentTimeMillis()).getTime() + ".sh");
file.createNewFile();
file.setExecutable(true);
PrintWriter writer = new PrintWriter(file.getPath(), StandardCharsets.UTF_8);
writer.println(command);
writer.close();

ProcessBuilder pb = new ProcessBuilder(file.getPath());
Process process = pb.start();
InputStream inputStream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
output += line + "\n";
}
if (output.isEmpty()) {
inputStream = process.getErrorStream();
reader = new BufferedReader(new InputStreamReader(inputStream));
while ((line = reader.readLine()) != null) {
output += line + "\n";
}
}
file.delete();
return output;
}

/**
* Provide user home directory based on command.
*
* @return user home directory
*/
public static String getUserHome() {
String userHome = System.getenv("HOME");
if (userHome == null) {
userHome = System.getProperty("user.home");
}
return userHome;
}

/**
* Execute smoke testing to verify installation.
*
* @param path Path to the bal file
* @param jBallerinaVersion Installed jBallerina version
* @param specVersion Installed language specification
* @param toolVersion Installed tool version
* @param versionDisplayText Installed version display text
*/
public static void testInstallation(String path, String jBallerinaVersion, String specVersion, String toolVersion,
String versionDisplayText) throws IOException {
Assert.assertEquals(TestUtils.executeCommand(path + " -v"),
TestUtils.getVersionOutput(jBallerinaVersion, specVersion, toolVersion, versionDisplayText));
}

/**
* Get version output for version command.
* @param jBallerinaVersion Installed jBallerina version
* @param specVersion Installed language specification
* @param toolVersion Installed tool version
* @param versionDisplayText display text for installed jBallerina version
*
* @return version output
*/
public static String getVersionOutput(String jBallerinaVersion, String specVersion, String toolVersion,
String versionDisplayText) {
String toolText = TestUtils.isOldToolVersion(toolVersion) ? "Ballerina tool" : "Update Tool";
if (jBallerinaVersion.contains(TestUtils.SWAN_LAKE_KEYWORD)) {
return "Ballerina Swan Lake " + versionDisplayText + "\n" + "Language specification " + specVersion +
"\n" + toolText + " " + toolVersion + "\n";
}

String ballerinaReference = isSupportedRelease(jBallerinaVersion) ? "jBallerina" : "Ballerina";
return ballerinaReference + " " + jBallerinaVersion + "\n" + "Language specification " + specVersion + "\n" +
toolText + " " + toolVersion + "\n";
}

/**
* To check whether older tool version before swan lake support
*
* @param toolVersion
* @return returns is a older version
*/
public static boolean isOldToolVersion(String toolVersion) {
return toolVersion.equals("0.8.5") || toolVersion.equals("0.8.0");
}

/**
* To check whether installation is a 1.0.x release.
*
* @return returns is a 1.0.x release
*/
public static boolean isSupportedRelease(String version) {
if (version.contains(SWAN_LAKE_KEYWORD)) {
return true;
}

String[] versions = version.split("\\.");
return !(versions[0].equals("1") && versions[1].equals("0"));
}
}
1 change: 1 addition & 0 deletions ballerina-test/src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<class name="org.ballerinalang.distribution.test.PlatformDistributionArtifactCheckTest"/>
<class name="org.ballerinalang.distribution.test.OpenAPIDistributionArtifactCheck"/>
<class name="org.ballerinalang.distribution.test.OpenAPIArtifactBuildTest"/>
<class name="org.ballerinalang.distribution.test.BallerinaCommandTest"/>
</classes>
</test>
</suite>
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ version=swan-lake-alpha1-SNAPSHOT
ballerinaVersion=2.0.0-alpha2
shortVersion=slalpha1
ballerinaJreVersion=0.8.0
ballerinaCommandVersion=0.8.11
specVersion=v2020-12-17
versionDisplayText=Alpha 1
ballerinaCommandVersion=0.8.14

# Stdlib Level 01
stdlibEncodingVersion=1.0.8
Expand Down

0 comments on commit e6ff2be

Please sign in to comment.