github.com/jaylevin/jenkins-library@v1.230.4/pkg/gcs/reporting_test.go (about) 1 package gcs 2 3 import ( 4 "errors" 5 "os" 6 "strings" 7 "testing" 8 "time" 9 10 "github.com/SAP/jenkins-library/pkg/gcs/mocks" 11 "github.com/bmatcuk/doublestar" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 type testFileInfo struct { 16 path string 17 } 18 19 func (t testFileInfo) Name() string { 20 return "" 21 } 22 23 func (t testFileInfo) Size() int64 { 24 return 0 25 } 26 27 func (t testFileInfo) Mode() os.FileMode { 28 return os.FileMode(0) 29 } 30 31 func (t testFileInfo) ModTime() time.Time { 32 return time.Time{} 33 } 34 35 func (t testFileInfo) IsDir() bool { 36 if strings.HasSuffix(t.path, "test2") { 37 return true 38 } 39 return false 40 } 41 42 func (t testFileInfo) Sys() interface{} { 43 return nil 44 } 45 46 type testStepConfig struct { 47 FirstParameter string 48 SecondParameter int 49 ThirdParameter string 50 FourthParameter bool 51 } 52 53 func TestPersistReportsToGCS(t *testing.T) { 54 var testCases = []struct { 55 testName string 56 gcsFolderPath string 57 gcsSubFolder string 58 outputParams []ReportOutputParam 59 expected []Task 60 detectedFiles []string 61 uploadFileErr error 62 expectedError error 63 }{ 64 { 65 testName: "success case", 66 gcsFolderPath: "test/folder/path", 67 gcsSubFolder: "sub/folder", 68 outputParams: []ReportOutputParam{ 69 {FilePattern: "*.json", ParamRef: "", StepResultType: "general"}, 70 {FilePattern: "*/test*", ParamRef: "", StepResultType: ""}, 71 {FilePattern: "*.json", ParamRef: "firstParameter", StepResultType: "general"}, 72 {FilePattern: "", ParamRef: "secondParameter", StepResultType: "general"}, 73 {FilePattern: "", ParamRef: "thirdParameter", StepResultType: ""}, 74 }, 75 expected: []Task{ 76 {SourcePath: "asdf.json", TargetPath: "test/folder/path/general/sub/folder/asdf.json"}, 77 {SourcePath: "folder/test1", TargetPath: "test/folder/path/sub/folder/folder/test1"}, 78 {SourcePath: "testFolder/test3", TargetPath: "test/folder/path/sub/folder/testFolder/test3"}, 79 {SourcePath: "report1.json", TargetPath: "test/folder/path/general/sub/folder/report1.json"}, 80 {SourcePath: "test-report.json", TargetPath: "test/folder/path/general/sub/folder/test-report.json"}, 81 {SourcePath: "test-report2.json", TargetPath: "test/folder/path/sub/folder/test-report2.json"}, 82 }, 83 detectedFiles: []string{"asdf.json", "someFolder/someFile", "folder/test1", "folder1/test2", "testFolder/test3"}, 84 uploadFileErr: nil, 85 expectedError: nil, 86 }, 87 { 88 testName: "failed upload to GCS", 89 gcsFolderPath: "test/folder/path", 90 gcsSubFolder: "", 91 outputParams: []ReportOutputParam{ 92 {FilePattern: "*.json", ParamRef: "", StepResultType: "general"}, 93 }, 94 expected: []Task{ 95 {SourcePath: "asdf.json", TargetPath: "test/folder/path/general/asdf.json"}, 96 }, 97 detectedFiles: []string{"asdf.json", "someFolder/someFile", "folder/test1", "folder1/test2", "testFolder/test3"}, 98 uploadFileErr: errors.New("upload failed"), 99 expectedError: errors.New("failed to persist reports: upload failed"), 100 }, 101 { 102 testName: "failed - input parameter does not exist", 103 gcsFolderPath: "test/folder/path", 104 gcsSubFolder: "", 105 outputParams: []ReportOutputParam{ 106 {FilePattern: "", ParamRef: "missingParameter", StepResultType: "general"}, 107 }, 108 expected: []Task{}, 109 detectedFiles: []string{"asdf.json", "someFolder/someFile", "folder/test1", "folder1/test2", "testFolder/test3"}, 110 uploadFileErr: nil, 111 expectedError: errors.New("there is no such input parameter as missingParameter"), 112 }, 113 { 114 testName: "failed - input parameter is empty", 115 gcsFolderPath: "test/folder/path", 116 outputParams: []ReportOutputParam{ 117 {FilePattern: "", ParamRef: "emptyParameter", StepResultType: "general"}, 118 }, 119 expected: []Task{}, 120 detectedFiles: []string{"asdf.json", "someFolder/someFile", "folder/test1", "folder1/test2", "testFolder/test3"}, 121 uploadFileErr: nil, 122 expectedError: errors.New("the value of the parameter emptyParameter must not be empty"), 123 }, 124 } 125 for _, tt := range testCases { 126 t.Run(tt.testName, func(t *testing.T) { 127 inputParameters := map[string]string{ 128 "firstParameter": "report1.json", 129 "secondParameter": "test-report.json", 130 "thirdParameter": "test-report2.json", 131 "emptyParameter": "", 132 } 133 gcsBucketID := "testBucketID" 134 mockedClient := &mocks.Client{} 135 136 for _, expectation := range tt.expected { 137 mockedClient.Mock.On("UploadFile", gcsBucketID, expectation.SourcePath, expectation.TargetPath).Return( 138 func(pipelineId string, sourcePath string, targetPath string) error { return tt.uploadFileErr }, 139 ).Once() 140 } 141 142 searchFn := func(path string) ([]string, error) { 143 matchedFiles := []string{} 144 for _, value := range tt.detectedFiles { 145 match, _ := doublestar.Match(path, value) 146 if match { 147 matchedFiles = append(matchedFiles, value) 148 } 149 } 150 return matchedFiles, nil 151 } 152 153 fileInfoFn := func(name string) (os.FileInfo, error) { 154 return testFileInfo{name}, nil 155 } 156 157 err := PersistReportsToGCS(mockedClient, tt.outputParams, inputParameters, tt.gcsFolderPath, gcsBucketID, tt.gcsSubFolder, searchFn, fileInfoFn) 158 if tt.expectedError == nil { 159 assert.NoError(t, err) 160 } else { 161 assert.Equal(t, tt.expectedError.Error(), err.Error()) 162 } 163 164 mockedClient.Mock.AssertNumberOfCalls(t, "UploadFile", len(tt.expected)) 165 mockedClient.Mock.AssertExpectations(t) 166 }) 167 } 168 }