1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/gcs/reporting_test.go
Siarhei Pazdniakou cd243ee542
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 15:07:47 +01:00

169 lines
5.2 KiB
Go

package gcs
import (
"errors"
"os"
"strings"
"testing"
"time"
"github.com/SAP/jenkins-library/pkg/gcs/mocks"
"github.com/bmatcuk/doublestar"
"github.com/stretchr/testify/assert"
)
type testFileInfo struct {
path string
}
func (t testFileInfo) Name() string {
return ""
}
func (t testFileInfo) Size() int64 {
return 0
}
func (t testFileInfo) Mode() os.FileMode {
return os.FileMode(0)
}
func (t testFileInfo) ModTime() time.Time {
return time.Time{}
}
func (t testFileInfo) IsDir() bool {
if strings.HasSuffix(t.path, "test2") {
return true
}
return false
}
func (t testFileInfo) Sys() interface{} {
return nil
}
type testStepConfig struct {
FirstParameter string
SecondParameter int
ThirdParameter string
FourthParameter bool
}
func TestPersistReportsToGCS(t *testing.T) {
var testCases = []struct {
testName string
gcsFolderPath string
gcsSubFolder string
outputParams []ReportOutputParam
expected []Task
detectedFiles []string
uploadFileErr error
expectedError error
}{
{
testName: "success case",
gcsFolderPath: "test/folder/path",
gcsSubFolder: "sub/folder",
outputParams: []ReportOutputParam{
{FilePattern: "*.json", ParamRef: "", StepResultType: "general"},
{FilePattern: "*/test*", ParamRef: "", StepResultType: ""},
{FilePattern: "*.json", ParamRef: "firstParameter", StepResultType: "general"},
{FilePattern: "", ParamRef: "secondParameter", StepResultType: "general"},
{FilePattern: "", ParamRef: "thirdParameter", StepResultType: ""},
},
expected: []Task{
{SourcePath: "asdf.json", TargetPath: "test/folder/path/general/sub/folder/asdf.json"},
{SourcePath: "folder/test1", TargetPath: "test/folder/path/sub/folder/folder/test1"},
{SourcePath: "testFolder/test3", TargetPath: "test/folder/path/sub/folder/testFolder/test3"},
{SourcePath: "report1.json", TargetPath: "test/folder/path/general/sub/folder/report1.json"},
{SourcePath: "test-report.json", TargetPath: "test/folder/path/general/sub/folder/test-report.json"},
{SourcePath: "test-report2.json", TargetPath: "test/folder/path/sub/folder/test-report2.json"},
},
detectedFiles: []string{"asdf.json", "someFolder/someFile", "folder/test1", "folder1/test2", "testFolder/test3"},
uploadFileErr: nil,
expectedError: nil,
},
{
testName: "failed upload to GCS",
gcsFolderPath: "test/folder/path",
gcsSubFolder: "",
outputParams: []ReportOutputParam{
{FilePattern: "*.json", ParamRef: "", StepResultType: "general"},
},
expected: []Task{
{SourcePath: "asdf.json", TargetPath: "test/folder/path/general/asdf.json"},
},
detectedFiles: []string{"asdf.json", "someFolder/someFile", "folder/test1", "folder1/test2", "testFolder/test3"},
uploadFileErr: errors.New("upload failed"),
expectedError: errors.New("failed to persist reports: upload failed"),
},
{
testName: "failed - input parameter does not exist",
gcsFolderPath: "test/folder/path",
gcsSubFolder: "",
outputParams: []ReportOutputParam{
{FilePattern: "", ParamRef: "missingParameter", StepResultType: "general"},
},
expected: []Task{},
detectedFiles: []string{"asdf.json", "someFolder/someFile", "folder/test1", "folder1/test2", "testFolder/test3"},
uploadFileErr: nil,
expectedError: errors.New("there is no such input parameter as missingParameter"),
},
{
testName: "failed - input parameter is empty",
gcsFolderPath: "test/folder/path",
outputParams: []ReportOutputParam{
{FilePattern: "", ParamRef: "emptyParameter", StepResultType: "general"},
},
expected: []Task{},
detectedFiles: []string{"asdf.json", "someFolder/someFile", "folder/test1", "folder1/test2", "testFolder/test3"},
uploadFileErr: nil,
expectedError: errors.New("the value of the parameter emptyParameter must not be empty"),
},
}
for _, tt := range testCases {
t.Run(tt.testName, func(t *testing.T) {
inputParameters := map[string]string{
"firstParameter": "report1.json",
"secondParameter": "test-report.json",
"thirdParameter": "test-report2.json",
"emptyParameter": "",
}
gcsBucketID := "testBucketID"
mockedClient := &mocks.Client{}
for _, expectation := range tt.expected {
mockedClient.Mock.On("UploadFile", gcsBucketID, expectation.SourcePath, expectation.TargetPath).Return(
func(pipelineId string, sourcePath string, targetPath string) error { return tt.uploadFileErr },
).Once()
}
searchFn := func(path string) ([]string, error) {
matchedFiles := []string{}
for _, value := range tt.detectedFiles {
match, _ := doublestar.Match(path, value)
if match {
matchedFiles = append(matchedFiles, value)
}
}
return matchedFiles, nil
}
fileInfoFn := func(name string) (os.FileInfo, error) {
return testFileInfo{name}, nil
}
err := PersistReportsToGCS(mockedClient, tt.outputParams, inputParameters, tt.gcsFolderPath, gcsBucketID, tt.gcsSubFolder, searchFn, fileInfoFn)
if tt.expectedError == nil {
assert.NoError(t, err)
} else {
assert.Equal(t, tt.expectedError.Error(), err.Error())
}
mockedClient.Mock.AssertNumberOfCalls(t, "UploadFile", len(tt.expected))
mockedClient.Mock.AssertExpectations(t)
})
}
}