Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonydahanne committed Jul 28, 2024
1 parent 60c1ea9 commit 94ac5b4
Show file tree
Hide file tree
Showing 18 changed files with 856 additions and 51 deletions.
26 changes: 6 additions & 20 deletions smoke/init_test.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
package smoke_test

import (
"encoding/json"
"flag"
"os"
"testing"
"time"

"github.com/paketo-buildpacks/occam"
"github.com/onsi/gomega/format"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"

. "github.com/onsi/gomega"
)

var (
Builder string

config struct {
Procfile string `json:"procfile"`
}
)
var Builder string

func init() {
flag.StringVar(&Builder, "name", "", "")
}

func TestSmoke(t *testing.T) {
format.MaxLength = 0
Expect := NewWithT(t).Expect

flag.Parse()
Expand All @@ -35,15 +28,8 @@ func TestSmoke(t *testing.T) {

SetDefaultEventuallyTimeout(60 * time.Second)

file, err := os.Open("../smoke.json")
Expect(err).NotTo(HaveOccurred())

Expect(json.NewDecoder(file).Decode(&config)).To(Succeed())
Expect(file.Close()).To(Succeed())

Expect(occam.NewDocker().Pull.Execute(config.Procfile))

suite := spec.New("Buildpack Smoke", spec.Parallel(), spec.Report(report.Terminal{}))
suite("Procfile", testProcfile)
suite := spec.New("Smoke", spec.Parallel(), spec.Report(report.Terminal{}))
suite("Java Native Image", testJavaNativeImage)
suite("Java", testJava)
suite.Run(t)
}
80 changes: 80 additions & 0 deletions smoke/java_native_image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package smoke_test

import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/paketo-buildpacks/occam"
"github.com/sclevine/spec"

. "github.com/onsi/gomega"
. "github.com/paketo-buildpacks/occam/matchers"
)

func testJavaNativeImage(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
Eventually = NewWithT(t).Eventually

pack occam.Pack
docker occam.Docker
)

it.Before(func() {
pack = occam.NewPack().WithVerbose().WithNoColor()
docker = occam.NewDocker()
})

context("detects a Java Native Image app", func() {
var (
image occam.Image
container occam.Container

name string
source string
)

it.Before(func() {
var err error
name, err = occam.RandomName()
Expect(err).NotTo(HaveOccurred())
})

it.After(func() {
Expect(docker.Container.Remove.Execute(container.ID)).To(Succeed())
Expect(docker.Volume.Remove.Execute(occam.CacheVolumeNames(name))).To(Succeed())
Expect(docker.Image.Remove.Execute(image.ID)).To(Succeed())
Expect(os.RemoveAll(source)).To(Succeed())
})

it("builds successfully", func() {
var err error
source, err = occam.Source(filepath.Join("testdata", "java-native-image"))
Expect(err).NotTo(HaveOccurred())

var logs fmt.Stringer
image, logs, err = pack.Build.
WithPullPolicy("never").
WithBuilder(Builder).
WithEnv(map[string]string{"BP_NATIVE_IMAGE": "true", "BP_JVM_VERSION": "17", "BP_MAVEN_BUILD_ARGUMENTS": "-Pnative --batch-mode -Dmaven.test.skip=true --no-transfer-progress package", "USE_NATIVE_IMAGE_JAVA_PLATFORM_MODULE_SYSTEM": "false"}).
Execute(name, source)
Expect(err).ToNot(HaveOccurred(), logs.String)

container, err = docker.Container.Run.
WithEnv(map[string]string{"PORT": "8080"}).
WithPublish("8080").
Execute(image.ID)
Expect(err).NotTo(HaveOccurred())

Eventually(container).Should(BeAvailable())

Expect(logs).To(ContainLines(ContainSubstring("Paketo Buildpack for BellSoft Liberica")))
Expect(logs).To(ContainLines(ContainSubstring("Paketo Buildpack for Maven")))
Expect(logs).To(ContainLines(ContainSubstring("Paketo Buildpack for Executable JAR")))
Expect(logs).To(ContainLines(ContainSubstring("Paketo Buildpack for Spring Boot")))
Expect(logs).To(ContainLines(ContainSubstring("Paketo Buildpack for Native Image")))
})
})
}
22 changes: 12 additions & 10 deletions smoke/procfile_test.go → smoke/java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
. "github.com/paketo-buildpacks/occam/matchers"
)

func testProcfile(t *testing.T, context spec.G, it spec.S) {
func testJava(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
Eventually = NewWithT(t).Eventually
Expand All @@ -27,7 +27,7 @@ func testProcfile(t *testing.T, context spec.G, it spec.S) {
docker = occam.NewDocker()
})

context("procfile buildpack specified at build time", func() {
context("detects a Java app", func() {
var (
image occam.Image
container occam.Container
Expand All @@ -40,9 +40,6 @@ func testProcfile(t *testing.T, context spec.G, it spec.S) {
var err error
name, err = occam.RandomName()
Expect(err).NotTo(HaveOccurred())

source, err = occam.Source(filepath.Join("testdata", "procfile"))
Expect(err).NotTo(HaveOccurred())
})

it.After(func() {
Expand All @@ -52,15 +49,17 @@ func testProcfile(t *testing.T, context spec.G, it spec.S) {
Expect(os.RemoveAll(source)).To(Succeed())
})

it("builds Procfile app successfully", func() {
it("builds successfully", func() {
var err error
// this is OK, the app also works for running with a JVM
source, err = occam.Source(filepath.Join("testdata", "java-native-image"))
Expect(err).NotTo(HaveOccurred())

var logs fmt.Stringer
image, logs, err = pack.Build.
WithPullPolicy("never").
WithBuilder(Builder).
WithBuildpacks(
config.Procfile,
).
WithEnv(map[string]string{"BP_JVM_VERSION": "17"}).
Execute(name, source)
Expect(err).ToNot(HaveOccurred(), logs.String)

Expand All @@ -72,7 +71,10 @@ func testProcfile(t *testing.T, context spec.G, it spec.S) {

Eventually(container).Should(BeAvailable())

Expect(logs).To(ContainLines(ContainSubstring("Paketo Buildpack for Procfile")))
Expect(logs).To(ContainLines(ContainSubstring("Paketo Buildpack for BellSoft Liberica")))
Expect(logs).To(ContainLines(ContainSubstring("Paketo Buildpack for Maven")))
Expect(logs).To(ContainLines(ContainSubstring("Paketo Buildpack for Executable JAR")))
Expect(logs).To(ContainLines(ContainSubstring("Paketo Buildpack for Spring Boot")))
})
})
}
33 changes: 33 additions & 0 deletions smoke/testdata/java-native-image/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2007-present the original author or authors.
*
* Licensed 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
*
* https://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 java.net.*;
import java.io.*;
import java.nio.channels.*;
import java.util.Properties;

public class MavenWrapperDownloader {

private static final String WRAPPER_VERSION = "0.5.6";
/**
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
*/
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";

/**
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
* use instead of the default one.
*/
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
".mvn/wrapper/maven-wrapper.properties";

/**
* Path where the maven-wrapper.jar will be saved to.
*/
private static final String MAVEN_WRAPPER_JAR_PATH =
".mvn/wrapper/maven-wrapper.jar";

/**
* Name of the property which should be used to override the default download url for the wrapper.
*/
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";

public static void main(String args[]) {
System.out.println("- Downloader started");
File baseDirectory = new File(args[0]);
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());

// If the maven-wrapper.properties exists, read it and check if it contains a custom
// wrapperUrl parameter.
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
String url = DEFAULT_DOWNLOAD_URL;
if(mavenWrapperPropertyFile.exists()) {
FileInputStream mavenWrapperPropertyFileInputStream = null;
try {
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
Properties mavenWrapperProperties = new Properties();
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
} catch (IOException e) {
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
} finally {
try {
if(mavenWrapperPropertyFileInputStream != null) {
mavenWrapperPropertyFileInputStream.close();
}
} catch (IOException e) {
// Ignore ...
}
}
}
System.out.println("- Downloading from: " + url);

File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
if(!outputFile.getParentFile().exists()) {
if(!outputFile.getParentFile().mkdirs()) {
System.out.println(
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
}
}
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
try {
downloadFileFromURL(url, outputFile);
System.out.println("Done");
System.exit(0);
} catch (Throwable e) {
System.out.println("- Error downloading");
e.printStackTrace();
System.exit(1);
}
}

private static void downloadFileFromURL(String urlString, File destination) throws Exception {
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
String username = System.getenv("MVNW_USERNAME");
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}
URL website = new URL(urlString);
ReadableByteChannel rbc;
rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}

}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar
21 changes: 21 additions & 0 deletions smoke/testdata/java-native-image/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Java Native Image Sample Application

## Building

```bash
pack build applications/native-image \
--builder paketobuildpacks/builder:tiny \
--env BP_NATIVE_IMAGE=true
```

## Running

```bash
docker run --rm --tty --publish 8080:8080 applications/native-image
```

## Viewing

```bash
curl -s http://localhost:8080/actuator/health | jq .
```
Loading

0 comments on commit 94ac5b4

Please sign in to comment.