Skip to content

Commit

Permalink
Merge pull request #692 from xlight05/enable-native-tests
Browse files Browse the repository at this point in the history
Add graalvm build args and enable native tests
  • Loading branch information
xlight05 committed Jun 16, 2023
2 parents 52bb437 + a9cf696 commit 9f5a85d
Show file tree
Hide file tree
Showing 17 changed files with 153 additions and 14 deletions.
4 changes: 2 additions & 2 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
org = "ballerina"
name = "cloud"
version = "2.7.1"
version = "2.8.0"
repository = "https://github.com/ballerina-platform/module-ballerina-c2c"
license = ["Apache-2.0"]
keywords = ["cloud", "kubernetes", "docker", "k8s", "c2c"]
distribution = "2201.6.0"
distribution = "2201.7.0"
2 changes: 1 addition & 1 deletion ballerina/CompilerPlugin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ id = "code2cloud"
class = "io.ballerina.c2c.C2CCompilerPlugin"

[[dependency]]
path = "../compiler-plugin/build/libs/cloud-compiler-plugin-2.7.1-SNAPSHOT.jar"
path = "../compiler-plugin/build/libs/cloud-compiler-plugin-2.8.0-SNAPSHOT.jar"
4 changes: 2 additions & 2 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.6.0"
distribution-version = "2201.7.0-20230616-003600-7453c9d7"

[[package]]
org = "ballerina"
name = "cloud"
version = "2.7.1"
version = "2.8.0"
modules = [
{org = "ballerina", packageName = "cloud", moduleName = "cloud"}
]
Expand Down
2 changes: 1 addition & 1 deletion build-config/resources/Ballerina.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ version = "@toml.version@"
repository = "https://github.com/ballerina-platform/module-ballerina-c2c"
license = ["Apache-2.0"]
keywords = ["cloud", "kubernetes", "docker", "k8s", "c2c"]
distribution = "2201.6.0"
distribution = "2201.7.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2023, 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 io.ballerina.c2c.test.samples;

import io.ballerina.c2c.exceptions.KubernetesPluginException;
import io.ballerina.c2c.test.utils.KubernetesTestUtils;
import io.ballerina.c2c.utils.KubernetesUtils;
import org.apache.commons.io.FilenameUtils;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static io.ballerina.c2c.KubernetesConstants.DOCKER;

/**
* Test cases for docker cloud option as a project.
*/
public class NativeArgsTest {

protected static final Path SAMPLE_DIR = Paths.get(FilenameUtils.separatorsToSystem(
System.getProperty("sampleDir")));
private static final Path SOURCE_DIR_PATH = SAMPLE_DIR.resolve("graalvm-build-args");
private static final Path DOCKER_TARGET_PATH =
SOURCE_DIR_PATH.resolve("target").resolve(DOCKER).resolve("native_args");
@Test
public void validateDockerBuildOption() throws IOException, InterruptedException, KubernetesPluginException {
Assert.assertEquals(KubernetesTestUtils.compileBallerinaProject(SOURCE_DIR_PATH), 0);
File dockerFile = DOCKER_TARGET_PATH.resolve("Dockerfile").toFile();
String content = Files.readString(dockerFile.toPath(), StandardCharsets.UTF_8);
Assert.assertTrue(dockerFile.exists());
Assert.assertTrue(content.contains("--static"));
KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
}

@Test(dependsOnMethods = { "validateDockerBuildOption" })
public void validateK8sBuildOption() throws IOException, InterruptedException, KubernetesPluginException {
Assert.assertEquals(KubernetesTestUtils.compileBallerinaProject(SOURCE_DIR_PATH, "k8s"), 0);
File dockerFile = DOCKER_TARGET_PATH.resolve("Dockerfile").toFile();
String content = Files.readString(dockerFile.toPath(), StandardCharsets.UTF_8);
Assert.assertTrue(dockerFile.exists());
Assert.assertTrue(content.contains("--static"));
KubernetesUtils.deleteDirectory(DOCKER_TARGET_PATH);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,30 @@ public static int compileBallerinaFile(Path sourceDirectory, String fileName, St
*/
public static int compileBallerinaProject(Path sourceDirectory) throws InterruptedException,
IOException {
return compileBallerinaProject(sourceDirectory, "");
}

/**
* Compile a ballerina project in a given directory.
*
* @param sourceDirectory Ballerina source directory
* @return Exit code
* @throws InterruptedException if an error occurs while compiling
* @throws IOException if an error occurs while writing file
*/
public static int compileBallerinaProject(Path sourceDirectory, String cloudFlag) throws InterruptedException,
IOException {
Path dependenciesToml = Paths.get(sourceDirectory.toAbsolutePath().toString(), "Dependencies.toml");
if (dependenciesToml.toFile().exists()) {
log.warn("Deleting already existing Dependencies.toml file.");
FileUtils.deleteQuietly(dependenciesToml.toFile());
}

ProcessBuilder pb = new ProcessBuilder(BALLERINA_COMMAND, BUILD);

if (!cloudFlag.equals("")) {
pb = new ProcessBuilder(BALLERINA_COMMAND, BUILD, "--cloud=" + cloudFlag);
}

log.info(COMPILING + sourceDirectory.normalize());
log.debug(EXECUTING_COMMAND + pb.command());
pb.directory(sourceDirectory.toFile());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
<class name="io.ballerina.c2c.test.samples.Sample10Test"/>
<class name="io.ballerina.c2c.test.samples.Sample11Test"/>
<class name="io.ballerina.c2c.test.samples.Sample12Test"/>
<!-- <class name="io.ballerina.c2c.test.samples.NativeTest"/>-->
<class name="io.ballerina.c2c.test.samples.NativeTest"/>
<class name="io.ballerina.c2c.test.samples.NativeArgsTest"/>
<class name="io.ballerina.c2c.test.samples.DockerProjectTest"/>
<class name="io.ballerina.c2c.test.samples.DockerSingleTest"/>
<class name="io.ballerina.c2c.test.SettingsMultiYAMLTest"/>
Expand Down
3 changes: 2 additions & 1 deletion compiler-plugin-tests/src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
<class name="io.ballerina.c2c.test.samples.Sample10Test"/>
<class name="io.ballerina.c2c.test.samples.Sample11Test"/>
<class name="io.ballerina.c2c.test.samples.Sample12Test"/>
<!-- <class name="io.ballerina.c2c.test.samples.NativeTest"/>-->
<class name="io.ballerina.c2c.test.samples.NativeTest"/>
<class name="io.ballerina.c2c.test.samples.NativeArgsTest"/>
<class name="io.ballerina.c2c.test.samples.DockerProjectTest"/>
<class name="io.ballerina.c2c.test.samples.DockerSingleTest"/>
<class name="io.ballerina.c2c.test.SettingsMultiYAMLTest"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class DockerModel {
private PackageID pkgId;
private Path fatJarPath;
private boolean thinJar = true;
private String graalvmBuildArgs;

public DockerModel() {
// Initialize with default values except for image name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public void codeGeneratedInternal(PackageID packageId, Path executableJarFile, P
BuildOptions buildOptions = currentPackage.project().buildOptions();
String buildType = buildOptions.cloud();
dataHolder.getDockerModel().setFatJarPath(executableJarFile);
String graalvmBuildArgs = buildOptions.graalVMBuildOptions();
dataHolder.getDockerModel().setGraalvmBuildArgs(graalvmBuildArgs);
KubernetesContext.getInstance().setCurrentPackage(packageId);
dataHolder.setPackageID(packageId);
executableJarFile = executableJarFile.toAbsolutePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@
public class NativeDockerGenerator extends DockerGenerator {

public NativeDockerGenerator(DockerModel dockerModel) {

super(dockerModel);
}

@Override
public void createArtifacts(PrintStream outStream, String logAppender, Path jarFilePath, Path outputDir)
throws DockerGenException {

String dockerContent = generateMultiStageDockerfile();
try {
DockerGenUtils.writeToFile(dockerContent, outputDir.resolve("Dockerfile"));
Expand Down Expand Up @@ -74,15 +76,21 @@ public void createArtifacts(PrintStream outStream, String logAppender, Path jarF
}

private String generateMultiStageDockerfile() {

String fatJarFileName = this.dockerModel.getFatJarPath().getFileName().toString();
String executableName = fatJarFileName.replaceFirst(".jar", "");
StringBuilder nativeBuildScriptExec = new StringBuilder().append("RUN sh build-native.sh ").
append(fatJarFileName).append(" ").append(executableName);
if (!this.dockerModel.getGraalvmBuildArgs().equals("")) {
nativeBuildScriptExec.append(" '").append(this.dockerModel.getGraalvmBuildArgs()).append("'");
}
StringBuilder dockerfileContent =
new StringBuilder().append("# Auto Generated Dockerfile").append(LINE_SEPARATOR).append("FROM ")
.append(DockerGenConstants.NATIVE_BUILDER_IMAGE).append(" as build").append(LINE_SEPARATOR)
.append(LINE_SEPARATOR).append("WORKDIR /app/build").append(LINE_SEPARATOR)
.append(LINE_SEPARATOR).append("COPY ").append(fatJarFileName).append(" .")
.append(LINE_SEPARATOR).append(LINE_SEPARATOR)
.append("RUN sh build-native.sh ").append(fatJarFileName).append(" ").append(executableName)
.append(nativeBuildScriptExec)
.append(LINE_SEPARATOR).append(LINE_SEPARATOR).append("FROM ")
.append(DockerGenConstants.RUNTIME_BASE_IMAGE).append(LINE_SEPARATOR)
.append(LINE_SEPARATOR);
Expand All @@ -109,6 +117,7 @@ private String generateMultiStageDockerfile() {

@Override
protected void appendUser(StringBuilder dockerfileContent) {

dockerfileContent.append("RUN useradd -ms /bin/bash ballerina").append(LINE_SEPARATOR);
}
}
3 changes: 2 additions & 1 deletion docker-images/native-builder/build-native.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@

native-image -jar "$1" \
--no-fallback \
-H:Name="$2"
-H:Name="${2}" \
${3:+$(echo " $3")}
10 changes: 10 additions & 0 deletions examples/graalvm-build-args/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
org = "hello"
name= "native_args"
version = "0.0.1"

[build-options]
observabilityIncluded = true
cloud = "docker"
graalvm = true
graalvmBuildOptions = "--static --libc=musl"
7 changes: 7 additions & 0 deletions examples/graalvm-build-args/Cloud.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[container.image]
name = "build-arg"
repository = "xlight05"
tag = "1.0.0"

[settings]
buildImage = false
25 changes: 25 additions & 0 deletions examples/graalvm-build-args/service.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2023 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.

import ballerina/http;

listener http:Listener helloWorldEP = new (9095);

service /hello on helloWorldEP {
resource function get . () returns string {
return "Hello World!";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ version = "0.0.1"
[build-options]
observabilityIncluded = true
cloud = "k8s"
native = true
graalvm = true
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
org.gradle.caching=true
org.gradle.jvmargs='-Dfile.encoding=UTF-8'
group=io.ballerina
version=2.7.1-SNAPSHOT
version=2.8.0-SNAPSHOT
systemProp.org.gradle.internal.publish.checksums.insecure=true
ballerinaLangVersion=2201.6.0
ballerinaLangVersion=2201.7.0-20230616-003600-7453c9d7
stdlibConstraintVersion=1.2.0
stdlibIoVersion=1.4.1
stdlibLogVersion=2.7.1
Expand Down

0 comments on commit 9f5a85d

Please sign in to comment.