Search in sources :

Example 61 with BuildResult

use of org.gradle.testkit.runner.BuildResult in project spring-boot by spring-projects.

the class PaketoBuilderTests method plainDistZipJarApp.

@Test
void plainDistZipJarApp() throws Exception {
    writeMainClass();
    String projectName = this.gradleBuild.getProjectDir().getName();
    String imageName = "paketo-integration/" + projectName;
    ImageReference imageReference = ImageReference.of(ImageName.of(imageName));
    BuildResult result = buildImage(imageName, "assemble", "bootDistZip");
    assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
    try (GenericContainer<?> container = new GenericContainer<>(imageName).withExposedPorts(8080)) {
        container.waitingFor(Wait.forHttp("/test")).start();
        ContainerConfig config = container.getContainerInfo().getConfig();
        ImageAssertions.assertThat(config).buildMetadata((metadata) -> {
            metadata.buildpacks().contains("paketo-buildpacks/ca-certificates", "paketo-buildpacks/bellsoft-liberica", "paketo-buildpacks/dist-zip", "paketo-buildpacks/spring-boot");
            metadata.processOfType("web").extracting("command", "args").containsExactly("/workspace/" + projectName + "/bin/" + projectName, Collections.emptyList());
            metadata.processOfType("dist-zip").extracting("command", "args").containsExactly("/workspace/" + projectName + "/bin/" + projectName, Collections.emptyList());
        });
        assertImageHasSbomLayer(imageReference, config, "dist-zip");
        DigestCapturingCondition digest = new DigestCapturingCondition();
        ImageAssertions.assertThat(config).lifecycleMetadata((metadata) -> metadata.appLayerShas().haveExactly(1, digest));
        ImageAssertions.assertThat(imageReference).layer(digest.getDigest(), (layer) -> layer.entries().contains(projectName + "/bin/" + projectName, projectName + "/lib/" + projectName + "-plain.jar").anyMatch((s) -> s.startsWith(projectName + "/lib/spring-boot-")).anyMatch((s) -> s.startsWith(projectName + "/lib/spring-core-")).anyMatch((s) -> s.startsWith(projectName + "/lib/spring-web-")));
    } finally {
        removeImage(imageReference);
    }
}
Also used : ContainerConfig(com.github.dockerjava.api.model.ContainerConfig) ImageName(org.springframework.boot.buildpack.platform.docker.type.ImageName) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) ContainerConfig(com.github.dockerjava.api.model.ContainerConfig) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TaskOutcome(org.gradle.testkit.runner.TaskOutcome) ImageReference(org.springframework.boot.buildpack.platform.docker.type.ImageReference) JarFile(java.util.jar.JarFile) ArrayList(java.util.ArrayList) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) GradleBuildInjectionExtension(org.springframework.boot.image.junit.GradleBuildInjectionExtension) GradleBuildExtension(org.springframework.boot.testsupport.gradle.testkit.GradleBuildExtension) GenericContainer(org.testcontainers.containers.GenericContainer) PrintWriter(java.io.PrintWriter) Wait(org.testcontainers.containers.wait.strategy.Wait) GradleBuild(org.springframework.boot.testsupport.gradle.testkit.GradleBuild) ImageAssertions(org.springframework.boot.image.assertions.ImageAssertions) Predicate(java.util.function.Predicate) FileWriter(java.io.FileWriter) IOException(java.io.IOException) BuildResult(org.gradle.testkit.runner.BuildResult) Attributes(java.util.jar.Attributes) Assertions.entry(org.assertj.core.api.Assertions.entry) File(java.io.File) DockerApi(org.springframework.boot.buildpack.platform.docker.DockerApi) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) List(java.util.List) Condition(org.assertj.core.api.Condition) Collections(java.util.Collections) StringUtils(org.springframework.util.StringUtils) ImageReference(org.springframework.boot.buildpack.platform.docker.type.ImageReference) BuildResult(org.gradle.testkit.runner.BuildResult) Test(org.junit.jupiter.api.Test)

Example 62 with BuildResult

use of org.gradle.testkit.runner.BuildResult in project spring-boot by spring-projects.

the class PaketoBuilderTests method executableJarAppWithAdditionalArgs.

@Test
void executableJarAppWithAdditionalArgs() throws Exception {
    writeMainClass();
    String imageName = "paketo-integration/" + this.gradleBuild.getProjectDir().getName();
    ImageReference imageReference = ImageReference.of(ImageName.of(imageName));
    BuildResult result = buildImage(imageName);
    assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
    try (GenericContainer<?> container = new GenericContainer<>(imageName).withCommand("--server.port=9090").withExposedPorts(9090)) {
        container.waitingFor(Wait.forHttp("/test")).start();
    } finally {
        removeImage(imageReference);
    }
}
Also used : ImageReference(org.springframework.boot.buildpack.platform.docker.type.ImageReference) BuildResult(org.gradle.testkit.runner.BuildResult) GenericContainer(org.testcontainers.containers.GenericContainer) Test(org.junit.jupiter.api.Test)

Example 63 with BuildResult

use of org.gradle.testkit.runner.BuildResult in project crate by crate.

the class JdkDownloadPluginFunctionalTest method runBuild.

private static void runBuild(String task, Consumer<BuildResult> assertions, String os, String arch, String vendor, String version) {
    var testKitDirPath = Paths.get(System.getProperty("user.dir"), "build", System.getProperty("user.name"));
    WireMockServer wireMock = new WireMockServer(0);
    try {
        wireMock.stubFor(head(urlEqualTo(urlPath(vendor, version, os, arch))).willReturn(aResponse().withStatus(200)));
        wireMock.stubFor(get(urlEqualTo(urlPath(vendor, version, os, arch))).willReturn(aResponse().withStatus(200).withBody(filebytes(vendor, version, os, arch))));
        wireMock.start();
        GradleRunner runner = GradleRunner.create().withDebug(true).withProjectDir(getTestKitProjectDir("jdk-download")).withTestKitDir(testKitDirPath.toFile()).withArguments(task, "-Dtests.jdk_vendor=" + vendor, "-Dtests.jdk_version=" + version, "-Dtests.jdk_repo=" + wireMock.baseUrl(), "-i").withPluginClasspath();
        BuildResult result = runner.build();
        assertions.accept(result);
    } catch (IOException e) {
        fail("cannot get artifacts from resources: " + e.getLocalizedMessage());
    } finally {
        wireMock.stop();
    }
}
Also used : BuildResult(org.gradle.testkit.runner.BuildResult) GradleRunner(org.gradle.testkit.runner.GradleRunner) IOException(java.io.IOException) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer)

Example 64 with BuildResult

use of org.gradle.testkit.runner.BuildResult in project swagger-core by swagger-api.

the class SwaggerResolveTest method testSwaggerResolveTask.

@Test
public void testSwaggerResolveTask() throws IOException {
    outputDir = testProjectDir.getRoot().toString() + "/target";
    outputFile = testProjectDir.getRoot().toString() + "/testAPI.json";
    // outputDir = "/tmp/a/target";
    String resolveTask = "resolve";
    String buildFileContent = "plugins {\n" + "    id 'java'\n" + "    id 'io.swagger.core.v3.swagger-gradle-plugin'\n" + "}\n" + "sourceSets {\n" + "    test {\n" + "        java {\n" + "            srcDirs('" + toNormalizedPath(new File("src/test/java").getAbsolutePath()) + "')\n" + "            exclude('**/*Test.java')\n" + "        }\n" + "    }\n" + "}\n" + "repositories {\n" + "    mavenLocal()\n" + "    mavenCentral()\n" + "}\n" + "dependencies {  \n" + "    compile group: 'io.swagger.core.v3', name: 'swagger-jaxrs2', version:'2.2.0-SNAPSHOT'\n" + "    compile group: 'javax.ws.rs', name: 'javax.ws.rs-api', version:'2.1'\n" + "    compile group: 'javax.servlet', name: 'javax.servlet-api', version:'3.1.0'\n" + "    testCompile group: 'com.github.tomakehurst', name: 'wiremock', version:'2.27.2'\n" + "    testCompile 'junit:junit:4+'\n" + "\n" + "\n" + "}\n" + resolveTask + " {\n" + "    outputFileName = 'PetStoreAPI'\n" + "    outputFormat = 'JSON'\n" + "    prettyPrint = 'TRUE'\n" + // "    classpath = compileTestJava.outputs.files\n" +
    "    classpath = sourceSets.test.runtimeClasspath\n" + "    resourcePackages = ['io.swagger.v3.plugins.gradle.petstore']\n" + "    outputPath = \'" + toNormalizedPath(outputDir) + "\'\n" + "    filterClass = \'io.swagger.v3.plugins.gradle.resources.MyFilter\'\n" + "    openApiFile = file(\'" + toNormalizedPath(openapiInputFile.getAbsolutePath()) + "\')\n" + "}";
    String settingsFileContent = "pluginManagement {\n" + "    repositories {\n" + "        maven {\n" + "            url mavenLocal().url\n" + "        }\n" + "        mavenCentral()\n" + "        gradlePluginPortal()\n" + "    }\n" + "}\n" + "rootProject.name = 'gradle-test'\n" + "\n";
    writeFile(buildFile, buildFileContent);
    writeFile(settingsFile, settingsFileContent);
    BuildResult result = GradleRunner.create().withPluginClasspath().withProjectDir(testProjectDir.getRoot()).withDebug(true).withArguments(resolveTask, "--stacktrace", "--info").forwardOutput().build();
    assertThat(result.taskPaths(SUCCESS), hasItem(format(":%s", resolveTask)));
    assertThat(new File(outputDir + "/PetStoreAPI.json").exists(), is(true));
    assertThat(new String(Files.readAllBytes(Paths.get(outputDir, "PetStoreAPI.json")), StandardCharsets.UTF_8), containsString("UPDATEDBYFILTER"));
}
Also used : BuildResult(org.gradle.testkit.runner.BuildResult) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 65 with BuildResult

use of org.gradle.testkit.runner.BuildResult in project gradle by gradle.

the class GreetingPluginFunctionalTest method canRunTask.

@Test
public void canRunTask() throws IOException {
    // Setup the test build
    File projectDir = new File("build/functionalTest");
    Files.createDirectories(projectDir.toPath());
    writeString(new File(projectDir, "settings.gradle"), "");
    writeString(new File(projectDir, "build.gradle"), "plugins {" + "  id('com.example.plugin.greeting')" + "}");
    // Run the build
    BuildResult result = GradleRunner.create().forwardOutput().withPluginClasspath().withArguments("greet").withProjectDir(projectDir).build();
    // Verify the result
    assertTrue(result.getOutput().contains("Hello from plugin 'com.example.plugin.greeting'"));
}
Also used : BuildResult(org.gradle.testkit.runner.BuildResult) File(java.io.File) Test(org.junit.Test)

Aggregations

BuildResult (org.gradle.testkit.runner.BuildResult)143 File (java.io.File)62 TestTemplate (org.junit.jupiter.api.TestTemplate)60 Test (org.junit.Test)47 Test (org.junit.jupiter.api.Test)20 StringWriter (java.io.StringWriter)19 GradleRunner (org.gradle.testkit.runner.GradleRunner)17 Test (org.testng.annotations.Test)10 ImageReference (org.springframework.boot.buildpack.platform.docker.type.ImageReference)8 JarFile (java.util.jar.JarFile)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 TestHelpers.findFile (me.tatarka.TestHelpers.findFile)6 TestHelpers.writeFile (me.tatarka.TestHelpers.writeFile)6 ContainerConfig (com.github.dockerjava.api.model.ContainerConfig)5 FileWriter (java.io.FileWriter)5 IOException (java.io.IOException)5 PrintWriter (java.io.PrintWriter)5 Path (java.nio.file.Path)5 BufferedReader (java.io.BufferedReader)4