github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/integration/integration_gradle_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  // can be executed with
     5  // go test -v -tags integration -run TestGradleIntegration ./integration/...
     6  
     7  package main
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  	"os"
    13  	"path/filepath"
    14  	"testing"
    15  
    16  	"github.com/stretchr/testify/assert"
    17  	"github.com/testcontainers/testcontainers-go"
    18  )
    19  
    20  func TestGradleIntegrationExecuteBuildJavaProjectBOMCreationUsingWrapper(t *testing.T) {
    21  	t.Parallel()
    22  	ctx := context.Background()
    23  
    24  	pwd, err := os.Getwd()
    25  	assert.NoError(t, err, "Getting current working directory failed.")
    26  	pwd = filepath.Dir(pwd)
    27  
    28  	// using custom createTmpDir function to avoid issues with symlinks on Docker for Mac
    29  	tempDir, err := createTmpDir(t)
    30  	assert.NoError(t, err, "Error when creating temp dir")
    31  
    32  	err = copyDir(filepath.Join(pwd, "integration", "testdata", "TestGradleIntegration", "java-project"), tempDir)
    33  	if err != nil {
    34  		t.Fatal("Failed to copy test project.")
    35  	}
    36  
    37  	//workaround to use test script util it is possible to set workdir for Exec call
    38  	testScript := fmt.Sprintf(`#!/bin/sh
    39  cd /test
    40  /piperbin/piper gradleExecuteBuild >test-log.txt 2>&1
    41  `)
    42  	os.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700)
    43  
    44  	reqNode := testcontainers.ContainerRequest{
    45  		Image: "adoptopenjdk/openjdk11:jdk-11.0.11_9-alpine",
    46  		Cmd:   []string{"tail", "-f"},
    47  		BindMounts: map[string]string{
    48  			pwd:     "/piperbin",
    49  			tempDir: "/test",
    50  		},
    51  	}
    52  
    53  	nodeContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
    54  		ContainerRequest: reqNode,
    55  		Started:          true,
    56  	})
    57  
    58  	code, err := nodeContainer.Exec(ctx, []string{"sh", "/test/runPiper.sh"})
    59  	assert.NoError(t, err)
    60  	assert.Equal(t, 0, code)
    61  
    62  	content, err := os.ReadFile(filepath.Join(tempDir, "/test-log.txt"))
    63  	if err != nil {
    64  		t.Fatal("Could not read test-log.txt.", err)
    65  	}
    66  	output := string(content)
    67  	assert.Contains(t, output, "info  gradleExecuteBuild - running command: ./gradlew tasks")
    68  	assert.Contains(t, output, "info  gradleExecuteBuild - running command: ./gradlew cyclonedxBom --init-script initScript.gradle.tmp")
    69  	assert.Contains(t, output, "info  gradleExecuteBuild - running command: ./gradlew build")
    70  	assert.Contains(t, output, "info  gradleExecuteBuild - BUILD SUCCESSFUL")
    71  	assert.Contains(t, output, "info  gradleExecuteBuild - SUCCESS")
    72  
    73  	//workaround to use test script util it is possible to set workdir for Exec call
    74  	testScript = fmt.Sprintf(`#!/bin/sh
    75  cd /test
    76  ls -l ./build/reports/ >files-list.txt 2>&1
    77  `)
    78  	os.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700)
    79  
    80  	code, err = nodeContainer.Exec(ctx, []string{"sh", "/test/runPiper.sh"})
    81  	assert.NoError(t, err)
    82  	assert.Equal(t, 0, code)
    83  
    84  	content, err = os.ReadFile(filepath.Join(tempDir, "/files-list.txt"))
    85  	if err != nil {
    86  		t.Fatal("Could not read files-list.txt.", err)
    87  	}
    88  	output = string(content)
    89  	assert.Contains(t, output, "bom-gradle.xml")
    90  }
    91  
    92  func TestGradleIntegrationExecuteBuildJavaProjectWithBomPlugin(t *testing.T) {
    93  	t.Parallel()
    94  	ctx := context.Background()
    95  
    96  	pwd, err := os.Getwd()
    97  	assert.NoError(t, err, "Getting current working directory failed.")
    98  	pwd = filepath.Dir(pwd)
    99  
   100  	// using custom createTmpDir function to avoid issues with symlinks on Docker for Mac
   101  	tempDir, err := createTmpDir(t)
   102  	assert.NoError(t, err, "Error when creating temp dir")
   103  
   104  	err = copyDir(filepath.Join(pwd, "integration", "testdata", "TestGradleIntegration", "java-project-with-bom-plugin"), tempDir)
   105  	if err != nil {
   106  		t.Fatal("Failed to copy test project.")
   107  	}
   108  
   109  	//workaround to use test script util it is possible to set workdir for Exec call
   110  	testScript := fmt.Sprintf(`#!/bin/sh
   111  cd /test
   112  /piperbin/piper gradleExecuteBuild >test-log.txt 2>&1
   113  `)
   114  	os.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700)
   115  
   116  	reqNode := testcontainers.ContainerRequest{
   117  		Image: "gradle:6-jdk11-alpine",
   118  		Cmd:   []string{"tail", "-f"},
   119  		BindMounts: map[string]string{
   120  			pwd:     "/piperbin",
   121  			tempDir: "/test",
   122  		},
   123  	}
   124  
   125  	nodeContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
   126  		ContainerRequest: reqNode,
   127  		Started:          true,
   128  	})
   129  
   130  	code, err := nodeContainer.Exec(ctx, []string{"sh", "/test/runPiper.sh"})
   131  	assert.NoError(t, err)
   132  	assert.Equal(t, 0, code)
   133  
   134  	content, err := os.ReadFile(filepath.Join(tempDir, "/test-log.txt"))
   135  	if err != nil {
   136  		t.Fatal("Could not read test-log.txt.", err)
   137  	}
   138  	output := string(content)
   139  	assert.Contains(t, output, "info  gradleExecuteBuild - running command: gradle tasks")
   140  	assert.Contains(t, output, "info  gradleExecuteBuild - running command: gradle cyclonedxBom")
   141  	assert.Contains(t, output, "info  gradleExecuteBuild - running command: gradle build")
   142  	assert.Contains(t, output, "info  gradleExecuteBuild - BUILD SUCCESSFUL")
   143  	assert.Contains(t, output, "info  gradleExecuteBuild - SUCCESS")
   144  
   145  	//workaround to use test script util it is possible to set workdir for Exec call
   146  	testScript = fmt.Sprintf(`#!/bin/sh
   147  cd /test
   148  ls -l ./build/reports/ >files-list.txt 2>&1
   149  `)
   150  	os.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700)
   151  
   152  	code, err = nodeContainer.Exec(ctx, []string{"sh", "/test/runPiper.sh"})
   153  	assert.NoError(t, err)
   154  	assert.Equal(t, 0, code)
   155  
   156  	content, err = os.ReadFile(filepath.Join(tempDir, "/files-list.txt"))
   157  	if err != nil {
   158  		t.Fatal("Could not read files-list.txt.", err)
   159  	}
   160  	output = string(content)
   161  	assert.Contains(t, output, "bom-gradle.xml")
   162  }