2022-03-21 11:17:03 +02:00
|
|
|
//go:build integration
|
|
|
|
// +build integration
|
|
|
|
|
|
|
|
// can be execute with go test -tags=integration ./integration/...
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/testcontainers/testcontainers-go"
|
|
|
|
)
|
|
|
|
|
2022-06-07 10:24:10 +02:00
|
|
|
func TestGradleExecuteBuild_JavaProject_BOMCreation_UsingWrapper(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
|
|
|
|
tempDir, err := createTmpDir("")
|
|
|
|
defer os.RemoveAll(tempDir) // clean up
|
|
|
|
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
|
|
|
`)
|
|
|
|
ioutil.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(filepath.Join(tempDir, "/test-log.txt"))
|
|
|
|
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
|
|
|
|
`)
|
|
|
|
ioutil.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700)
|
|
|
|
|
|
|
|
code, err = nodeContainer.Exec(ctx, []string{"sh", "/test/runPiper.sh"})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, code)
|
|
|
|
|
|
|
|
content, err = ioutil.ReadFile(filepath.Join(tempDir, "/files-list.txt"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not read files-list.txt.", err)
|
|
|
|
}
|
|
|
|
output = string(content)
|
|
|
|
assert.Contains(t, output, "bom.xml")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGradleExecuteBuild_JavaProjectWithBomPlugin(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
|
|
|
|
tempDir, err := createTmpDir("")
|
|
|
|
defer os.RemoveAll(tempDir) // clean up
|
|
|
|
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
|
|
|
|
`)
|
|
|
|
ioutil.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
content, err := ioutil.ReadFile(filepath.Join(tempDir, "/test-log.txt"))
|
|
|
|
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
|
|
|
|
`)
|
|
|
|
ioutil.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700)
|
|
|
|
|
|
|
|
code, err = nodeContainer.Exec(ctx, []string{"sh", "/test/runPiper.sh"})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 0, code)
|
|
|
|
|
|
|
|
content, err = ioutil.ReadFile(filepath.Join(tempDir, "/files-list.txt"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not read files-list.txt.", err)
|
|
|
|
}
|
|
|
|
output = string(content)
|
|
|
|
assert.Contains(t, output, "bom.xml")
|
|
|
|
}
|