2022-03-21 11:17:03 +02:00
|
|
|
//go:build integration
|
|
|
|
// +build integration
|
|
|
|
|
2022-09-19 12:47:13 +02:00
|
|
|
// can be executed with
|
|
|
|
// go test -v -tags integration -run TestGradleIntegration ./integration/...
|
2022-03-21 11:17:03 +02:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/testcontainers/testcontainers-go"
|
|
|
|
)
|
|
|
|
|
2022-09-19 12:47:13 +02:00
|
|
|
func TestGradleIntegrationExecuteBuildJavaProjectBOMCreationUsingWrapper(t *testing.T) {
|
2022-03-29 07:38:11 +02:00
|
|
|
t.Parallel()
|
2022-03-21 11:17:03 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
pwd, err := os.Getwd()
|
|
|
|
assert.NoError(t, err, "Getting current working directory failed.")
|
|
|
|
pwd = filepath.Dir(pwd)
|
|
|
|
|
|
|
|
// using custom createTmpDir function to avoid issues with symlinks on Docker for Mac
|
2022-07-12 15:19:12 +02:00
|
|
|
tempDir, err := createTmpDir(t)
|
2022-03-21 11:17:03 +02:00
|
|
|
assert.NoError(t, err, "Error when creating temp dir")
|
|
|
|
|
|
|
|
err = copyDir(filepath.Join(pwd, "integration", "testdata", "TestGradleIntegration", "java-project"), tempDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to copy test project.")
|
|
|
|
}
|
|
|
|
|
|
|
|
//workaround to use test script util it is possible to set workdir for Exec call
|
|
|
|
testScript := fmt.Sprintf(`#!/bin/sh
|
|
|
|
cd /test
|
2022-06-07 10:24:10 +02:00
|
|
|
/piperbin/piper gradleExecuteBuild >test-log.txt 2>&1
|
2022-03-21 11:17:03 +02:00
|
|
|
`)
|
2023-08-16 12:57:04 +02:00
|
|
|
os.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700)
|
2022-03-21 11:17:03 +02:00
|
|
|
|
|
|
|
reqNode := testcontainers.ContainerRequest{
|
2022-06-07 10:24:10 +02:00
|
|
|
Image: "adoptopenjdk/openjdk11:jdk-11.0.11_9-alpine",
|
2022-03-21 11:17:03 +02:00
|
|
|
Cmd: []string{"tail", "-f"},
|
|
|
|
BindMounts: map[string]string{
|
|
|
|
pwd: "/piperbin",
|
|
|
|
tempDir: "/test",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
|
|
|
|
ContainerRequest: reqNode,
|
|
|
|
Started: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
code, err := nodeContainer.Exec(ctx, []string{"sh", "/test/runPiper.sh"})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, code)
|
|
|
|
|
2023-08-16 12:57:04 +02:00
|
|
|
content, err := os.ReadFile(filepath.Join(tempDir, "/test-log.txt"))
|
2022-03-21 11:17:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not read test-log.txt.", err)
|
|
|
|
}
|
|
|
|
output := string(content)
|
2022-06-07 10:24:10 +02:00
|
|
|
assert.Contains(t, output, "info gradleExecuteBuild - running command: ./gradlew tasks")
|
|
|
|
assert.Contains(t, output, "info gradleExecuteBuild - running command: ./gradlew cyclonedxBom --init-script initScript.gradle.tmp")
|
|
|
|
assert.Contains(t, output, "info gradleExecuteBuild - running command: ./gradlew build")
|
2022-03-21 11:17:03 +02:00
|
|
|
assert.Contains(t, output, "info gradleExecuteBuild - BUILD SUCCESSFUL")
|
|
|
|
assert.Contains(t, output, "info gradleExecuteBuild - SUCCESS")
|
|
|
|
|
|
|
|
//workaround to use test script util it is possible to set workdir for Exec call
|
|
|
|
testScript = fmt.Sprintf(`#!/bin/sh
|
|
|
|
cd /test
|
|
|
|
ls -l ./build/reports/ >files-list.txt 2>&1
|
|
|
|
`)
|
2023-08-16 12:57:04 +02:00
|
|
|
os.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700)
|
2022-03-21 11:17:03 +02:00
|
|
|
|
|
|
|
code, err = nodeContainer.Exec(ctx, []string{"sh", "/test/runPiper.sh"})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, code)
|
|
|
|
|
2023-08-16 12:57:04 +02:00
|
|
|
content, err = os.ReadFile(filepath.Join(tempDir, "/files-list.txt"))
|
2022-03-21 11:17:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not read files-list.txt.", err)
|
|
|
|
}
|
|
|
|
output = string(content)
|
2022-08-01 13:38:49 +02:00
|
|
|
assert.Contains(t, output, "bom-gradle.xml")
|
2022-03-21 11:17:03 +02:00
|
|
|
}
|
|
|
|
|
2022-09-19 12:47:13 +02:00
|
|
|
func TestGradleIntegrationExecuteBuildJavaProjectWithBomPlugin(t *testing.T) {
|
2022-03-29 07:38:11 +02:00
|
|
|
t.Parallel()
|
2022-03-21 11:17:03 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
pwd, err := os.Getwd()
|
|
|
|
assert.NoError(t, err, "Getting current working directory failed.")
|
|
|
|
pwd = filepath.Dir(pwd)
|
|
|
|
|
|
|
|
// using custom createTmpDir function to avoid issues with symlinks on Docker for Mac
|
2022-07-12 15:19:12 +02:00
|
|
|
tempDir, err := createTmpDir(t)
|
2022-03-21 11:17:03 +02:00
|
|
|
assert.NoError(t, err, "Error when creating temp dir")
|
|
|
|
|
|
|
|
err = copyDir(filepath.Join(pwd, "integration", "testdata", "TestGradleIntegration", "java-project-with-bom-plugin"), tempDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to copy test project.")
|
|
|
|
}
|
|
|
|
|
|
|
|
//workaround to use test script util it is possible to set workdir for Exec call
|
|
|
|
testScript := fmt.Sprintf(`#!/bin/sh
|
|
|
|
cd /test
|
|
|
|
/piperbin/piper gradleExecuteBuild >test-log.txt 2>&1
|
|
|
|
`)
|
2023-08-16 12:57:04 +02:00
|
|
|
os.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700)
|
2022-03-21 11:17:03 +02:00
|
|
|
|
|
|
|
reqNode := testcontainers.ContainerRequest{
|
|
|
|
Image: "gradle:6-jdk11-alpine",
|
|
|
|
Cmd: []string{"tail", "-f"},
|
|
|
|
BindMounts: map[string]string{
|
|
|
|
pwd: "/piperbin",
|
|
|
|
tempDir: "/test",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
|
|
|
|
ContainerRequest: reqNode,
|
|
|
|
Started: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
code, err := nodeContainer.Exec(ctx, []string{"sh", "/test/runPiper.sh"})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, code)
|
|
|
|
|
2023-08-16 12:57:04 +02:00
|
|
|
content, err := os.ReadFile(filepath.Join(tempDir, "/test-log.txt"))
|
2022-03-21 11:17:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not read test-log.txt.", err)
|
|
|
|
}
|
|
|
|
output := string(content)
|
|
|
|
assert.Contains(t, output, "info gradleExecuteBuild - running command: gradle tasks")
|
2022-06-07 10:24:10 +02:00
|
|
|
assert.Contains(t, output, "info gradleExecuteBuild - running command: gradle cyclonedxBom")
|
2022-03-21 11:17:03 +02:00
|
|
|
assert.Contains(t, output, "info gradleExecuteBuild - running command: gradle build")
|
|
|
|
assert.Contains(t, output, "info gradleExecuteBuild - BUILD SUCCESSFUL")
|
|
|
|
assert.Contains(t, output, "info gradleExecuteBuild - SUCCESS")
|
|
|
|
|
|
|
|
//workaround to use test script util it is possible to set workdir for Exec call
|
|
|
|
testScript = fmt.Sprintf(`#!/bin/sh
|
|
|
|
cd /test
|
|
|
|
ls -l ./build/reports/ >files-list.txt 2>&1
|
|
|
|
`)
|
2023-08-16 12:57:04 +02:00
|
|
|
os.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700)
|
2022-03-21 11:17:03 +02:00
|
|
|
|
|
|
|
code, err = nodeContainer.Exec(ctx, []string{"sh", "/test/runPiper.sh"})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, code)
|
|
|
|
|
2023-08-16 12:57:04 +02:00
|
|
|
content, err = os.ReadFile(filepath.Join(tempDir, "/files-list.txt"))
|
2022-03-21 11:17:03 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not read files-list.txt.", err)
|
|
|
|
}
|
|
|
|
output = string(content)
|
2022-08-01 13:38:49 +02:00
|
|
|
assert.Contains(t, output, "bom-gradle.xml")
|
2022-03-21 11:17:03 +02:00
|
|
|
}
|