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

     1  //go:build integration
     2  // +build integration
     3  
     4  // can be executed with
     5  // go test -v -tags integration -run TestPythonIntegration ./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 TestPythonIntegrationBuildProject(t *testing.T) {
    21  	t.Parallel()
    22  	ctx := context.Background()
    23  	pwd, err := os.Getwd()
    24  	assert.NoError(t, err, "Getting current working directory failed.")
    25  	pwd = filepath.Dir(pwd)
    26  
    27  	tempDir, err := createTmpDir(t)
    28  	assert.NoError(t, err, "Error when creating temp dir")
    29  
    30  	err = copyDir(filepath.Join(pwd, "integration", "testdata", "TestPythonIntegration", "python-project"), tempDir)
    31  	if err != nil {
    32  		t.Fatal("Failed to copy test project.")
    33  	}
    34  
    35  	//workaround to use test script util it is possible to set workdir for Exec call
    36  	testScript := fmt.Sprintf(`#!/bin/sh
    37  		cd /test
    38  		/piperbin/piper pythonBuild >test-log.txt 2>&1`)
    39  	os.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700)
    40  
    41  	reqNode := testcontainers.ContainerRequest{
    42  		Image: "python:3.9",
    43  		Cmd:   []string{"tail", "-f"},
    44  		BindMounts: map[string]string{
    45  			pwd:     "/piperbin",
    46  			tempDir: "/test",
    47  		},
    48  	}
    49  
    50  	nodeContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
    51  		ContainerRequest: reqNode,
    52  		Started:          true,
    53  	})
    54  
    55  	code, err := nodeContainer.Exec(ctx, []string{"sh", "/test/runPiper.sh"})
    56  	assert.NoError(t, err)
    57  	assert.Equal(t, 0, code)
    58  
    59  	content, err := os.ReadFile(filepath.Join(tempDir, "/test-log.txt"))
    60  	if err != nil {
    61  		t.Fatal("Could not read test-log.txt.", err)
    62  	}
    63  	output := string(content)
    64  
    65  	assert.Contains(t, output, "info  pythonBuild - running command: python setup.py sdist bdist_wheel")
    66  	assert.Contains(t, output, "info  pythonBuild - running command: piperBuild-env/bin/pip install --upgrade cyclonedx-bom")
    67  	assert.Contains(t, output, "info  pythonBuild - running command: piperBuild-env/bin/cyclonedx-py --e --output bom-pip.xml")
    68  	assert.Contains(t, output, "info  pythonBuild - SUCCESS")
    69  
    70  	//workaround to use test script util it is possible to set workdir for Exec call
    71  	testScript = fmt.Sprintf(`#!/bin/sh
    72  		cd /test
    73  		ls -l . dist build >files-list.txt 2>&1`)
    74  	os.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700)
    75  
    76  	code, err = nodeContainer.Exec(ctx, []string{"sh", "/test/runPiper.sh"})
    77  	assert.NoError(t, err)
    78  	assert.Equal(t, 0, code)
    79  
    80  	content, err = os.ReadFile(filepath.Join(tempDir, "/files-list.txt"))
    81  	if err != nil {
    82  		t.Fatal("Could not read files-list.txt.", err)
    83  	}
    84  	output = string(content)
    85  	assert.Contains(t, output, "bom-pip.xml")
    86  	assert.Contains(t, output, "example-pkg-0.0.1.tar.gz")
    87  	assert.Contains(t, output, "example_pkg-0.0.1-py3-none-any.whl")
    88  }