1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/integration/integration_gcs_test.go

183 lines
6.0 KiB
Go
Raw Normal View History

feat(whitesourceExecuteScan): GitHub issue creation + SARIF (#3535) * Add GH issue creation + SARIF * Code cleanup * Fix fmt, add debug * Code enhancements * Fix * Added debug info * Rework UA log scan * Fix code * read UA version * Fix nil reference * Extraction * Credentials * Issue creation * Error handling * Fix issue creation * query escape * Query escape 2 * Revert * Test avoid update * HTTP client * Add support for custom TLS certs * Fix code * Fix code 2 * Fix code 3 * Disable cert check * Fix auth * Remove implicit trust * Skip verification * Fix * Fix client * Fix HTTP auth * Fix trusted certs * Trim version * Code * Add token * Added token handling to client * Fix token * Cleanup * Fix token * Token rework * Fix code * Kick out oauth client * Kick out oauth client * Transport wrapping * Token * Simplification * Refactor * Variation * Check * Fix * Debug * Switch client * Variation * Debug * Switch to cert check * Add debug * Parse self * Cleanup * Update resources/metadata/whitesourceExecuteScan.yaml * Add debug * Expose subjects * Patch * Debug * Debug2 * Debug3 * Fix logging response body * Cleanup * Cleanup * Fix request body logging * Cleanup import * Fix import cycle * Cleanup * Fix fmt * Fix NopCloser reference * Regenerate * Reintroduce * Fix test * Fix tests * Correction * Fix error * Code fix * Fix tests * Add tests * Fix code climate issues * Code climate * Code climate again * Code climate again * Fix fmt * Fix fmt 2 Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2022-02-23 10:30:19 +02:00
//go:build integration
feat(gcs): allow upload to gcs from steps (#3034) * Upload reports to Google Cloud Storage bucket * Added tests. Made fixes * Update step generation. GCS client was moved to GeneralConfig * Code was refactored * Fixed issues * Fixed issues * Code correction due to PR comments * Improved gcs client and integration tests * Integrated gcp config. Updated step metadata * Fixed issues. Added tests * Added cpe, vault, aliases resolving for reporting parameters * Added tests * Uncommented DeferExitHandler. Removed useless comments * fixed cloning of config * Added comments for exported functions. Removed unused mock * minor fix * Implemented setting of report name via paramRef * some refactoring. Writing tests * Update pkg/config/reporting.go * Update cmd/sonarExecuteScan_generated.go * Apply suggestions from code review * Update pkg/config/reporting.go * Update pkg/config/reporting.go * fixed removing valut secret files * Update pkg/config/reporting.go * restore order * restore order * Apply suggestions from code review * go generate * fixed tests * Update resources/metadata/sonarExecuteScan.yaml * Update resources.go * Fixed tests. Code was regenerated * changed somewhere gcp to gcs. Fixed one test * move gcsSubFolder to input parameters * fixed removing valut secret files * minor fix in integration tests * fix integration tests Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com> Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> Co-authored-by: Sven Merk <33895725+nevskrem@users.noreply.github.com>
2021-12-15 16:07:47 +02:00
// +build integration
feat(whitesourceExecuteScan): GitHub issue creation + SARIF (#3535) * Add GH issue creation + SARIF * Code cleanup * Fix fmt, add debug * Code enhancements * Fix * Added debug info * Rework UA log scan * Fix code * read UA version * Fix nil reference * Extraction * Credentials * Issue creation * Error handling * Fix issue creation * query escape * Query escape 2 * Revert * Test avoid update * HTTP client * Add support for custom TLS certs * Fix code * Fix code 2 * Fix code 3 * Disable cert check * Fix auth * Remove implicit trust * Skip verification * Fix * Fix client * Fix HTTP auth * Fix trusted certs * Trim version * Code * Add token * Added token handling to client * Fix token * Cleanup * Fix token * Token rework * Fix code * Kick out oauth client * Kick out oauth client * Transport wrapping * Token * Simplification * Refactor * Variation * Check * Fix * Debug * Switch client * Variation * Debug * Switch to cert check * Add debug * Parse self * Cleanup * Update resources/metadata/whitesourceExecuteScan.yaml * Add debug * Expose subjects * Patch * Debug * Debug2 * Debug3 * Fix logging response body * Cleanup * Cleanup * Fix request body logging * Cleanup import * Fix import cycle * Cleanup * Fix fmt * Fix NopCloser reference * Regenerate * Reintroduce * Fix test * Fix tests * Correction * Fix error * Code fix * Fix tests * Add tests * Fix code climate issues * Code climate * Code climate again * Code climate again * Fix fmt * Fix fmt 2 Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2022-02-23 10:30:19 +02:00
feat(gcs): allow upload to gcs from steps (#3034) * Upload reports to Google Cloud Storage bucket * Added tests. Made fixes * Update step generation. GCS client was moved to GeneralConfig * Code was refactored * Fixed issues * Fixed issues * Code correction due to PR comments * Improved gcs client and integration tests * Integrated gcp config. Updated step metadata * Fixed issues. Added tests * Added cpe, vault, aliases resolving for reporting parameters * Added tests * Uncommented DeferExitHandler. Removed useless comments * fixed cloning of config * Added comments for exported functions. Removed unused mock * minor fix * Implemented setting of report name via paramRef * some refactoring. Writing tests * Update pkg/config/reporting.go * Update cmd/sonarExecuteScan_generated.go * Apply suggestions from code review * Update pkg/config/reporting.go * Update pkg/config/reporting.go * fixed removing valut secret files * Update pkg/config/reporting.go * restore order * restore order * Apply suggestions from code review * go generate * fixed tests * Update resources/metadata/sonarExecuteScan.yaml * Update resources.go * Fixed tests. Code was regenerated * changed somewhere gcp to gcs. Fixed one test * move gcsSubFolder to input parameters * fixed removing valut secret files * minor fix in integration tests * fix integration tests Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com> Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> Co-authored-by: Sven Merk <33895725+nevskrem@users.noreply.github.com>
2021-12-15 16:07:47 +02:00
// can be execute with go test -tags=integration ./integration/...
package main
import (
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"path/filepath"
"strings"
"testing"
"github.com/SAP/jenkins-library/pkg/gcs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"google.golang.org/api/option"
)
func Test_gcsClient(t *testing.T) {
t.Parallel()
ctx := context.Background()
testdataPath, err := filepath.Abs("testdata/TestGCSIntegration")
assert.NoError(t, err)
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
AlwaysPullImage: true,
Image: "fsouza/fake-gcs-server:1.30.2",
ExposedPorts: []string{"4443/tcp"},
WaitingFor: wait.ForListeningPort("4443/tcp"),
Cmd: []string{"-scheme", "https", "-public-host", "localhost"},
BindMounts: map[string]string{
testdataPath: "/data",
},
},
Started: true,
}
gcsContainer, err := testcontainers.GenericContainer(ctx, req)
require.NoError(t, err)
defer gcsContainer.Terminate(ctx)
ip, err := gcsContainer.Host(ctx)
require.NoError(t, err)
port, err := gcsContainer.MappedPort(ctx, "4443")
endpoint := fmt.Sprintf("https://%s:%s/storage/v1/", ip, port.Port())
httpclient := http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
t.Run("Test list files - success", func(t *testing.T) {
bucketID := "sample-bucket"
gcsClient, err := gcs.NewClient(gcs.WithClientOptions(option.WithEndpoint(endpoint), option.WithoutAuthentication(), option.WithHTTPClient(&httpclient)))
assert.NoError(t, err)
fileNames, err := gcsClient.ListFiles(bucketID)
assert.NoError(t, err)
assert.Equal(t, []string{"dir/test_file2.yaml", "test_file.txt"}, fileNames)
err = gcsClient.Close()
assert.NoError(t, err)
})
t.Run("Test list files in missing bucket", func(t *testing.T) {
bucketID := "missing-bucket"
gcsClient, err := gcs.NewClient(gcs.WithClientOptions(option.WithEndpoint(endpoint), option.WithoutAuthentication(), option.WithHTTPClient(&httpclient)))
defer gcsClient.Close()
assert.NoError(t, err)
_, err = gcsClient.ListFiles(bucketID)
assert.Error(t, err, "bucket doesn't exist")
err = gcsClient.Close()
assert.NoError(t, err)
})
t.Run("Test upload & download files - success", func(t *testing.T) {
bucketID := "upload-bucket"
file1Reader, file1Writer := io.Pipe()
file2Reader, file2Writer := io.Pipe()
gcsClient, err := gcs.NewClient(gcs.WithOpenFileFunction(openFileMock), gcs.WithCreateFileFunction(getCreateFileMock(file1Writer, file2Writer)),
gcs.WithClientOptions(option.WithEndpoint(endpoint), option.WithoutAuthentication(), option.WithHTTPClient(&httpclient)))
assert.NoError(t, err)
err = gcsClient.UploadFile(bucketID, "file1", "test/file1")
assert.NoError(t, err)
err = gcsClient.UploadFile(bucketID, "folder/file2", "test/folder/file2")
assert.NoError(t, err)
fileNames, err := gcsClient.ListFiles(bucketID)
assert.NoError(t, err)
assert.Equal(t, []string{"placeholder", "test/file1", "test/folder/file2"}, fileNames)
go gcsClient.DownloadFile(bucketID, "test/file1", "file1")
fileContent, err := ioutil.ReadAll(file1Reader)
assert.NoError(t, err)
assert.Equal(t, file1Content, string(fileContent))
go gcsClient.DownloadFile(bucketID, "test/folder/file2", "file2")
fileContent, err = ioutil.ReadAll(file2Reader)
assert.NoError(t, err)
assert.Equal(t, file2Content, string(fileContent))
err = gcsClient.Close()
assert.NoError(t, err)
})
t.Run("Test upload missing file", func(t *testing.T) {
bucketID := "upload-bucket"
gcsClient, err := gcs.NewClient(gcs.WithOpenFileFunction(openFileMock),
gcs.WithClientOptions(option.WithEndpoint(endpoint), option.WithoutAuthentication(), option.WithHTTPClient(&httpclient)))
assert.NoError(t, err)
err = gcsClient.UploadFile(bucketID, "file3", "test/file3")
assert.Contains(t, err.Error(), "could not open source file")
err = gcsClient.Close()
assert.NoError(t, err)
})
t.Run("Test download missing file", func(t *testing.T) {
bucketID := "upload-bucket"
gcsClient, err := gcs.NewClient(gcs.WithOpenFileFunction(openFileMock),
gcs.WithClientOptions(option.WithEndpoint(endpoint), option.WithoutAuthentication(), option.WithHTTPClient(&httpclient)))
assert.NoError(t, err)
err = gcsClient.DownloadFile(bucketID, "test/file3", "file3")
assert.Contains(t, err.Error(), "could not open source file")
err = gcsClient.Close()
assert.NoError(t, err)
})
t.Run("Test download file - failed file creation", func(t *testing.T) {
bucketID := "upload-bucket"
_, file1Writer := io.Pipe()
_, file2Writer := io.Pipe()
gcsClient, err := gcs.NewClient(gcs.WithOpenFileFunction(openFileMock), gcs.WithCreateFileFunction(getCreateFileMock(file1Writer, file2Writer)),
gcs.WithClientOptions(option.WithEndpoint(endpoint), option.WithoutAuthentication(), option.WithHTTPClient(&httpclient)))
assert.NoError(t, err)
err = gcsClient.DownloadFile(bucketID, "placeholder", "file3")
assert.Contains(t, err.Error(), "could not create target file")
err = gcsClient.Close()
assert.NoError(t, err)
})
}
const (
file1Content = `test file`
file2Content = `
foo : bar
pleh : help
stuff : {'foo': 'bar', 'bar': 'foo'}
`
)
func openFileMock(name string) (io.ReadCloser, error) {
var fileContent string
switch name {
case "file1":
fileContent = file1Content
case "folder/file2":
fileContent = file2Content
default:
return nil, errors.New("open file faled")
}
return ioutil.NopCloser(strings.NewReader(fileContent)), nil
}
func getCreateFileMock(file1Writer io.WriteCloser, file2Writer io.WriteCloser) func(name string) (io.WriteCloser, error) {
return func(name string) (io.WriteCloser, error) {
switch name {
case "file1":
return file1Writer, nil
case "file2":
return file2Writer, nil
default:
return nil, errors.New("could not create target file")
}
}
}